3

有没有办法通过父母、约束或与另一个对象的连接来检查一个对象是否依赖?我想在养育一个对象之前做这个检查,看看它是否会导致依赖循环。

我记得 3DsMax 有一个命令可以准确地执行此操作。我检查了OpenMaya但找不到任何东西。有cmds.cycleCheck,但这仅在当前有一个周期时才有效,这对我来说太晚了。

棘手的是这两个对象可能位于场景层次结构中的任何位置,因此它们可能有也可能没有直接的父子关系。


编辑

检查层次结构是否会导致任何问题相对容易:

children = cmds.listRelatives(obj1, ad = True, f = True)
if obj2 in children:
    print "Can't parent to its own children!"

不过,检查约束或连接是另一回事。

4

2 回答 2

2

取决于您要查找的内容,cmds.listHistory或者cmds.listConnections会告诉您给定节点的内容。 listHistory仅限于驱动形状节点更改的可能连接的子集,因此如果您对约束感兴趣,则需要遍历listConnections节点并查看上游的内容。该列表可以任意大,因为它可能包含许多隐藏节点,例如您可能不想关心的单元翻译、组部分等。

这是控制节点的传入连接并获取传入连接树的简单方法:

def input_tree(root_node):
    visited = set()  # so we don't get into loops

    # recursively extract input connections
    def upstream(node,  depth = 0):    
        if node not in visited:
            visited.add(node)
            children = cmds.listConnections(node, s=True, d=False)
            if children:
                grandparents  = ()
                for history_node in children:
                    grandparents += (tuple(d for d in  upstream(history_node, depth + 1)))
                yield node,  tuple((g for g in grandparents if len(g)))

    # unfold the recursive generation of the tree
    tree_iter = tuple((i for i  in upstream(root_node)))
    # return the grandparent array of the first node
    return tree_iter[0][-1]

这应该产生一个嵌套的输入连接列表,例如

 ((u'pCube1_parentConstraint1',
   ((u'pSphere1',
     ((u'pSphere1_orientConstraint1', ()),
     (u'pSphere1_scaleConstraint1', ()))),)),
   (u'pCube1_scaleConstraint1', ()))

其中每个级别都包含一个输入列表。然后,您可以浏览它以查看您提议的更改是否包含该项目。

但是,这不会告诉您连接是否会导致真正的循环:这取决于不同节点内的数据流。一旦你确定了可能的循环,你就可以回去看看循环是真实的(例如,两个项目会影响彼此的翻译)还是无害的(我影响你的轮换,你影响我的翻译)。

于 2015-10-29T21:10:32.123 回答
1

这不是最优雅的方法,但它是一种快速而肮脏的方法,到目前为止似乎工作正常。这个想法是,如果发生循环,则只需撤消操作并停止脚本的其余部分。使用钻机进行测试,无论连接多么复杂,它都会抓住它。

# 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!")
于 2015-10-30T02:44:24.870 回答