Python Logic "LoopingIterations of itself" (Noobie)

Hey guys I am currently working on a script and i’m trying to figure out a method which this could work. I was wondering if you guys could help me out or point me in the right direction

I have a script which I want to traverse down a joint system get the data and then place them back together

joint 1
—joint 2
—joint 3
------joint 4

(joint name) (joint children) (joint rotation) (joint Orientation)
shapes = [‘joint1’, [u’joint2’, u’joint3’], [0.0, 0.0, 0.0], [0.0, 112.6421802471919, 0.0]]

so I figure i would make a function like this

def my function(shapes):
masterlist = []
if rootjoint[1] == None:
skip? or end? *(don’t really know what to write here to make the script keep going and end on lists which simply have no children)
else:
datasource = shapes[1]
for childjoinSet in datasource:
masterlist.append(JointListDataChildRotOri(childjoinSet)) ##### (Creates more of the lists from other joints i.e [‘joint2’, None, [0.0, 0.0, 0.0], [0.0, 112.6421802471919, 0.0]])

So on onto its children resulting in something like this

[‘joint1’, [u’joint2’, u’joint3’], [0.0, 0.0, 0.0], [0.0, 112.6421802471919, 0.0]], [‘joint2’, None, [0.0, 0.0, 0.0], [0.0, 112.6421802471919, 0.0]] , [‘joint3’, [u’joint 4’], [0.0, 0.0, 0.0], [0.0, 112.6421802471919, 0.0]], [‘joint4’, None, [0.0, 0.0, 0.0], [0.0, 112.6421802471919, 0.0]]

With this i was hoping to attach and reattach joints structures back again using the parent joints and children joints as well as store needed information.

Thanks in advance for the help! and i’ll probably try clarifying it a bit more since i’m not sure if i’m clear enough.

Can you please us the Code tags python code is pretty much un readable with out proper indentation.

What you will most likely have to do is use a recursive function, a function that excepts a argument, that runs its self during its execution. This is how hierarchy based stuff is usually traversed.


import maya.cmds as cmds

########################
def TupleToList(sourceTuple):
    if type(sourceTuple) == list:
        sourceTuple = list(sourceTuple[0])
        return sourceTuple
    else:
        sourceTuple = list(sourceTuple)
        return sourceTuple
########################
def ChildRotOriList(BaseJoint, ChildLis, RotLis, OriLis):		
	ChildRotOrimyList = []
	ChildRotOrimyList.append(BaseJoint)
	ChildRotOrimyList.append(ChildLis)
	ChildRotOrimyList.append(TupleToList(RotLis))
	ChildRotOrimyList.append(TupleToList(OriLis))
	return ChildRotOrimyList
########################

def JointListDataChildRotOri(sourcejoint):
    ChildrenInnList = cmds.listRelatives( sourcejoint, children=True)
    RotationInnList = cmds.getAttr(sourcejoint + '.rotate')
    JointOriInnList = cmds.getAttr(sourcejoint + '.jointOrient')
    JointDataList = ChildRotOriList(sourcejoint, ChildrenInnList, RotationInnList, JointOriInnList)
    return JointDataList
########################

def QuerySourceChildren(listsourceData):
	if listsourceData[1] == None:
		return "false"
	else:
		for Subchild in listsourceData[1]:
			JointListDataChildRotOri[child]
#######################



something like this is what i have as progress. I got them into the list However i’m trying to figure out a way to get it so when i produce a list it would produce more lists if there is a list within listsourceData[1]
then append it back into a massive list with all the bits and pieces.

end list result would look like this:
[‘joint1’, [u’joint2’, u’joint3’], [0.0, 0.0, 0.0], [0.0, 112.6421802471919, 0.0]], [‘joint2’, None, [0.0, 0.0, 0.0], [0.0, 112.6421802471919, 0.0]] , [‘joint3’, [u’joint 4’], [0.0, 0.0, 0.0], [0.0, 112.6421802471919, 0.0]], [‘joint4’, None, [0.0, 0.0, 0.0], [0.0, 112.6421802471919, 0.0]]

then to figure out how to parent it all back together again but baby-steps for me first.

here’s a couple of things you can do to make this easier on yourself:

#1 : order the data. It’s hard to work with things by position, so you can use the excellent namedtuple class to make a nice readable object for each of your entries:


from collections import namedtuple
bone_record = namedtuple('bone', ['name', 'rot', 'orient'])

def get_bone(bone):
     ro = cmds.getAttr(bone + ".r")
     jo = cmds.getAttr(bone + ".jo")
     return bone_record(bone, ro, jo)

print get_bone('Character')

bone(name='Character', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)])

a namedtuple is still a tuple, so bone[2] is the same as bone.orient, but obviously the second one is easier to read

2: recursions

Recursive functions are great for what you want to do, but they have some subtleties in python, especially in maya.

Here’s the minimal example:


def hierarchy(bone):
    for item in cmds.listRelatives(bone, c=True) or []:
        for grandchild in hierarchy(item):
            yield grandchild
    yield bone
    
for item in hierarchy("Character"):
        print item
