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

Maya transform node not appearing in list

下面的 Maya python 代码通过首先取两个 nurbs 球体 nurbsSphere1 和 nurbsSphere2 的差值来给出 nurbs 布尔表面 nurbsBooleanSurface1。然后它取此表面与第三个球体 nurbsSphere3 的差值。如大纲中所示,结果是三个 nurbs 球体加上一个 surfaceVarGroup,nurbsBooleanSurface1,其中 'parents' 三个变换节点 nurbsBooleanSurface1_1、nurbsBooleanSurface1_2 和 nurbsBooleanSurface1_3.

import maya.cmds as cmds

cmds.sphere(nsp=10, r=50)

cmds.sphere(nsp=4, r=5)
cmds.setAttr("nurbsSphere2.translateX",-12.583733)
cmds.setAttr("nurbsSphere2.translateY",-2.2691557)
cmds.setAttr("nurbsSphere2.translateZ",48.33736)

cmds.nurbsBoolean("nurbsSphere1", "nurbsSphere2", nsf=1, op=1)

cmds.sphere(nsp=4, r=5)
cmds.setAttr("nurbsSphere3.translateX",-6.7379503)
cmds.setAttr("nurbsSphere3.translateY",3.6949043)
cmds.setAttr("nurbsSphere3.translateZ",49.40595)

cmds.nurbsBoolean("nurbsBooleanSurface1", "nurbsSphere3", nsf=1, op=1)

print(cmds.ls("nurbsBooleanSurface1_*", type="transform"))

Strangley(对我来说),列表命令 cmds.ls("nurbsBooleanSurface1_*", type="transform") 只产生 [u'nurbsBooleanSurface1_1', u'nurbsBooleanSurface1_2']; nurbsBooleanSurface1_3 缺失。

但是当执行完上面的代码后,打印命令

print(cmds.ls("nurbsBooleanSurface1_*", type="transform"))

重新执行,结果为[u'nurbsBooleanSurface1_1', u'nurbsBooleanSurface1_2', u'nurbsBooleanSurface1_3'].

我曾尝试使用 time.sleep(n) 延迟执行最终打印命令,但无济于事。我曾经考虑过这样一个想法,即丢失的节点可能已经脱离到另一个命名空间,然后在执行块完成时重新出现(绝望,我知道!)。我已经尝试使用函数和线程(后者只是表面上)重命名球体和表面。

第一次执行未列出 nurbsBooleanSurface1_3 的原因
print(cmds.ls("nurbsBooleanSurface1_*", type="transform"))

仍然是个谜。任何帮助将不胜感激。

一种肮脏的方法(但我能找到的唯一方法)是在脚本期间调用 cmds.refresh()

我在这里重写了你的脚本。请注意,我将每个球体存储在一个变量中,这是确保它能正常工作的好习惯,即使现有对象已经被称为 nurbsSphere3。

import maya.cmds as cmds

sphere1 = cmds.sphere(nsp=10, r=50)

sphere2 = cmds.sphere(nsp=4, r=5)
cmds.setAttr(sphere2[0] + ".translateX",-12.583733)
cmds.setAttr(sphere2[0] + ".translateY",-2.2691557)
cmds.setAttr(sphere2[0] + ".translateZ",48.33736)

nurbsBool1 = cmds.nurbsBoolean("nurbsSphere1", "nurbsSphere2", nsf=1, op=1)

sphere3 = cmds.sphere(nsp=4, r=5)
cmds.setAttr(sphere3[0] + ".translateX",-6.7379503)
cmds.setAttr(sphere3[0] + ".translateY",3.6949043)
cmds.setAttr(sphere3[0] + ".translateZ",49.40595)

nurbsBool2 = cmds.nurbsBoolean(nurbsBool1[0], sphere3[0], nsf=1, op=1)

cmds.refresh(currentView=True)  # Force evaluation, of current view only

print(cmds.listRelatives(nurbsBool2[0], children=True, type="transform"))

当您使用 cmds.sphere() 创建对象时,它会 returns 对象名称列表等。要访问它,您可以使用

mySphere = cmds.sphere()
print(mySphere)  
# Result: [u'nurbsSphere1', u'makeNurbSphere1']

print(mySphere[0])  # the first element in the list is the object name
# Result: nurbsSphere1

布尔运算也是如此。在文档中查找 Return value http://help.autodesk.com/cloudhelp/2016/ENU/Maya-Tech-Docs/CommandsPython/index.html

下的命令