[Help] reparentTo VS attachNewNode?

Hi, I’m a newbie in Panda3D. Now I find something confusing, I have no idea about when to use [color=red]reparentTo or [color=red]attachNewNode.
I made some changes to the following code:
previous:

        self.ambientLight = AmbientLight('ambientLight')
        self.ambientLight.setColor(Vec4(0.2, 0.2, 0.2, 1))
        self.ambientLightNP = render.attachNewNode(self.ambientLight.upcastToPandaNode())
        render.setLight(self.ambientLightNP)

after:

        self.ambientLight = AmbientLight('ambientLight')
        self.ambientLight.setColor(Vec4(0.2, 0.2, 0.2, 1))
        self.ambientLight.reparentTo(render)
        render.setLight(self.ambientLightNP)

But I tried the changed code, it doesn’t work. My question is that why the Light object, which is also a node, cannot directly be attached to another NodePath.

Thanks a lot!

1 Like

Please check out first how the Scene Graph works.

self.ambientLightNP is a (node)path in the scenegraph to the light.
What attachNewNode does, is it parents the light to the top of the scenegraph (which is called render.)
attachNewNode returns the path to the newly reparented light as NodePath, not as light. It returns “render/ambientLight” in this case.
render.setLight only takes a nodepath, not a light itself. So, you first need to set the nodepath, to the value returned by attachNewNode.
This should work:

        self.ambientLight = AmbientLight('ambientLight')
        self.ambientLight.setColor(Vec4(0.2, 0.2, 0.2, 1))
        self.ambientLightNP = render.attachNewNode(self.ambientLight)
        render.setLight(self.ambientLightNP)

Thank you very much! I get it! :exclamation:

@rdb What’s the functional difference between the ‘previous’ snippet from the op which uses attachNewNode(), and your answer, which uses reparentTo()? Or are these just different ways of achieving the same thing?

My snippet is actually incorrect, since AmbientLight (or PandaNode) has no method called reparentTo. I will correct it. attachNewNode is the correct name of the method.