Skip to content

Commit

Permalink
EMSUSD-1113 suport legacy metarial scope mode
Browse files Browse the repository at this point in the history
- Add a legacyMaterialScope flag to the export command.
- The flag default to false (off).
- When turned on, we use the previous method of determining where a material is generated, which is under the common root prim of all meshes using the material.
- The merge-to-USD workflow use that flag to ensure it receives the material under the mesh.
- The export-to-USD UI forces that flag to be false (off), to ensure we always use the new material scope behavior.
- Added some unit tests for the legacy mode.
  • Loading branch information
pierrebai-adsk committed Jun 7, 2024
1 parent c6beb04 commit 9c306c7
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/mayaUsd/commands/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ their own purposes, similar to the Alembic export chaser example.
| `-exportColorSets` | `-cls` | bool | true | Enable or disable the export of color sets |
| `-exportMaterials` | `-mat` | bool | true | Enable or disable the export of materials |
| `-exportAssignedMaterials` | `-ama` | bool | true | Export materials only if they are assigned to a mesh |
| `-exportMaterialUnderPrim` | `-mup` | bool | false | Export materials under the prim that is using them |
| `-legacyMaterialScope` | `-lms` | bool | false | Export materials under a scope determined using the same algorithm as MayaUSD circa 0.28 |
| `-exportInstances` | `-ein` | bool | true | Enable or disable the export of instances |
| `-referenceObjectMode` | `-rom` | string | `none` | Determines how to export reference objects for meshes. The reference object's points are exported as a primvar on the mesh object; the primvar name is determined by querying `UsdUtilsGetPrefName()`, which defaults to `pref`. Valid values are: `none` - No reference objects are exported, `attributeOnly` - Only meshes set with a valid "referenceObject" attached will be exported, `defaultToMesh` - Meshes with no "referenceObject" attached will export their own points |
| `-exportRefsAsInstanceable` | `-eri` | bool | false | Will cause all references created by USD reference assembly nodes or explicitly tagged reference nodes to be set to be instanceable (`UsdPrim::SetInstanceable(true)`). |
Expand Down
4 changes: 2 additions & 2 deletions lib/mayaUsd/commands/baseExportCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ MSyntax MayaUSDExportCommand::createSyntax()
UsdMayaJobExportArgsTokens->exportAssignedMaterials.GetText(),
MSyntax::kBoolean);
syntax.addFlag(
kExportMaterialUnderPrimFlag,
UsdMayaJobExportArgsTokens->exportMaterialUnderPrim.GetText(),
kLegacyMaterialScopeFlag,
UsdMayaJobExportArgsTokens->legacyMaterialScope.GetText(),
MSyntax::kBoolean);
syntax.addFlag(
kStripNamespacesFlag,
Expand Down
2 changes: 1 addition & 1 deletion lib/mayaUsd/commands/baseExportCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MAYAUSD_CORE_PUBLIC MayaUSDExportCommand : public MPxCommand
static constexpr auto kExportColorSetsFlag = "cls";
static constexpr auto kExportMaterialsFlag = "mat";
static constexpr auto kExportAssignedMaterialsFlag = "ama";
static constexpr auto kExportMaterialUnderPrimFlag = "mup";
static constexpr auto kLegacyMaterialScopeFlag = "lms";
static constexpr auto kExportUVsFlag = "uvs";
static constexpr auto kExportRelativeTexturesFlag = "rtx";
static constexpr auto kEulerFilterFlag = "ef";
Expand Down
10 changes: 4 additions & 6 deletions lib/mayaUsd/fileio/jobs/jobArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,7 @@ UsdMayaJobExportArgs::UsdMayaJobExportArgs(
, exportMaterials(extractBoolean(userArgs, UsdMayaJobExportArgsTokens->exportMaterials))
, exportAssignedMaterials(
extractBoolean(userArgs, UsdMayaJobExportArgsTokens->exportAssignedMaterials))
, exportMaterialUnderPrim(
extractBoolean(userArgs, UsdMayaJobExportArgsTokens->exportMaterialUnderPrim))
, legacyMaterialScope(extractBoolean(userArgs, UsdMayaJobExportArgsTokens->legacyMaterialScope))
, exportDefaultCameras(extractBoolean(userArgs, UsdMayaJobExportArgsTokens->defaultCameras))
, exportDisplayColor(extractBoolean(userArgs, UsdMayaJobExportArgsTokens->exportDisplayColor))
, exportDistanceUnit(extractBoolean(userArgs, UsdMayaJobExportArgsTokens->exportDistanceUnit))
Expand Down Expand Up @@ -731,8 +730,7 @@ std::ostream& operator<<(std::ostream& out, const UsdMayaJobExportArgs& exportAr
<< "exportMaterials: " << TfStringify(exportArgs.exportMaterials) << std::endl
<< "exportAssignedMaterials: " << TfStringify(exportArgs.exportAssignedMaterials)
<< std::endl
<< "exportMaterialUnderPrim: " << TfStringify(exportArgs.exportMaterialUnderPrim)
<< std::endl
<< "legacyMaterialScope: " << TfStringify(exportArgs.legacyMaterialScope) << std::endl
<< "exportDefaultCameras: " << TfStringify(exportArgs.exportDefaultCameras) << std::endl
<< "exportDisplayColor: " << TfStringify(exportArgs.exportDisplayColor) << std::endl
<< "exportDistanceUnit: " << TfStringify(exportArgs.exportDistanceUnit) << std::endl
Expand Down Expand Up @@ -1023,7 +1021,7 @@ const VtDictionary& UsdMayaJobExportArgs::GetDefaultDictionary()
d[UsdMayaJobExportArgsTokens->exportColorSets] = true;
d[UsdMayaJobExportArgsTokens->exportMaterials] = true;
d[UsdMayaJobExportArgsTokens->exportAssignedMaterials] = true;
d[UsdMayaJobExportArgsTokens->exportMaterialUnderPrim] = false;
d[UsdMayaJobExportArgsTokens->legacyMaterialScope] = false;
d[UsdMayaJobExportArgsTokens->exportDisplayColor] = false;
d[UsdMayaJobExportArgsTokens->exportDistanceUnit] = true;
d[UsdMayaJobExportArgsTokens->exportInstances] = true;
Expand Down Expand Up @@ -1128,7 +1126,7 @@ const VtDictionary& UsdMayaJobExportArgs::GetGuideDictionary()
d[UsdMayaJobExportArgsTokens->exportColorSets] = _boolean;
d[UsdMayaJobExportArgsTokens->exportMaterials] = _boolean;
d[UsdMayaJobExportArgsTokens->exportAssignedMaterials] = _boolean;
d[UsdMayaJobExportArgsTokens->exportMaterialUnderPrim] = _boolean;
d[UsdMayaJobExportArgsTokens->legacyMaterialScope] = _boolean;
d[UsdMayaJobExportArgsTokens->exportDisplayColor] = _boolean;
d[UsdMayaJobExportArgsTokens->exportDistanceUnit] = _boolean;
d[UsdMayaJobExportArgsTokens->exportInstances] = _boolean;
Expand Down
4 changes: 2 additions & 2 deletions lib/mayaUsd/fileio/jobs/jobArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ TF_DECLARE_PUBLIC_TOKENS(
(exportColorSets) \
(exportMaterials) \
(exportAssignedMaterials) \
(exportMaterialUnderPrim) \
(legacyMaterialScope) \
(exportDisplayColor) \
(exportDistanceUnit) \
(exportInstances) \
Expand Down Expand Up @@ -210,7 +210,7 @@ struct UsdMayaJobExportArgs
const bool exportColorSets;
const bool exportMaterials;
const bool exportAssignedMaterials;
const bool exportMaterialUnderPrim;
const bool legacyMaterialScope;
const bool exportDefaultCameras;
const bool exportDisplayColor;
const bool exportDistanceUnit;
Expand Down
2 changes: 1 addition & 1 deletion lib/mayaUsd/fileio/primUpdaterManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ PushCustomizeSrc pushExport(
// This ensures the materials will be under the prim, so that
// when exported it is under the node being merged and will thus
// be merged too.
userArgs[UsdMayaJobExportArgsTokens->exportMaterialUnderPrim] = true;
userArgs[UsdMayaJobExportArgsTokens->legacyMaterialScope] = true;

UsdMayaJobExportArgs jobArgs = UsdMayaJobExportArgs::CreateFromDictionary(
userArgs, dagPaths, fullObjectList, timeSamples);
Expand Down
39 changes: 28 additions & 11 deletions lib/mayaUsd/fileio/shading/shadingModeExporterContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,16 +384,36 @@ static SdfPath _GetCommonAncestor(
return commonAncestor;
}

static UsdPrim _GetMaterialParent(
static UsdPrim _GetLegacyMaterialParent(
const UsdStageRefPtr& stage,
const std::string& defaultPrim,
const TfToken& materialsScopeName,
const UsdMayaShadingModeExportContext::AssignmentVector& assignments)
{
SdfPath shaderExportLocation = _GetCommonAncestor(stage, assignments);

if (shaderExportLocation.IsEmpty())
shaderExportLocation = SdfPath::AbsoluteRootPath();
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));
Expand Down Expand Up @@ -540,8 +560,6 @@ UsdPrim UsdMayaShadingModeExportContext::MakeStandardMaterialPrim(
const AssignmentsInfo& assignmentsInfo,
const std::string& name) const
{
static const AssignmentVector emptyAssignments;

const UsdMayaJobExportArgs& exportArgs = GetExportArgs();

if (!shouldExportMaterial(assignmentsInfo, GetSurfaceShader(), exportArgs))
Expand All @@ -552,12 +570,11 @@ UsdPrim UsdMayaShadingModeExportContext::MakeStandardMaterialPrim(
if (materialName.empty())
return UsdPrim();

const AssignmentVector& assignments
= exportArgs.exportMaterialUnderPrim ? assignmentsInfo.assignments : emptyAssignments;

UsdStageRefPtr stage = GetUsdStage();
UsdPrim materialParent = _GetMaterialParent(
stage, exportArgs.defaultPrim, exportArgs.materialsScopeName, assignments);
UsdPrim materialParent = exportArgs.legacyMaterialScope
? _GetLegacyMaterialParent(
stage, exportArgs.materialsScopeName, assignmentsInfo.assignments)
: _GetMaterialParent(stage, exportArgs.defaultPrim, exportArgs.materialsScopeName);
if (!materialParent)
return UsdPrim();

Expand Down
3 changes: 3 additions & 0 deletions plugin/adsk/scripts/mayaUsdTranslatorExport.mel
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,9 @@ global proc int mayaUsdTranslatorExport (string $parent,
// Append the final excludeExportTypes options to the options list
$currentOptions = $currentOptions + ";" + "excludeExportTypes=[" + $excludeExportTypes + "]";

// Make sure we use the new material scope logic, not the legacy one.
$currentOptions = $currentOptions + ";legacyMaterialScope=0";

eval($resultCallback+" \""+$currentOptions+"\"");
$bResult = 1;

Expand Down
1 change: 1 addition & 0 deletions test/lib/usd/translators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ endforeach()
if (MAYA_APP_VERSION VERSION_GREATER 2022)
set(MTLX_TEST_SCRIPT_FILES
testUsdExportMaterialScope.py
testUsdExportMaterialScopeLegacy.py
testUsdExportMaterialsOnly.py
testUsdExportAssignedMaterials.py
testUsdExportMaterialX.py
Expand Down
Loading

0 comments on commit 9c306c7

Please sign in to comment.