Skip to content

Instantly share code, notes, and snippets.

@SEVEZ
Created August 29, 2015 11:31
Show Gist options
  • Save SEVEZ/bb1065dd8caa24ff6ee8 to your computer and use it in GitHub Desktop.
Save SEVEZ/bb1065dd8caa24ff6ee8 to your computer and use it in GitHub Desktop.
Select object objects, closest to cursor
import maya.cmds as cmds
from pymel.core import *
from pymel.core.datatypes import *
import maya.OpenMaya as om
import maya.OpenMayaUI as omui
class SamsClass():
ctx = cmds.currentCtx()
cto = cmds.contextInfo(ctx,c=1)
print cto
def __init__(self):
pass
def contextPress(self, *args):
maya3DViewHandle = omui.M3dView()
activeView = maya3DViewHandle.active3dView()
self.Context = 'Context'
self.meshSelection = cmds.ls(g=1)
if cmds.draggerContext( self.Context, ex=1 ):
cmds.deleteUI( self.Context )
cmds.draggerContext( self.Context, dragCommand=self.onDrag, releaseCommand=self.onRelease, name=self.Context, cursor='crossHair' )
cmds.setToolTo( self.Context )
self.xIn=[]
self.yIn=[]
self.nmIn=[]
for mesh in self.meshSelection:
mSel = om.MSelectionList()
mSel.add( mesh )
mObj = om.MObject()
mSel.getDependNode(0,mObj)
mFnDependNode = om.MFnDependencyNode(mObj)
self.nmIn.append(mFnDependNode.name())
XYZ = cmds.objectCenter(mFnDependNode.name(),gl=1)
vertexMPoint = om.MPoint(XYZ[0],XYZ[1],XYZ[2])
argX = om.MScriptUtil()
argX.createFromInt(0)
x = argX.asShortPtr()
argY = om.MScriptUtil()
argY.createFromInt(0)
y = argY.asShortPtr()
activeView.worldToView(vertexMPoint, x, y)
xPos = om.MScriptUtil().getShort(x)
yPos = om.MScriptUtil().getShort(y)
self.xIn.append(xPos)
self.yIn.append(yPos)
def onDrag(self):
self.Context = 'Context'
vpX, vpY, _ = cmds.draggerContext( self.Context, q=1, dp=1 )
dista=[]
for x in range (0,(len(self.xIn))):
dist = math.hypot(self.xIn[x] - vpX, self.yIn[x] - vpY)
dista.append(dist)
cmds.select(self.nmIn[dista.index(min(dista))],r=1)
cmds.refresh()
def onRelease(self):
if(self.cto=='manipMove'):
cmds.setToolTo('moveSuperContext')
elif(self.cto=='manipRotate'):
cmds.setToolTo('RotateSuperContext')
elif(self.cto=='manipScale'):
cmds.setToolTo('scaleSuperContext')
elif(self.cto=='selectTool'):
cmds.setToolTo('selectSuperContext')
a = SamsClass()
a.contextPress()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment