~ubuntu-branches/ubuntu/wily/opencollada/wily-proposed

« back to all changes in this revision

Viewing changes to COLLADAMaya/src/COLLADAMayaShaderHelper.cpp

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2015-05-14 17:23:27 UTC
  • Revision ID: package-import@ubuntu.com-20150514172327-f862u8envms01fra
Tags: upstream-0.1.0~20140703.ddf8f47+dfsg1
ImportĀ upstreamĀ versionĀ 0.1.0~20140703.ddf8f47+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include "COLLADAMayaStableHeaders.h"
 
17
 
 
18
#include <maya/MFnAttribute.h>
 
19
#include <maya/MFnMesh.h>
 
20
//Hack to circumvent bad code in Maya7.0
 
21
 
 
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>
 
28
 
 
29
#include "COLLADAMayaDagHelper.h"
 
30
#include "COLLADAMayaShaderHelper.h"
 
31
#include "COLLADAMayaSyntax.h"
 
32
 
 
33
namespace COLLADAMaya
 
34
{
 
35
 
 
36
    //----------------------------------
 
37
    MObject ShaderHelper::getLayeredTextures ( 
 
38
        const MObject& layeredTexture,
 
39
        MObjectArray& fileTextures,
 
40
        MIntArray& blendModes )
 
41
    {
 
42
        MFnDependencyNode layeredTextureFn ( layeredTexture );
 
43
        MStatus rc;
 
44
 
 
45
        MPlug inputs = findPlug ( layeredTextureFn, "inputs", &rc );
 
46
        CHECK_STATUS ( rc, MString ( "Unable to locate inputs array plug on layeredTexture: " ) + layeredTextureFn.name() );
 
47
 
 
48
        uint elementCount = inputs.numElements();
 
49
        for ( int i = ( int ) ( elementCount - 1 ); i >= 0; --i )
 
50
        {
 
51
            MPlug input = inputs.elementByPhysicalIndex ( i, &rc );
 
52
            CHECK_STATUS ( rc, MString ( "Unable to locate input#" ) + i + MString ( " on layeredTexture: " ) + layeredTextureFn.name() );
 
53
 
 
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() );
 
56
 
 
57
            MObject connection = DagHelper::getNodeConnectedTo ( colorInput );
 
58
            if ( connection != MObject::kNullObj )
 
59
            {
 
60
                fileTextures.append ( connection );
 
61
 
 
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() );
 
64
 
 
65
                int mode;
 
66
                blendMode.getValue ( mode );
 
67
                blendModes.append ( mode );
 
68
            }
 
69
        }
 
70
 
 
71
        return layeredTexture;
 
72
    }
 
73
 
 
74
    //----------------------------------
 
75
    MObject ShaderHelper::getShadingEngine ( MObject shadingNetwork )
 
76
    {
 
77
        MStatus rc = MStatus::kSuccess;
 
78
        MItDependencyGraph it ( shadingNetwork,
 
79
                                MFn::kShadingEngine,
 
80
                                MItDependencyGraph::kDownstream,
 
81
                                MItDependencyGraph::kBreadthFirst,
 
82
                                MItDependencyGraph::kNodeLevel, &rc );
 
83
        return ( rc == MStatus::kSuccess ? it.thisNode() : MObject::kNullObj );
 
84
    }
 
85
 
 
86
    //----------------------------------
 
87
    MStatus ShaderHelper::getConnectedShaders ( 
 
88
        const MFnDependencyNode& node, 
 
89
        MObjectArray& shaders )
 
90
    {
 
91
        MPlugArray nodePlugs;
 
92
        node.getConnections ( nodePlugs );
 
93
 
 
94
        for ( uint i = 0; i < nodePlugs.length(); i++ )
 
95
        {
 
96
            const MPlug& nodePlug = nodePlugs[i];
 
97
            MPlugArray nodeDestinations;
 
98
            nodePlug.connectedTo ( nodeDestinations, false, true );
 
99
            for ( uint j = 0; j < nodeDestinations.length(); j++ )
 
100
            {
 
101
                if ( nodeDestinations[j].node().apiType() == MFn::kShadingEngine )
 
102
                {
 
103
                    shaders.append ( nodeDestinations[j].node() );
 
104
                }
 
105
            }
 
106
        }
 
107
 
 
108
        return ( shaders.length() > 0 ? MStatus::kSuccess : MStatus::kFailure );
 
109
    }
 
110
 
 
111
    //----------------------------------
 
112
    MPlug ShaderHelper::findPlug ( 
 
113
        const MFnDependencyNode& node, 
 
114
        const MString& plugName, 
 
115
        MStatus* rc )
 
116
    {
 
117
        MStatus st;
 
118
        MPlug p = node.findPlug ( plugName, &st );
 
119
        if ( st == MStatus::kSuccess )
 
120
        {
 
121
            if ( rc != NULL ) *rc = MStatus::kSuccess;
 
122
 
 
123
            return p;
 
124
        }
 
125
 
 
126
        MPlugArray plugs;
 
127
        st = node.getConnections ( plugs );
 
128
        if ( st != MStatus::kSuccess )
 
129
        {
 
130
            if ( rc != NULL ) *rc = st;
 
131
 
 
132
            return p;
 
133
        }
 
134
 
 
135
        for ( unsigned int i = 0; i < plugs.length(); ++i )
 
136
        {
 
137
            p = plugs[i];
 
138
            MFnAttribute attribute ( p.attribute() );
 
139
            if ( attribute.name() ==  plugName )
 
140
            {
 
141
                if ( rc != NULL ) *rc = MStatus::kSuccess;
 
142
                return p;
 
143
            }
 
144
        }
 
145
 
 
146
        if ( rc != NULL ) *rc = MStatus::kNotFound;
 
147
 
 
148
        return p;
 
149
    }
 
