Skip to content

Instantly share code, notes, and snippets.

@SEVEZ
Last active June 28, 2016 11:56
Show Gist options
  • Save SEVEZ/a619c1d57d09e69752c5 to your computer and use it in GitHub Desktop.
Save SEVEZ/a619c1d57d09e69752c5 to your computer and use it in GitHub Desktop.
import maya.cmds as cmds
from pymel.core import *
from pymel.core.datatypes import *
import maya.api.OpenMaya as om
import maya.api.OpenMayaUI as omui
class SamsClass():
def __init__(self):
pass
def pointLocator(self, *args):
'''
pass in the name of a mesh, or just select one
'''
self.meshSelection = cmds.ls( type='mesh' )
self.Context = 'Context'
# clear any previous contexts
if cmds.draggerContext( self.Context, ex=1 ):
print 'deleting'
cmds.deleteUI( self.Context )
cmds.setToolTo( 'selectSuperContext' )
# create context and set tool
cmds.draggerContext( self.Context, pressCommand=self.onPress, dragCommand=self.onDrag, name=self.Context, cursor='crossHair' )
cmds.setToolTo( self.Context )
def onDrag( self ):
vpX, vpY, _ = cmds.draggerContext( self.Context, q=1, dp=1 )
cp = om.MPoint()
dir = om.MVector()
omui.M3dView().active3dView().viewToWorld( int(vpX), int(vpY), cp, dir )
minpos = None
mindist = float("inf")
clmsh = None
for mesh in self.meshSelection:
selectionList = om.MSelectionList()
selectionList.add( mesh )
dagPath = selectionList.getDagPath(0)
self.fnMesh = om.MFnMesh(dagPath)
self.intersection = self.fnMesh.closestIntersection( om.MFloatPoint( cp ), om.MFloatVector( dir ), om.MSpace.kWorld, 99999, False )
if self.intersection :
hitPoint, hitRayParam, hitFace, hitTriangle, hitBary1, hitBary2 = self.intersection
if hitTriangle != -1:
pos = Point( *hitPoint )
if minpos == None:
clmsh = mesh
minpos = pos
mindist = hitRayParam
else:
if hitRayParam < mindist:
clmsh = mesh
minpos = pos
mindist = hitRayParam
if ( mindist < float("inf") ):
# self.loc1.setTranslation( minpos , space='world' )
# print( clmsh )
cmds.hilite( clmsh, r=1 )
cmds.refresh()
def onPress( self ):
vpX, vpY, _ = cmds.draggerContext( self.Context, q=1, ap=1 )
cp = om.MPoint()
dir = om.MVector()
omui.M3dView().active3dView().viewToWorld( int(vpX), int(vpY), cp, dir )
minpos = None
mindist = float("inf")
for mesh in self.meshSelection:
selectionList = om.MSelectionList()
selectionList.add( mesh )
dagPath = selectionList.getDagPath(0)
self.fnMesh = om.MFnMesh(dagPath)
self.intersection = self.fnMesh.closestIntersection( om.MFloatPoint( cp ), om.MFloatVector( dir ), om.MSpace.kWorld, 99999, False )
if self.intersection :
hitPoint, hitRayParam, hitFace, hitTriangle, hitBary1, hitBary2 = self.intersection
if hitTriangle != -1:
pos = Point( *hitPoint )
if minpos == None:
minpos = pos
mindist = hitRayParam
else:
if hitRayParam < mindist:
minpos = pos
mindist = hitRayParam
# if ( mindist < float("inf") ):
# self.loc1 = spaceLocator()
# self.loc1.setTranslation( minpos , space='world' )
# cmds.refresh()
a = SamsClass()
a.pointLocator()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment