Script execution order problem.

I have a script in which I’m importing data from another program, to correct for the ZY reversal I rotate the objects 90 degrees. After all objects have been positioned and key framed I run a function that does any necessary parenting of objects. The problem is even though the script executes in that order the parenting seems to happen before the rotation. Does anyone know why this would be? Are there any helper functions to make sure something runs after objects have been added and their data set? Like an internal “after” method you could places your code in?

The following is a part of the script, which shows the execution order. The main function “run” is called, which loops through data creating new blender objects of different types (and adding them to the scene), in each loop it checks for a parent property as well and adds the child and parent name to an array for later use. After all the data has been looped through and the objects created the set_parents() function is run, which goes through the before mentioned array and created any necessary parent/child relationships.

Thanks, any help is appreciated.


class Main():
  def __init__(self, json_data):
    self.json_data = json_data
    self.defaultComp = json_data['compositions'][0]
    self.compSettings = self.defaultComp['compSettings']
    self.relationships = []
    #['ObjectName1':'ParentName1', 'ObjectName2:ParentName2']


  def run(self):
    layers = self.defaultComp['layers']
    for layer in layers:  
      layerType = layer['layerType']


      if(layerType == 'Null'):
        obj = Empty(self.compSettings, layer)
      elif(layerType == 'Solid'):
        obj = Mesh(self.compSettings, layer)
      elif(layerType == 'Camera'):
        obj = Camera(self.compSettings, layer)
      
      self.check_for_parent(layer)
    self.set_parents()


  def check_for_parent(self, child):
    if(child['parent'] != 0):
      self.add_to_relationships(child['parent'], child['name'])


  def add_to_relationships(self, parentName, childName):
    self.relationships.append({'parent': parentName, 'child': childName})


  def set_parents(self):
    print('set_parents')
    for relation in self.relationships:
      child  = bpy.data.objects[relation['child']]
      parent = bpy.data.objects[relation['parent']]
      child.parent = parent

Full script here https://github.com/ichabodcole/JSON2Blender/blob/master/app/JSON2Blender.py

Hey all, made a little progress on this. I figured out I was setting the parent the wrong way.
I now do it like this


bpy.ops.object.select_all(action='DESELECT')
child  = bpy.data.objects['childObject]
parent = bpy.data.objects['parentObject']
child.select = True
parent.select = True
bpy.ops.parent_set(type='OBJECT')

The problem I am having now is that I can’t figure out how to completely unselect any previously selected objects, which means my two newly selected objects get parented to a previous object.

I have attached a screen shot showing the issue. You can see that two objects are selected, though they don’t get highlighted in the outliner, however a third object has it’s origin highlighted and is highlighted in the outliner.


Anyone know what I’m doing wrong here?

And the answer is!
The selection / parenting can be done in at least the following to ways. (thanks to cardpi via irc for the help)

      pmat = parent.matrix_world.inverted()
      child.parent = parent
      child.matrix_parent_inverse = pmat

      bpy.ops.object.select_all(action='DESELECT')
      bpy.context.scene.objects.active = parent
      child.select = True
      bpy.ops.object.parent_set(type='OBJECT')