From ea43cdd868643a26c4e00677d3b67e8f8659b775 Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Mon, 22 Jan 2018 08:27:56 +0100 Subject: [PATCH] Remove cb dependency for getting highest in hierarchy - embed in lib.py --- colorbleed/maya/lib.py | 50 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/colorbleed/maya/lib.py b/colorbleed/maya/lib.py index 82ab58e5f4..8eb4d5799a 100644 --- a/colorbleed/maya/lib.py +++ b/colorbleed/maya/lib.py @@ -1126,8 +1126,56 @@ def get_container_transforms(container, members=None, root=False): results = cmds.ls(members, type="transform", long=True) if root: - root = core.getHighestInHierarchy(results) + root = get_highest_in_hierarchy(results) if root: results = root[0] return results + + +def get_highest_in_hierarchy(nodes): + """Return highest nodes in the hierarchy that are in the `nodes` list. + + The "highest in hierarchy" are the nodes closest to world: top-most level. + + Args: + nodes (list): The nodes in which find the highest in hierarchies. + + Returns: + list: The highest nodes from the input nodes. + + """ + + # Ensure we use long names + nodes = cmds.ls(nodes, long=True) + lookup = set(nodes) + + highest = [] + for node in nodes: + # If no parents are within the nodes input list + # then this is a highest node + if not any(n in lookup for n in iter_parents(node)): + highest.append(node) + + return highest + + +def iter_parents(node): + """Iter parents of node from its long name. + + Note: The `node` *must* be the long node name. + + Args: + node (str): Node long name. + + Yields: + str: All parent node names (long names) + + """ + while True: + split = node.rsplit("|", 1) + if len(split) == 1: + return + + node = split[0] + yield node