R_Toe_Tip
R_Toe_orientConstraint1
R_Toe
R_Ankle_orientConstraint1
R_Ankle
R_Knee_orientConstraint1
R_Knee
R_Hip_orientConstraint1
R_Hip
L_Toe_Tip
L_Toe_orientConstraint1
L_Toe
L_Ankle_orientConstraint1
L_Ankle
L_Knee_orientConstraint1
L_Knee
L_Hip_orientConstraint1
L_Hip
R_IndexC_Tip
R_IndexC_orientConstraint1
R_IndexC
R_IndexB_orientConstraint1
R_IndexB
R_IndexA_orientConstraint1
R_IndexA
R_MiddleC_Tip
R_MiddleC_orientConstraint1
R_MiddleC
R_MiddleB_orientConstraint1
R_MiddleB
R_MiddleA_orientConstraint1
R_MiddleA
R_PinkyC_Tip
R_PinkyC_orientConstraint1
R_PinkyC
R_PinkyB_orientConstraint1
R_PinkyB
R_PinkyA_orientConstraint1
R_PinkyA
R_RingC_Tip
R_RingC_orientConstraint1
R_RingC
R_RingB_orientConstraint1
R_RingB
R_RingA_orientConstraint1
R_RingA
R_ThumbC_Tip
R_ThumbC_orientConstraint1
R_ThumbC
R_ThumbB_orientConstraint1
R_ThumbB
R_ThumbA_orientConstraint1
R_ThumbA
R_Wrist_orientConstraint1
R_Wrist
R_Elbow_orientConstraint1
R_Elbow
R_Shoulder_orientConstraint1
R_Shoulder
R_Collar_orientConstraint1
R_Collar
L_IndexC_Tip
L_IndexC_orientConstraint1
L_IndexC
L_IndexB_orientConstraint1
L_IndexB
L_IndexA_orientConstraint1
L_IndexA
L_MiddleC_Tip
L_MiddleC_orientConstraint1
L_MiddleC
L_MiddleB_orientConstraint1
L_MiddleB
L_MiddleA_orientConstraint1
L_MiddleA
L_PinkyC_Tip
L_PinkyC_orientConstraint1
L_PinkyC
L_PinkyB_orientConstraint1
L_PinkyB
L_PinkyA_orientConstraint1
L_PinkyA
L_RingC_Tip
L_RingC_orientConstraint1
L_RingC
L_RingB_orientConstraint1
L_RingB
L_RingA_orientConstraint1
L_RingA
L_ThumbC_Tip
L_ThumbC_orientConstraint1
L_ThumbC
L_ThumbB_orientConstraint1
L_ThumbB
L_ThumbA_orientConstraint1
L_ThumbA
L_Wrist_orientConstraint1
L_Wrist
L_Elbow_orientConstraint1
L_Elbow
L_Shoulder_orientConstraint1
L_Shoulder
L_Collar_orientConstraint1
L_Collar
Head_Tip
R_Eye_Tip
R_Eye_orientConstraint1
R_Eye
Jaw_Tip
Jaw
L_Eye_Tip
L_Eye_orientConstraint1
L_Eye
Head_orientConstraint1
Head
Neck_orientConstraint1
Neck
Spine4_orientConstraint1
Spine4
Spine3_orientConstraint1
Spine3
Spine2_orientConstraint1
Spine2
Spine1_orientConstraint1
Spine1
Pelvis_parentConstraint1
Pelvis
Trajectory_parentConstraint1
Trajectory_pointConstraint1
Trajectory
Character

The bits worth noting are:

or []

using ‘listRelatives (c=True) or ’ fixes maya’s habit of returning ‘None’ to mean ‘no children’ which breaks the recursion. By saying ‘or ’, we replace “None” with an empty list, which keeps the loop reall y simple

Yield
Yield is basically like return – but unlike return, the code keeps running (see iterator - What does the "yield" keyword do in Python? - Stack Overflow) The good side is that you can write 3 or 4 lines to walk through something like a hierarchy. The bad side is that the result is not a return value, it’s an iterator, ie, something you can loop over popping out the values one at a time. Just trying to capture it would do this:


print hierarchy("Character")
<generator object hierarchy at 0x00000000382C5630>

However you can capture the results easily like this:



[bone for bone in hierarchy('Character')]

