Public Member Functions | Static Public Member Functions | Static Public Attributes

MPxThreadedDeviceNode Class Reference

This reference page is linked to from the following overview topics: Autodesk Maya 2013.


Search for all occurrences

Detailed Description

Base class for threaded node creation.

MPxThreadedDeviceNode is the base class used for creating threaded maya device nodes.

Maya handles thread management. The plug-in needs to implement the threadHandler() and threadShutdownHandler(). Maya will call these handlers as it manages the thread.

The interface implements a memory queue system where data pointers are acquired, filled, then pushed into the node using mutex locking. This is done from a separate, secondary thread. When the data has been acquired, the thread will mark the output attribute as dirty so that the compute in the main thread will be called. The main thread will pop the oldest data buffer, process it, then release the thread data. The helper class MCharBuffer is used with the memory methods.

The "live" attribute allows finer control over the secondary thread. It can be used to stop the handling of incoming data without disconnecting the output attributes. Note that the thread continues to run even if the the live attribute is set to false.

To implement an MPxThreadedDeviceNode plug-in, you must: 1. Use the post constructor to call setRefreshOutputAttributes() and createMemoryPools() 2. Implement the destructor to destroy the memory pools 3. Implement the threadHandler() virtual. Maya will call this handler when it creates the thread. Typically the thread handler will implement a loop for collecting data. This class contains a "done" state which is accessible via the isDone() and setDone() methods. Loop while isDone() is false 4. Implement the threadShutdownHandler(). This method would be called by Maya on File -> New and Exit for example. Call setDone( true ) to stop the isDone() loop implemented in the threadHandler if the plug-in uses the done state 5. The secondary thread should call beginThreadLoop() and endThreadLoop() with the data operations happening in-between. The endThreadLoop() method will perform the throttling based on the frame rate attribute and additionally cause the output attribute to be marked dirty. This in turn will cause compute() to be called in the main thread once Maya processes the dirty message 6. The thread loop should acquireDataStorage() to get a buffer, populate the buffer with information and then call pushThreadData() 8. The compute method should popThreadData() and if it's valid, process the data and release the data pointer by calling releaseDataStorage().

The live and frameRate attributes are included in this class. Changes to these attributes will cause the thread to be shutdown and restarted so that the new settings can be in effect.

NOTE: This class relies on the idle event queue to perform the node attribute updating. As a result, it will not work in Maya batch mode.

#include <MPxThreadedDeviceNode.h>

Inheritance diagram for MPxThreadedDeviceNode:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 MPxThreadedDeviceNode ()
 Constructor.
virtual ~MPxThreadedDeviceNode ()
 Class destructor.
virtual MPxNode::Type type () const
 Returns the type of node that this is.
virtual void threadHandler ()
 This method should be overridden in user defined nodes.
virtual void threadShutdownHandler ()
 This method should be overridden in user defined nodes.
bool isLive ()
 Returns the true if the node is live.
bool isDone ()
 Returns the true if the node has finished running.
MStatus setDone (bool done)
 This method is used to set done state of the thread.
MStatus beginThreadLoop ()
 This method should be called at the beginning of the loop in the secondary thread.
MStatus endThreadLoop ()
 This method should be called at the end of the loop in the secondary thread.
MStatus createMemoryPools (int nCount, int nElements, long nSize)
 This method should be called from the post constructor to create the memory buffers required.
MStatus destroyMemoryPools ()
 This method should be called to destroy the memory pools.
MStatus acquireDataStorage (MCharBuffer &buffer)
 This method is used to acquire a pointer to a block of storage that was allocated by the createMemoryPools() method.
MStatus pushThreadData (MCharBuffer &buffer)
 This method is used to push the block of storage onto the internal queue.
MStatus popThreadData (MCharBuffer &buffer)
 This method is used to pop the block of storage from the internal queue.
long threadDataCount ()
 This method returns the number of pieces of thread data that are currently in the node's queue.
MStatus releaseDataStorage (MCharBuffer &buffer)
 After popThreadData() is called, the pointer should be released so that it can be used again.
MStatus setRefreshOutputAttributes (const MObjectArray &attributeList)
 This method is used to set the list of output attributes which will be marked dirty by the secondary thread.

Static Public Member Functions

static const char * className ()
 Returns the name of this class.

Static Public Attributes

static MObject output
 Output data, generic attribute.
static MObject live
 Makes node live, boolean attribute.
static MObject frameRate
 Capture frame rate, double attribute.

Constructor & Destructor Documentation

Constructor.

The constructor should never call any methods from MPxNode or make any calls that require the existence of the MObject associated with the user defined node. The postConstructor method should be used to do any initialization of this kind.

For example, the postConstructor method would be used to call: setRefreshOutputAttributes()


