Skip to content

Instantly share code, notes, and snippets.

@petfactory
Created April 12, 2015 10:34
Show Gist options
  • Save petfactory/7dc577f5f4be0121ba1e to your computer and use it in GitHub Desktop.
Save petfactory/7dc577f5f4be0121ba1e to your computer and use it in GitHub Desktop.
import maya.api.OpenMaya as om
import pymel.core as pm
def intersect_mesh(pos, vec, mesh_name):
selectionList = om.MSelectionList()
selectionList.add(mesh_name)
dagPath = selectionList.getDagPath(0)
fnMesh = om.MFnMesh(dagPath)
intersection = fnMesh.closestIntersection( om.MFloatPoint(pos),
om.MFloatVector(vec),
om.MSpace.kWorld, 99999, False)
if intersection:
hitPoint, hitRayParam, hitFace, hitTriangle, hitBary1, hitBary2 = intersection
if hitTriangle > -1:
return hitPoint
mesh_name = 'pSphere1'
crv = pm.PyNode('curve1')
crv_shape = crv.getShape()
cv_list = crv_shape.getCVs(space='world')
cv_vec = (cv_list[1] - cv_list[0]).normal()
pos = om.MPoint(cv_list[1])
vec = om.MVector(cv_vec)
hit_point = intersect_mesh(pos, vec, mesh_name)
loc = pm.spaceLocator()
loc.translate.set(hit_point)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment