测试一个对象是否依赖于另一个对象

2024-06-16 11:17:04 发布

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

有没有一种方法可以检查一个对象是否依赖于另一个对象的父对象、约束或连接?我想在为一个对象建立父子关系之前做这个检查,看看它是否会导致依赖循环。在

我记得3DsMax有一个命令可以做到这一点。我检查了OpenMaya,但什么也没找到。有cmds.cycleCheck,但这只在当前有一个循环时有效,这对我来说太晚了。在

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


编辑

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

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

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


Tags: 对象方法命令true编辑层次结构场景children
2条回答

这不是最优雅的方法,但它是一种快速而肮脏的方法,目前看来效果还不错。其思想是,如果发生循环,则只需撤消操作并停止脚本的其余部分。用钻机测试,不管连接有多复杂,它都会抓住它。在

# 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!")

根据您要查找的内容,cmds.listHistory或{}将告诉您给定节点的内容。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]

它应该生成一个嵌套的输入连接列表,比如

^{pr2}$

其中每个级别都包含一个输入列表。然后你可以浏览一下,看看你提议的变更是否包括这个项目。在

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

相关问题 更多 >