hi all,
I write a custom node with a plug that can retrieve particle node's world 
position attribute (a vector array attibute) and for debuging i print out 
the array length of this attribute in compute(). Everything goes well 
during developing except when closing the scene and reopen it. We all know 
that as scene opened the compute() will be forced to run once, and i notice 
the first time evaluation of compute() give out a wrong array length 
(length = 0) but actually i already have several particle in scene. Why i 
get wrong array length in first compute() evaluation but correct array 
length after that ?

(This is a simple test code that could show the problem, if anyone interest 
you can reference the attached scene file how i setup. It shows wrong array 
length when open the scene, but correct array length when you drag the time 
slider ... )

import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx

kPluginNodeId = OpenMaya.MTypeId(0x87013)
kPluginNodeTypeName = "Test"

class Test(OpenMayaMPx.MPxNode):
   frameAttr = OpenMaya.MObject()
   particleInAttr = OpenMaya.MObject()
   particleOutAttr = OpenMaya.MObject()

   def __init__(self):
      OpenMayaMPx.MPxNode.__init__(self)

   def compute(self, plug, dataBlock):
      if(plug == Test.particleOutAttr):
         inputHandle = dataBlock.inputValue(Test.frameAttr)
         frame = inputHandle.asFloat()
         print "Frame:", frame

         inputHandle = dataBlock.inputValue(Test.particleInAttr)
         inParticlePos = inputHandle.data()
         vectorArrayFn = OpenMaya.MFnVectorArrayData(inParticlePos)
         inPosArray = vectorArrayFn.array()
         particleAmount = inPosArray.length()

         # Why 0 when open file ???
         print "Particle Amount:", inPosArray.length()

def nodeCreator():
   return OpenMayaMPx.asMPxPtr(Test())

def nodeInitializer():
   tAttr = OpenMaya.MFnTypedAttribute()
   nAttr = OpenMaya.MFnNumericAttribute()

   Test.frameAttr = nAttr.create("frame", "frm", 
OpenMaya.MFnNumericData.kFloat)
   nAttr.setKeyable(False)
   nAttr.setReadable(False)
   Test.addAttribute(Test.frameAttr)

   vectorArrayDataFn = OpenMaya.MFnVectorArrayData()
   inVectorArray = OpenMaya.MVectorArray()
   vectorArrayDataFn.create(inVectorArray)
   Test.particleInAttr = tAttr.create("particleInPos", "ptip", 
OpenMaya.MFnData.kVectorArray, vectorArrayDataFn.object())
   tAttr.setReadable(False)
   tAttr.setHidden(True)
   Test.addAttribute(Test.particleInAttr)

   outVectorArray = OpenMaya.MVectorArray()
   vectorArrayDataFn.create(outVectorArray)
   Test.particleOutAttr = tAttr.create("particleOutPos", "ptop", 
OpenMaya.MFnData.kVectorArray, vectorArrayDataFn.object())
   tAttr.setWritable(False)
   tAttr.setHidden(True)
   Test.addAttribute(Test.particleOutAttr)

   Test.attributeAffects(Test.frameAttr, Test.particleOutAttr)
   Test.attributeAffects(Test.particleInAttr, Test.particleOutAttr)

def initializePlugin(mobject):
   mplugin = OpenMayaMPx.MFnPlugin(mobject)

   try:
      mplugin.registerNode(kPluginNodeTypeName, kPluginNodeId, nodeCreator, 
nodeInitializer)
   except:
      sys.stderr.write("Failed to register node: %s" % kPluginNodeTypeName)
      raise

def uninitializePlugin(mobject):
   mplugin = OpenMayaMPx.MFnPlugin(mobject)

   try:
      mplugin.deregisterNode(kPluginNodeId)
   except:
      sys.stderr.write("Failed to deregister node: %s" % 
kPluginNodeTypeName)
      raise

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Attachment: Test1.rar
Description: application/rar

Reply via email to