fire a ray out to check for intersections python?~

1,520 views
Skip to first unread message

e955...@gmail.com

unread,
Sep 22, 2014, 2:20:50 PM9/22/14
to python_in...@googlegroups.com
Hello friends,

i have a piece of code here in python which gives me the 2d viewpoint coords of the cursor whenever i click the mouse:

########################################

import maya.cmds as cmds

ctx = 'myCtx'
def onPress():
print cmds.draggerContext(ctx, query=True, anchorPoint=True)
if cmds.draggerContext(ctx, exists=True):
cmds.deleteUI(ctx)
cmds.draggerContext(ctx, pressCommand=onPress, name=ctx, cursor='crossHair')
cmds.setToolTo(ctx)

#########################################

What i need is to cast a ray whenever i click, coming from the viewpoint of the camera and using the 2d coordinates generated by this script to check for the intersection point on a poly mesh.

Then i just want it to return the 3d xyz coords of that intersection point

im pretty stuck with this. But im sure it can't be to extensive. I anyone can help i would be super happpyyyyyy!!!

thanks,
Sam

Fredrik Averpil

unread,
Sep 22, 2014, 2:29:34 PM9/22/14
to python_in...@googlegroups.com
Hey Sam,

This is actually on my todo list. If you happen to solve this, I would be super happy if you would like to share the code.

Regards,
Fredrik



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/758864e0-0946-4126-939a-c1ba3bc21a48%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

e955...@gmail.com

unread,
Sep 22, 2014, 4:12:46 PM9/22/14
to python_in...@googlegroups.com
ha, yep of course. Been trying to find a clear response to this problem for a while. Hopefully someone on here will know !;)

Sam

Marcus Ottosson

unread,
Sep 22, 2014, 4:45:24 PM9/22/14
to python_in...@googlegroups.com

Hi Sam,

I’m confident there is a method within the Python API you could use to do this, but a poor man’s version might be to:

  1. Create a plane, 3x3 divisions
  2. Parent to current camera and zero out transformations, with normals pointing from camera to target mesh.
  3. Create a follicle on plane at uv=(0.5, 0.5), it will end up at the center vertex.
  4. Transfer attributes of plane to target mesh, set to “Along Normals” 
  5. Delete history, delete plane and use the transform of the follicle.

Best,
Marcus


On 22 September 2014 21:12, <e955...@gmail.com> wrote:
ha, yep of course. Been trying to find a clear response to this problem for a while. Hopefully someone on here will know !;)

Sam

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Luke Harris

unread,
Sep 22, 2014, 7:37:42 PM9/22/14
to python_in...@googlegroups.com
Hey Sam
Had a little play with this and came up with this. I'm just learning the api so just consider it a starting off point. Someone better might chime in with nicer solution. Its with the 2.0 api, which may require 2015

import maya.api.OpenMaya as om
import maya.api.OpenMayaUI as omui
import maya.cmds as cmds

ctx = 'myCtx'
def onPress():
    vpX, vpY, _ = cmds.draggerContext(ctx, query=True, anchorPoint=True)
    pos = om.MPoint()
    dir = om.MVector()
    omui.M3dView().active3dView().viewToWorld(int(vpX), int(vpY), pos, dir)
    for mesh in cmds.ls(type='mesh'):
        selectionList = om.MSelectionList()
        selectionList.add(mesh)
        dagPath = selectionList.getDagPath(0)
        fnMesh = om.MFnMesh(dagPath)
        intersection = fnMesh.closestIntersection(
            om.MFloatPoint(pos),
            om.MFloatVector(dir),
            om.MSpace.kWorld, 99999, False)
        if intersection:
            print "we're hit!!"
            hitPoint, hitRayParam, hitFace, hitTriangle, hitBary1, hitBary2 = intersection
            x,y,z,_ = hitPoint
            cmds.spaceLocator(p=(x,y,z))
            
       
if cmds.draggerContext(ctx, exists=True):
    cmds.deleteUI(ctx)
cmds.draggerContext(ctx, pressCommand=onPress, name=ctx, cursor='crossHair')
cmds.setToolTo(ctx)


one good deed deserves another ;)

charles....@epicgames.com

unread,
Apr 23, 2015, 3:30:15 PM4/23/15
to python_in...@googlegroups.com
Hello, Thanks for posting this snippet of code, with some small changes it is working great for me.

I have one issue though. When I use this, its very fast to get the intersection point and run the create locator commands, but then after all of that has been printed to the log I get a fairly sizable delay when using this in a larger scene that has lots of dag nodes in it. Basically it runs the code and then pauses at the end in some cases for almost a second before it actually creates the locator.

Thoughts on what might be causing that?
Reply all
Reply to author
Forward
0 new messages