Hello Sir,
I used to connect the '*Out Mesh*' to the inMesh.
Just as you said, I change it to *worldMesh[0]*, and it works !!! But I 
don't know why...
Can you tell me the difference between worldMesh and Out Mesh?
Thank you very much
Yixiong Xu
2019.01.05

在 2019年1月4日星期五 UTC+8下午11:55:03,serguei.k...@gmail.com写道:
>
> Hi Yixiong,
> Are you connecting worldMesh[0] as input for inMesh?
>
> On Thu, Jan 3, 2019 at 11:46 PM 徐一雄 <xuyixi...@gmail.com <javascript:>> 
> wrote:
>
>> Hello everyone, i'm a newbee to maya python API.
>> Recently, I'm struggle in the single ray tracer.
>> I build a custom MPxNode to compute the hitPoint when the ray hit the 
>> sphere. 
>> The ray is build by 2 locators in space.
>> If the sphere is at the origin point of the world space, everything seems 
>> OK.
>> But here is the* problem*:
>> When I transform the sphere object, e.x translate a little distance. The 
>> hitPoint should translate on the sphere but now it doesn't .
>> I know that when I transform the sphere, the input values of the custom 
>> node have't change and compute function doesn't be called.
>> I have tried that to add a transform attribute to call the compute 
>> function but it doesn't work. I think the hitPoint should be calculate by 
>> multiply a matrix.
>> So how can I achieve that when I transform the sphere, the hit point on 
>> the sphere also change by the direction of the ray.
>> Thank you very much.
>>
>> Here is the *code*:
>>
>> import maya.OpenMaya as om
>> import maya.OpenMayaMPx as ompx
>>
>> nodeName = 'RayTracer'
>> nodeID = om.MTypeId(0X100fff)
>>
>>
>> class MeshIntersectionNode(ompx.MPxNode):
>>     # Class Attrs
>>     # INPUT
>>     Loc_1_Pos = om.MObject()
>>     Loc_2_Pos = om.MObject()
>>
>>     inMesh = om.MObject()
>>
>>     # OUTPUT
>>     Loc_3_Pos = om.MObject()
>>
>>     def __init__(self):
>>         ompx.MPxNode.__init__(self)
>>
>>     def compute(self, plug, dataBlock):
>>         if plug != MeshIntersectionNode.Loc_3_Pos:
>>             return om.kUnknownParameter
>>
>>         locator1Vector = 
>> om.MVector(dataBlock.inputValue(MeshIntersectionNode.Loc_1_Pos).asFloatVector())
>>         locator2Vector = 
>> om.MVector(dataBlock.inputValue(MeshIntersectionNode.Loc_2_Pos).asFloatVector())
>>         inputMeshObj = 
>> dataBlock.inputValue(MeshIntersectionNode.inMesh).asMesh()
>>
>>         direction = om.MFloatVector(locator2Vector - locator1Vector)
>>
>>         sourcePoint = 
>> om.MFloatPoint(dataBlock.inputValue(MeshIntersectionNode.Loc_1_Pos).asFloatVector())
>>
>>         inputMeshMFn = om.MFnMesh(inputMeshObj)
>>
>>         hitPoints = om.MFloatPoint()
>>
>>         tolerance = float(0.0)
>>
>>         inputMeshMFn.closestIntersection(sourcePoint, direction, None, None, 
>> False, om.MSpace.kWorld,
>>                                             10000.0, False, None, hitPoints, 
>> None, None, None, None, None, tolerance)
>>
>>         outputPoint = dataBlock.outputValue(MeshIntersectionNode.Loc_3_Pos)
>>
>>         if hitPoints[0]:
>>             outputVector = om.MFloatVector(hitPoints.x, hitPoints.y, 
>> hitPoints.z)
>>         else:
>>             outputVector = om.MFloatVector(0.0, 0.0, 0.0)
>>
>>         outputPoint.setMFloatVector(outputVector)
>>
>>         dataBlock.setClean(plug)
>>
>>
>> def nodeCreator():
>>     return ompx.asMPxPtr(MeshIntersectionNode())
>>
>>
>> def nodeInitializer():
>>     MFnNumericAttr = om.MFnNumericAttribute()
>>     MFnMeshAttribute = om.MFnTypedAttribute()
>>
>>     # create attribute
>>     # input
>>     MeshIntersectionNode.Loc_1_Pos = 
>> MFnNumericAttr.createPoint('Locator1Position', 'Loc1Pos')
>>     MFnNumericAttr.setWritable(1)
>>     MFnNumericAttr.setKeyable(1)
>>     MeshIntersectionNode.addAttribute(MeshIntersectionNode.Loc_1_Pos)
>>
>>     MeshIntersectionNode.Loc_2_Pos = 
>> MFnNumericAttr.createPoint('Locator2Position', 'Loc2Pos')
>>     MFnNumericAttr.setWritable(1)
>>     MFnNumericAttr.setKeyable(1)
>>     MeshIntersectionNode.addAttribute(MeshIntersectionNode.Loc_2_Pos)
>>
>>     MeshIntersectionNode.inMesh = MFnMeshAttribute.create('inMesh', 'im', 
>> om.MFnData.kMesh)
>>     MFnMeshAttribute.setWritable(1)
>>     MFnMeshAttribute.setKeyable(1)
>>     MeshIntersectionNode.addAttribute(MeshIntersectionNode.inMesh)
>>
>>     # output
>>     MeshIntersectionNode.Loc_3_Pos = 
>> MFnNumericAttr.createPoint('Locator3Position', 'Loc3Pos')
>>     MFnNumericAttr.setWritable(0)
>>     MFnNumericAttr.setReadable(1)
>>     MeshIntersectionNode.addAttribute(MeshIntersectionNode.Loc_3_Pos)
>>
>>     # make influence
>>     MeshIntersectionNode.attributeAffects(MeshIntersectionNode.Loc_1_Pos, 
>> MeshIntersectionNode.Loc_3_Pos)
>>     MeshIntersectionNode.attributeAffects(MeshIntersectionNode.Loc_2_Pos, 
>> MeshIntersectionNode.Loc_3_Pos)
>>     MeshIntersectionNode.attributeAffects(MeshIntersectionNode.inMesh, 
>> MeshIntersectionNode.Loc_3_Pos)
>>
>>
>> def initializePlugin(mObj):
>>     mPlugin = ompx.MFnPlugin(mObj, 'Yixiong Xu', '1.0')
>>
>>     try:
>>         mPlugin.registerNode(nodeName, nodeID, nodeCreator, nodeInitializer)
>>     except:
>>         raise RuntimeError
>>         print 'Failed to register node.'
>>
>>
>> def uninitializePlugin(mObj):
>>     mPlugin = ompx.MFnPlugin(mObj)
>>
>>     try:
>>         mPlugin.deregisterNode(nodeID)
>>     except:
>>         raise RuntimeError
>>         print 'Failed to deregister node.'
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Python Programming for Autodesk Maya" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to python_inside_maya+unsubscr...@googlegroups.com <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/python_inside_maya/9002b2ca-1d44-43d7-9f76-b8dcce2455f6%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/python_inside_maya/9002b2ca-1d44-43d7-9f76-b8dcce2455f6%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/c30a5d56-923b-4edc-a361-67bfa721b8a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to