How to select root of two joint hierarchies and add each to list in Python?

How to select root of two joint hierarchies and add each to list in Python?

mostly_human
Advocate Advocate
1,584 Views
3 Replies
Message 1 of 4

How to select root of two joint hierarchies and add each to list in Python?

mostly_human
Advocate
Advocate

I am learning Python and trying to figure out the code where the user can select the root of two joint hierarchies and it will store each hierarchy in their own list.  This will be for operations on two separate character skeletons so each list needs to have every joint from its skeleton in its respective list. It seems like I would use scriptCtx but i'm still struggling with it a bit. I'm also working from the assumption that as long as the two skeletons are identically structured the resulting two lists will have the same joints in the same index [0]=hips [1]=pelvis and so on.

0 Likes
Accepted solutions (1)
1,585 Views
3 Replies
Replies (3)
Message 2 of 4

olarn
Advocate
Advocate
Accepted solution

Would this be good enough?

select first, second root then run.

def demo():
    import pymel.core as pm
    skl_root1, skl_root2 = pm.ls(selection=True)
    print("root1 {0} root2 {1}".format(skl_root1, skl_root2))

    def hie_to_list(root_node):
        return root_node.getChildren(allDescendents=True)

    skl_1_childrens = hie_to_list(skl_root1)
    skl_2_childrens = hie_to_list(skl_root2)

    assert len(skl_1_childrens) == len(skl_2_childrens)

    print("idx, child1, child2")
    for i, (n1, n2,) in enumerate(
            zip(skl_1_childrens, skl_2_childrens)
    ):
        print(
            "{0}: \n"
            "\troot_1: {1}\n"
            "\troot_2: {2}".format(i, n1, n2)
        )

demo()
0 Likes
Message 3 of 4

mostly_human
Advocate
Advocate

Thank you, Olarn! This is almost working, looks like one skeleton has some constraints that the other doesn't have so its causing the indexes to lose sync, I added a type=joint to fix that issue, then I realized one skeleton does have some extra bones, could I hide these bones and then specify in Python to ignore hidden?  If so how could I do that? I tried searching and but couldn't find a clear answer.

0 Likes
Message 4 of 4

olarn
Advocate
Advocate

There are several ways to filter node of specific type

 

using type flag
http://help.autodesk.com/cloudhelp/2018/ENU/Maya-Tech-Docs/CommandsPython/listRelatives.html#flagtyp...

        return root_node.getChildren(
            allDescendents=True,
            type="joint"
        )

Using list comprehension

        list_or_something = root_node.getChildren(
            allDescendents=True,
        )

        return [
            n
            for
            n in list_or_something
            if n.type() == "joint"
        ]

etc

0 Likes