Member Function Documentation

MPxNode::Type type ( ) const [virtual]

Returns the type of node that this is.

This is used to differentiate user defined nodes that are derived off different MPx base classes.

It is not necessary to override this method.

Returns:
Type of the node

Reimplemented from MPxNode.

Reimplemented in MPxClientDeviceNode.

void threadHandler ( ) [virtual]

This method should be overridden in user defined nodes.

Thread management( creation/destruction/shutdown ) will be handled by Maya. This method will be called when the thread has been created. In a typical implementation, the thread handler will implement a loop for reading information from an external device.

void threadShutdownHandler ( ) [virtual]

This method should be overridden in user defined nodes.

This method is used to shutdown the loop running in the thread handler.

MStatus setDone ( bool  done)

This method is used to set done state of the thread.

Parameters:
[in]donebool to set
Status Codes:
  • MS::kSuccess operation successful
  • MS::kFailure this MPxNode has not been initialized properly. Make sure this method is not being called from the constructor.
MStatus beginThreadLoop ( )

This method should be called at the beginning of the loop in the secondary thread.

Returns:
Status code
Status Codes:
MStatus endThreadLoop ( )

This method should be called at the end of the loop in the secondary thread.

This method will throttle the thread loop based on the frame rate attribute value. For example, if you set the frameTime attribute to 1 and the processing of your thread loop body takes .25 seconds, then this method will cause a delay of .75 seconds before the thread loop restarts. This method will also mark the refresh output attribute as dirty which will make compute() be called in the main thread.

Returns:
Status code
Status Codes:
MStatus createMemoryPools ( int  nCount,
int  nElements,
long  nSize 
)

This method should be called from the post constructor to create the memory buffers required.

Parameters:
[in]nCountthe number of pools to create
[in]nElementsthe number of elements in each pool
[in]nSizethe size of each elememt
Returns:
Status code
Status Codes:
MStatus destroyMemoryPools ( )

This method should be called to destroy the memory pools.

This can be done in the destructor.

Returns:
Status code
Status Codes:
MStatus acquireDataStorage ( MCharBuffer buffer)

This method is used to acquire a pointer to a block of storage that was allocated by the createMemoryPools() method.

Parameters:
[out]buffercontains a pointer to the buffer that should be written into
Returns:
Status code
Status Codes:
MStatus pushThreadData ( MCharBuffer buffer)

This method is used to push the block of storage onto the internal queue.

This is usually done by a secondary thread.

NOTE: the buffer parameter will be cleared upon the successful return of this method. This is to avoid reusing the pointer.

Parameters:
[in,out]buffercontains a pointer to the buffer that should be put onto the internal queue
Returns:
Status code
Status Codes:
  • MS::kSuccess Operation successful
  • MS::kFailure this MPxNode has not been initialized properly or the push failed or the buffer size was 0 or the buffer pointer was NULL
MStatus popThreadData ( MCharBuffer buffer)

This method is used to pop the block of storage from the internal queue.

This is usually done in the main thread's compute() method.

Parameters:
[out]buffercontains a pointer to the buffer that should be processed
Returns:
Status code
Status Codes:
long threadDataCount ( )

This method returns the number of pieces of thread data that are currently in the node's queue.

For example if you called pushThreadData() 10 times but only called popThreadData() 3 times, you would have a thread data count of 7.

Returns:
thread data count
MStatus releaseDataStorage ( MCharBuffer buffer)

After popThreadData() is called, the pointer should be released so that it can be used again.

This is usually done in the main thread's compute() method. NOTE: the buffer pointer will be reset to NULL if this method succeeds.

Parameters:
[in,out]buffercontains a pointer to the buffer that has been processed and should now be released
Returns:
Status code
Status Codes:
  • MS::kSuccess Operation successful
  • MS::kFailure this MPxNode has not been initialized properly or the release of the memory buffer failed or the buffer pointer is NULL
MStatus setRefreshOutputAttributes ( const MObjectArray attributeList)

This method is used to set the list of output attributes which will be marked dirty by the secondary thread.

After the secondary thread receives data, it sends a message to dirty the attributes specified in the parameter list. This will then cause a refresh to make the object update.

Parameters:
[in]attributeLista list of output attributes that will cause the node to refresh automatically. NOTE: Currently, only one attribute is supported
Status Codes:
  • MS::kSuccess operation successful
  • MS::kFailure this MPxNode has not been initialized properly. Make sure this method is not being called from the constructor.
const char * className ( ) [static]

Returns the name of this class.

Returns:
The name of this class.

Reimplemented from MPxNode.

Reimplemented in MPxClientDeviceNode.


MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode
MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode MPxThreadedDeviceNode