python - 相同的 Python 代码在不同的 Maya (2012 - 2015) 上有不同的表现

标签 python maya maya-api

这个简单的代码

import maya.cmds as cmd

circle1 = cmd.circle(nr=(0, 0, 1), c=(0, -1.1, 0), ch=1)
circle2 = cmd.duplicate(circle1[0], ic=1)
circle3 = cmd.duplicate(circle1[0], ic=1)
cmd.setAttr(circle2[0] + '.rotateZ', 120)
cmd.setAttr(circle3[0] + '.rotateZ', -120)

allCurves = circle1[0], circle2[0], circle3[0]
cmd.select(allCurves)
cmd.makeIdentity(apply=True, t=1, r=1, s=1, n=0)

在 Maya 2012 中完美运行,给我这样的结果:

enter image description here

相反,在 Maya 2015 中,相同代码的结果是这样的:

enter image description here

所有圆都移动到原点。

似乎命令 cmd.makeIdentity 的工作方式不同,但阅读 maya 文档命令是相同的。构建历史设置也相同。我无法理解 Maya 在幕后做了什么。

为什么这行代码的工作方式不同?

最佳答案

真正的问题是因为新 Maya (2015) 在节点共享历史/连接/节点(在本例中为 >makeNurbCircle 节点)。它似乎正在创建临时 transformGeometry 节点,以补偿链中错误顺序的待卡住变换。在 Maya 2012 中情况并非如此,当时的顺序似乎是正确的。如果您查看下面的比较,就会明白原因。

左边是 2012 年;右边是 2015 年: 2012 on the left; 2015 on the right

无论哪种方式,如果您想保留此共享历史并出于任何原因以这种方式进行卡住转换,您可能必须手动执行 makeIdentity 尝试执行的操作,但以您想要的更简洁的方式它;即,在手动卡住转换上的转换(使用 xform)之前,以正确的顺序正确连接 transformGeometry 节点。

这是我刚想做的事情:(我会在以后找到时间时用评论和解释更新答案)

import maya.cmds as cmds


def makeIdentityCurvesWithSharedHistory(curves=[]):
    for curve in curves:
        curveShape = cmds.listRelatives(curve, shapes=True)[0]    
        makeCircle = cmds.listConnections(curveShape, type='makeNurbCircle')[0]
        transformation = cmds.xform(curve, q=True, matrix=True)    
        transformGeoNode = cmds.createNode('transformGeometry')
        cmds.setAttr('%s.transform' % transformGeoNode, transformation, type='matrix')
        cmds.connectAttr('%s.outputCurve' % makeCircle, '%s.inputGeometry' % transformGeoNode)
        cmds.connectAttr('%s.outputGeometry' % transformGeoNode, '%s.create' % curveShape, force=True)
        cmds.xform(curve, matrix=[1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0])


circle1 = cmd.circle(nr=(0, 0, 1), c=(0, -1.1, 0), ch=1)
circle2 = cmd.duplicate(circle1[0], ic=1)
circle3 = cmd.duplicate(circle1[0], ic=1)
cmd.setAttr(circle2[0] + '.rotateZ', 120)
cmd.setAttr(circle3[0] + '.rotateZ', -120)

allCurves = circle1[0], circle2[0], circle3[0]
makeIdentityCurvesWithSharedHistory(allCurves)

如果使用上面的代码: enter image description here

免责声明:理论上,这应该适用于任何版本的 Maya;但我只在 Maya 2015 上测试过它。

关于python - 相同的 Python 代码在不同的 Maya (2012 - 2015) 上有不同的表现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30934889/

相关文章:

python - 编写程序/脚本以从一堆文件名的开头删除相同的字符串的最简单方法是什么?

带有参数数据的 Python 请求发布

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

c++ - Maya C++ 插件 MPxImagePlane

python - 在任何 3 维表面上生成随机点

python - 使用 numpy 运算从每行填充 numpy 数组(不包括填充)和非填充值的数量中选择随机数的最快方法

python - Maya 变换节点未出现在列表中

python - 如何使用 MEL 或 Python 修改 Maya 中的现有面板?

c++ - 将自定义数据传递给 MPxLocator (Maya API) 的属性

Python >=3.5 : Checking type annotation at runtime