1. 程式人生 > >Houdini低模解算轉成高模關鍵幀動畫並拆分碎塊UV

Houdini低模解算轉成高模關鍵幀動畫並拆分碎塊UV

    bakeFragDetailsUV.otl可以實現將剛體解算的低模運動狀態轉換成高模關鍵幀動畫,碎塊UV進行表面和內部的分離,以便匯入maya使用,基於軟體版本Houdini FX 15.0.244.16。

節點連線注意節點的引數設定,相同節點設定可能有所不同:

低模進行剛體解算,高模匹配動畫關鍵幀包括位移旋轉。

對碎塊進行UV拆分,將outsideinsideUV進行分開。

圖一連線節點並設定好引數之後,選擇bakeFragDetailsUV1節點設定好需要bake的起始幀和結束幀之後,點選Bake Frag Details UV按鈕,/obj路徑生成

group即為動畫UV的高模

PythonModule程式碼

from time import *

#Bake RBDs simulation to "group" subnetwork
#Packed RBDs simulation and get attributes as Trans,Orient,Pivot etc
#Transfer the Orient attribute to rotation
def bakeFragDetailsUV():
    tstart = time()
    hou_node = hou.pwd()
    hou_geo = hou_node.geometry()
    points = hou_geo.points()
    points_num = len(points)
#   print points_num

    point_loc = hou.node('/obj')
    group_node = point_loc.createNode('subnet','group')
    rbd_node = hou_node.node('..')
    group_node.setPosition(rbd_node.position())
    group_node.move([0,-1])
    group_node.setSelected(True)
    rbd_node.setSelected(False)

#   create copy piece,set the pivot
    for point in points:
        frags_node = group_node.createNode('geo','due'+str(point.number()))

        a_node = hou.node('/obj/group/due'+str(point.number()))
        b_node = a_node.children()
        b_node[0].destroy()

        c_node = a_node.createNode('object_merge','piece'+str(point.number()))
        obj_path = c_node.parm('objpath1')
        obj_path.set('/obj/'+rbd_node.name()+'/pack2')
        group_piece = c_node.parm('group1')
        group_piece.set('@name=piece'+str(point.number()))
        unpack_node = a_node.createNode('unpack','unpack'+str(point.number()))
        unpack_node.setPosition(c_node.position())
        unpack_node.move([0,-1])
        unpack_node.setNextInput(c_node)
        unpack_node.setDisplayFlag(True)
        unpack_node.setRenderFlag(True)

        pivot_origin = point.attribValue('pivot')
#       print pivot_origin
        a_node.setParms({'px':pivot_origin[0], 'py':pivot_origin[1], 'pz':pivot_origin[2]})
#       print a_node.evalParmTuple('p')

#   setKeyframe tx,ty,tz,rx,ry,rz
#   for x in range(int(hou.expandString('$FSTART')), int(hou.expandString('$FEND'))+1, 1):
    for x in range(int(hou_node.evalParm('f1')), int(hou_node.evalParm('f2'))+1, 1):
        hou.setFrame(x)
        print str(x) + ' frame' + ',please wait...'
        for point in points:
            pos1 = point.position()
#           print "(%d) -> x=%f, y=%f, z=%f" % (point.number(), pos1[0], pos1[1], pos1[2])
            pos2 = point.attribValue('pivot')
#           print "(%d) -> x=%f, y=%f, z=%f" % (point.number(), pos2[0], pos2[1], pos2[2])
            pos3 = [0,0,0]
            if(x != 1):
                pos3[0] = pos1[0]-pos2[0]
                pos3[1] = pos1[1]-pos2[1]
                pos3[2] = pos1[2]-pos2[2]
#           print "(%d) -> x=%f, y=%f, z=%f" % (point.number(), pos3[0], pos3[1], pos3[2])
            orient_num = point.attribValue('orient')
#           print "(%d) -> x=%f, y=%f, z=%f, n=%f" % (point.number(), orient_num[0], orient_num[1], orient_num[2], orient_num[3])

            frags_selet_node = hou.node('/obj/group/due'+str(point.number()))
            orient_qua = hou.Quaternion(orient_num).normalized()

#           print type(orient_qua)
            matrix3 = orient_qua.extractRotationMatrix3()
#           print matrix3
            rot = matrix3.extractRotates()
#           print rot

            point_tx = frags_selet_node.parm('tx')
            point_keyframe = hou.Keyframe()
            point_keyframe.setValue(pos3[0])
            point_tx.setKeyframe(point_keyframe)

            point_ty = frags_selet_node.parm('ty')
            point_keyframe = hou.Keyframe()
            point_keyframe.setValue(pos3[1])
            point_ty.setKeyframe(point_keyframe)

            point_tz = frags_selet_node.parm('tz')
            point_keyframe = hou.Keyframe()
            point_keyframe.setValue(pos3[2])
            point_tz.setKeyframe(point_keyframe)

            point_rx = frags_selet_node.parm('rx')
            point_keyframe = hou.Keyframe()
            point_keyframe.setValue(rot[0])
            point_rx.setKeyframe(point_keyframe)

            point_ry = frags_selet_node.parm('ry')
            point_keyframe = hou.Keyframe()
            point_keyframe.setValue(rot[1])
            point_ry.setKeyframe(point_keyframe)

            point_rz = frags_selet_node.parm('rz')
            point_keyframe = hou.Keyframe()
            point_keyframe.setValue(rot[2])
            point_rz.setKeyframe(point_keyframe)

    print 'Done'
    tend = time()
    t = tend - tstart
    print 'time use ' + str(int(t)) +" s"