在python中删除父约束时如何将定位器保持在父中心

时间:2019-07-27 23:54:11

标签: python maya

client/sensor/year/month/day

我正在尝试在删除约束节点之后将定位器(驱动器)保持在所选对象的中心。我可以得到一些建议吗?

1 个答案:

答案 0 :(得分:0)

我不确定要了解您的问题,但是这里有一些解决方案,希望我会使用数学而不是约束来帮助您

from __future__ import division
import maya.cmds as cmds

# =========================
#just to illustrate an exemple selection of 10 sphere combined
import random
psph = [cmds.polySphere()[0] for i in range(4)]
for i in psph:
    cmds.setAttr(i+'.t', random.uniform(0.0, 10), random.uniform(0.0, 10), random.uniform(0.0, 10))
# sph_comb = cmds.polyUnite(psph)
cmds.delete(psph , ch=True)
# ===============================

def getCentroid(sel):

    obj = cmds.ls(sel, o=True)
    sel = cmds.polyListComponentConversion(sel, tv=True)
    sel = cmds.ls(sel, flatten=1)

    if len(obj) > 1:
        pos = []
        for s in sel:
            p = cmds.xform(s, q=1, t=1, ws=1)
            pos += p
    else:
        pos = cmds.xform(sel, q=1, t=1, ws=1)
    nb = len(sel)
    myCenter = sum(pos[0::3]) / nb, sum(pos[1::3]) / nb, sum(pos[2::3]) / nb

    return myCenter

# example for each sphere
sel= cmds.ls(psph)
for i in sel:
    loc = cmds.spaceLocator(n = 'driver')[0]
    coord = getCentroid(i)
    cmds.setAttr(loc+'.t', *coord)

# for only one at the center of all sphres
sel= cmds.ls(psph)
coord = getCentroid(sel)
loc = cmds.spaceLocator()[0]
cmds.setAttr(loc+'.t', *coord)

编辑:修复了我的函数getCentroid上的一些代码问题,因为xform可以获取多个对象