# Result: [u'R_Toe_Tip', u'R_Toe_orientConstraint1', u'R_Toe', u'R_Ankle_orientConstraint1', u'R_Ankle', u'R_Knee_orientConstraint1', u'R_Knee', u'R_Hip_orientConstraint1', u'R_Hip', u'L_Toe_Tip', u'L_Toe_orientConstraint1', u'L_Toe', u'L_Ankle_orientConstraint1', u'L_Ankle', u'L_Knee_orientConstraint1', u'L_Knee', u'L_Hip_orientConstraint1', u'L_Hip', u'R_IndexC_Tip', u'R_IndexC_orientConstraint1', u'R_IndexC', u'R_IndexB_orientConstraint1', u'R_IndexB', u'R_IndexA_orientConstraint1', u'R_IndexA', u'R_MiddleC_Tip', u'R_MiddleC_orientConstraint1', u'R_MiddleC', u'R_MiddleB_orientConstraint1', u'R_MiddleB', u'R_MiddleA_orientConstraint1', u'R_MiddleA', u'R_PinkyC_Tip', u'R_PinkyC_orientConstraint1', u'R_PinkyC', u'R_PinkyB_orientConstraint1', u'R_PinkyB', u'R_PinkyA_orientConstraint1', u'R_PinkyA', u'R_RingC_Tip', u'R_RingC_orientConstraint1', u'R_RingC', u'R_RingB_orientConstraint1', u'R_RingB', u'R_RingA_orientConstraint1', u'R_RingA', u'R_ThumbC_Tip', u'R_ThumbC_orientConstraint1', u'R_ThumbC', u'R_ThumbB_orientConstraint1', u'R_ThumbB', u'R_ThumbA_orientConstraint1', u'R_ThumbA', u'R_Wrist_orientConstraint1', u'R_Wrist', u'R_Elbow_orientConstraint1', u'R_Elbow', u'R_Shoulder_orientConstraint1', u'R_Shoulder', u'R_Collar_orientConstraint1', u'R_Collar', u'L_IndexC_Tip', u'L_IndexC_orientConstraint1', u'L_IndexC', u'L_IndexB_orientConstraint1', u'L_IndexB', u'L_IndexA_orientConstraint1', u'L_IndexA', u'L_MiddleC_Tip', u'L_MiddleC_orientConstraint1', u'L_MiddleC', u'L_MiddleB_orientConstraint1', u'L_MiddleB', u'L_MiddleA_orientConstraint1', u'L_MiddleA', u'L_PinkyC_Tip', u'L_PinkyC_orientConstraint1', u'L_PinkyC', u'L_PinkyB_orientConstraint1', u'L_PinkyB', u'L_PinkyA_orientConstraint1', u'L_PinkyA', u'L_RingC_Tip', u'L_RingC_orientConstraint1', u'L_RingC', u'L_RingB_orientConstraint1', u'L_RingB', u'L_RingA_orientConstraint1', u'L_RingA', u'L_ThumbC_Tip', u'L_ThumbC_orientConstraint1', u'L_ThumbC', u'L_ThumbB_orientConstraint1', u'L_ThumbB', u'L_ThumbA_orientConstraint1', u'L_ThumbA', u'L_Wrist_orientConstraint1', u'L_Wrist', u'L_Elbow_orientConstraint1', u'L_Elbow', u'L_Shoulder_orientConstraint1', u'L_Shoulder', u'L_Collar_orientConstraint1', u'L_Collar', u'Head_Tip', u'R_Eye_Tip', u'R_Eye_orientConstraint1', u'R_Eye', u'Jaw_Tip', u'Jaw', u'L_Eye_Tip', u'L_Eye_orientConstraint1', u'L_Eye', u'Head_orientConstraint1', u'Head', u'Neck_orientConstraint1', u'Neck', u'Spine4_orientConstraint1', u'Spine4', u'Spine3_orientConstraint1', u'Spine3', u'Spine2_orientConstraint1', u'Spine2', u'Spine1_orientConstraint1', u'Spine1', u'Pelvis_parentConstraint1', u'Pelvis', u'Trajectory_parentConstraint1', u'Trajectory_pointConstraint1', u'Trajectory', 'Character']


That’s called a list comprehension, it’s a neater way of writing


results = []
for bone in hierarchy('Character'):
    results.append(bone)

order
The results are ‘backwards’, because recursive algorithms will go down the hierarchy until they run out of children, and those get passed up the chain of calls so the furthest bones come back first. You can collect the results and reverse them, but you will still have issues when the hierarchy branches. However you can create a tree structure – like the original hierarchy – by collecting each node’s children before yielding them. With a small change to the original function:


def hierarchy(bone):
    children = []
    for item in cmds.listRelatives(bone, c=True) or []:
        for grandchild in hierarchy(item):
            children.append (grandchild)
    yield bone, children
[bone for bone in hierarchy("Character")]

# Result: 
[('Character',
  [(u'Pelvis',
    [(u'R_Hip',
      [(u'R_Knee',
        [(u'R_Ankle',
          [(u'R_Toe',
            [(u'R_Toe_Tip', []), (u'R_Toe_orientConstraint1', [])]),
           (u'R_Ankle_orientConstraint1', [])]),
         (u'R_Knee_orientConstraint1', [])]),
       (u'R_Hip_orientConstraint1', [])]),
     (u'L_Hip',
      [(u'L_Knee',
        [(u'L_Ankle',
          [(u'L_Toe',
            [(u'L_Toe_Tip', []), (u'L_Toe_orientConstraint1', [])]),
           (u'L_Ankle_orientConstraint1', [])]),
         (u'L_Knee_orientConstraint1', [])]),
       (u'L_Hip_orientConstraint1', [])]),
     (u'Spine1',
      [(u'Spine2',
        [(u'Spine3',
          [(u'Spine4',
            [(u'R_Collar',
              [(u'R_Shoulder',
                [(u'R_Elbow',
                  [(u'R_Wrist',
                    [(u'R_IndexA',
                      [(u'R_IndexB',
                        [(u'R_IndexC',
                          [(u'R_IndexC_Tip', []),
                           (u'R_IndexC_orientConstraint1',
                            [])]),
                         (u'R_IndexB_orientConstraint1',
                          [])]),
                       (u'R_IndexA_orientConstraint1',
                        [])]),
                     (u'R_MiddleA',
                      [(u'R_MiddleB',
                        [(u'R_MiddleC',
                          [(u'R_MiddleC_Tip', []),
                           (u'R_MiddleC_orientConstraint1',
                            [])]),
                         (u'R_MiddleB_orientConstraint1',
                          [])]),
                       (u'R_MiddleA_orientConstraint1',
                        [])]),
                     (u'R_PinkyA',
                      [(u'R_PinkyB',
                        [(u'R_PinkyC',
                          [(u'R_PinkyC_Tip', []),
                           (u'R_PinkyC_orientConstraint1',
                            [])]),
                         (u'R_PinkyB_orientConstraint1',
                          [])]),
                       (u'R_PinkyA_orientConstraint1',
                        [])]),
                     (u'R_RingA',
                      [(u'R_RingB',
                        [(u'R_RingC',
                          [(u'R_RingC_Tip', []),
                           (u'R_RingC_orientConstraint1',
                            [])]),
                         (u'R_RingB_orientConstraint1',
                          [])]),
                       (u'R_RingA_orientConstraint1',
                        [])]),
                     (u'R_ThumbA',
                      [(u'R_ThumbB',
                        [(u'R_ThumbC',
                          [(u'R_ThumbC_Tip', []),
                           (u'R_ThumbC_orientConstraint1',
                            [])]),
                         (u'R_ThumbB_orientConstraint1',
                          [])]),
                       (u'R_ThumbA_orientConstraint1',
                        [])]),
                     (u'R_Wrist_orientConstraint1', [])]),
                   (u'R_Elbow_orientConstraint1', [])]),
                 (u'R_Shoulder_orientConstraint1', [])]),
               (u'R_Collar_orientConstraint1', [])]),
             (u'L_Collar',
              [(u'L_Shoulder',
                [(u'L_Elbow',
                  [(u'L_Wrist',
                    [(u'L_IndexA',
                      [(u'L_IndexB',
                        [(u'L_IndexC',
                          [(u'L_IndexC_Tip', []),
                           (u'L_IndexC_orientConstraint1',
                            [])]),
                         (u'L_IndexB_orientConstraint1',
                          [])]),
                       (u'L_IndexA_orientConstraint1',
                        [])]),
                     (u'L_MiddleA',
                      [(u'L_MiddleB',
                        [(u'L_MiddleC',
                          [(u'L_MiddleC_Tip', []),
                           (u'L_MiddleC_orientConstraint1',
                            [])]),
                         (u'L_MiddleB_orientConstraint1',
                          [])]),
                       (u'L_MiddleA_orientConstraint1',
                        [])]),
                     (u'L_PinkyA',
                      [(u'L_PinkyB',
                        [(u'L_PinkyC',
                          [(u'L_PinkyC_Tip', []),
                           (u'L_PinkyC_orientConstraint1',
                            [])]),
                         (u'L_PinkyB_orientConstraint1',
                          [])]),
                       (u'L_PinkyA_orientConstraint1',
                        [])]),
                     (u'L_RingA',
                      [(u'L_RingB',
                        [(u'L_RingC',
                          [(u'L_RingC_Tip', []),
                           (u'L_RingC_orientConstraint1',
                            [])]),
                         (u'L_RingB_orientConstraint1',
                          [])]),
                       (u'L_RingA_orientConstraint1',
                        [])]),
                     (u'L_ThumbA',
                      [(u'L_ThumbB',
                        [(u'L_ThumbC',
                          [(u'L_ThumbC_Tip', []),
                           (u'L_ThumbC_orientConstraint1',
                            [])]),
                         (u'L_ThumbB_orientConstraint1',
                          [])]),
                       (u'L_ThumbA_orientConstraint1',
                        [])]),
                     (u'L_Wrist_orientConstraint1', [])]),
                   (u'L_Elbow_orientConstraint1', [])]),
                 (u'L_Shoulder_orientConstraint1', [])]),
               (u'L_Collar_orientConstraint1', [])]),
             (u'Neck',
              [(u'Head',
                [(u'Head_Tip', []),
                 (u'R_Eye',
                  [(u'R_Eye_Tip', []),
                   (u'R_Eye_orientConstraint1', [])]),
                 (u'Jaw', [(u'Jaw_Tip', [])]),
                 (u'L_Eye',
                  [(u'L_Eye_Tip', []),
                   (u'L_Eye_orientConstraint1', [])]),
                 (u'Head_orientConstraint1', [])]),
               (u'Neck_orientConstraint1', [])]),
             (u'Spine4_orientConstraint1', [])]),
           (u'Spine3_orientConstraint1', [])]),
         (u'Spine2_orientConstraint1', [])]),
       (u'Spine1_orientConstraint1', [])]),
     (u'Pelvis_parentConstraint1', [])]),
   (u'Trajectory',
    [(u'Trajectory_parentConstraint1', []),
     (u'Trajectory_pointConstraint1', [])])])] # 

