kunz Difference between revisions of "Useful"

Difference between revisions of "Useful"

From kunz
Line 1: Line 1:
<syntaxhighlight lang='python'>
<syntaxhighlight lang='python'>
''Python parameter expressions''
hou.Matrix4(hou.node(hou.ch('spare_input0')).geometry().attribValue('transform')).extractTranslates()[0]
hou.Matrix4(hou.node(hou.ch('spare_input0')).geometry().attribValue('transform')).extractTranslates()[0]
hou.Matrix4(hou.node(hou.ch('spare_input0')).geometry().attribValue('transform')).extractTranslates()[1]
hou.Matrix4(hou.node(hou.ch('spare_input0')).geometry().attribValue('transform')).extractTranslates()[1]

Revision as of 21:32, 12 March 2024

''Python parameter expressions''
hou.Matrix4(hou.node(hou.ch('spare_input0')).geometry().attribValue('transform')).extractTranslates()[0]
hou.Matrix4(hou.node(hou.ch('spare_input0')).geometry().attribValue('transform')).extractTranslates()[1]
hou.Matrix4(hou.node(hou.ch('spare_input0')).geometry().attribValue('transform')).extractTranslates()[2]

hou.Matrix4(hou.node(hou.ch('spare_input0')).geometry().attribValue('transform')).extractRotates()[0]
hou.Matrix4(hou.node(hou.ch('spare_input0')).geometry().attribValue('transform')).extractRotates()[1]
hou.Matrix4(hou.node(hou.ch('spare_input0')).geometry().attribValue('transform')).extractRotates()[2]

hou.Matrix4(hou.node(hou.ch('spare_input0')).geometry().attribValue('transform')).extractScales()[0]
hou.Matrix4(hou.node(hou.ch('spare_input0')).geometry().attribValue('transform')).extractScales()[1]
hou.Matrix4(hou.node(hou.ch('spare_input0')).geometry().attribValue('transform')).extractScales()[2]

flbk.py ➔

import hou, toolutils, os, subprocess, shlex, time, shutil

