Clearing a Joint of Objects

I have a character(ralph from Roaming Ralph), and a function to put an object in his hands:

def putInHand(self, obj, scale):
        self.inHand = obj
        self.rightHand = self.actor.exposeJoint(None, 'modelRoot', 'RightHand')
        sword = loader.loadModel(self.inHand)
        sword.setScale(scale)
        sword.reparentTo(self.rightHand)

Now this works perfectly, except when I try to put two objects in his hand. For example:

putInHand('models/sword', 1)
putInHand('models/candycane', 1)

Both objects will be put in his hand, which looks a little awkward.
Is there any way to clear his hands of everything, like a joint.removeAllAttached()? Because I only want one thing in his hand at one same time.

You are passing a string for the obj argument and saving that in self.inHand.
What you probably want to do is save the loaded NodePath sword object in self.inHand.
Then you could just call self.inHand.detachNode() to remove it. The detached node will still be valid
as long as something is referencing it so if you don’t immediate reassign self.inHand to something else,
other code that references self.inHand will still find the node there, though it is not in the scenegraph.

If you don’t want to change self.inHand being a string you can still do in an indirect way:
self.rightHand.node().removeAllChildren()

Thank you!

Here is the working function:

self.rightHand = self.actor.exposeJoint(None, 'modelRoot', 'RightHand')

def putInHand(self, obj, scale):
        self.inHand = obj
        self.rightHand.node().removeAllChildren()
        sword = loader.loadModel(self.inHand)
        sword.setScale(scale)
        sword.reparentTo(self.rightHand)