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:

scene=bge.logic.getSceneList()[1]
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
print(self.elements['bkgObj'])
print(self.elements['nodNameObj'])
print(self.elements['nodNameObj'].parent)


the result on console is:
bkgObj
nodNameObj
None

Why has not the parent set?

Thanks!

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:


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

# for debug
print(bkgObj)
print(nodNameObj)
print(nodNameObj.parent)

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.
e.g.

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(son,id(son))
        son.setParent(dad)        
        print('parent...', son.parent)
        print(dad, id(dad))
        print(son,id(son))
        
        self.elements['nodNameObj']=son
        self.elements['bkgObj']=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.