Remove lights and meshes with shadows

Hello, I have a problem with shadows. In my application I need to create and destroy meshes and lights. But, when I do this, the number of nodes is constantly increasing (and framerate goes down). I replicate the problem in this shorter example:

from panda3d.core import loadPrcFileData
loadPrcFileData( '', 'want-pstats 1' )
loadPrcFileData( '', 'task-timer-verbose 1 pstats-tasks 1' )
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import Spotlight

class World( DirectObject ):

  def __init__( self ):
    base.disableMouse()
    camera.setPos( 0, -20, 2 )
    self.accept( 'mouse1', self.destroyAndCreate )
    render.setShaderAuto()
    self.rootNode = None

  def destroyAndCreate( self ):
    if self.rootNode:
      self.rootNode.removeNode()
    self.rootNode = render.attachNewNode( 'rootNode' )
    self.light = self.rootNode.attachNewNode( Spotlight( 'Spot' ) )
    self.lightNode = self.light.node()
    self.lightNode.setScene( self.rootNode )
    self.lightNode.setShadowCaster( True, 1024, 1024 )
    self.rootNode.setLight( self.light )
    loader.loadModel( 'teapot' ).reparentTo( self.rootNode )
    
w = World()
run()

In pstats, if I trace the number of nodes (menu entry Graphs/Nodes), I see that the number of nodes increments every time I click the left mouse button.

But, if I comment the line:

# self.lightNode.setShadowCaster( True, 1024, 1024 )

the number of nodes remains the same after each click.

Maybe I’m cleaning badly the “objects” when shadows are active. What is the correct way to clean lights and meshes with shadows? Very thanks!

It’s not the number of nodes.
The reported number of nodes by pstats and analyze() are different. Maybe pstats doesn’t report the number of nodes currently.

If you have :

    base.bufferViewer.enable(1)

you’d see that shadow buffers are not destroyed and keep rendering the scene.

So I just did :

       self.lightNode.setShadowCaster(False)

before removing the node.

Wow, now it works! Very thanks! :slight_smile: