Houdini----Python

这篇记录自己学习H过程中Py的部分脚本工具。

#来自吕老师的一个教程:
#需要注意的就是Houdini中设置父子关系的时候,如果要保留子级的原始的transform,在界面操作中要勾选Keep Position When Parenting,而在Py中操作时相当于保存transform,设置父子关系后在把transform复原回去


def createParentGeo(num):
    geo = []
    i = 0
    while i < num:
        geo.append(hou.node('/obj').createNode('geo'))
        geo[i].parm('ty').set(3*i)
        i+=1
    j = 1
    while j
#根据距离分组

#获取当前的节点
node = hou.pwd()

#获取到当前节点的几何体(上级传入的几何体)
geo = node.geometry()

#创建一个距离组
pointGrp = geo.createPointGroup('dist_group')

#获取Parameters
dist = hou.parm('distance').eval()
pos = hou.parmTuple('position').eval()

#功能部分
for point in geo.points():
    length = hou.Vector3(point.position() - hou.Vector3(pos)).length()
    if length < dist:
        pointGrp.add(point)
    
# 在已知点的周围撒点
# This code is called when instances of this SOP cook.
node = hou.pwd()
geo = node.geometry()


num = hou.parm('insertNum').eval()
range = hou.parm('range').eval()
seed = hou.parm('seed').eval()
delete = hou.parm('delOriginPoints').eval()

pos = []


for point in geo.points():
    i = 0
    while i < num:
        rnd = hou.hmath.rand(point.number())#rand($PT)
        xrand = point.position()[0] + (hou.hmath.rand(rnd+i+0.1+seed) - 0.5) * 2 * range
        yrand = point.position()[1] + (hou.hmath.rand(rnd+i+2.6+seed) - 0.5) * 2 * range
        zrand = point.position()[2] + (hou.hmath.rand(rnd+i+9.3+seed) - 0.5) * 2 * range
        
        randPos = hou.Vector3(xrand, yrand, zrand)
        
        pos.append(randPos)
        i += 1

if delete == 1:
    geo.clear()

j = 0
while j < len(pos):
    p = geo.createPoint()
    p.setPosition(pos[j])
    j += 1

 

 

# 在每一个点组的平均位置上生成一个点
# This code is called when instances of this SOP cook.
node = hou.pwd()
geo = node.geometry()

positionList = []

numPointGroup = len(geo.pointGroups())

for currentGroup in geo.pointGroups():
    xsum = ysum = zsum = 0
    for currentPoint in currentGroup.points():
        xsum += currentPoint.position()[0]
        ysum += currentPoint.position()[1]
        zsum += currentPoint.position()[2]
        
    xaverage = xsum / len(currentGroup.points())
    yaverage = ysum / len(currentGroup.points())
    zaverage = zsum / len(currentGroup.points())
    
    positionList.append((xaverage, yaverage, zaverage))

geo.clear()


for i in range(0, numPointGroup, 1):
    point = geo.createPoint()
    point.setPosition(positionList[i])








 

# 用点驱动模型运动,第一个输入点是驱动的模型,第二个输入点是动画的点,第三个是动画点的初始位置
# This code is called when instances of this SOP cook.
node = hou.pwd()
geo = node.geometry()

input2_geo = node.inputs()[1].geometry()
input3_geo = node.inputs()[2].geometry()

initPos = []

for input3point in input3_geo.points():
    initPos.append(input3point.position())

tempPos = []

for input2Point in input2_geo.points():
    tempPos.append(input2Point.position())

i = 0
for currentGroup in geo.pointGroups():
    for currentPoint in currentGroup.points():
        pos = tempPos[i] - initPos[i] + currentPoint.position()
        pos = hou.Vector3(pos)
        currentPoint.setPosition(pos)
    i+=1
        
    



# This code is called when instances of this SOP cook.
# 把每一帧的属性值累加起来,如果在时间轴上收到输入帧数,此节点无效
node = hou.pwd()
geo = node.geometry()

attribName = node.parm('attribname').eval()

accumulateName = 'accumulated_' + attribName

points_tmp = geo.points()

#这里的第三个参数是默认值,如果只写0的话会被默认成整数,如果是浮点数要写成0.0
geo.addAttrib(hou.attribType.Point, accumulateName, 0.0)

if hou.frame()==1:
    accumulate = []
    for i in range(0,len(points_tmp),1):
        accumulate.append( points_tmp[i].attribValue(attribName))
        points_tmp[i].setAttribValue(accumulateName,accumulate[i])
else:
    for i in range(0, len(points_tmp), 1):
        accumulate[i] += points_tmp[i].attribValue(attribName)
        points_tmp[i].setAttribValue(accumulateName,accumulate[i])

 

 

 

你可能感兴趣的:(Houdini)