Skip to content

Instantly share code, notes, and snippets.

@SEVEZ
Created August 29, 2015 11:22
Show Gist options
  • Save SEVEZ/009869facc3c114a5097 to your computer and use it in GitHub Desktop.
Save SEVEZ/009869facc3c114a5097 to your computer and use it in GitHub Desktop.
Create locator on surface of any visible mesh object
import maya.cmds as cmds
import pymel.core as pm
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(Context, exists=True):
print 'deleting'
cmds.deleteUI(Context)
cmds.setToolTo('selectSuperContext')
# parse args and selection
mesh = None
self.geo = None
# create context and set tool
cmds.draggerContext( self.Context, pressCommand=self.onPress, dragCommand=self.onDrag, name=Context, cursor='crossHair')
cmds.setToolTo(Context)
def onPress(self):
vpX, vpY, _ = cmds.draggerContext( self.Context, query=True, anchorPoint=True)
pos = om.MPoint()
direction = om.MVector()
omui.M3dView().active3dView().viewToWorld(int(vpX) , int(vpY), pos, direction)
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( pos ), om.MFloatVector(direction), om.MSpace.kWorld, 99999, False )
if self.intersection:
hitPoint, hitRayParam, hitFace, hitTriangle, hitBary1, hitBary2 = self.intersection
if hitTriangle != -1:
self.loc1= pm.spaceLocator(p=(hitPoint[0],hitPoint[1],hitPoint[2]), a=True)
cmds.refresh()
def onDrag(self):
vpX, vpY, _ = cmds.draggerContext( self.Context, query=True, dragPoint=True)
pos = om.MPoint()
direction = om.MVector()
omui.M3dView().active3dView().viewToWorld(int(vpX) , int(vpY), pos, direction)
for mesh in self.meshSelection:
selectionList = om.MSelectionList()
selectionList.add(mesh)
dagPath = selectionList.getDagPath(0)
self.fnMesh = om.MFnMesh(dagPath)
dragIntersection = self.fnMesh.closestIntersection( om.MFloatPoint( pos ), om.MFloatVector(direction), om.MSpace.kWorld, 99999, False )
if dragIntersection:
hitPoint, hitRayParam, hitFace, hitTriangle, hitBary1, hitBary2 = dragIntersection
if hitTriangle != -1:
self.loc1.localPosition.set( 0,0,0 )
pos = pm.datatypes.Point(*hitPoint)
'''
closest = None
minLength = None
verts = [self.geo.vtx[i] for i in self.geo.f[hitFace].getVertices()]
for v in verts:
vpos = v.getPosition(space='world')
thisLength = (pos - vpos).length()
if minLength is None or thisLength < minLength:
minLength = thisLength
closest = vpos
'''
self.loc1.setTranslation( pos , space='world')
cmds.refresh()
'''
Select the base mesh before running the following
'''
a = SamsClass()
a.pointLocator()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment