[PYTHON] return sorted dictionary

Hey guys,

I am stuck with trying to recreate the lights structure inside maya.
My dictionary returns wildly random mixed lights.

I tried implementing the Ordered dict function but not sure how to use it on the code.
https://docs.python.org/2/library/collections.html#collections.OrderedDict

here my code:


			allObjects['LGT'] = {"type": "transform", "parent": None}
			

			for i in range (0, x):

				transform, shape = self.getTransformAndShape(allTransforms[i])
				lgt = transform.split('|')[-1:][0]
	

							
				if self.isLight(shape):
					parents = mc.listRelatives(transform, parent=True)
					parent = None
					if parents:
						parent = parents[0]
					if parent == "lgt":
						parent = None
					
					allObjects[lgt] = {"type": "light", "parent": parent}

					
				elif self.isLightSource(shape):
					
					parents = mc.listRelatives(transform, parent=True)
					parent = None
					if parents:
						parent = parents[0]
					if parent == "lgt":
						parent = None

					allObjects[lgt] = {"type": "meshLight", "parent": parent}

					
					
					
				elif self.isEnvironmentLight(shape):
					
					parents = mc.listRelatives(transform, parent=True)

					parent = None
					if parents:
						parent = parents[0]
					if parent == "lgt":
						parent = None
					allObjects[lgt] = {"type": "environmentLight", "parent": parent}

					

				elif self.hasLightsChildren(transform):

					if transform == "lgt":
						pass
					
					
					parents = mc.listRelatives(transform, parent=True)
					
					parent = None
					if parents:
						parent = parents[0]
					if parent == "lgt":
						parent = None
					
					
					allObjects[lgt] = {"type": "transform", "parent": parent}


                   return allObjects

CODE BREAKDOWN
the output is:

@

				if self.isLight(shape):
					parents = mc.listRelatives(transform, parent=True)
					parent = None
					if parents:
						parent = parents[0]
					if parent == "lgt":
						parent = None
					
					allObjects[lgt] = {"type": "light", "parent": parent}

:

allObjects returns: {u’TEST_KEY’: {‘type’: ‘light’, ‘parent’: u’TEST_lights’}, u’ALL_KEY16’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY15’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY14’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY11’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY10’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’TEST_KEY1’: {‘type’: ‘light’, ‘parent’: u’TEST_lights’}, u’TEST_KEY2’: {‘type’: ‘light’, ‘parent’: u’TEST_lights’}, u’ALL_KEY_switchtest’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’LOC_KEY’: {‘type’: ‘light’, ‘parent’: u’LOC_lights’}, u’TEST_lights’: {‘type’: ‘transform’, ‘parent’: u’TEST’}, u’ALL’: {‘type’: ‘transform’, ‘parent’: u’LGT’}, u’ALL_KEY8’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY3’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY2’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY1’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY7’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY6’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY5’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY4’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_lights’: {‘type’: ‘transform’, ‘parent’: u’ALL’}, u’TEST’: {‘type’: ‘transform’, ‘parent’: u’LGT’}, ‘LGT’: {‘type’: ‘transform’, ‘parent’: None}}

parent returns: ALL_lights
parent returns: ALL_lights
parent returns: ALL_lights
parent returns: ALL_lights
parent returns: ALL_lights
parent returns: ALL_lights
parent returns: ALL_lights
parent returns: ALL_lights
parent returns: ALL_lights
parent returns: ALL_lights
parent returns: ALL_lights
parent returns: ALL_lights
parent returns: ALL_lights
parent returns: ALL_lights
parent returns: ALL_lights
parent returns: TEST_lights
parent returns: TEST_lights
parent returns: TEST_lights
parent returns: LOC_lights
parent returns: LOC_lights

lgt returns: ALL_KEY
lgt returns: ALL_KEY1
lgt returns: ALL_KEY2
lgt returns: ALL_KEY3
lgt returns: ALL_KEY4
lgt returns: ALL_KEY5
lgt returns: ALL_KEY6
lgt returns: ALL_KEY_switchtest
lgt returns: ALL_KEY7
lgt returns: ALL_KEY8
lgt returns: ALL_KEY14
lgt returns: ALL_KEY15
lgt returns: ALL_KEY16
lgt returns: ALL_KEY10
lgt returns: ALL_KEY11
lgt returns: TEST_KEY
lgt returns: TEST_KEY1
lgt returns: TEST_KEY2
lgt returns: LOC_KEY
lgt returns: LOC_KEY1

  • the sort order of lgt is still correct here, and that’s how it needs to appear in the groups when parented here further down.

@

	elif self.hasLightsChildren(transform):

					if transform == "lgt":
						pass
					
					
					parents = mc.listRelatives(transform, parent=True)
					
					parent = None
					if parents:
						parent = parents[0]
					if parent == "lgt":
						parent = None
					
					print 'allObjects returns: %s' %allObjects
					#print 'parent returns: %s' %parent					
					allObjects[lgt] = {"type": "transform", "parent": parent}

:

allObjects returns: {u’TEST_KEY’: {‘type’: ‘light’, ‘parent’: u’TEST_lights’}, u’LOC_lights’: {‘type’: ‘transform’, ‘parent’: u’LOC’}, u’ALL_KEY16’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY15’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY14’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY11’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY10’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’TEST_KEY1’: {‘type’: ‘light’, ‘parent’: u’TEST_lights’}, u’TEST_KEY2’: {‘type’: ‘light’, ‘parent’: u’TEST_lights’}, u’ALL_KEY_switchtest’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’LOC_KEY1’: {‘type’: ‘light’, ‘parent’: u’LOC_lights’}, u’LOC_KEY’: {‘type’: ‘light’, ‘parent’: u’LOC_lights’}, u’TEST_lights’: {‘type’: ‘transform’, ‘parent’: u’TEST’}, u’ALL’: {‘type’: ‘transform’, ‘parent’: u’LGT’}, u’ALL_KEY8’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY3’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY2’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY1’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY7’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY6’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY5’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY4’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_lights’: {‘type’: ‘transform’, ‘parent’: u’ALL’}, u’TEST’: {‘type’: ‘transform’, ‘parent’: u’LGT’}, ‘LGT’: {‘type’: ‘transform’, ‘parent’: None}}

parent returns: ALL
parent returns: LGT
parent returns: TEST
parent returns: LGT
parent returns: LOC
parent returns: LGT

lgt returns: ALL_lights
lgt returns: ALL
lgt returns: TEST_lights
lgt returns: TEST
lgt returns: LOC_lights
lgt returns: LOC

for the purpose of simplicity I only attached the returns of the transforms and groups.

now the final output is:

allObjects output is: {u’TEST_KEY’: {‘type’: ‘light’, ‘parent’: u’TEST_lights’}, u’LOC’: {‘type’: ‘transform’, ‘parent’: u’LGT’}, u’LOC_lights’: {‘type’: ‘transform’, ‘parent’: u’LOC’}, u’ALL_KEY16’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY15’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY14’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY11’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY10’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’TEST_KEY1’: {‘type’: ‘light’, ‘parent’: u’TEST_lights’}, u’TEST_KEY2’: {‘type’: ‘light’, ‘parent’: u’TEST_lights’}, u’ALL_KEY_switchtest’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’LOC_KEY1’: {‘type’: ‘light’, ‘parent’: u’LOC_lights’}, u’LOC_KEY’: {‘type’: ‘light’, ‘parent’: u’LOC_lights’}, u’TEST_lights’: {‘type’: ‘transform’, ‘parent’: u’TEST’}, u’ALL’: {‘type’: ‘transform’, ‘parent’: u’LGT’}, u’ALL_KEY8’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY3’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY2’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY1’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY7’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY6’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY5’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_KEY4’: {‘type’: ‘light’, ‘parent’: u’ALL_lights’}, u’ALL_lights’: {‘type’: ‘transform’, ‘parent’: u’ALL’}, u’TEST’: {‘type’: ‘transform’, ‘parent’: u’LGT’}, ‘LGT’: {‘type’: ‘transform’, ‘parent’: None}}

as we see, the sort order is complete random.
How can I append to the dictionary while maintaining the order how he loops?

thanks a lot for help, I tried all kind of variations with the OrderedDict module but I don’t know how to write the code to get it ordered. I’m pretty much a dictionary noob and trying for a few weeks now to get familiar with it.

big big thank you for your time.
Bastian

OrderedDict preserves the the order in which items were added, so you’d need to presort the items into a list of key-value pairs in before converting to an OederedDict:


z = ("z", 1)
x= ("x", 2)
y = ("y", 3)
items = [z,x,y]
items.sort()
print OrderedDict(items)
# OrderedDict([('x',2), ('y',3), ('z':1)])

items added to the OrderedDict with square brackets will retain the order in which they were added:


test = OrderedDict()
test['z'] = 1
test['a'] = 2
print test
# OrderedDict([('z':1), ('a':2)])

In many cases OrderedDicts are not recessary. In your case can’t you just maintain a separate list for each type? So the dictionary would look like


light_types = {
    'TEST_Light': ['TEST_KEY1', 'TEST_KEY2', ....],
    'ALL_Lights': ['ALL_KEY4', 'ALL_KEY3',....],
   ... etc
}

You can sort the lists easily, and if you care about the order of getting things out of the dictionary you can just put that order into a list:


light_keys = ['ALL_lights', 'TEST_'lights', ...]
for lk in light_keys:
     print lk
     for light in light_types[lk]:
          print "/t", light