相同的python代码在不同的Maya上的工作方式不同(2012-2015)

2024-05-19 02:54:12 发布

您现在位置:Python中文网/ 问答频道 /正文

这个简单的代码

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文档时命令是相同的。构建历史设置也是相同的。我不明白玛雅在幕后干什么。在

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


Tags: 代码命令cmd方式icmayaduplicatesetattr
3条回答

真正的问题是,当节点共享历史/连接/节点(在本例中是makeNurbCircle节点)时,新Maya(2015)在执行makeIdentity准备工作时可能存在缺陷。它似乎正在创建临时的transformGeometry节点来补偿链中顺序错误的待冻结转换。玛雅2012的情况并非如此,当时的秩序似乎是正确的。如果你看看下面的比较,你就会明白为什么。在

左侧为2012年;右侧为2015年:2012 on the left; 2015 on the right

不管是哪种方式,如果您想保留这个共享历史记录并以这种方式进行冻结转换,那么您可能需要手动执行makeIdentity尝试的操作,但要以更干净的方式进行;i、 e.在手动冻结转换之前,按正确的顺序正确连接transformGeometry节点(使用xform)。在

下面是我刚刚想做的事情:(我会在以后有时间的时候用评论和解释来更新答案)

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上测试过。

如果将makeIdentity移动到链上,它会在视觉上起作用:

circle1 = cmds.circle(nr=(0, 0, 1), c=(0, -1.1, 0), ch=1)
circle2 = cmds.duplicate(circle1[0], ic=1)
circle3 = cmds.duplicate(circle1[0], ic=1)
cmds.makeIdentity(apply=True, t=1, r=1, s=1, n=0)
cmds.setAttr(circle2[0] + '.rotateZ', 120)
cmds.setAttr(circle3[0] + '.rotateZ', -120)

真正的问题是MakeIdentity会更改几何体,以便在保持相同外观的同时将所拥有的变换置零;如果共享形状(ic=True),则当对多个对象调用它时,结果有点随机。检查您的历史记录,您应该会看到圆形状上的多个“transformGeometry”节点,每个节点都试图影响几何体。在

如果您希望实例都在本地清零,那么只需添加另一个转换可能更容易。在

重复输入连接出现问题。 以前,有一个bug。可能。。。不确定。在

工作代码:

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=0) #InputConnections=0
circle3 = cmd.duplicate(circle1[0], ic=0) #InputConnections=0
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)

相关问题 更多 >

    热门问题