Message 1 of 4
MFnMesh.getClosestIntersection method is causing problem

Not applicable
09-18-2012
11:16 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello Everyone,
I'm quite new to Maya API programming. Recently I was trying to develop a plugin which uses ray projection, I've used MFnMesh.getClosestIntersection method to do so. But it's causing some problem which I'm unable to figure out. This plugin works on my laptop but on every other computer, whenever the plugin goes into Compute method, Maya crashes. I'm attaching the code with this post. If anyone could help me figure out what the problem is then would be really great.
The plugin requires a scene with polyPlane, polySphere and a locator. Then connect the attribute as below.
polyPlane.worldMesh -> project1.inputMeshSrc
locator1.translate -> project1.inputPoint
project1.output -> pSphere1.translate
Please also note that I'm testing this code on Maya 2012. The indentations are not displayed correctly on this page but my code has proper indentations.
I'm quite new to Maya API programming. Recently I was trying to develop a plugin which uses ray projection, I've used MFnMesh.getClosestIntersection method to do so. But it's causing some problem which I'm unable to figure out. This plugin works on my laptop but on every other computer, whenever the plugin goes into Compute method, Maya crashes. I'm attaching the code with this post. If anyone could help me figure out what the problem is then would be really great.
''' Author - Pranay Meher
Date - 26-8-2012
This script emulates the wheel collission on a ground plane
'''
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
import sys
kPluginNodeTypeName = 'project'
kPluginNodeId = OpenMaya.MTypeId( 0x800010 )
# Class implementaion
class projectNode( OpenMayaMPx.MPxNode ):
inputMeshsrc=OpenMaya.MObject()
iteration = OpenMaya.MObject()
inputPoint = OpenMaya.MObject()
output = OpenMaya.MObject()
radius = OpenMaya.MObject()
def __init__( self ):
OpenMayaMPx.MPxNode.__init__( self )
def compute( self, plug, data ):
if plug == self.output:
print 'In Calculation'
# get the inputMeshSrc and in point data handle
inMeshSrcHandle = data.inputValue( self.inputMeshSrc )
inPointHandle = data.inputValue( self.inputPoint )
meshsrc=inMeshSrcHandle.asMesh()
mFnMeshSource = OpenMaya.MFnMesh( meshSrc )
inRadiusHandle = data.inputValue( self.radius )
inRadius = inRadiusHandle.asFloat()
inPoint = inPointHandle.asFloat3()
groundPoint = self.rayProject( mFnMeshSource, inPoint )
outHandle = data.outputValue( self.output )
outHandle.set3Float( groundPoint.x, groundPoint.y, groundPoint.z )
print 'Throwing Ground Point'
data.setClean( plug )
else:
return OpenMaya.MStatus.kUnknownParameter
@classmethod
def nodeCreator( cls ):
return OpenMayaMPx.asMPxPtr( cls() )
def rayProject( self, mFnMeshSource, pointSource ):
# This attribute will store the intersection Point in worldspace
hitPoint = OpenMaya.MFloatPoint()
#Setting up attributes required by MFn mesh to calculate intersection
rayDirection = OpenMaya.MFloatVector( 0, -1, 0 )
raySource = OpenMaya.MFloatPoint( pointSource, pointSource, pointSource )
hitFacePtr = OpenMaya.MScriptUtil().asIntPtr()
idsSorted = False
testBothDirections = True
faceIds = None
triIds = None
accelParams = None
hitRayParam = None
hitTriangle = None
hitBary1 = None
hitBary2 = None
maxParamPtr = 99999999
tolerance = 0.1
hit = mFnMeshSource.closestIntersection(raySource,
rayDirection,
faceIds,
triIds,
idsSorted,
OpenMaya.MSpace.kWorld,
maxParamPtr,
testBothDirections,
accelParams,
hitPoint,
hitRayParam,
hitFacePtr,
hitTriangle,
hitBary1,
hitBary2)
if hit:
outTemp = OpenMaya.MPoint( hitPoint.x, hitPoint.y, hitPoint.z )
hitMPoint = OpenMaya.MPoint( hitPoint.x, hitPoint.y, hitPoint.z )
return hitMPoint
@classmethod
def nodeInitializer( cls ):
fnTypedAttr = OpenMaya.MFnTypedAttribute()
fnNumericAttr = OpenMaya.MFnNumericAttribute()
# Setting up the input Mesh attribute
cls.inputMeshsrc=fnTypedAttr.create( 'InputMeshSrc', 'inMeshSrc', OpenMaya.MFnData.kMesh )
fnTypedAttr.setReadable( True )
fnTypedAttr.setHidden( True )
cls.radius = fnNumericAttr.create( 'Radius', 'rds', OpenMaya.MFnNumericData.kFloat )
# Setting the trasformation point attributes
cls.inputPoint = fnNumericAttr.createPoint( 'inputPoint', 'inPoint' )
fnNumericAttr.setConnectable( True )
# Setting up output compound attribute
cls.output = fnNumericAttr.createPoint( 'output', 'out' )
fnNumericAttr.setWritable( False )
fnNumericAttr.setStorable( False )
cls.addAttribute( cls.inputMeshSrc )
cls.addAttribute( cls.radius )
cls.addAttribute( cls.inputPoint )
cls.addAttribute( cls.output )
cls.attributeAffects( cls.inputPoint, cls.output )
cls.attributeAffects( cls.inputMeshSrc, cls.output )
cls.attributeAffects( cls.radius, cls.output )
def initializePlugin( mobject ):
mplugin = OpenMayaMPx.MFnPlugin( mobject )
try:
mplugin.registerNode( kPluginNodeTypeName, kPluginNodeId, projectNode.nodeCreator, projectNode.nodeInitializer )
except:
sys.stderr.write( 'Could not register plugin - %s\n' % kPluginNodeTypeName )
raise
def uninitializePlugin( mobject ):
mplugin = OpenMayaMPx.MFnPlugin( mobject )
try:
mplugin.deregisterNode( kPluginNodeId )
except:
sys.stderr.write('Could not unregister plugin - %s\n' % kPluginNodeTypeName )
The plugin requires a scene with polyPlane, polySphere and a locator. Then connect the attribute as below.
polyPlane.worldMesh -> project1.inputMeshSrc
locator1.translate -> project1.inputPoint
project1.output -> pSphere1.translate
Please also note that I'm testing this code on Maya 2012. The indentations are not displayed correctly on this page but my code has proper indentations.