Attach Model to Mouse

Hi,

this is my first post, and I am fairly new to the python coding side of things.

I try to connect a model to the mouse pointer. Everthing shows up, but the model wont follow the mouse movement. What is wrong with the code?

class Application(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

#        base.disableMouse()

        self.disc = self.loader.loadModel("disc")
        self.disc.setScale(0.2)
        self.disc.reparentTo(render)
        self.cam.setPos(0, -20, 0)
        base.setBackgroundColor(0,0,0)
 #       self.mouse_pos()

        alight = AmbientLight('alight')
        alnp = render.attachNewNode(alight)
        alight.setColor(Vec4(0.2, 0.3, 0.4, 1))
        render.setLight(alnp)

    def mouse_pos(self, task):
        
       if base.mouseWatcherNode.hasMouse():
            x=base.mouseWatcherNode.getMouseX()
            y=base.mouseWatcherNode.getMouseY()
            self.disc.setX(x)
            self.disc.setY(y)
            print str(x)+" is mouse X"
            print str(y)+" is mouse Y"

        return Task.cont

Hi, welcome to the forums!

You forgot to add mouse_pos to the task manager, so it never gets called. Put this in your init:

taskMgr.add(self.mouse_pos, "my mouse updating task")

Moreover, the disc model is parented to render.
It should be parented to render2d or aspect2d.
If it’s parented to aspect2d, you should convert the mouse XY coordinate from render2d coordinate space to aspect2d’s.

The first made this version work as follow:

from direct.showbase.ShowBase import ShowBase
from panda3d.core import *
from direct.interval.IntervalGlobal import *
from direct.task import Task

class Application(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

        base.disableMouse()

        self.disc = self.loader.loadModel("disc")
        self.disc.setScale(0.1)
        self.disc.reparentTo(render)
        self.cam.setPos(0, -10, 0)
        base.setBackgroundColor(0,0,0)
        taskMgr.add(self.mouse_pos, "my mouse updating task")

        alight = AmbientLight('alight')
        alnp = render.attachNewNode(alight)
        alight.setColor(Vec4(0.2, 0.3, 0.4, 1))
        render.setLight(alnp)

    def mouse_pos(self, task):
        if base.mouseWatcherNode.hasMouse():
            x=base.mouseWatcherNode.getMouseX()
            y=base.mouseWatcherNode.getMouseY()
            self.disc.setX(x * 5)
            self.disc.setZ(y * 3)

        return Task.cont

The second reply gave me the idea to use the aspect2d to connect mouse coordinates to a point what drives a model in the 3d scene (need it, as I want use particle effects and layers later on). Still, both are relative to y in 3D and size of window.

Well, it is working for my purpose at the moment.

I will take a look at it again later in time, as a solution what keeps position of 2d mouse coords and xz position of 3d sync no matter what resolution you choose and no matter how far the model is away from the camera.

Thanks for your help and fast reply!