Anyone have any idea why SelectFromScreen might not work? I’m pretty sure its coming from the mouse position I’m providing as I used Nathan Horne’s example and just plugging in the values gives me an empty list but giving a mouse position a bit too the left or right works. Also when really zoomed in on the object, get no hits. Any other way to get an object name without selecting it under the mouse? I saw claydough’s example but it works of pre-selection hilighting.
Here’s some updated code:
def contextPress():
pressPosition = cmds.draggerContext( 'sampleContext', query=True, anchorPoint=True)
print ("Press: " + str(pressPosition))
# grabbing current 3d view
active_view = mui.M3dView.active3dView()
# Screen position of mouse
x = int(pressPosition[0])
y = int(pressPosition[1])
print x,y
#making my ray source and direction
ray_source = om.MPoint()
ray_direction = om.MVector()
#Converting to world
active_view.viewToWorld(x,y, ray_source, ray_direction)
'''
#get mesh dag path
sel = om.MSelectionList()
om.MGlobal.getActiveSelectionList(sel)
'''
#Select from screen
om.MGlobal.selectFromScreen(x,y, om.MGlobal.kReplaceList)
objects = om.MSelectionList()
om.MGlobal.getActiveSelectionList(objects)
print objects.length()
#restore selection if there was a selection
#om.MGlobal.setActiveSelectionList(sel, om.MGlobal.kReplaceList)
#Convert from MSelectionList object to string
fromScreen = []
objects.getSelectionStrings(fromScreen)
#sel.add(fromScreen[0])
mesh_dag = om.MDagPath()
objects.getDagPath(0,mesh_dag)
#cast the ray at the mesh and find out if and where it hits
hit_pnt, face_idx = closest_mesh_intersection(mesh_dag, ray_source, ray_direction)
rayHitPosition = [(hit_pnt.x),(hit_pnt.y),(hit_pnt.z)]
print "HIT AT: " + str(rayHitPosition)
print "FACE AT: " + str(face_idx)
cmds.setToolTo('moveSuperContext')
print("CHANGED TOOL")
def selectByColorValue(*args):
print("SELECTING BY COLOR 2")
if(cmds.draggerContext('sampleContext', exists=True)):
cmds.deleteUI('sampleContext')
cmds.draggerContext( 'sampleContext', pressCommand = "contextPress()", cursor='hand', projection = "s")
cmds.setToolTo('sampleContext')
def closest_mesh_intersection(mesh_dag, ray_source, ray_direction):
meshFn = om.MFnMesh(mesh_dag)
#Making my Hit Point
hit_point = om.MFloatPoint()
ray_source_float = om.MFloatPoint(ray_source.x, ray_source.y, ray_source.z)
ray_direction_float = om.MFloatVector(ray_direction.x, ray_direction.y, ray_direction.z)
#Pointer nonsense.
face_idx_util = om.MScriptUtil()
face_idx_util.createFromInt(-1)
face_int_ptr = face_idx_util.asIntPtr()
#Args for closest Interestion
meshFn.closestIntersection(
ray_source_float,#const MFloatPoint & raySource,
ray_direction_float,#const MFloatVector & rayDirection,
None, #const MIntArray * faceIds,
None, #const MIntArray * triIds,
False, #bool idsSorted,
om.MSpace().kWorld, #MSpace::Space space,
9999, #float maxParam,
False, #bool testBothDirections,
None, #MMeshIsectAccelParams * accelParams,
hit_point, #MFloatPoint & hitPoint,
None, #float * hitRayParam,
face_int_ptr, #int * hitFace,
None, #int * hitTriangle,
None, #float * hitBary1,
None, #float * hitBary2,
.000001 #float tolerance = 1e-6,
)
#Again more pointer nonsense. Need to look into this more.
face_idx = face_idx_util.getInt(face_int_ptr)
return (hit_point, face_idx)
EDIT: Got it to work. Seems like looking at the Maya Api docs and referencing the marquee tool, it was probably using kWireframeSelectMethod instead of kSurfaceSelectMethod. This maya api stuff is quite fun haha.