Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

MFnMesh.getClosestIntersection method is causing problem

MFnMesh.getClosestIntersection method is causing problem

Anonymous
Not applicable
535 Views
3 Replies
Message 1 of 4

MFnMesh.getClosestIntersection method is causing problem

Anonymous
Not applicable
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.



''' 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.

projectionnode.zip

0 Likes
536 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
Since code is not displayed with proper indentations, I can mail the plugin code whoever is willing to help me. Thanks in advance. Please send me a mail on pranaymeher@gmail.com
0 Likes
Message 3 of 4

Steve_Curley
Mentor
Mentor
If you don't mind it being made publically available attach it to the above post - must be zipped or it won't attach. See this post for help with attachments.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 4 of 4

Anonymous
Not applicable
Thanks Steve,
I've attached the test scene and plugin code to my post.
0 Likes