150
 
 
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 )
 
155
    {
 
156
        for ( int i = 0; i < g_projectionTypeValueCount; ++i )
 
157
        {
 
158
            if ( strcmp ( type, g_projectionTypeValues[i] ) == 0 ) return i;
 
159
        }
 
160
 
 
161
        // According to the documentation, "PLANAR" is the default value
 
162
        return defaultProjectionType();
 
163
    }
 
164
 
 
165
    //----------------------------------
 
166
    const char* ShaderHelper::projectionTypeToString ( int type )
 
167
    {
 
168
        if ( type >= 0 && type < g_projectionTypeValueCount )
 
169
        {
 
170
            return g_projectionTypeValues[type];
 
171
        }
 
172
 
 
173
        else
 
174
        {
 
175
            return g_projectionTypeValues[defaultProjectionType() ];
 
176
        }
 
177
    }
 
178
 
 
179
    //----------------------------------
 
180
    int ShaderHelper::defaultProjectionType()
 
181
    {
 
182
        // According to the documentation, "PLANAR" is the default value
 
183
        return 1;
 
184
    }
 
185
 
 
186
    //----------------------------------
 
187
    uint ShaderHelper::getAssociatedUVSet(const MObject& shape, const MObject& textureNode)
 
188
    {
 
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.
 
193
        MStatus status;
 
194
        MObject node = textureNode;
 
195
        while(node.apiType() != MFn::kUvChooser)
 
196
        {
 
197
            MFnDependencyNode dgFn(node);
 
198
            MPlug plug = dgFn.findPlug(MString("uvCoord"), &status);
 
199
            if (status == MS::kSuccess && plug.isConnected())
 
200
            {
 
201
                // Get the connection - there can be at most one input to a plug
 
202
                //
 
203
                MPlugArray connections;
 
204
                plug.connectedTo(connections, true, false);
 
205
                if (connections.length() > 0)
 
206
                {
 
207
                    node = connections[ 0].node();
 
208
                }
 
209
                else
 
210
                {
 
211
                    break;
 
212
                }
 
213
            }
 
214
            else
 
215
            {
 
216
                break;
 
217
            }
 
218
        }
 
219
 
 
220
        // Did we find a uvChooser?
 
221
        //
 
222
        if (node.apiType() == MFn::kUvChooser)
 
223
        {
 
224
            // Find the uvSet connection to this shape
 
225
            //
 
226
            MFnDependencyNode dgFn(node);
 
227
            MPlug plug = dgFn.findPlug(MString("uvSets"), &status);
 
228
            if (status == MS::kSuccess && plug.isArray())
 
229
            {
 
230
                // Iterate over all the elements in this array looking for
 
231
                // one which connects to our shape
 
232
                //
 
233
                uint numUVSets = plug.evaluateNumElements();
 
234
                for (uint i = 0; i < numUVSets; i++)
 
235
                {
 
236
                    MPlug uvSetPlug = plug.elementByPhysicalIndex(i, &status);
 
237
                    if (status && uvSetPlug.isConnected())
 
238
                    {
 
239
                        // Get the connection - there can be at most one input to a plug
 
240
                        //
 
241
                        MPlugArray connections;
 
242
                        uvSetPlug.connectedTo(connections, true, false);
 
243
                        if (connections.length() > 0 && connections[0].node() == shape)
 
244
                        {
 
245
                            // connected through name element
 
246
                            MPlug uvSetElement= connections[0].parent();
 
247
                            MPlug uvSetArray = uvSetElement.array();
 
248
                            if (uvSetArray.isArray())
 
249
                            {
 
250
                                uint logicalIndex = uvSetElement.logicalIndex();
 
251
                                for (uint child = 0; child < uvSetArray.numElements(); ++child)
 
252
                                {
 
253
                                    MPlug childPlug = uvSetArray.elementByPhysicalIndex(child);
 
254
                                    if (childPlug.logicalIndex() == logicalIndex)
 
255
                                    {
 
256
                                        return child;
 
257
                                    }
 
258
                                }
 
259
                            } 
 
260
                        }
 
261
                    }
 
262
                }
 
263
            }
 
264
        }
 
265
 
 
266
 
 
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.
 
273
        //
 
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.
 
276
 
 
277
        MFnMesh mesh(shape);
 
278
        MStringArray setNames;
 
279
        mesh.getUVSetNames(setNames);
 
280
 
 
281
        MPlug uvSetPlug = mesh.findPlug("uvSet");
 
282
 
 
283
        for (uint i = 0; i < uvSetPlug.numElements(); ++i)
 
284
        {
 
285
            // get uvSet[<index>]
 
286
            MPlug uvSetElememtPlug = uvSetPlug.elementByPhysicalIndex(i);
 
287
 
 
288
            // get uvSet[<index>].uvSetName
 
289
            MPlug uvSetNamePlug = uvSetElememtPlug.child(0);
 
290
 
 
291
            // get value of plug (uvSet's name)
 
292
            MString uvSetName;
 
293
 
 
294
            uvSetNamePlug.getValue(uvSetName);
 
295
 
 
296
            if (uvSetName==setNames[0])
 
297
                return i;
 
298
 
 
299
        }
 
300
 
 
301
        // we don't want to get here, because it'll end up with wrong results.
 
302
        return 0;
 
303
    }
 
304
}
 
 
b'\\ No newline at end of file'