Jump to content

How to get mouse coordinates in the viewport?


Storm Keeper

Recommended Posts

Hello everyone!

 

I am developing a custom tool that should be able for fast primitive selection by clicking at the given pixel and finding all the primitives in the given radius around it. Something very similar to inspect tool, but searching not just by one pixel. I found that Python's hou.GeometryViewport class has a functions queryNodeAtPixel(x, y) and  queryPrimAtPixel(node, x, y). So, if I knew the x and y, I would get what I need. But I didn't find any function that returns these values in Python. 

 

My question is: is there such a function in Python and how fast it will be to iterate over, for example, 10000 pixels and using queryInspectedPrim? Or shall I dive into HDK to solve this problem?

Link to comment
Share on other sites

Thanks to this, http://odforce.net/wiki/doku.php?id=viewers

I found that there is a method selectPositions in hou.SceneViewer class, which prompts a selection and returns a position in a given space (Screen, viewport XY and UV). So, the code might be:

import toolutils
pos = toolutils.sceneViewer().selectPositions(position_type=hou.positionType.ViewportXY)
x = pos[0]
y = pos[1]

Unfortunately, it is not very suitable to use as a brush since it requires a click and cannot be used in an event loop with hou.ui.addEventLoopCallback() function. Still searching for a better solution. Will try to search into HDK.

Link to comment
Share on other sites

Alexey, thanks for your reply! That's exactly what I needed, but as I expected, it works too slow, so I can't interactively query primitives when the brush radius is larger than approximately 5 pixels. Here is my code (I use a square brush for simplicity):

def getPrimsUnderBrush(xy, rad):
    view = toolutils.sceneViewer().curViewport()
    
    xc = xy[0]
    yc = xy[1]
    
    for x in range(xc - rad, xc + rad):
        for y in range(yc - rad, yc + rad):
            node = view.queryNodeAtPixel(x, y)
            prim = view.queryPrimAtPixel(node, x, y)
Link to comment
Share on other sites

Why not just get the artist to change to brush selection mode?

 

Yes, the brush selection mode would be a good choice where selection has to be dene before the actual brush editing. But in my case I have to select only the area that is currently under the brush.

 

 

What about doing it procedurally using an HDA around the Stroke SOP?

 

Yes, Edward, that's exactly what I've done. I takе the Stroke SOP, collect stroke_dir and stroke_origin attributes, and find the distances from the stroke ray to the points of my geometry, then filter these points by this distance. Then, the brush influence is calculated as a distance function from any given point to the stroke ray.

 

The problem was that I implemented this algorithm in Python and it was too slow, so I tried to find an other solution. Now I've ported it to HDK and it works really fast.

Edited by Dmitry Shurov
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...