1 /*
2     Copyright (c) 2008-2009 NetAllied Systems GmbH
3 
4 	This file is part of COLLADAMaya.
5 
6     Portions of the code are:
7     Copyright (c) 2005-2007 Feeling Software Inc.
8     Copyright (c) 2005-2007 Sony Computer Entertainment America
9     Copyright (c) 2004-2005 Alias Systems Corp.
10 
11     Licensed under the MIT Open Source License,
12     for details please see LICENSE file or the website
13     http://www.opensource.org/licenses/mit-license.php
14 */
15 
16 #ifndef __COLLADA_MAYA_DAG_HELPER_H__
17 #define __COLLADA_MAYA_DAG_HELPER_H__
18 
19 #ifndef _MFnNumericData
20 #include <maya/MFnNumericData.h>
21 #endif // _MFnNumericData
22 
23 #include "COLLADAMayaPrerequisites.h"
24 #include "COLLADAMayaPlatform.h"
25 
26 namespace COLLADAMaya
27 {
28 
29     class DagHelper
30     {
31 
32     public:
33         // Handle node connections
34         static MObject  getNodeConnectedTo ( const MObject& node, const String& attribute );
35         static MObject  getSourceNodeConnectedTo ( const MObject& node, const MString& attribute );
36         static bool   getPlugConnectedTo ( const MObject& node, const String& attribute, MPlug& plug );
37         static MObject  getSourceNodeConnectedTo ( const MPlug& inPlug );
38         static bool   getPlugConnectedTo ( const MPlug& inPlug, MPlug& plug );
39         static bool   getPlugArrayConnectedTo ( const MObject& node, const MString& attribute, MPlug& plug );
40         static MObject  getNodeConnectedTo ( const MPlug& plug );
41         static int   getNextAvailableIndex ( const MObject& object, const MString& attribute, int startIndex = 0 );
42         static int   getNextAvailableIndex ( const MPlug& p, int startIndex = 0 );
43         static bool   connect ( const MObject& source, const MString& sourceAttribute, const MObject& destination, const MString& destinationAttribute );
44         static bool   connect ( const MObject& source, const MString& sourceAttribute, const MPlug& destination );
45         static bool   connect ( const MPlug& source, const MObject& destination, const MString& destinationAttribute );
46         static bool   connect ( const MPlug& source, const MPlug& destination );
47         static bool   connectToList ( const MObject& source, const MString& sourceAttribute, const MObject& destination, const MString& destinationAttribute, int* index = NULL );
48         static bool   connectToList ( const MPlug& source, const MObject& destination, const MString& destinationAttribute, int* index = NULL );
49         static bool   hasConnection ( const MPlug& plug, bool asSource = true, bool asDestination = true );
50 
51         // Get/set the bind pose for a transform
52         static MMatrix  getBindPoseInverse ( const MObject& controller, const MObject& influence );
53         static MStatus  setBindPoseInverse ( const MObject& node, const MMatrix& bindPoseInverse );
54 
55         // Find a child plug
56         static MPlug  getChildPlug ( const MPlug& parent, const MString& name, MStatus* rc=NULL );
57         static int   getChildPlugIndex ( const MPlug& parent, const MString& name, MStatus* rc=NULL ); // Useful for performance reasons when iterating over many plugs
58 
59         // Get/set a plug's value
60         static bool   getPlugValue ( const MObject& node, const String& attributeName, double &value );
61         static bool   getPlugValue ( const MObject& node, const String& attributeName, float &value );
62         static bool   getPlugValue ( const MObject& node, const String& attributeName, MEulerRotation& value );
63         static bool   getPlugValue ( const MObject& node, const String& attributeName, bool& value );
64         static bool   getPlugValue ( const MObject& node, const String& attributeName, int& value );
65         static bool   getPlugValue ( const MObject& node, const String& attributeName, MMatrix& value );
66         static bool   getPlugValue ( const MObject& node, const String& attributeName, MColor& value );
67         static bool   getPlugValue ( const MObject& node, const String& attributeName, MString& value );
68         static bool   getPlugValue ( const MObject& node, const String& attributeName, MVector& value );
69         static bool   getPlugValue ( const MObject& node, const String& attributeName, int& enumValue, MString& enumName );
70         static bool   getPlugValue ( const MObject& node, const String& attributeName, MObject& value );
71         static bool   getPlugValue ( const MPlug& plug, bool& value );
72         static bool   getPlugValue ( const MPlug& plug, MMatrix& value );
73         static bool   getPlugValue ( const MPlug& plug, MColor& value );
74         static bool   getPlugValue ( const MPlug& plug, int& value );
75         static bool   getPlugValue ( const MPlug& plug, uint32& value );
76         static bool   getPlugValue ( const MPlug& plug, short& value );
77         static bool   getPlugValue ( const MPlug& plug, uint16& value );
78         static bool   getPlugValue ( const MPlug& plug, char& value );
79         static bool   getPlugValue ( const MPlug& plug, uint8& value );
80         static bool   getPlugValue ( const MPlug& plug, float& x );
81         static bool   getPlugValue ( const MPlug& plug, float& x, float& y );
82         static bool   getPlugValue ( const MPlug& plug, float& x, float& y, float& z );
83         static bool   getPlugValue ( const MPlug& plug, MVector& value );
84         static bool   getPlugValue ( const MPlug& plug, int& enumValue, MString& enumName );
85         static bool   getPlugValue ( const MPlug& plug, MObject& value );
86         static void   getPlugValue ( const MObject& node, const String& attributeName, MStringArray& output, MStatus* status = NULL );
87         static void   getPlugValue ( const MPlug& plug, MStringArray& output, MStatus* status = NULL );
88 
89         static bool   setPlugValue ( MPlug& plug, const MVector& value );
90         static bool   setPlugValue ( MPlug& plug, const MMatrix& value );
91         static bool   setPlugValue ( MPlug& plug, const MColor& value );
92         static bool   setPlugValue ( MPlug& plug, const String& value );
93 #ifdef UNICODE
94         static bool   setPlugValue ( MPlug& plug, const String& value );
95 #endif // UNICODE
setPlugValue(MPlug & plug,const MString & value)96         static bool   setPlugValue ( MPlug& plug, const MString& value )
97         {
98             return plug.setValue ( const_cast<MString&> ( value ) ) == MStatus::kSuccess;
99         }
100 
101         static bool   setPlugValue ( MPlug& plug, float value );
102         static bool   setPlugValue ( MPlug& plug, double value );
103         static bool   setPlugValue ( MPlug& plug, float x, float y );
104         static bool   setPlugValue ( MPlug& plug, int value );
105 #if defined(WIN32)
setPlugValue(MPlug & plug,int32 value)106         static bool   setPlugValue ( MPlug& plug, int32 value )
107         {
108             return setPlugValue ( plug, ( int ) value );
109         }
110 
111 #endif // windows-only.
112         static bool setPlugValue ( MPlug& plug, bool value );
113         static bool setPlugValue ( MPlug& plug, MStringArray& stringArray );
114 
115         // Helper to avoid the findPlug.
116         template <class ValueType>
setPlugValue(const MObject & node,const char * attributeName,const ValueType & value)117         static bool setPlugValue (
118             const MObject& node,
119             const char* attributeName,
120             const ValueType& value )
121         {
122             MPlug p = MFnDependencyNode ( node ).findPlug ( attributeName );
123             return setPlugValue ( p, value );
124         }
125 
126         // set an array plug size before creating the element plugs
127         static void setArrayPlugSize ( MPlug& plug, uint size );
128 
129         // Attempt to translate a String path/name into a dag path or node
130         static MDagPath getShortestDagPath ( const MObject& node );
131         static MObject getNode ( const MString& name );
132 
133         // Create an animation curve for the given plug
134         static MObject createAnimationCurve ( const MObject& node, const char* attributeName, const char* curveType );
135         static MObject createAnimationCurve ( const MPlug& plug, const char* curveType );
136         static bool plugHasAnimation ( const MPlug& plug );
137         static MObject createExpression ( const MPlug& plug, const MString& expression );
138 
139         // Create a String-typed attribute on a given node
140         static MObject createAttribute ( const MObject& node, const char* attributeName, const char* attributeShortName, MFnData::Type type, const char *value );
141         static MObject createAttribute ( const MObject& node, const char* attributeName, const char* attributeShortName, MFnNumericData::Type type, const char *value );
142         static MPlug addAttribute ( const MObject& node, const MObject& attribute );
143 
144         // get the first empty item in the named array plug
145         static unsigned getFirstEmptyElementId ( const MPlug& parent );
146 
147     private:
148 
149         /** Don't create an object of this class. */
DagHelper()150         DagHelper() {};
151     };
152 }
153 
154 #endif // __COLLADA_MAYA_DAG_HELPER_H__
155