2

下面的 Maya python 代码通过首先获取两个 nurbs 球体 nurbsSphere1 和 nurbsSphere2 的差异来给出 nurbs 布尔曲面,从而给出 nurbs 曲面 nurbsBooleanSurface1。然后,它获取此曲面和第三个球体 nurbsSphere3 的差异。结果,如大纲所示,是三个 nurbs 球体加上一个 surfaceVarGroup,nurbsBooleanSurface1,它“父”三个变换节点 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"))

仍然是一个谜。任何帮助将非常感激。

4

1 回答 1

2

一种肮脏的方式(但我能找到的唯一方式)是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()它创建一个对象时,它会返回一个对象名称列表等。要访问它,您可以使用

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

布尔运算也是如此。在返回值 http://help.autodesk.com/cloudhelp/2016/ENU/Maya-Tech-Docs/CommandsPython/index.html下查看命令的文档

于 2015-12-01T11:18:48.030 回答