Skip to content

Instantly share code, notes, and snippets.

@ben-hearn-sb
Created September 27, 2016 20:47
Show Gist options
  • Save ben-hearn-sb/3fc8c5518cc76626dec1424f2aa308ee to your computer and use it in GitHub Desktop.
Save ben-hearn-sb/3fc8c5518cc76626dec1424f2aa308ee to your computer and use it in GitHub Desktop.
import maya.OpenMaya as om
import maya.OpenMayaUI as omui
import maya.cmds as cmds
ctx = 'myCtx'
sel = cmds.ls(sl=True)[0]
selectedMesh = cmds.listRelatives(sel, s=True)[0]
'''
closestIntersection(raySource, rayDirection, space, maxParam,
testBothDirections, faceIds=None, triIds=None, idsSorted=False,
accelParams=None, tolerance=kIntersectTolerance)
-> (hitPoint, hitRayParam, hitFace, hitTriangle, hitBary1, hitBary2)
'''
def onDrag():
vpX, vpY, _ = cmds.draggerContext(ctx, query=True, dragPoint=True)
#print(vpX, vpY)
pos = om.MPoint()
dir = om.MVector()
omui.M3dView().active3dView().viewToWorld(int(vpX), int(vpY), pos, dir)
pos2 = om.MFloatPoint(pos.x, pos.y, pos.z)
#for mesh in cmds.ls(type='mesh'):
selectionList = om.MSelectionList()
selectionList.add(selectedMesh)
dagPath = om.MDagPath()
selectionList.getDagPath(0, dagPath)
fnMesh = om.MFnMesh(dagPath)
raySource = om.MFloatPoint(pos2)
rayDirection = om.MFloatVector(dir)
faceIds = None
triIds = None
idsSorted = False
maxParamPtr = 99999999
testBothDirections = False
accelParams = None
hitpoint = om.MFloatPoint()
hitRayParam = None
hitFacePtr = om.MScriptUtil().asIntPtr()
hitTriangle = None
hitBary1 = None
hitBary2 = None
intersection = fnMesh.closestIntersection(raySource,
rayDirection,
faceIds,
triIds,
idsSorted,
om.MSpace.kWorld,
maxParamPtr,
testBothDirections,
accelParams,
hitpoint,
hitRayParam,
hitFacePtr,
hitTriangle,
hitBary1,
hitBary2)
if intersection:
print hitFacePtr
#x = hitpoint.x
#y = hitpoint.y
#z = hitpoint.z
#print x, y, z
#cmds.spaceLocator(p=(x,y,z))
def main():
if cmds.draggerContext(ctx, exists=True):
cmds.deleteUI(ctx)
cmds.draggerContext(ctx, dragCommand=onDrag, name=ctx, cursor='crossHair')
cmds.setToolTo(ctx)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment