[Maya-Python] api: creating default array elements

850 views
Skip to first unread message

Chad Dombrova

unread,
Apr 26, 2010, 7:37:07 PM4/26/10
to Maya Python Group
hi,
i'm writing a custom node that has an input array attribute.  when an instance of this node is created i would like to populate the array with several default values.  within the context of an MPxNode where is the best place to do this and how?  calling getAttr myNode.myAttr[0] after creation will add the element, but this seems pretty hacky to me and maya's nodes don't resort to this.  for example, the following code shows how a fluidShape's incandescence has 3 elements after a createNode call:

s = createNode('fluidShape')

print getAttr(s + '.incandescence', size=1)


thanks,

chad




--
http://groups.google.com/group/python_inside_maya

br...@meljunky.com

unread,
Apr 26, 2010, 9:27:16 PM4/26/10
to python_in...@googlegroups.com
I had this in my compute function when I added default elements:

        thisNode = self.thisMObject()
        wPlug = OpenMaya.MPlug( thisNode, weightListNode.aWeights )

        #Write into aWeightList
        for i in range(3):
            wPlug.selectAncestorLogicalIndex( i, weightListNode.aWeightsList )
            wHandle = wPlug.constructHandle(dataBlock)
            arrayHandle = OpenMaya.MArrayDataHandle( wHandle )
            arrayBuilder = arrayHandle.builder()
            for j in range( 5 ):
                handle = arrayBuilder.addElement( j )
                value = i+j
                handle.setFloat(value)
            arrayHandle.set(arrayBuilder)
            wPlug.setMDataHandle(wHandle)
            wPlug.destructHandle(wHandle)


Don't have time to strip it down to its bare minimum or why I nested the loops.

Hope that gets you going,
-brian

--
http://groups.google.com/group/python_inside_maya

Chad Dombrova

unread,
Apr 27, 2010, 12:06:55 AM4/27/10
to python_in...@googlegroups.com
On the next call to compute won't that overwrite whatever values the user inputs for those elements?  i was hoping to setup some defaults just on creation.  i could hack together something with a boolean variable that tracks whether i've setup the defaults and check it on every call to compute, but that just seems pretty heavy handed. 

-chad

--
http://groups.google.com/group/python_inside_maya

Chad Vernon

unread,
Apr 27, 2010, 12:47:12 AM4/27/10
to python_in...@googlegroups.com
If it is a array data type like doubleArray or intArray, you can set the default array in the attribute initialization function.  If it is a setArray(True) attribute, you can do it a couple ways. 

You can:
1. Use MDGMessage::addNodeAddedCallback, and populate the array with the node creation callback.
or
2. Have a boolean initialization function that starts as false.  In your compute, if it is false, set your default values, then set it to true.
--
http://groups.google.com/group/python_inside_maya

br...@meljunky.com

unread,
Apr 27, 2010, 12:50:06 AM4/27/10
to python_in...@googlegroups.com
I realize the plug-in I pulled it from didn't really do anything but print out some results. Was created for testing purpose and I only made the compute run once (Think I converted it from C++). Your right it would try to create the plugs each time. I keep coming to the same conclusion of some form of boolean or comparing the element count unless one the the methods in the MPxNode can help.

--
http://groups.google.com/group/python_inside_maya

Judah Baron

unread,
Apr 27, 2010, 12:08:16 PM4/27/10
to python_in...@googlegroups.com
Initializing something in compute and checking it later every time compute is called is just wrong. The whole purpose of the dependency graph is to maintain efficient [lazy] evaluation of the network. For that reason, I personally would not put such a check into compute. If the node is part of some larger system then I'd build this initialization into the system. For instance, we have a basic nodetype that we hang a lot of data off of. If the node is created with the standard 'createNode' then there is no unique data on that node. It is incumbent on the system to format the node upon creation so that the node has meaning to the system. We have an api that facilitates node creation and manipulation, such as connection management, querying, etc., that wraps all of the core maya calls, and does all of our specific handling. It's a data driven system that uses an xml file to describe the structure of the data that should appear on each of the nodes.We also register a number of callbacks for these nodes when they are created, as Chad Vernon mentions. If the node is not part of a larger system then you may not have the code infrastructure in place that wold allow you to do this sort of thing easily. I would still consider wrapping the creation call into a special purpose call designed to set this node up properly. I know initializing the attribute values in this way isn't perhaps as cool or tidy, but it will keep the dg running efficiently.

A couple of questions come to mind: Who is going to be creating this node? When will the node be created?
--
http://groups.google.com/group/python_inside_maya
Reply all
Reply to author
Forward
0 new messages