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<num:
        xform = geo[j].worldTransform()
        geo[j].setFirstInput(geo[j-1])
        geo[j].moveToGoodPosition()
        geo[j].setWorldTransform(xform)
        j+=1

def cancelParents():
    selgeo = hou.selectedNodes()
    i = 0
    while i<len(selgeo):
        xform = selgeo[i].worldTransform()
        selgeo[i].setFirstInput(None)
        selgeo[i].setWorldTransform(xform)
        i+=1
        
#根据距离分组

#获取当前的节点
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])

 

 

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值