Jump to content

Geometry Point under the mouse


Alexey Vanzhula

Recommended Posts

I can find primitive under the mouse with MouseCHOP.

Now i wat to find closest point at this primitive.

The problem is that nearpoint expression function return near point to viewport plane. As the result I can find valid point number in flat grid and via one of ortho views.

How to properly find closest point ? I have to consider direction (in my code) returned by mapToWorld. But How ?

import hou, toolutils

cur_viewport = toolutils.sceneViewer().curViewport()   # CURRENT VIEWPORT
mx, my = hou.evalParmTuple("/obj/geo1/dm1/core/mouse") # THIS IS PATH TO MOUSE-CHOP
vx, vy = v.mapFromMouseChop(mx,my)                     # GET VIEWPORT COORDINATES
direction, position =v.mapToWorld(vx,vy)               # CONVERT VIEWPORT TO WORLD COORDINATES

node = cur_viewport.queryNodeAtPixel(vx, vy)           # NODE AT WORLD COORDINATES

if node is not None:
    prim = v.queryPrimAtPixel( node, vx, vy )          # PRIM AT WORLD COORDINATES
    hou.hscriptExpression( 'nearpoint("{}",{},{},{})'.format(node.path(), position[0], position[1], position[2]) )      # POINT NUMBER
Link to comment
Share on other sites

Alexey, my solution is to use origin and ray vectors and then iterate over the primitive's vertices to find minimal distance to the ray. Here is my code: 

def getN(point, orig, dir):
    pos = point.position()
    OP = pos - orig
    proj = OP.dot(dir)
    n = OP - proj * dir
    return n

def nearestPrimPoint(prim, orig, dir):
    minD = float('inf')
    nearestPoint = prim.vertices()[0].point()

    for v in prim.vertices():
        P = v.point()
        N = getN(P, orig, dir)
        D = N.length()
            
        if D < minD:
            minD = D
            nearestPoint = P
                
    return nearestPoint

So you can pass the direction and position vectors to this function.

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...