Remove class after calling

If I have that code:

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task

class sphere(DirectObject):
    def __init__(self):
        self.sphere=loader.loadModel("sphere")
        self.sphere.reparentTo(render)
        self.accept("space",self.change)
        
        
class cube():
    def __init__(self):
        self.cube=loader.loadModel("cube")
        self.cube.reparentTo(render)
        taskMgr.add(self.move,"move")
    def move(self,task):
        self.cube.setX(self.cube.getX()+0.2)
        return Task.cont
    
sph=sphere()

run()

No I want that when I press a key (for example Ctrl key) the (sph) turned from sphere class to cube class

Is there a way to do that??
Thanks in advance

Surely, you, for instance, would like to create a destroy method for each class, and then build a simple function that swaps them.

import direct.directbase.DirectStart 
from direct.showbase.DirectObject import DirectObject 
from direct.task.Task import Task 

class sphere(DirectObject): 
    def __init__(self): 
        self.sphere=loader.loadModel("sphere") 
        self.sphere.reparentTo(render) 
        self.accept("space",self.change) 
        
    def destroy(self):
        self.ignoreAll() # DirectObject has this for ignoring all self.accept'ed events
        self.sphere.remove()

class cube(): 
    def __init__(self): 
        self.cube=loader.loadModel("cube") 
        self.cube.reparentTo(render) 
        taskMgr.add(self.move,"move") 
    def move(self,task): 
        self.cube.setX(self.cube.getX()+0.2) 
        return Task.cont 
    def destroy(self):
        self.cube.remove()
        taskMgr.remove('move') # remove the 'move' task

item = sphere()
itemIsSphere = True

def swapThem():
    if itemIsSphere:
        item.destroy()
        item = cube()
        itemIsSphere = False
    else:
        item.destroy()
        item = sphere()
        itemIsSphere = True

base.accept('s', swapThem)
#when s is pressed, it will swap
run()

that should mostly work, it’s untested though so no… killing me if it’s broken a tad, it’s the really basic idea behind it though :slight_smile:

Hope this helps,
~powerpup118

That did the trick…thanks a lot :smiley: