Maya custom node: display custom draw under cursor


#1

Hi I am practicing Maya custom node building. So I would like to make a custom node which draws custom shapes under viewport 2.0. This node will track current cursor position and only display the custom shape when the cursor is hovering on the node. I know similar things can be done with tagging the node as “controller”. But that does not work with my custom implemented drawoverride in the viewport.

So my current approach is : install a event filter on maya main window to track the cursor position. And use this position to decide if my custom node should draw the shape in view port or not. It works with one node in maya. But because I created an QObject with event filter under the custom node to track cursor, if I create more than one instance of this custom node, I will end up with more than one event filter object being installed, which I don’t want to, is there a way to make one event filter and shared with all instance of the same custom node ?

Is there a better approach ?

And also what is the correct way to implement a custom node with custom drawoverride that allow “controller” tag to work ?


#2

After a few days, I still don’t know what would be the best to do it, but I have figure out one way of doing it.

So I made a global QObject , it live with the plugin, when loaded custom plugin, this global object is created and install itself as event filter on Maya main window, when unload custom plugin, this QObject will de deleted and uninstall itself from Maya main window, it will track Mouse Hover events.

Then I let my custom draw node access this QObject’s current cursor position property. I implemented my custom viewport 2.0 draw, and convert my draw content into 2d screen position on current active viewport, check if my draw content on screen is intersecting with current cursor position on screen. So my custom node will draw when mouse is hovering on it, and draw nothing when mouse moved away.

After all these, I just need to tag my custom node as “controller” object, and switch the controller tag to “show on mouse proximity”, then it will work like regular Maya nodes that works with the controller tag.

But without tagging my custom node as controller, I can’t trigger draw updates in viewport when the viewport has no change. It will only updates draw when mouse clicked, not hover move.