Maya Python-将对象枢轴设置到选择中心 [英] Maya Python - Set object pivot to selection center

查看:265
本文介绍了Maya Python-将对象枢轴设置到选择中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将所选对象的轴枢轴移动到所选对象的顶点的中心.

I'm trying to move the selected object pivot to the center of the objects selected vertices.

运行代码时,我没有收到任何错误,并且几乎所有工作都按预期进行,但是(obj)我选择的对象的枢轴似乎没有将自身设置为定位器xform(piv).

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.

推荐答案

我认为主要的问题是,当您使用obj = cmds.ls(*sel, o=True)时,它仅捕获对象的形状节点而不是其变换.您可以使用cmds.listRelatives来获取形状的变换.您也不需要创建定位器,因为集群已经为您提供了位置.

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

这篇关于Maya Python-将对象枢轴设置到选择中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