2
Copyright (c) 2008-2009 NetAllied Systems GmbH
4
This file is part of COLLADAMaya.
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.
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
16
#include "COLLADAMayaStableHeaders.h"
18
#include <maya/MFnAttribute.h>
19
#include <maya/MFnMesh.h>
20
//Hack to circumvent bad code in Maya7.0
22
class MFnNurbsSurface;
23
#include <maya/MFnNurbsSurface.h>
24
#include <maya/MItDependencyGraph.h>
25
#include <maya/MItDependencyNodes.h>
26
#include <maya/MSelectionList.h>
27
#include <maya/MFnComponentListData.h>
29
#include "COLLADAMayaDagHelper.h"
30
#include "COLLADAMayaShaderHelper.h"
31
#include "COLLADAMayaSyntax.h"
36
//----------------------------------
37
MObject ShaderHelper::getLayeredTextures (
38
const MObject& layeredTexture,
39
MObjectArray& fileTextures,
40
MIntArray& blendModes )
42
MFnDependencyNode layeredTextureFn ( layeredTexture );
45
MPlug inputs = findPlug ( layeredTextureFn, "inputs", &rc );
46
CHECK_STATUS ( rc, MString ( "Unable to locate inputs array plug on layeredTexture: " ) + layeredTextureFn.name() );
48
uint elementCount = inputs.numElements();
49
for ( int i = ( int ) ( elementCount - 1 ); i >= 0; --i )
51
MPlug input = inputs.elementByPhysicalIndex ( i, &rc );
52
CHECK_STATUS ( rc, MString ( "Unable to locate input#" ) + i + MString ( " on layeredTexture: " ) + layeredTextureFn.name() );
54
MPlug colorInput = DagHelper::getChildPlug ( input, "c", &rc ); // "color"
55
CHECK_STATUS ( rc, MString ( "Unable to locate input#" ) + i + MString ( "'s color child plug on layeredTexture: " ) + layeredTextureFn.name() );
57
MObject connection = DagHelper::getNodeConnectedTo ( colorInput );
58
if ( connection != MObject::kNullObj )
60
fileTextures.append ( connection );
62
MPlug blendMode = DagHelper::getChildPlug ( input, "bm", &rc ); // "blendMode"
63
CHECK_STATUS ( rc, MString ( "Unable to locate input#" ) + i + MString ( "'s blend mode child plug on layeredTexture: " ) + layeredTextureFn.name() );
66
blendMode.getValue ( mode );
67
blendModes.append ( mode );
71
return layeredTexture;
74
//----------------------------------
75
MObject ShaderHelper::getShadingEngine ( MObject shadingNetwork )
77
MStatus rc = MStatus::kSuccess;
78
MItDependencyGraph it ( shadingNetwork,
80
MItDependencyGraph::kDownstream,
81
MItDependencyGraph::kBreadthFirst,
82
MItDependencyGraph::kNodeLevel, &rc );
83
return ( rc == MStatus::kSuccess ? it.thisNode() : MObject::kNullObj );
86
//----------------------------------
87
MStatus ShaderHelper::getConnectedShaders (
88
const MFnDependencyNode& node,
89
MObjectArray& shaders )
92
node.getConnections ( nodePlugs );
94
for ( uint i = 0; i < nodePlugs.length(); i++ )
96
const MPlug& nodePlug = nodePlugs[i];
97
MPlugArray nodeDestinations;
98
nodePlug.connectedTo ( nodeDestinations, false, true );
99
for ( uint j = 0; j < nodeDestinations.length(); j++ )
101
if ( nodeDestinations[j].node().apiType() == MFn::kShadingEngine )
103
shaders.append ( nodeDestinations[j].node() );
108
return ( shaders.length() > 0 ? MStatus::kSuccess : MStatus::kFailure );
111
//----------------------------------
112
MPlug ShaderHelper::findPlug (
113
const MFnDependencyNode& node,
114
const MString& plugName,
118
MPlug p = node.findPlug ( plugName, &st );
119
if ( st == MStatus::kSuccess )
121
if ( rc != NULL ) *rc = MStatus::kSuccess;
127
st = node.getConnections ( plugs );
128
if ( st != MStatus::kSuccess )
130
if ( rc != NULL ) *rc = st;
135
for ( unsigned int i = 0; i < plugs.length(); ++i )
138
MFnAttribute attribute ( p.attribute() );
139
if ( attribute.name() == plugName )
141
if ( rc != NULL ) *rc = MStatus::kSuccess;
146
if ( rc != NULL ) *rc = MStatus::kNotFound;
151
//----------------------------------
152
static const int g_projectionTypeValueCount = 9;
153
static const char* g_projectionTypeValues[g_projectionTypeValueCount] = { ( "NONE" ), ( "PLANAR" ), ( "SPHERICAL" ), ( "CYLINDRICAL" ), ( "BALL" ), ( "CUBIC" ), ( "TRIPLANAR" ), ( "CONCENTRIC" ), ( "PERSPECTIVE" ) };
154
int ShaderHelper::toProjectionType ( const char* type )
156
for ( int i = 0; i < g_projectionTypeValueCount; ++i )
158
if ( strcmp ( type, g_projectionTypeValues[i] ) == 0 ) return i;
161
// According to the documentation, "PLANAR" is the default value
162
return defaultProjectionType();
165
//----------------------------------
166
const char* ShaderHelper::projectionTypeToString ( int type )
168
if ( type >= 0 && type < g_projectionTypeValueCount )
170
return g_projectionTypeValues[type];
175
return g_projectionTypeValues[defaultProjectionType() ];
179
//----------------------------------
180
int ShaderHelper::defaultProjectionType()
182
// According to the documentation, "PLANAR" is the default value
186
//----------------------------------
187
uint ShaderHelper::getAssociatedUVSet(const MObject& shape, const MObject& textureNode)
189
// Rather than trying to find all the node types which may be derived
190
// off texture2d, then handling optionally handling placement nodes,
191
// we can simply walk backwards along the uvCoord attributes until
192
// we find a uvChooser.
194
MObject node = textureNode;
195
while(node.apiType() != MFn::kUvChooser)
197
MFnDependencyNode dgFn(node);
198
MPlug plug = dgFn.findPlug(MString("uvCoord"), &status);
199
if (status == MS::kSuccess && plug.isConnected())
201
// Get the connection - there can be at most one input to a plug
203
MPlugArray connections;
204
plug.connectedTo(connections, true, false);
205
if (connections.length() > 0)
207
node = connections[ 0].node();
220
// Did we find a uvChooser?
222
if (node.apiType() == MFn::kUvChooser)
224
// Find the uvSet connection to this shape
226
MFnDependencyNode dgFn(node);
227
MPlug plug = dgFn.findPlug(MString("uvSets"), &status);
228
if (status == MS::kSuccess && plug.isArray())
230
// Iterate over all the elements in this array looking for
231
// one which connects to our shape
233
uint numUVSets = plug.evaluateNumElements();
234
for (uint i = 0; i < numUVSets; i++)
236
MPlug uvSetPlug = plug.elementByPhysicalIndex(i, &status);
237
if (status && uvSetPlug.isConnected())
239
// Get the connection - there can be at most one input to a plug
241
MPlugArray connections;
242
uvSetPlug.connectedTo(connections, true, false);
243
if (connections.length() > 0 && connections[0].node() == shape)
245
// connected through name element
246
MPlug uvSetElement= connections[0].parent();
247
MPlug uvSetArray = uvSetElement.array();
248
if (uvSetArray.isArray())
250
uint logicalIndex = uvSetElement.logicalIndex();
251
for (uint child = 0; child < uvSetArray.numElements(); ++child)
253
MPlug childPlug = uvSetArray.elementByPhysicalIndex(child);
254
if (childPlug.logicalIndex() == logicalIndex)
267
// [KThacker] Another weird thing. MFnMesh keeps an internal list of uvSet names.
268
// This is not necessarily in the physical order of the plugs under uvSet (elementByPhysicalIndex),
269
// or the logical order (through MEL uvSet[<logical index>]).
270
// When no uvChooser is present it uses the first uvSet name from the list
271
// to define which uvSet to use as default, if the name is different it uses a uvChooser.
272
// Got this problem after importing a file from FBX.
274
// So we get the list of names and search through the plugs in physical order
275
// to get the correct physical index to return.
278
MStringArray setNames;
279
mesh.getUVSetNames(setNames);
281
MPlug uvSetPlug = mesh.findPlug("uvSet");
283
for (uint i = 0; i < uvSetPlug.numElements(); ++i)
285
// get uvSet[<index>]
286
MPlug uvSetElememtPlug = uvSetPlug.elementByPhysicalIndex(i);
288
// get uvSet[<index>].uvSetName
289
MPlug uvSetNamePlug = uvSetElememtPlug.child(0);
291
// get value of plug (uvSet's name)
294
uvSetNamePlug.getValue(uvSetName);
296
if (uvSetName==setNames[0])
301
// we don't want to get here, because it'll end up with wrong results.
b'\\ No newline at end of file'