Many Maya methods can return an MObject. To get the MObject or
MDagPath a node whose name you
know, you can use an MSelectionList:
import maya.OpenMaya as om
selList = om.MSelection list;
nodeObject = om.MObject();
selList.getDependNode(0, nodeObject)
> If someone could even post what a valid call to
> MFnMesh.closestIntersection looks like, I might be able to work
> backwards? Thanks.
Here's a function that takes the name of mesh node, a start point and
a direction
for a ray and returns the interestion point, if found, otherwise None.
import maya.OpenMaya as om
def findMeshIntersection(meshName, raySource, rayDir):
# Create an empty selection list.
selectionList = om.MSelectionList()
# Put the mesh's name on the selection list.
selectionList.add(meshName)
# Create an empty MDagPath object.
meshPath = om.MDagPath()
# Get the first item on the selection list (which will be our mesh)
# as an MDagPath.
selectionList.getDagPath(0, meshPath)
# Create an MFnMesh functionset to operate on the node pointed to by
# the dag path.
meshFn = om.MFnMesh(meshPath)
# Convert the 'raySource' parameter into an MFloatPoint.
raySource = om.MFloatPoint(raySource[0], raySource[1], raySource[2])
# Convert the 'rayDir' parameter into an MVector.`
rayDirection = om.MFloatVector(rayDir[0], rayDir[1], rayDir[2])
# Create an empty MFloatPoint to receive the hit point from the call.
hitPoint = om.MFloatPoint()
# Set up a variable for each remaining parameter in the
# MFnMesh::closestIntersection call. We could have supplied these as
# literal values in the call, but this makes the example more readable.
sortIds = False
maxDist = 10.0
bothDirections = False
noFaceIds = None
noTriangleIds = None
noAccelerator = None
noHitParam = None
noHitFace = None
noHitTriangle = None
noHitBary1 = None
noHitBary2 = None
# Get the closest intersection.
gotHit = meshFn.closestIntersection(
raySource, rayDirection,
noFaceIds, noTriangleIds,
sortIds, om.MSpace.kWorld, maxDist, bothDirections,
noAccelerator,
hitPoint,
noHitParam, noHitFace, noHitTriangle, noHitBary1, noHitBary2
)
# Return the intersection as a Pthon list.
if gotHit:
return [hitPoint.x, hitPoint.y, hitPoint.z]
else:
return None
--
-deane
hitPoint = om.MFloatPoint()
gotHit = meshFn.closestIntersection(
raySource, rayDirection,
faceIds, triangleIds,
sortIds, om.MSpace.kWorld, maxDist, bothDirections,
noAccelerator,
hitPoint,
hitParam, hitFace, hitTriangle, hitBary1, hitBary2
)
Yes, I very deliberately avoided retrieving any values whose parameters
were pointers or references to simple types like 'int' because that's more
complex.
If you pass a python object to a function the function can modify attributes
of that object and the new values of those attributes will be available to the
caller. So we can pass down an MPoint object, let the closestIntersection
method change its x, y and z attributes and then have those new attribute
values available to the caller when the method returns.
With simple types like integers and floats, that approach won't work
because there are no attributes whose values the called function can
modify. So there's no way for closestIntersection to pass the hitFace
back to us because it is a simple integer.
To work around this the Maya API provides the MScriptUtil class.
Since the 'hitFace' parameter is a pointer to an int we first use
MScriptUtil to create a pointer to an int:
hitFacePtr = om.MScriptUtil().asIntPtr()
We pass that pointer to the closestIntersection method:
gotHit = meshFn.closestIntersection(
raySource, rayDirection,
faceIds, triangleIds,
sortIds, om.MSpace.kWorld, maxDist, bothDirections,
noAccelerator,
hitPoint,
hitParam, hitFacePtr, hitTriangle, hitBary1, hitBary2
)
Finally, we use MScriptUtil again to retrieve the integer value from
the pointer:
hitFace = om.MScriptUtil(hitFacePtr).asInt()
--
-deane