有没有一种方法可以检查对象是否通过育儿,约束或与另一个对象的连接而相互依赖?我想在为对象添加父对象之前进行此检查,以查看它是否会导致依赖循环。
我记得3DsMax有一个命令可以准确地做到这一点。我检查了OpenMaya
但找不到任何东西。有cmds.cycleCheck
,但这仅在当前有一个周期时才有效,这对我来说太晚了。
棘手的是,这两个对象可能位于场景层次结构中的任何位置,因此它们可能具有或不具有直接的育儿关系。
编辑
检查层次结构是否会引起任何问题相对容易:
children = cmds.listRelatives(obj1, ad = True, f = True)
if obj2 in children:
print "Can't parent to its own children!"
但是,检查约束或连接是否是另一回事。
这不是最优雅的方法,但是到目前为止,这似乎是一种行之有效的快捷方法。想法是,如果发生周期,则只需撤消该操作并停止脚本的其余部分即可。使用钻机进行测试,连接的复杂程度无关紧要,它都能捕获到。
# Class to use to undo operations
class UndoStack():
def __init__(self, inputName = ''):
self.name = inputName
def __enter__(self):
cmds.undoInfo(openChunk = True, chunkName = self.name, length = 300)
def __exit__(self, type, value, traceback):
cmds.undoInfo(closeChunk = True)
# Create a sphere and a box
mySphere = cmds.polySphere()[0]
myBox = cmds.polyCube()[0]
# Parent box to the sphere
myBox = cmds.parent(myBox, mySphere)[0]
# Set constraint from sphere to box (will cause cycle)
with UndoStack("Parent box"):
cmds.parentConstraint(myBox, mySphere)
# If there's a cycle, undo it
hasCycle = cmds.cycleCheck([mySphere, myBox])
if hasCycle:
cmds.undo()
cmds.warning("Can't do this operation, a dependency cycle has occurred!")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句