Problems with tag and getTag when clicking an object

Hi, I ve defined some models, Ive loaded them with loadmodel copy, well, then I define everything for getting collision on click above an object , just like the example in the documentation (by the way the example had some syntax fails) :

base.cTrav = CollisionTraverser()
myTraverser = base.cTrav
myHandler = CollisionHandlerQueue()

pickerNode = CollisionNode(‘mouseRay’)
pickerNP=camera.attachNewNode(pickerNode)
pickerNode.setFromCollideMask(GeomNode.getDefaultCollideMask())
pickerRay=CollisionRay()
pickerNode.addSolid(pickerRay)
myTraverser.addCollider(pickerNP, myHandler)

Load the models and tag them
model1 = loader.loadModel(‘whatevermodel’)
model1.reparentTo(render)
model1.setPos(5, 25,0)
model1.setTag(‘myObjectTag’, ‘1’)
print model1.getTag(‘myObjectTag’)# prints 1 runs ok

Now the class…

class event(DirectObject.DirectObject):
def init(self):
self.accept(‘mouse1’,self.mifuncion)
def mifuncion(self):
mpos=base.mouseWatcherNode.getMouse()# da coordenadas de mouse
#This makes the ray’s origin the camera and makes the ray point
#to the screen coordinates of the mouse
pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY())
myTraverser.traverse(render)
#assume for simplicity’s sake that myHandler is a CollisionHandlerQueue
if myHandler.getNumEntries() > 0:
myHandler.sortEntries() #this is so we get the closest object
#pickedObj=myHandler.getEntry(0).getIntoNodePath()
pickedObj=myHandler.getEntry(0).getIntoNode()
print pickedObj.getTag(‘myObjectTag’)# prints “” blank string

Well at this point I ve tried getIntoNodePath and getIntoNode, also findNetTag and getTag, but tis always the same blank str… so im stucked here

Ive looked at the chessboard example and it seems there the tag is :
self.squares[i].find("**/polygon").node().setTag(‘square’, str(i))

But if I try this way I get an error :
AssertionError: !is_empty()
Maybe it is because I ve loaded my own model and this is not like the chessboard example

Someone knows something that could help me ?

Tricky. When you pick your object, you probably either pick a child of or a parent of the nodepath which has the tag. The chessboard example fixes this by finding the child node which is called “polygon”. In your case, you should try the same, but with the name of your model:

getIntoNodePath().find("**/whatevermodel").getTag("myObjectTag")

or something similar. If it does not work the name might not be whatevermodel, but try “print model1” to find the name of the model in the scene graph.

PS. as of the latest version, loadModelCopy does nothing more than loadModel, since the model cache has been introduced a few versions ago.

In the chessboard example it does the find(**/Polygon) when tagging, not when getting the tag

Tried the code you told me but doesnt solve anything

If I try to get the tag from squares[i] when i = integer from 0 to 64 Im able to get the tag with get tag

So as you said I did a print from squares[i] :
render/squareRoot/casilla_llanura2.egg

And the print that returns the picked object is:
render/squareRoot/casilla_llanura2.egg/Cube

So I had to do when gettint the tag :
pickedObj.find(’**/Cube’).getTag(‘mytag’)

But its still returning an empty string…

What about:
pickedObj.getParent().getTag(‘mytag’)

Nice :slight_smile:

It works now thx a lot guy

Another Option when you look for the Tag is to use the getNetTag.

It will find the first ancestor of your node that have the Tag on it.
This way it works also for attachments,collision volume etc…