cleanup lights and other objects, not successful


I tried to remove the lights and other objects from the scene but not very successful. I’ve read the following post
and write my own code as:

remove my tasks


I want to use the default camera, so I detach it first

remove lights

for l in render.findAllMatches(’**/+LightNode’).asList():

remove spot light ?

for l in render.findAllMatches(’**/+LightLensNode’).asList():

remove everything


restore the camera

If I repeatedly reload the scene and cleanup using the above code, for several times, the screen slowing down very much.

What is missing from the above code ?

Secondly, if I have made a light to cast on something, I want to remove the light completely, what should I write ?
alight = AmbientLight(‘alight’)
alight.setColor(Vec4(0.2, 0.2, 0.2, 1))
alnp = render.attachNewNode(alight)

    #clean up code correct?
    # delete alight explicitly, or not required ?
    del alight
    alight = None

Thank you very much !

First, setLightOff() is not the same thing as clearLight(). setLightOff() adds an attribute that specifically disables use of a particular light; if you call it repeatedly on a node, you will be adding many attributes to that node. You should use clearLight() instead, which removes the attribute.

But you don’t need to do all that work in your first code sample at all. If you are removing the entire scene graph, just remove it:


Your second code sample looks fine. It is not necessary to call “del alight” before assigning alight = None. This is a common mistake made by newcomers to Python. In general, it doesn’t make sense to del a variable before you assign it to something else in Python; “del alight” does not actually delete anything other than the binding of the variable “alight”.


Thank you for the clarification.

What will happen in the long run if I forgot to call clearLight before I remove the light node?

Also, if the light is set to be shader input, setShaderInput(“light”, plnp), is it necessary to call clearShaderInput before I remove the light node ? What happens if I forget to call ?

The light is not removed until you call clearLight() and also remove the light node itself. It doesn’t matter which order you do this in.

If you simply remove the light node and don’t call clearLight(), the light attribute remains, and so the light still affects the scene graph. If you create a bunch of lights and never call clearLight(), they will accumulate, and all of them will be illuminating the scene together.

If you simply call clearLight() and don’t remove the light node, the light node itself remains in the scene graph, even though it’s no longer illuminating anything. If you create a bunch of lights and never remove them, they will accumulate and you’ll have a bunch of useless nodes in your scene graph.


Thank you very much for your explanations. It would be great if this information can be kept in a manual or wiki book !