Maya Python - Set object pivot to selection center

坚强是说给别人听的谎言 提交于 2019-12-24 05:47:08

问题


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).

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.


回答1:


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. You can use cmds.listRelatives to get the shape's transform. 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]) )


来源:https://stackoverflow.com/questions/39902493/maya-python-set-object-pivot-to-selection-center

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!