which is more like what you want, although it’s a bit hard to read.

So, puttting it all together you can yield up those bone_records from above instead of names and you’d have the structure you want:




def hierarchy(bone):
    children = []
    for item in cmds.listRelatives(bone, c=True) or []:
        for grandchild in hierarchy(item):
            children.append (grandchild)
    yield get_bone(bone), children

[bone for bone in hierarchy("Character")]

# Result: [(record(bone='Character', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
  [(record(bone=u'Pelvis', rot=[(-9.879814783143772, 0.8408292820789117, 2.1099012177516294)], orient=[(0.0, -84.99999999999989, 90.00000000000044)]),
    [(record(bone=u'R_Hip', rot=[(0.8265624681452368, -1.8218816462029295, 363.81454736982806)], orient=[(0.0, 173.0270792174115, 0.0)]),
      [(record(bone=u'R_Knee', rot=[(-4.0715498152317406e-13, 20.149644851684553, -1.248383409283965e-16)], orient=[(0.0, 10.015884015680315, 0.0)]),
        [(record(bone=u'R_Ankle', rot=[(12.214663432584734, -3.0670476485245635, 352.0184078203879)], orient=[(0.0, -54.424501548569545, 0.0)]),
          [(record(bone=u'R_Toe', rot=[(-5.721418933434973e-14, 7.957758139005398e-30, -360.0)], orient=[(0.0, -43.48131487763502, 0.0)]),
            [(record(bone=u'R_Toe_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
              []),
             (record(bone=u'R_Toe_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
              [])]),
           (record(bone=u'R_Ankle_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
            [])]),
         (record(bone=u'R_Knee_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
          [])]),
       (record(bone=u'R_Hip_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
        [])]),
     (record(bone=u'L_Hip', rot=[(-161.57443827898396, -163.98721374729737, -197.55091761985906)], orient=[(0.0, -6.9729207825885, 0.0)]),
      [(record(bone=u'L_Knee', rot=[(2.682627323779344e-15, 20.085859298706083, -360.0)], orient=[(0.0, 10.015884015680317, 0.0)]),
        [(record(bone=u'L_Ankle', rot=[(11.007904932627657, 2.121891494221345, -359.7986158692528)], orient=[(0.0, -54.424501548569545, 0.0)]),
          [(record(bone=u'L_Toe', rot=[(2.0557092568803936e-13, -1.9083328088781044e-14, -3.20735594291083e-14)], orient=[(0.0, -43.481314877635015, 0.0)]),
            [(record(bone=u'L_Toe_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
              []),
             (record(bone=u'L_Toe_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
              [])]),
           (record(bone=u'L_Ankle_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
            [])]),
         (record(bone=u'L_Knee_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
          [])]),
       (record(bone=u'L_Hip_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
        [])]),
     (record(bone=u'Spine1', rot=[(-0.7811365360013248, 0.4922462439004074, -5.681757116438823)], orient=[(-0.006573784960485013, 0.0007591346803265704, -0.0005752188280368029)]),
      [(record(bone=u'Spine2', rot=[(-2.6661241955118595, 0.43372490120868024, 1.3046036741990734)], orient=[(0.015651545620800122, -9.999237403751252, -0.0013689186886895342)]),
        [(record(bone=u'Spine3', rot=[(-2.9220005073801163, -0.09921398528282256, 1.7879446862032324)], orient=[(-0.7086658437206859, 5.139907468791756, -0.0017560378108884924)]),
          [(record(bone=u'Spine4', rot=[(-2.327203105863261, 0.0036964836457301273, 0.14395440875910925)], orient=[(0.7375081563924253, -4.019718334042471, -0.04988576448600573)]),
            [(record(bone=u'R_Collar', rot=[(1.2710768729000798e-10, 0.0, 0.0)], orient=[(-92.33030090572728, -11.222483692313572, -97.73005785882515)]),
              [(record(bone=u'R_Shoulder', rot=[(-0.8113137885011928, -4.066616359792401, -13.240559849724919)], orient=[(83.4988325470483, 48.2618041160866, 0.5819033727079836)]),
                [(record(bone=u'R_Elbow', rot=[(-0.07131942957135864, 11.3251417600768, -0.7192720717517931)], orient=[(1.3108207314068623, 19.985473343786005, -0.7781889282141612)]),
                  [(record(bone=u'R_Wrist', rot=[(-7.130682020074875, -1.6239012983373906, 3.923213942203651)], orient=[(-3.2014475623256886, 5.906788286984406, 5.188772026571801)]),
                    [(record(bone=u'R_IndexA', rot=[(0.00503114357267409, -0.947322450809065, -3.785798670666614)], orient=[(-101.07315308881039, -3.7389598009365668, -36.655957798349434)]),
                      [(record(bone=u'R_IndexB', rot=[(0.7765740534241219, 14.873179574029077, -0.007239969567618316)], orient=[(-0.03805265140637183, 24.55237387716586, 2.5188460790642835)]),
                        [(record(bone=u'R_IndexC', rot=[(-5.376402611608926e-07, 5.759966324912045e-08, 1.0787894584998503e-06)], orient=[(3.492526291283068, 0.3355902970158527, -0.9530670538172774)]),
                          [(record(bone=u'R_IndexC_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
                            []),
                           (record(bone=u'R_IndexC_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                            [])]),
                         (record(bone=u'R_IndexB_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                          [])]),
                       (record(bone=u'R_IndexA_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                        [])]),
                     (record(bone=u'R_MiddleA', rot=[(9.985660954773621, 9.65560764432983, -2.5994981015897816)], orient=[(-96.3746903852129, -5.372436181222914, -39.983184580311196)]),
                      [(record(bone=u'R_MiddleB', rot=[(-0.2981070020781412, 16.207410714425656, 0.49509478521066846)], orient=[(2.576375763444896, 14.069822469622295, -0.17148888638882137)]),
                        [(record(bone=u'R_MiddleC', rot=[(-2.1840238518058818e-07, 3.763279758607752e-08, 5.995660606478153e-07)], orient=[(0.8138998222944301, 8.935925944234588, 0.5599464011571893)]),
                          [(record(bone=u'R_MiddleC_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
                            []),
                           (record(bone=u'R_MiddleC_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                            [])]),
                         (record(bone=u'R_MiddleB_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                          [])]),
                       (record(bone=u'R_MiddleA_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                        [])]),
                     (record(bone=u'R_PinkyA', rot=[(25.026999402787308, 12.763753027703308, 5.795234768089211)], orient=[(-71.55369956821697, -15.673853241553534, -40.827029680054814)]),
                      [(record(bone=u'R_PinkyB', rot=[(-0.8516549569727242, 41.344986283949666, -7.637357709152174)], orient=[(0.2365663087748581, 8.041305237915221, 1.341208467523328)]),
                        [(record(bone=u'R_PinkyC', rot=[(9.422856525602527e-07, 1.0209452515277054e-08, -3.511473955354424e-07)], orient=[(1.4074957612135564, 20.215782729192224, 0.41492496435138154)]),
                          [(record(bone=u'R_PinkyC_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
                            []),
                           (record(bone=u'R_PinkyC_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                            [])]),
                         (record(bone=u'R_PinkyB_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                          [])]),
                       (record(bone=u'R_PinkyA_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                        [])]),
                     (record(bone=u'R_RingA', rot=[(15.913645780571413, 12.06589546779005, 1.954841763567716)], orient=[(-87.28118910835785, -9.676405965492766, -43.11017663930668)]),
                      [(record(bone=u'R_RingB', rot=[(0.06346801332295715, 30.35455160361012, 1.8611775924023095)], orient=[(-2.9565011385407143, 14.122798491348034, -0.6806796638127749)]),
                        [(record(bone=u'R_RingC', rot=[(-2.8330568582576446e-07, -1.718603452900939e-08, 6.587818221956502e-07)], orient=[(1.1371371437506297, 8.343279044526712, 1.7369472753509578)]),
                          [(record(bone=u'R_RingC_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
                            []),
                           (record(bone=u'R_RingC_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                            [])]),
                         (record(bone=u'R_RingB_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                          [])]),
                       (record(bone=u'R_RingA_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                        [])]),
                     (record(bone=u'R_ThumbA', rot=[(-5.2215533939737195, 8.154105554514885, -12.410759214394231)], orient=[(173.42218143049723, 25.96936411197073, -47.33772250231443)]),
                      [(record(bone=u'R_ThumbB', rot=[(-0.001356106762913048, 1.910508175124555, -0.08136154571624646)], orient=[(-2.9824196406207664, 11.333226651014519, -1.0681697129854975)]),
                        [(record(bone=u'R_ThumbC', rot=[(0.8258330520922782, 20.87999597278051, 0.08944823576657202)], orient=[(-1.8977513707212288, 10.680104252549823, 1.7756002511449462)]),
                          [(record(bone=u'R_ThumbC_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
                            []),
                           (record(bone=u'R_ThumbC_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                            [])]),
                         (record(bone=u'R_ThumbB_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                          [])]),
                       (record(bone=u'R_ThumbA_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                        [])]),
                     (record(bone=u'R_Wrist_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                      [])]),
                   (record(bone=u'R_Elbow_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                    [])]),
                 (record(bone=u'R_Shoulder_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                  [])]),
               (record(bone=u'R_Collar_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                [])]),
             (record(bone=u'L_Collar', rot=[(179.18535065679353, 187.29253019459338, -173.54177628170035)], orient=[(87.65983164103841, 11.151184705189523, -82.26697825939135)]),
              [(record(bone=u'L_Shoulder', rot=[(14.329838599836311, -14.143897949969816, 331.87259628451903)], orient=[(83.49883254704828, 48.26180411608662, 0.5819033727079458)]),
                [(record(bone=u'L_Elbow', rot=[(-180.26417780748878, 158.72330824503047, 178.59557905224264)], orient=[(1.3108207314072875, 19.985473343785998, -0.778188928214148)]),
                  [(record(bone=u'L_Wrist', rot=[(-19.94764583576392, -0.07765342424013177, 26.669433998152048)], orient=[(-3.201447562326313, 5.906788286984427, 5.188772026571753)]),
                    [(record(bone=u'L_IndexA', rot=[(-1.925226495230128, 359.79404732869995, -1.023274940941629)], orient=[(-101.07315308881026, -3.7389598009364335, -36.65595779834941)]),
                      [(record(bone=u'L_IndexB', rot=[(-180.0, 179.99999999999994, 180.0)], orient=[(-0.038052651405805805, 24.55237387716586, 2.5188460790643092)]),
                        [(record(bone=u'L_IndexC', rot=[(-179.99999999999997, 180.00000000000003, 180.0)], orient=[(3.492526291282677, 0.33559029701577286, -0.9530670538171309)]),
                          [(record(bone=u'L_IndexC_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
                            []),
                           (record(bone=u'L_IndexC_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                            [])]),
                         (record(bone=u'L_IndexB_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                          [])]),
                       (record(bone=u'L_IndexA_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                        [])]),
                     (record(bone=u'L_MiddleA', rot=[(181.471059019457, 169.41265169028838, 184.35484704286893)], orient=[(-96.37469038521276, -5.372436181222724, -39.983184580311196)]),
                      [(record(bone=u'L_MiddleB', rot=[(179.57338104219934, 153.1538572294964, -179.2087268618998)], orient=[(2.5763757634450344, 14.069822469622284, -0.1714888863888362)]),
                        [(record(bone=u'L_MiddleC', rot=[(180.0, 180.0, -180.00000000000003)], orient=[(0.8138998222952822, 8.935925944234553, 0.5599464011571572)]),
                          [(record(bone=u'L_MiddleC_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
                            []),
                           (record(bone=u'L_MiddleC_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                            [])]),
                         (record(bone=u'L_MiddleB_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                          [])]),
                       (record(bone=u'L_MiddleA_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                        [])]),
                     (record(bone=u'L_PinkyA', rot=[(-168.92348005118694, -200.5753999358666, 184.81719686720314)], orient=[(-71.55369956821677, -15.67385324155335, -40.827029680054856)]),
                      [(record(bone=u'L_PinkyB', rot=[(-184.29003724535798, -237.86613212064233, 166.86177285472624)], orient=[(0.23656630877134818, 8.041305237915244, 1.3412084675233211)]),
                        [(record(bone=u'L_PinkyC', rot=[(-180.0, 180.0, 180.0)], orient=[(1.4074957612170058, 20.215782729192245, 0.4149249643526697)]),
                          [(record(bone=u'L_PinkyC_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
                            []),
                           (record(bone=u'L_PinkyC_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                            [])]),
                         (record(bone=u'L_PinkyB_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                          [])]),
                       (record(bone=u'L_PinkyA_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                        [])]),
                     (record(bone=u'L_RingA', rot=[(188.27750223187812, 170.13136184099224, 183.77616998266697)], orient=[(-87.28118910835775, -9.67640596549261, -43.11017663930668)]),
                      [(record(bone=u'L_RingB', rot=[(180.57533360425955, 135.33796910462016, -176.9841235014528)], orient=[(-2.9565011385405127, 14.122798491348048, -0.6806796638127591)]),
                        [(record(bone=u'L_RingC', rot=[(180.0, -179.99999999999997, 179.99999999999994)], orient=[(1.137137143750911, 8.343279044526717, 1.7369472753509243)]),
                          [(record(bone=u'L_RingC_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
                            []),
                           (record(bone=u'L_RingC_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                            [])]),
                         (record(bone=u'L_RingB_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                          [])]),
                       (record(bone=u'L_RingA_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                        [])]),
                     (record(bone=u'L_ThumbA', rot=[(-3.93425738885168, 370.4204230087013, -4.860732078712345)], orient=[(173.42218143049715, 25.969364111970894, -47.33772250231436)]),
                      [(record(bone=u'L_ThumbB', rot=[(-9.857155946104646e-05, 0.4163471471578494, -0.017844958056269088)], orient=[(-2.98241964062023, 11.33322665101456, -1.0681697129854595)]),
                        [(record(bone=u'L_ThumbC', rot=[(-179.35010896459826, -196.69246517154954, 180.04038332793172)], orient=[(-1.8977513707208353, 10.680104252549809, 1.7756002511448639)]),
                          [(record(bone=u'L_ThumbC_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
                            []),
                           (record(bone=u'L_ThumbC_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                            [])]),
                         (record(bone=u'L_ThumbB_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                          [])]),
                       (record(bone=u'L_ThumbA_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                        [])]),
                     (record(bone=u'L_Wrist_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                      [])]),
                   (record(bone=u'L_Elbow_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                    [])]),
                 (record(bone=u'L_Shoulder_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                  [])]),
               (record(bone=u'L_Collar_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                [])]),
             (record(bone=u'Neck', rot=[(-0.12007329592409688, -0.051547818484423455, 0.3964815796395936)], orient=[(-0.05207272791475376, 18.814509827907703, -0.013452001426144368)]),
              [(record(bone=u'Head', rot=[(23.733769860098054, 2.5194167650888475, -0.6294198783721092)], orient=[(-89.99999999999997, -14.935916866927853, 0.0)]),
                [(record(bone=u'Head_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
                  []),
                 (record(bone=u'R_Eye', rot=[(359.99999944921143, -4.452838211011711e-14, -1.2827731230502698e-10)], orient=[(0.0, -0.0, 89.99999999999999)]),
                  [(record(bone=u'R_Eye_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
                    []),
                   (record(bone=u'R_Eye_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                    [])]),
                 (record(bone=u'Jaw', rot=[(0.0, -20.032735722320083, 0.0)], orient=[(-89.99999999999994, 3.1805546814635164e-15, 100.73948122440315)]),
                  [(record(bone=u'Jaw_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
                    [])]),
                 (record(bone=u'L_Eye', rot=[(359.99999944921143, -4.452838211011711e-14, -1.2827731230502698e-10)], orient=[(0.0, -0.0, 89.99999999999999)]),
                  [(record(bone=u'L_Eye_Tip', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
                    []),
                   (record(bone=u'L_Eye_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                    [])]),
                 (record(bone=u'Head_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                  [])]),
               (record(bone=u'Neck_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
                [])]),
             (record(bone=u'Spine4_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
              [])]),
           (record(bone=u'Spine3_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
            [])]),
         (record(bone=u'Spine2_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
          [])]),
       (record(bone=u'Spine1_orientConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
        [])]),
     (record(bone=u'Pelvis_parentConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
      [])]),
   (record(bone=u'Trajectory', rot=[(0.0, 0.0, 0.0)], orient=[(0.0, 0.0, 0.0)]),
    [(record(bone=u'Trajectory_parentConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
      []),
     (record(bone=u'Trajectory_pointConstraint1', rot=[(0.0, 0.0, 0.0)], orient=None),
      [])])])] # 

import maya.cmds as cmds

masterListSource = {}
    ########################
def TupleToList(sourceTuple):
    if type(sourceTuple) == list:
        sourceTuple = list(sourceTuple[0])
        return sourceTuple
    else:
        sourceTuple = list(sourceTuple)
        return sourceTuple
########################
def ChildRotOriList(BaseJoint, ChildLis, RotLis, OriLis):		
	ChildRotOrimyList = []
	ChildRotOrimyList.append(ChildLis)
	ChildRotOrimyList.append(TupleToList(RotLis))
	ChildRotOrimyList.append(TupleToList(OriLis))
	return ChildRotOrimyList
########################

def JointListDataChildRotOri(sourcejoint):
    ChildrenInnList = cmds.listRelatives( sourcejoint, children=True) or []
    RotationInnList = cmds.getAttr(sourcejoint + '.rotate')
    JointOriInnList = cmds.getAttr(sourcejoint + '.jointOrient')
    JointDataList = ChildRotOriList(sourcejoint, ChildrenInnList, RotationInnList, JointOriInnList)
    CheckSourceChildren(JointDataList)
    masterListSource[sourcejoint] = (JointDataList)
    #return JointDataList
    
########################

def CheckSourceChildren(listsourceData):
	if listsourceData[0] == None:
		print masterListSource
	else:
		for SubchildList in listsourceData[0]:
			JointListDataChildRotOri(SubchildList)
		
JointListDataChildRotOri("joint1")

print masterListSource

########################
########################

def ReconnectJointSys(rootJointSys):    
    if masterListSource[rootJointSys][0] == None:
        print masterListSource
    else:
        for JointchildConnect in masterListSource[rootJointSys][0]:
            ParentingJointListDataChildRotOri(JointchildConnect)

def ParentingJointListDataChildRotOri(sourcejoint):
    childjoints = masterListSource[sourcejoint][0]
    cmds.parent( childjoints, sourcejoint)
    ReconnectJointSys(childjoints)
    
ParentingJointListDataChildRotOri("joint1")

so the code has been updated a bit and Holy cow thanks for the quick reply(haven’t gone through all of theo’s advice (yeild and recursion)):

But i did make some changes:

ChildrenInnList = cmds.listRelatives( sourcejoint, children=True) or []

masterListSource to a dictionary for easier joint access.

I started working on try to reconnect the joints again however it seems like I’ve run into some issues also.

with the script portion:

def ReconnectJointSys(rootJointSys):    
    if masterListSource[rootJointSys][0] == None:
        print masterListSource
    else:
        for JointchildConnect in masterListSource[rootJointSys][0]:
            ParentingJointListDataChildRotOri(JointchildConnect)

def ParentingJointListDataChildRotOri(sourcejoint):
    childjoints = masterListSource[sourcejoint][0]
    cmds.parent( childjoints, sourcejoint)
    ReconnectJointSys(childjoints)
    
ParentingJointListDataChildRotOri("joint1")

it seems like its not playing well with it after I’ve created the masterlist.

the error i receive is:

# Error: TypeError: file <maya console> line 2: unhashable type: ‘list’ #

Any clue on what i may be doing wrong would be much apperciated and as always thanks a ton for all your help!

‘unhashable type’ means you’re trying to use a list as a dictionary key, which is not possible. If you can make the list into a tuple (and you don’t need to change it’s contents) that would work as an alternative