简体   繁体   English

Maya Python-将对象枢轴设置到选择中心

[英]Maya Python - Set object pivot to selection center

I'm trying to move the selected object pivot to the center of the objects selected vertices. 我正在尝试将所选对象的轴枢轴移动到所选对象的顶点的中心。

When I run the code I don't recieve any errors and almost everything works as intended, However the pivot of (obj)my selected object doesn't seem to set itself to the locator xform(piv). 当我运行代码时,我没有收到任何错误,并且几乎所有工作都按预期进行,但是(obj)我选择的对象的枢轴似乎没有将自身设置为定位器xform(piv)。

import maya.cmds as cmds

sel = cmds.ls(sl=True)
print sel
obj = cmds.ls(*sel, o=True)
print obj

selVerts = cmds.ls(sl=True)
tempClstr = cmds.cluster()
pos = cmds.xform(tempClstr[1], q=True, ws=True, rp=True)
loc = cmds.spaceLocator()
cmds.move(pos[0], pos[1], pos[2])
cmds.delete(tempClstr)

piv = cmds.xform (loc[1], piv=True, q=True, ws=True)
print piv
cmds.xform( obj, ws=True, piv=(piv[0], piv[1], piv[2]) )

Need some help on this one fast. 在这方面需要一些帮助。 Any extra eyes that can spot what I'm missing would be greatly appreciated. 可以发现我所缺少的任何额外的眼睛将不胜感激。

I think the main issue was that when you were using obj = cmds.ls(*sel, o=True) , it was only capturing the object's shape node instead of its transform. 我认为主要问题是,当您使用obj = cmds.ls(*sel, o=True) ,它仅捕获对象的形状节点,而不是其变换。 You can use cmds.listRelatives to get the shape's transform. 您可以使用cmds.listRelatives来获取形状的变换。 You also don't need to create the locator as the cluster already gives you the position. 您也不需要创建定位器,因为集群已经为您提供了位置。

This seems to work for me, though you may want to consider some additional error checking for the selection portion as it assumes a lot. 这似乎对我有用,尽管您可能要考虑选择部分的一些其他错误检查,因为它假设很多。

import maya.cmds as cmds

sel = cmds.ls(sl=True)
shapes = cmds.ls(sel, o=True)
obj = cmds.listRelatives(shapes[0], f=True, parent=True)[0]

selVerts = cmds.ls(sl=True)
tempClstr = cmds.cluster()
piv = cmds.xform(tempClstr[1], q=True, ws=True, rp=True)
cmds.delete(tempClstr)

cmds.xform(obj, ws=True, piv=(piv[0], piv[1], piv[2]) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM