setParent does not work

Hi all,

in a .py file I’ve a python class where I TRY to set a parent, but it doesn’t work:

self.elements['bkgObj']=scene.addObject(scene.objectsInactive['bkgObj'], scene.objects['builder'])  
self.elements['nodNameObj']=scene.addObject(scene.objectsInactive['nodNameObj'], scene.objects['builder']) self.elements['nodNameObj'].setParent(self.elements['bkgObj'])

# for debug

the result on console is:

Why has not the parent set?


Argh… are you sure you want to write it that way?

How about some intermediate variables. It makes it much more understandable.

I do not see why you place that processing in a class.

It seems you accidentally removed a new line while preparing the post.

The names of the objects are no good descriptors (except “builder”).

Let me rephrase the code:

bkgObj = scene.addObject('bkgObj', 'builder')  
nodNameObj = scene.addObject('nodNameObj', 'builder') 

# for debug

I do not see a problem. The code should work as expected
I suggest you add some prints before and directly after setParent(), showing the names and ids of each participating object.

print(object, id(object))

Thanks Monster! Yes, I was too concise, I apologize :slight_smile:

The context is quite complex, I want to dynamically display a icon (bkgObj) plus a text (the node name, nodNameObj), so I put them in a class container (the “self”). During its init(self,…) they are created by means scene.addObject. The objects are fine created: they have different ID, they are displayed, i can destroy them, i can move (individually), set visible or invisible,… BUT the parent!

I’m trying everything: change the variables name, check all the ID,… The scene where I add the objects is not the currentScene so I simulated it in debugging files but don’t find any problems. May be is there a flag in the blender that prevents the setting of parents? I don’t know what are the differences between debugging files and main project…

        dad=scene.addObject(scene.objectsInactive['bkgObj'], scene.objects['builder'])
        son=scene.addObject(scene.objectsInactive['nodNameObj'], scene.objects['builder'])  

        # debugging
        print('parent...', son.parent)
        print(dad, id(dad))
        print('parent...', son.parent)
        print(dad, id(dad))

the output is:

parent... None
bkgObj 231857200
nodNameObj 231857248
parent... None
bkgObj 231857200
nodNameObj 231857248

This is strange. I would need a demo.blend.

It should be a very simple one, demonstrating the problem only. I’m not interested in your meshes, texture, sounds and any objects/logic that is not related to your problem.

(Hint: while preparing this file, you might discover the solution for your problem by yourself. If not we can check it)

It seems to be a problem with objectsInactive list, try to use a string instead like:

dad = scene.addObject("bkgObj" , scene.objects["builder"]

Edit: looking at your code again, you’re setting parent after printing?
I’m sure this isn’t a bug, I was doing something similar recently that involves parenting added objects.