def main():
    # Define the directory to store flipbooks.  In this case, parent dir of folder containing the hip_file_path (same as $HIP/../ )
    flipbook_dir = os.path.join(os.path.abspath( os.path.join(hou.hipFile.path(), os.pardir, os.pardir) ), 'flip')
    if not os.path.exists(flipbook_dir):
        print('Creating directory: '+flipbook_dir)
        os.makedirs(flipbook_dir)
    jpeg_output_path = os.path.join(flipbook_dir, hou.getenv('HIPNAME') + os.path.sep + '$F4.jpeg')

    # Save a backup of the scene file to the flipbook dir
    tmp_backup_file = hou.hipFile.saveAsBackup()
    date_time = time.strftime("%Y_%m_%d__%H.%M.%S")
    backup_file_path = os.path.join(flipbook_dir, hou.getenv('HIPNAME'), date_time+"_"+hou.hipFile.basename())
    backup_file_path = str(backup_file_path)
    if not os.path.exists(os.path.dirname(backup_file_path)):
        print('Creating directory: '+os.path.dirname(backup_file_path))
        os.makedirs(os.path.dirname(backup_file_path))
    # Rename and move the tmp_backup_file file to our destination path and filename
    shutil.move(tmp_backup_file, backup_file_path)

    # Don't modifiy the existing settings (make a copy)
    flbk_settings = toolutils.sceneViewer().flipbookSettings().stash()
    # If OCIO is enabled don't apply gamma
    if toolutils.sceneViewer().usingOCIO():
        flbk_settings.overrideGamma(True)
        flbk_settings.overrideGamma(1.0)
    # Set the output path for flipbook frames
    flbk_settings.output(jpeg_output_path)
    if hou.licenseCategory() == hou.licenseCategoryType.Apprentice:
        flbk_settings.useResolution(1)
        flipbook_options.resolution((1280,720))
    else:
        # Disable the resolution override
        flbk_settings.useResolution(0)

    if flbk_settings.sessionLabel() is '':
        flbk_settings.sessionLabel( str(os.getpid()) )

    start_timer = time.perf_counter()
    seq_profile = hou.PerfMonProfile
    #global seq_profile
    seq_profile = hou.perfMon.startProfile("Seq")

    # Keeps a history file of all the flipbooks performed in your user dir
    history_file = open(os.environ['HOME']+'/flbk.txt', 'a+')
    history_file.write(os.path.splitext(backup_file_path)[0]+'.mp4')
    history_file.write('\n')
    history_file.close()


    # Perform the flipbook
    toolutils.sceneViewer().flipbook(viewport=None, settings=flbk_settings, open_dialog=False)

    seq_profile.stop()
    seq_profile.exportAsCSV(os.path.splitext(backup_file_path)[0]+".csv")
    # these .hperf files were too big
    #seq_profile.save(os.path.splitext(backup_file_path)[0]+".hperf")
    
    jpeg_output_path = '"' + jpeg_output_path.replace('$F4', '%04d') + '"'
    #convert2mov_command ='/usr/local/bin/ffmpeg'
    convert2mov_command ='ffmpeg'
    convert2mov_command+=' -start_number '+str( int(flbk_settings.frameRange()[0]) )
    convert2mov_command+=' -framerate '+str(hou.fps())
    convert2mov_command+=' -apply_trc iec61966_2_1'
    convert2mov_command+=' -i '+jpeg_output_path
    convert2mov_command+=' -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2"'
    convert2mov_command+=' -f mp4 -vcodec libx264 -pix_fmt yuv420p'
    convert2mov_command+=' -r '+str(hou.fps())
    convert2mov_command+=' -crf 22'
    convert2mov_command+=' -metadata title="'+hou.getenv('HIPNAME')+'"'
    convert2mov_command+=' -metadata comment="'
    convert2mov_command+='SOURCE SCENE: '+hou.hipFile.path()+'\n'
    convert2mov_command+='BACKUP SCENE: '+backup_file_path+'\n'
    convert2mov_command+='VERSION: '+hou.applicationVersionString()+'\n'
    convert2mov_command+='PLATFORM: '+hou.applicationPlatformInfo()+'\n'
    convert2mov_command+='OS: '+os.environ['HOUDINI_OS']+'\n'
    convert2mov_command+='USER: '+os.environ['USER']+'\n'
    convert2mov_command+='USERNAME: '+hou.userName()+'\n'
    convert2mov_command+='HOSTNAME: '+hou.machineName()+'\n'
    convert2mov_command+='EXECUTED ON: '+date_time+'\n'
    convert2mov_command+='EXECUTED IN: '+str(round(time.perf_counter() - start_timer, 2))+' seconds'
    convert2mov_command+='"'
    convert2mov_command+=' -x264opts colormatrix=bt709'
    mp4_output_path = '"' + os.path.splitext(backup_file_path)[0]+'.mp4' + '"'
    convert2mov_command+=' '+mp4_output_path
    try:
        # The subprocess.run() function only exists in Python 3.5 and newer.
        subprocess.run(shlex.split(convert2mov_command), shell=True, capture_output=True, text=True, check=True)
    except:
        # Support versions of Python older then 3.5
        subprocess.Popen( shlex.split(convert2mov_command) ).wait()

    # Remove the .jpegs generated by the flipbook
    jpeg_output_path = jpeg_output_path.strip('"\'')
    for file in [file for file in os.listdir(os.path.dirname(jpeg_output_path)) if file.endswith('.jpeg')]:
	    os.remove( os.path.join(os.path.dirname(jpeg_output_path), file) )
    print('Flipbook Finished: ' + mp4_output_path.strip('"'))

ffmpeg combine all .mkv files in a dir ➔

ffmpeg -f concat -safe 0 -i <(find . -type f -name '*.mkv' -printf "file '$PWD/%p'\n" | sort) -c copy $PWD/output.mkv