批量约束对象(羽毛到翅膀)

2024-04-27 04:57:10 发布

您现在位置:Python中文网/ 问答频道 /正文

不久前,我的第一个愚蠢的问题在这里得到了回答,所以。。。我又来了,希望有一个不那么愚蠢,更有趣的头巾。请记住,我仍然是我的婴儿步脚本!你知道吗

就是这样:我需要装备一个有羽毛的翅膀,而且我已经把所有的羽毛都准备好了。我想模仿我最近制作的另一个装备,它将羽毛点约束到手臂和前臂,并将方向约束到手臂上的其他三个控制器:每个羽毛一次约束到其中的两个控制器,并且约束的权重会随着前臂朝手腕的方向移动,所以肘部和前臂之间的一根羽毛会被两个控制器均匀地约束。。。你明白了。你知道吗

我的推理是这样的:让我们做一个循环,遍历每根羽毛,得到它的世界位置,找到从羽毛到每个方向控制器的距离(通过毕达哥拉斯),将其规格化,并将值输入到方向约束的权重属性中。我甚至可以多走一英里,通过正弦函数传递标准化的距离,以得到羽毛轮廓的一个很好的缓和。你知道吗

我的伪代码既难看又坏,但这只是一次尝试。我的问题是内联的。你知道吗

再试试! 它现在起作用,但只作用于活动对象,而不是整个选择。会发生什么?你知道吗

import maya.cmds as cmds

# find world space position of targets
base_pos = cmds.xform('base',q=1,ws=1,rp=1)
tip_pos = cmds.xform('tip',q=1,ws=1,rp=1)

def relative_dist_from_pos(pos, ref):
# vector substract to get relative pos
    pos_from_ref = [m - n for m, n in zip(pos, ref)]
# pythagoras to get distance from vector
    dist_from_ref = (pos_from_ref[0]**2 + pos_from_ref[1]**2 + pos_from_ref[2]**2)**.5
    return dist_from_ref

def weight_from_dist(dist_from_base, dist_to_tip):
    normalize_fac = (1/(dist_from_base + dist_to_tip))
    dist_from_base *= normalize_fac
    dist_to_tip *= normalize_fac
    return dist_from_base, dist_to_tip

sel = cmds.ls(selection=True)

for obj in sel:
# find world space pos of feather
    feather_pos = cmds.xform(obj, q=1, ws=1, rp=1)

# call relative_dist_from_pos
    dist_from_base = relative_dist_from_pos(feather_pos, base_pos)
    dist_to_tip = relative_dist_from_pos(feather_pos, tip_pos)

# normalize distances
    weight_from_dist(dist_from_base, dist_to_tip)

# constrain the feather - weights are inverted
# because the smaller the distance, the stronger the constraint
    cmds.orientConstraint('base', obj, w=dist_to_tip)
    cmds.orientConstraint('tip', obj, w=dist_from_base)

你来了。如有任何提示,我们将不胜感激。你知道吗

晚安

哈德里斯


Tags: thetofromposrefbasedist控制器