-
Notifications
You must be signed in to change notification settings - Fork 201
/
shadingModeExporterContext.cpp
1068 lines (928 loc) · 40.2 KB
/
shadingModeExporterContext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "shadingModeExporterContext.h"
#include "pxr/usd/usd/specializes.h"
#include <mayaUsd/fileio/jobs/jobArgs.h>
#include <mayaUsd/fileio/translators/translatorUtil.h>
#include <mayaUsd/fileio/utils/meshReadUtils.h>
#include <mayaUsd/fileio/utils/roundTripUtil.h>
#include <mayaUsd/fileio/utils/writeUtil.h>
#include <mayaUsd/fileio/writeJobContext.h>
#include <mayaUsd/utils/json.h>
#include <mayaUsd/utils/util.h>
#include <usdUfe/utils/Utils.h>
#include <pxr/base/tf/diagnostic.h>
#include <pxr/base/tf/envSetting.h>
#include <pxr/base/tf/iterator.h>
#include <pxr/base/tf/staticTokens.h>
#include <pxr/base/tf/token.h>
#include <pxr/base/vt/types.h>
#include <pxr/usd/sdf/path.h>
#include <pxr/usd/sdf/types.h>
#include <pxr/usd/usd/prim.h>
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usdGeom/scope.h>
#include <pxr/usd/usdGeom/subset.h>
#include <pxr/usd/usdShade/material.h>
#include <pxr/usd/usdShade/materialBindingAPI.h>
#include <pxr/usd/usdShade/shader.h>
#include <pxr/usd/usdUtils/pipeline.h>
#include <maya/MCommandResult.h>
#include <maya/MDGContext.h>
#include <maya/MDagPath.h>
#include <maya/MDagPathArray.h>
#include <maya/MFnDagNode.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnSet.h>
#include <maya/MGlobal.h>
#include <maya/MItMeshPolygon.h>
#include <maya/MNamespace.h>
#include <maya/MObject.h>
#include <maya/MObjectArray.h>
#include <maya/MPlug.h>
#include <maya/MStatus.h>
#include <maya/MString.h>
#include <maya/MUuid.h>
#include <regex>
#include <string>
#include <utility>
// Certain applications do not currently support reading
// TexCoord names via connections, and instead expect a default value.
TF_DEFINE_ENV_SETTING(
MAYAUSD_PROVIDE_DEFAULT_TEXCOORD_PRIMVAR_NAME,
false,
"Add a default value for texcoord names in addition to connections");
PXR_NAMESPACE_OPEN_SCOPE
// clang-format off
TF_DEFINE_PRIVATE_TOKENS(
_tokens,
(surfaceShader)
(volumeShader)
(displacementShader)
(varname)
(varnameStr)
(map1)
);
// clang-format on
UsdMayaShadingModeExportContext::UsdMayaShadingModeExportContext(
const MObject& shadingEngine,
UsdMayaWriteJobContext& writeJobContext,
const UsdMayaUtil::MDagPathMap<SdfPath>& dagPathToUsdMap)
: _shadingEngine(shadingEngine)
, _stage(writeJobContext.GetUsdStage())
, _dagPathToUsdMap(dagPathToUsdMap)
, _writeJobContext(writeJobContext)
, _surfaceShaderPlugName(_tokens->surfaceShader)
, _volumeShaderPlugName(_tokens->volumeShader)
, _displacementShaderPlugName(_tokens->displacementShader)
{
if (GetExportArgs().dagPaths.empty()) {
// if none specified, push back '/' which encompasses all
_bindableRoots.insert(SdfPath::AbsoluteRootPath());
} else {
const bool hasExportRootsMapping = !GetExportArgs().rootMapFunction.IsNull();
auto findBindableRootFn
= [this, hasExportRootsMapping](const MDagPath& inDagPath, SdfPath& outPath) {
auto iter = _dagPathToUsdMap.find(inDagPath);
if (iter != _dagPathToUsdMap.end()) {
outPath = iter->second;
return true;
}
// We may be only exporting some roots from under the selected hierarchy.
// Search for root but only if export root mapping is set to save time.
if (hasExportRootsMapping) {
for (auto pair : _dagPathToUsdMap) {
if (MFnDagNode(pair.first).hasParent(inDagPath.node())) {
outPath = pair.second;
return true;
}
}
}
return false;
};
TF_FOR_ALL(bindableRootIter, GetExportArgs().dagPaths)
{
const MDagPath& bindableRootDagPath = *bindableRootIter;
SdfPath usdPath;
if (!findBindableRootFn(bindableRootDagPath, usdPath)) {
// Geometry w/ this material bound doesn't seem to exist in USD.
continue;
}
// If usdModelRootOverridePath is not empty, replace the root
// namespace with it.
if (!GetExportArgs().usdModelRootOverridePath.IsEmpty()) {
usdPath = usdPath.ReplacePrefix(
usdPath.GetPrefixes()[0], GetExportArgs().usdModelRootOverridePath);
}
_bindableRoots.insert(usdPath);
}
}
}
void UsdMayaShadingModeExportContext::SetSurfaceShaderPlugName(const TfToken& surfaceShaderPlugName)
{
_surfaceShaderPlugName = surfaceShaderPlugName;
}
void UsdMayaShadingModeExportContext::SetVolumeShaderPlugName(const TfToken& volumeShaderPlugName)
{
_volumeShaderPlugName = volumeShaderPlugName;
}
void UsdMayaShadingModeExportContext::SetDisplacementShaderPlugName(
const TfToken& displacementShaderPlugName)
{
_displacementShaderPlugName = displacementShaderPlugName;
}
static MPlug
_GetShaderPlugFromShadingEngine(const MObject& shadingEngine, const TfToken& shaderPlugName)
{
MStatus status;
const MFnDependencyNode seDepNodeFn(shadingEngine, &status);
if (status != MS::kSuccess) {
return MPlug();
}
const MPlug shaderPlug = seDepNodeFn.findPlug(
shaderPlugName.GetText(),
/* wantNetworkedPlug = */ true,
&status);
if (status != MS::kSuccess) {
return MPlug();
}
return shaderPlug;
}
static MObject
_GetShaderFromShadingEngine(const MObject& shadingEngine, const TfToken& shaderPlugName)
{
MStatus status;
const MPlug shaderPlug = _GetShaderPlugFromShadingEngine(shadingEngine, shaderPlugName);
if (shaderPlug.isNull()) {
return MObject();
}
MObject shaderObj = shaderPlug.asMObject(&status);
if (status != MS::kSuccess || shaderObj.isNull()) {
return MObject();
}
return UsdMayaUtil::GetConnected(shaderPlug).node();
}
MPlug UsdMayaShadingModeExportContext::GetSurfaceShaderPlug() const
{
return _GetShaderPlugFromShadingEngine(_shadingEngine, _surfaceShaderPlugName);
}
MObject UsdMayaShadingModeExportContext::GetSurfaceShader() const
{
return _GetShaderFromShadingEngine(_shadingEngine, _surfaceShaderPlugName);
}
MPlug UsdMayaShadingModeExportContext::GetVolumeShaderPlug() const
{
return _GetShaderPlugFromShadingEngine(_shadingEngine, _volumeShaderPlugName);
}
MObject UsdMayaShadingModeExportContext::GetVolumeShader() const
{
return _GetShaderFromShadingEngine(_shadingEngine, _volumeShaderPlugName);
}
MPlug UsdMayaShadingModeExportContext::GetDisplacementShaderPlug() const
{
return _GetShaderPlugFromShadingEngine(_shadingEngine, _displacementShaderPlugName);
}
MObject UsdMayaShadingModeExportContext::GetDisplacementShader() const
{
return _GetShaderFromShadingEngine(_shadingEngine, _displacementShaderPlugName);
}
UsdMayaShadingModeExportContext::AssignmentsInfo
UsdMayaShadingModeExportContext::GetAssignments() const
{
AssignmentsInfo ret;
MStatus status;
MFnDependencyNode seDepNode(_shadingEngine, &status);
if (!status) {
return ret;
}
#if MAYA_HAS_GET_MEMBER_PATHS
MFnSet fnSet(_shadingEngine, &status);
if (!status) {
return ret;
}
// Get all the dagPaths using this shadingEngine...
MDagPathArray dagPaths;
fnSet.getMemberPaths(dagPaths, true); // get all the dagPath related to shading
SdfPathSet seenBoundPrimPaths;
for (auto& dagPath : dagPaths) {
unsigned int instanceNumber = dagPath.instanceNumber();
#else
// Maya 2022 and older use this version
MPlug dsmPlug = seDepNode.findPlug("dagSetMembers", true, &status);
if (!status) {
return ret;
}
SdfPathSet seenBoundPrimPaths;
for (unsigned int i = 0; i < dsmPlug.numConnectedElements(); i++) {
MPlug dsmElemPlug(dsmPlug.connectionByPhysicalIndex(i));
MPlug connectedPlug = UsdMayaUtil::GetConnected(dsmElemPlug);
// Maya connects shader bindings for instances based on element indices
// of the instObjGroups[x] or instObjGroups[x].objectGroups[y] plugs.
// The instance number is the index of instObjGroups[x]; the face set
// (if any) is the index of objectGroups[y].
if (connectedPlug.isElement() && connectedPlug.array().isChild()) {
// connectedPlug is instObjGroups[x].objectGroups[y] (or its
// equivalent), so go up two levels to get to instObjGroups[x].
MPlug objectGroups = connectedPlug.array();
MPlug instObjGroupsElem = objectGroups.parent();
connectedPlug = instObjGroupsElem;
}
// connectedPlug should be instObjGroups[x] here. Get the index.
unsigned int instanceNumber = connectedPlug.logicalIndex();
// Get the correct DAG path for this instance number.
MDagPathArray allDagPaths;
MDagPath::getAllPathsTo(connectedPlug.node(), allDagPaths);
if (instanceNumber >= allDagPaths.length()) {
TF_RUNTIME_ERROR(
"Instance number is %d (from plug '%s') but node only has "
"%d paths",
instanceNumber,
connectedPlug.name().asChar(),
allDagPaths.length());
continue;
}
MDagPath dagPath = allDagPaths[instanceNumber];
TF_VERIFY(dagPath.instanceNumber() == instanceNumber);
#endif
MFnDagNode dagNode(dagPath, &status);
if (!status) {
continue;
}
ret.hasAnyAssignment = true;
// Note: the connections will be to the mesh, but the selection often
// contains the transform above the mesh or a higher-up ancestor,
// so also check the ancestors of the mesh.
for (MDagPath walkUpPath(dagPath); walkUpPath.length() > 0; walkUpPath.pop())
if (GetExportArgs().fullObjectList.hasItem(walkUpPath))
ret.hasAnyAssignmentInSelection = true;
auto iter = _dagPathToUsdMap.find(dagPath);
if (iter == _dagPathToUsdMap.end()) {
// Geometry w/ this material bound doesn't seem to exist in USD.
continue;
}
SdfPath usdPath = iter->second;
// If usdModelRootOverridePath is not empty, replace the
// root namespace with it.
if (!GetExportArgs().usdModelRootOverridePath.IsEmpty()) {
usdPath = usdPath.ReplacePrefix(
usdPath.GetPrefixes()[0], GetExportArgs().usdModelRootOverridePath);
}
// If this path has already been processed, skip it.
if (!seenBoundPrimPaths.insert(usdPath).second) {
continue;
}
// If the bound prim's path is not below a bindable root, skip it.
if (SdfPathFindLongestPrefix(_bindableRoots, usdPath) == _bindableRoots.end()) {
continue;
}
MObjectArray sgObjs, compObjs;
status = dagNode.getConnectedSetsAndMembers(instanceNumber, sgObjs, compObjs, true);
if (status != MS::kSuccess) {
continue;
}
for (unsigned int j = 0u; j < sgObjs.length(); ++j) {
// If the shading group isn't the one we're interested in, skip it.
if (sgObjs[j] != _shadingEngine) {
continue;
}
VtIntArray faceIndices;
if (!compObjs[j].isNull()) {
MItMeshPolygon faceIt(dagPath, compObjs[j]);
faceIndices.reserve(faceIt.count());
for (faceIt.reset(); !faceIt.isDone(); faceIt.next()) {
faceIndices.push_back(faceIt.index());
}
}
ret.assignments.push_back(Assignment {
usdPath, faceIndices, TfToken(dagNode.name().asChar()), dagNode.object() });
}
}
return ret;
}
static SdfPath _GetCommonAncestor(
const UsdStageRefPtr& stage,
const UsdMayaShadingModeExportContext::AssignmentVector& assignments)
{
SdfPath commonAncestor;
TF_FOR_ALL(iter, assignments)
{
const SdfPath& assn = iter->boundPrimPath;
if (stage->GetPrimAtPath(assn)) {
if (commonAncestor.IsEmpty()) {
commonAncestor = assn;
} else {
commonAncestor = commonAncestor.GetCommonPrefix(assn);
}
}
}
return commonAncestor;
}
static UsdPrim _GetLegacyMaterialParent(
const UsdStageRefPtr& stage,
const TfToken& materialsScopeName,
const UsdMayaShadingModeExportContext::AssignmentVector& assignments)
{
SdfPath shaderExportLocation = _GetCommonAncestor(stage, assignments);
if (shaderExportLocation.IsEmpty()) {
return UsdPrim();
}
if (shaderExportLocation == SdfPath::AbsoluteRootPath()) {
return stage->GetPseudoRoot();
}
while (!shaderExportLocation.IsRootPrimPath()) {
shaderExportLocation = shaderExportLocation.GetParentPath();
}
shaderExportLocation = shaderExportLocation.AppendChild(materialsScopeName);
return UsdGeomScope::Define(stage, shaderExportLocation).GetPrim();
}
static UsdPrim _GetMaterialParent(
const UsdStageRefPtr& stage,
const std::string& defaultPrim,
const TfToken& materialsScopeName)
{
SdfPath shaderExportLocation = SdfPath::AbsoluteRootPath();
if (!defaultPrim.empty() && defaultPrim != materialsScopeName && defaultPrim != "None")
shaderExportLocation = shaderExportLocation.AppendChild(TfToken(defaultPrim));
if (!materialsScopeName.IsEmpty())
shaderExportLocation = shaderExportLocation.AppendChild(materialsScopeName);
return UsdGeomScope::Define(stage, shaderExportLocation).GetPrim();
}
/// Determines if the \p path would be an instance proxy path on \p stage if
/// it existed, i.e., if any of its ancestor paths are instances.
/// (Note that if \p path itself is an instance, then it is _not_ an instance
/// proxy path.)
static bool _IsInstanceProxyPath(const UsdStageRefPtr& stage, const SdfPath& path)
{
for (const SdfPath& prefix : path.GetParentPath().GetPrefixes()) {
if (const UsdPrim prim = stage->GetPrimAtPath(prefix)) {
if (prim.IsInstance()) {
return true;
}
}
}
return false;
}
/// Ensures that a prim exists at \p path on \p stage and that the prim is
/// neither an instance nor an instance proxy.
static UsdPrim
_UninstancePrim(const UsdStageRefPtr& stage, const SdfPath& path, const std::string& reason)
{
bool didUninstance = false;
for (const SdfPath& prefix : path.GetPrefixes()) {
if (const UsdPrim prim = stage->GetPrimAtPath(prefix)) {
if (prim.IsInstance()) {
prim.SetInstanceable(false);
didUninstance = true;
}
} else {
break;
}
}
if (didUninstance) {
TF_WARN("Uninstanced <%s> (and ancestors) because: %s", path.GetText(), reason.c_str());
}
return stage->OverridePrim(path);
}
namespace {
// Detect a name that was generated directly from a dg node typename:
const std::regex kTemplatedRegex("^([a-zA-Z]+)([0-9]*)(SG)?$");
bool isSurfaceNodeType(const std::string& nodeType)
{
static std::vector<std::string> sKnownSurfaces;
if (sKnownSurfaces.empty()) {
MString listSurfCmd("stringArrayToString(`listNodeTypes \"shader/surface\"`, \" \");");
std::string cmdResult = MGlobal::executeCommandStringResult(listSurfCmd).asChar();
sKnownSurfaces = TfStringTokenize(cmdResult);
// O(logN) will be close to O(N) for searches since N will usually be small, but with enough
// plugin surface nodes added it could start to matter, so let's sort the vector.
std::sort(sKnownSurfaces.begin(), sKnownSurfaces.end());
}
return std::binary_search(sKnownSurfaces.cbegin(), sKnownSurfaces.cend(), nodeType);
}
std::string getMaterialName(
const std::string& materialName,
const MObject& shadingEngine,
const MObject& surfaceShader)
{
if (!materialName.empty())
return materialName;
MFnDependencyNode fnDepNode;
if (fnDepNode.setObject(shadingEngine) != MS::kSuccess)
return materialName;
std::string sgName = fnDepNode.name().asChar();
std::smatch sgMatch;
// Is the SG name following the standard Maya naming protocol for a known surface nodeType?
if (!std::regex_match(sgName, sgMatch, kTemplatedRegex))
return sgName;
if (!isSurfaceNodeType(sgMatch[1].str()))
return sgName;
// Check if the surface shader has a more descriptive name
if (fnDepNode.setObject(surfaceShader) == MS::kSuccess) {
std::string surfName = fnDepNode.name().asChar();
std::smatch surfMatch;
if (std::regex_match(surfName, surfMatch, kTemplatedRegex)) {
// Surface node name is also templated. Check the nodeType part.
if (!isSurfaceNodeType(surfMatch[1].str())) {
// The surface is not named after a standard nodeType, so its name is more
// interesting:
sgName = surfName + "SG";
}
} else {
// Surface node is definitely more interesting since it does not follow a
// templated name:
sgName = surfName + "SG";
}
}
return sgName;
}
bool shouldExportMaterial(
const UsdMayaShadingModeExportContext::AssignmentsInfo& assignmentsInfo,
const MObject& surfaceShader,
const UsdMayaJobExportArgs& exportArgs)
{
// The export-assigned-materials flag means to remove materials
// that are not assigned to any meshes. (We take into consideration
// even meshes that are not exported.)
if (exportArgs.exportAssignedMaterials) {
if (!assignmentsInfo.hasAnyAssignment) {
return false;
}
}
// In export-selected mode, ony export selected materials.
// Materials of exported meshes are also added to the selection.
if (exportArgs.exportSelected) {
if (assignmentsInfo.hasAnyAssignmentInSelection) {
return true;
}
return exportArgs.fullObjectList.hasItem(surfaceShader);
} else {
// In export-all mode, export all materials by default.
return true;
}
}
} // namespace
UsdPrim UsdMayaShadingModeExportContext::MakeStandardMaterialPrim(
const AssignmentsInfo& assignmentsInfo,
const std::string& name) const
{
const UsdMayaJobExportArgs& exportArgs = GetExportArgs();
if (!shouldExportMaterial(assignmentsInfo, GetSurfaceShader(), exportArgs))
return UsdPrim();
const std::string materialName
= UsdUfe::sanitizeName(getMaterialName(name, _shadingEngine, GetSurfaceShader()));
if (materialName.empty())
return UsdPrim();
UsdStageRefPtr stage = GetUsdStage();
UsdPrim materialParent = exportArgs.legacyMaterialScope
? _GetLegacyMaterialParent(
stage, exportArgs.materialsScopeName, assignmentsInfo.assignments)
: _GetMaterialParent(stage, exportArgs.defaultPrim, exportArgs.materialsScopeName);
if (!materialParent)
return UsdPrim();
SdfPath materialPath = materialParent.GetPath().AppendChild(TfToken(materialName));
UsdShadeMaterial material = UsdShadeMaterial::Define(GetUsdStage(), materialPath);
return material.GetPrim();
}
namespace {
/// We can have multiple mesh with differing UV channel names and we need to make sure the exported
/// material has varname or varnameStr inputs that match the texcoords exported by the shape
class _UVMappingManager
{
public:
_UVMappingManager(
const UsdShadeMaterial& material,
const UsdMayaShadingModeExportContext::AssignmentVector& assignmentsToBind,
const UsdMayaJobExportArgs& exportArgs)
: _material(material)
, _exportArgs(exportArgs)
{
// Find out the nodes requiring mapping:
//
// The following naming convention is used on UsdShadeMaterial inputs to declare Maya
// shader nodes contained in the material that have UV inputs that requires mapping:
//
// token inputs:node_with_uv_input:varname = "st"
// string inputs:node_with_uv_input:varnameStr = "st"
//
// The "node_with_uv_input" is a dependency node which is a valid target for the Maya
// "uvLink" command, which describes UV linkage for all shapes in the scene that reference
// the material containing the exported shader node.
//
// See lib\usd\translators\shading\usdFileTextureWriter.cpp for an example of an exporter
// declaring UV inputs that require linking.
for (const UsdShadeInput& input : material.GetInputs()) {
const UsdAttribute& usdAttr = input.GetAttr();
std::vector<std::string> splitName = usdAttr.SplitName();
if (splitName.back() != _tokens->varname.GetString()
&& splitName.back() != _tokens->varnameStr.GetString()) {
continue;
}
switch (splitName.size()) {
case 3: _nodesWithUVInput.push_back(TfToken(splitName[1])); break;
default
: // NOTE: (yliangsiew) Means that we have a Maya node with a namespace/multiple
// namespaces.
{
std::string mayaNodeName = splitName[1];
for (size_t i = 2; i < splitName.size() - 1; ++i) {
mayaNodeName += ":" + splitName[i];
}
_nodesWithUVInput.push_back(TfToken(mayaNodeName));
break;
}
}
}
if (_nodesWithUVInput.empty()) {
return;
}
std::set<TfToken> exportedShapes;
for (const auto& iter : assignmentsToBind) {
exportedShapes.insert(iter.shapeName);
}
// Ask Maya about UV linkage:
for (const TfToken& nodeName : _nodesWithUVInput) {
MString uvLinkCmd;
uvLinkCmd.format(
"stringArrayToString(`uvLink -q -t \"^1s\"`, \" \");", nodeName.GetText());
std::string uvLinkResult = MGlobal::executeCommandStringResult(uvLinkCmd).asChar();
for (std::string uvSetRef : TfStringTokenize(uvLinkResult)) {
// NOTE: If the mesh shape has the same name as the transform, then we will get a
// complete path like
// |mesh|mesh.uvSet[0].uvSetName
// the best way to prevent confusion is to move to the object model
// immediately and process from there.
MSelectionList selList;
selList.add(uvSetRef.c_str());
MPlug uvNamePlug;
selList.getPlug(0, uvNamePlug);
MFnMesh meshFn(uvNamePlug.node());
TfToken shapeName(meshFn.name().asChar());
if (!exportedShapes.count(shapeName)) {
continue;
}
MString uvSetName = uvNamePlug.asString();
// UV set renaming still exists. See if the UV was renamed:
MStringArray uvSets;
meshFn.getUVSetNames(uvSets);
for (unsigned int i = 0; i < uvSets.length(); i++) {
if (uvSets[i] == uvSetName) {
uvSetName = UsdMayaWriteUtil::UVSetExportedName(
uvSets, _exportArgs.preserveUVSetNames, _exportArgs.remapUVSetsTo, i);
break;
}
}
_shapeNameToUVNames[shapeName].push_back(TfToken(uvSetName.asChar()));
}
}
// Group the shapes by UV mappings:
using MappingGroups = std::map<TfTokenVector, TfTokenVector>;
MappingGroups mappingGroups;
for (const auto& iter : _shapeNameToUVNames) {
const TfToken& shapeName = iter.first;
const TfTokenVector& streams = iter.second;
mappingGroups[streams].push_back(shapeName);
}
// Find out the most common one, which will take over the unspecialized material:
size_t largestSize = 0;
TfTokenVector largestSet;
for (const auto& iter : mappingGroups) {
if (iter.second.size() > largestSize) {
largestSize = iter.second.size();
largestSet = iter.first;
}
}
// Update the original material with the most common mapping:
if (largestSize) {
TfTokenVector::const_iterator itNode = _nodesWithUVInput.cbegin();
TfTokenVector::const_iterator itName = largestSet.cbegin();
for (; itNode != _nodesWithUVInput.cend(); ++itNode, ++itName) {
TF_VERIFY(itName != largestSet.cend());
std::string inputName(
TfStringPrintf("%s:%s", itNode->GetText(), _tokens->varname.GetText()));
UsdShadeInput materialInput = material.GetInput(TfToken(inputName.c_str()));
if (materialInput) {
// varname becomes a std::string in USD 20.11
if (materialInput.GetTypeName() == SdfValueTypeNames->Token) {
materialInput.Set(*itName);
} else {
materialInput.Set(itName->GetString());
}
}
inputName
= TfStringPrintf("%s:%s", itNode->GetText(), _tokens->varnameStr.GetText());
materialInput = material.GetInput(TfToken(inputName.c_str()));
if (materialInput) {
materialInput.Set(itName->GetString());
}
}
_uvNamesToMaterial[largestSet] = material;
}
if (TfGetEnvSetting(MAYAUSD_PROVIDE_DEFAULT_TEXCOORD_PRIMVAR_NAME)) {
// We'll traverse the material prims children and look for items
// with connections, and use that to set the value
TfToken readerType { "UsdPrimvarReader_float2" };
TfToken shaderID;
UsdShadeConnectableAPI source;
TfToken sourceInputName;
UsdShadeAttributeType sourceType;
VtValue varnameValue;
auto materialPrim = material.GetPrim();
for (const auto child : materialPrim.GetDescendants()) {
if (!child.IsA<UsdShadeShader>()) {
continue;
}
auto shader = UsdShadeShader(child);
if (shader.GetShaderId(&shaderID) && shaderID != readerType) {
continue;
}
auto varnameInput = shader.GetInput(TfToken("varname"));
if (!varnameInput) {
continue;
}
if (!UsdShadeConnectableAPI::GetConnectedSource(
varnameInput, &source, &sourceInputName, &sourceType)) {
continue;
}
auto targetVarname = material.GetInput(sourceInputName);
if (!targetVarname) {
continue;
}
targetVarname.Get(&varnameValue);
varnameInput.Set(varnameValue);
}
}
}
const UsdShadeMaterial& getMaterial(const TfToken& shapeName)
{
// Look for the UV set names linked to this shape.
ShapeToStreams::const_iterator shapeStreamIter = _shapeNameToUVNames.find(shapeName);
if (shapeStreamIter == _shapeNameToUVNames.cend()) {
// No UV sets linked to this shape, so just use the original
// material.
return _material;
}
// Look for an existing material for the requested shape based on its
// UV sets.
const TfTokenVector& uvNames = shapeStreamIter->second;
MaterialMappings::const_iterator iter = _uvNamesToMaterial.find(uvNames);
if (iter != _uvNamesToMaterial.end()) {
return iter->second;
}
// Create a specialized material:
std::string newName = _material.GetPrim().GetName();
for (const TfToken& t : uvNames) {
newName += "_";
newName += t.GetString();
}
SdfPath newPath
= _material.GetPrim().GetPath().GetParentPath().AppendChild(TfToken(newName.c_str()));
UsdShadeMaterial newMaterial
= UsdShadeMaterial::Define(_material.GetPrim().GetStage(), newPath);
newMaterial.SetBaseMaterial(_material);
TfTokenVector::const_iterator itNode = _nodesWithUVInput.cbegin();
TfTokenVector::const_iterator itName = uvNames.cbegin();
for (; itNode != _nodesWithUVInput.cend(); ++itNode, ++itName) {
std::string inputName(
TfStringPrintf("%s:%s", itNode->GetText(), _tokens->varname.GetText()));
UsdShadeInput materialInput = newMaterial.GetInput(TfToken(inputName.c_str()));
if (materialInput) {
// varname becomes a std::string in USD 20.11
if (materialInput.GetTypeName() == SdfValueTypeNames->Token) {
materialInput.Set(*itName);
} else {
materialInput.Set(itName->GetString());
}
}
inputName = TfStringPrintf("%s:%s", itNode->GetText(), _tokens->varnameStr.GetText());
materialInput = newMaterial.GetInput(TfToken(inputName.c_str()));
if (materialInput) {
materialInput.Set(itName->GetString());
}
}
auto insertResult
= _uvNamesToMaterial.insert(MaterialMappings::value_type { uvNames, newMaterial });
return insertResult.first->second;
}
private:
/// The original material:
const UsdShadeMaterial& _material;
/// The export args (for UV set remapping)
const UsdMayaJobExportArgs& _exportArgs;
/// Helper structures for UV set mappings:
TfTokenVector _nodesWithUVInput;
using ShapeToStreams = std::map<TfToken, TfTokenVector>;
ShapeToStreams _shapeNameToUVNames;
using MaterialMappings = std::map<TfTokenVector, UsdShadeMaterial>;
MaterialMappings _uvNamesToMaterial;
};
/// We might have one or more component tags that have roundtrip data that covers this
// set of faces. Assign them to those tags and return the remaining unhandled faces.
VtIntArray _AssignComponentTags(
const UsdMayaShadingModeExportContext* ctx,
const UsdShadeMaterial& materialToBind,
const MObjectHandle& geomHandle,
const std::vector<UsdGeomSubset>& currentGeomSubsets,
const VtIntArray& faceIndices,
SdfPathSet* const boundPrimPaths)
{
if (currentGeomSubsets.empty() || faceIndices.empty() || !geomHandle.isValid()) {
return faceIndices;
}
JsValue info;
if (UsdMayaMeshReadUtils::getGeomSubsetInfo(geomHandle.object(), info) && info) {
auto subsetInfoDict = info.GetJsObject();
std::set<VtIntArray::ElementType> faceSet, handledSet;
faceSet.insert(faceIndices.cbegin(), faceIndices.cend());
MFnDependencyNode shadingGroupDepFn(ctx->GetShadingEngine());
std::string shadingGroupUUID = shadingGroupDepFn.uuid().asString().asChar();
for (auto&& geomSubset : currentGeomSubsets) {
std::string ssName = geomSubset.GetPrim().GetName();
const auto infoIt = subsetInfoDict.find(ssName);
if (infoIt != subsetInfoDict.cend()) {
// Check candidate:
if (!infoIt->second.IsObject()) {
TF_RUNTIME_ERROR(
"Invalid GeomSubset roundtrip info on node '%s': "
"info for subset '%s' is not a dictionary.",
UsdMayaUtil::GetMayaNodeName(geomHandle.object()).c_str(),
ssName.c_str());
continue;
}
const auto& geomSubsetInfo = infoIt->second.GetJsObject();
const auto uuidIt = geomSubsetInfo.find(UsdMayaGeomSubsetTokens->MaterialUuidKey);
if (uuidIt == geomSubsetInfo.cend()) {
continue;
}
if (!uuidIt->second.IsString()) {
TF_RUNTIME_ERROR(
"Invalid GeomSubset roundtrip info on node '%s': "
"material info for subset '%s' is not a string.",
UsdMayaUtil::GetMayaNodeName(geomHandle.object()).c_str(),
ssName.c_str());
continue;
}
if (uuidIt->second.GetString() != shadingGroupUUID) {
continue;
}
VtIntArray subsetIndices;
geomSubset.GetIndicesAttr().Get(&subsetIndices);
// We bind if the componentTag is a subset of the material indices
bool bindMaterial = true;
for (auto it = subsetIndices.cbegin(); it != subsetIndices.cend(); ++it) {
if (faceSet.count(*it) == 0) {
// The componentTag is out of sync with the material assignment.
// We currently do not try to reconcile as we have no idea how
// the situation happened.
bindMaterial = false;
break;
}
}
if (!bindMaterial) {
continue;
}
if (!ctx->GetExportArgs().exportCollectionBasedBindings) {
UsdShadeMaterialBindingAPI subsetBindingAPI
= UsdMayaTranslatorUtil::GetAPISchemaForAuthoring<
UsdShadeMaterialBindingAPI>(geomSubset.GetPrim());
subsetBindingAPI.Bind(materialToBind);
}
if (boundPrimPaths) {
boundPrimPaths->insert(geomSubset.GetPath());
}
// Mark those faces as handled
handledSet.insert(subsetIndices.cbegin(), subsetIndices.cend());
}
}
if (!handledSet.empty()) {
// Prune handled faces:
for (auto&& handledFace : handledSet) {
faceSet.erase(handledFace);
}
VtIntArray unhandledFaces;
unhandledFaces.assign(faceSet.begin(), faceSet.end());
return unhandledFaces;
}
}
return faceIndices;
}
} // namespace
void UsdMayaShadingModeExportContext::BindStandardMaterialPrim(
const UsdPrim& materialPrim,
const AssignmentVector& assignmentsToBind,
SdfPathSet* const boundPrimPaths) const
{
UsdShadeMaterial material(materialPrim);
if (!material) {
TF_RUNTIME_ERROR("Invalid material prim.");
return;
}
_UVMappingManager uvMappingManager(material, assignmentsToBind, GetExportArgs());
UsdStageRefPtr stage = GetUsdStage();
TfToken materialNameToken(materialPrim.GetName());
for (const auto& iter : assignmentsToBind) {
const SdfPath& boundPrimPath = iter.boundPrimPath;
const VtIntArray& faceIndices = iter.faceIndices;
const TfToken& shapeName = iter.shapeName;
const UsdShadeMaterial& materialToBind = uvMappingManager.getMaterial(shapeName);
// In the standard material binding case, skip if we're authoring
// direct (non-collection-based) bindings and we're an instance
// proxy.
// In the case of per-face bindings, un-instance the prim in order
// to author the append face sets or create a geom subset, since
// collection-based bindings won't help us here.
if (faceIndices.empty()) {
if (!GetExportArgs().exportCollectionBasedBindings) {
if (_IsInstanceProxyPath(stage, boundPrimPath)) {
// XXX: If we wanted to, we could try to author the
// binding on the parent prim instead if it's an
// instance prim with only one child (i.e. if it's the
// transform prim corresponding to our shape prim).
TF_WARN(
"Can't author direct material binding on "
"instance proxy <%s>; try enabling "
"collection-based material binding",
boundPrimPath.GetText());
} else {
UsdPrim boundPrim = stage->OverridePrim(boundPrimPath);
UsdShadeMaterialBindingAPI bindingAPI
= UsdMayaTranslatorUtil::GetAPISchemaForAuthoring<
UsdShadeMaterialBindingAPI>(boundPrim);
bindingAPI.Bind(materialToBind);
}
}
if (boundPrimPaths) {
boundPrimPaths->insert(boundPrimPath);
}
} else {
UsdPrim boundPrim