Skip to content

Commit

Permalink
Add a "mtl" scope when authoring usd materials #1486 (#1517)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienblor authored May 5, 2023
1 parent 0f43465 commit 777f0f1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Feature
- [usd#1492](https://github.com/Autodesk/arnold-usd/issues/1492) - Add Arnold render status and estimated render time to viewport annotation
- [usd#1499](https://github.com/Autodesk/arnold-usd/issues/1499) - Add support for camera filtermap and uv_remap
- [usd#1486](https://github.com/Autodesk/arnold-usd/issues/1486) - Add a "Mtl" scope for materials when authoring usd files

### Build
- [usd#1480](https://github.com/Autodesk/arnold-usd/issues/1480) - Allow to specify a testsuite output folder
Expand Down
1 change: 1 addition & 0 deletions common/constant_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ ASTR(missing);
ASTR(missing_texture_color);
ASTR(motion_end);
ASTR(motion_start);
ASTR(mtl_scope);
ASTR(mtlx);
ASTR(multiply);
ASTR(name);
Expand Down
6 changes: 6 additions & 0 deletions procedural/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ scene_write
if (AiParamValueMapGetStr(params, str::scope, &scope))
writer->SetScope(std::string(scope.c_str()));

AtString mtlScope;
if (!AiParamValueMapGetStr(params, str::mtl_scope, &mtlScope))
mtlScope = AtString("/mtl");

writer->SetMtlScope(std::string(mtlScope.c_str()));

AtString defaultPrim;
if (AiParamValueMapGetStr(params, str::defaultPrim, &defaultPrim))
writer->SetDefaultPrim(std::string(defaultPrim.c_str()));
Expand Down
2 changes: 1 addition & 1 deletion testsuite/test_0138/data/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main(int argc, char **argv)
AiMsgError("/beautiful/scope/my/cone/name doesn't exist");
success = false;
}
if (!AiNodeLookUpByName(nullptr, "/beautiful/scope/lambert1/lambert1")) {
if (!AiNodeLookUpByName(nullptr, "/beautiful/scope/mtl/lambert1/lambert1")) {
AiMsgError("/beautiful/scope/lambert1 doesn't exist");
success = false;
}
Expand Down
28 changes: 14 additions & 14 deletions testsuite/test_0208/data/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ int main(int argc, char **argv)
AiParamValueMapDestroy(params);
AiEnd();

std::vector<std::string> includeList = {"/aiStandard3/aiStandard3",
"/aiStandard3/checker",
"/aiStandard3displacementShader1/aiStandard3",
"/aiStandard3displacementShader1/checker",
"/aiStandard3displacementShader1/displacementShader1",
"/aiStandard3displacementShader1/bulge1",
"/aiStandard3displacementShader2/aiStandard3",
"/aiStandard3displacementShader2/checker",
"/aiStandard3displacementShader2/displacementShader2",
"/aiStandard3displacementShader2/noise1",
"/aiStandard5displacementShader3/aiStandard5",
"/aiStandard5displacementShader3/displacementShader3",
"/aiStandard5displacementShader3/checker1_cc",
"/aiStandard5displacementShader3/checker1",
std::vector<std::string> includeList = {"/mtl/aiStandard3/aiStandard3",
"/mtl/aiStandard3/checker",
"/mtl/aiStandard3displacementShader1/aiStandard3",
"/mtl/aiStandard3displacementShader1/checker",
"/mtl/aiStandard3displacementShader1/displacementShader1",
"/mtl/aiStandard3displacementShader1/bulge1",
"/mtl/aiStandard3displacementShader2/aiStandard3",
"/mtl/aiStandard3displacementShader2/checker",
"/mtl/aiStandard3displacementShader2/displacementShader2",
"/mtl/aiStandard3displacementShader2/noise1",
"/mtl/aiStandard5displacementShader3/aiStandard5",
"/mtl/aiStandard5displacementShader3/displacementShader3",
"/mtl/aiStandard5displacementShader3/checker1_cc",
"/mtl/aiStandard5displacementShader3/checker1",
"/place2dTexture1_u",
"/place2dTexture1_v",
"/place2dTexture1",
Expand Down
4 changes: 2 additions & 2 deletions testsuite/test_1457/data/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ int main(int argc, char **argv)
AiBegin();
AiSceneLoad(nullptr, "scene.usda", nullptr);

const static AtString shaderName("/test/aiStandardSurface1/aiStandardSurface1");
const static AtString shaderName("/mtl/test/aiStandardSurface1/aiStandardSurface1");
AtNode *shader = AiNodeLookUpByName(nullptr, shaderName);
const static AtString noiseName("/test/aiStandardSurface1/my_noise");
const static AtString noiseName("/mtl/test/aiStandardSurface1/my_noise");
AtNode *noise = AiNodeLookUpByName(nullptr, noiseName);
bool success = (shader != nullptr && noise != nullptr);
AiEnd();
Expand Down
20 changes: 12 additions & 8 deletions translator/writer/prim_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1107,21 +1107,26 @@ void UsdArnoldPrimWriter::_WriteMatrix(UsdGeomXformable& xformable, const AtNode

static void processMaterialBinding(AtNode* shader, AtNode* displacement, UsdPrim& prim, UsdArnoldWriter& writer)
{
std::string shaderName = (shader) ? UsdArnoldPrimWriter::GetArnoldNodeName(shader, writer) : "";
std::string dispName = (displacement) ? UsdArnoldPrimWriter::GetArnoldNodeName(displacement, writer) : "";


// Special case : by default when no shader is assigned, the shader that is returned
// is the arnold default shader "ai_default_reflection_shader". Since it's an implicit node that
// isn't exported to arnold, we don't want to consider it
static const std::string ai_default_reflection_shader = "ai_default_reflection_shader";
if (shader && std::string(AiNodeGetName(shader)) == ai_default_reflection_shader) {
shader = nullptr;
shaderName = "";
}

if (shader == nullptr && displacement == nullptr)
return; // nothing to export

const std::string scope = writer.GetScope();
std::string mtlScope = scope + writer.GetMtlScope();
writer.SetScope("");
const std::string stripHierarchy = writer.GetStripHierarchy();

std::string shaderName = (shader) ? UsdArnoldPrimWriter::GetArnoldNodeName(shader, writer) : "";
std::string dispName = (displacement) ? UsdArnoldPrimWriter::GetArnoldNodeName(displacement, writer) : "";

#if PXR_VERSION >= 2002
UsdShadeMaterial mat = UsdShadeMaterialBindingAPI(prim).ComputeBoundMaterial();
#else
Expand All @@ -1137,7 +1142,7 @@ static void processMaterialBinding(AtNode* shader, AtNode* displacement, UsdPrim
// per combination of surface shader + displacement instead of duplicating it
// for every geometry.
if (!shaderName.empty()) {
materialName = shaderName;
materialName = mtlScope + shaderName;
if (!dispName.empty()) {
size_t namePos = dispName.find_last_of('/');
materialName += (namePos == std::string::npos) ? dispName : dispName.substr(namePos + 1);
Expand All @@ -1162,10 +1167,9 @@ static void processMaterialBinding(AtNode* shader, AtNode* displacement, UsdPrim
// one arnold shader could eventually be duplicated in the usd file if he's used with
// different displacement shaders. We also need to strip the material's parent hierarchy
// from each shader name, otherwise the scope might appear twice under the shaders
const std::string scope = writer.GetScope();
const std::string stripHierarchy = writer.GetStripHierarchy();

writer.SetScope(materialName);
std::string materialPath = TfGetPathName(materialName);
std::string materialPath = TfGetPathName(shaderName);
if (materialPath != "/")
writer.SetStripHierarchy(materialPath);

Expand Down
15 changes: 15 additions & 0 deletions translator/writer/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ class UsdArnoldWriter {
_scope = _scope.substr(0, _scope.length() - 1);
}

}
const std::string &GetMtlScope() const {return _mtlScope;}
void SetMtlScope(const std::string &scope) {
_mtlScope = scope;
if (!_mtlScope.empty()) {
// First character needs to be a slash
if (_mtlScope[0] != '/')
_mtlScope = std::string("/") + _mtlScope;
// Last character should *not* be slash, otherwise we could have
// double slashes in the nodes names, which can crash usd
if (_mtlScope.back() == '/')
_mtlScope = _mtlScope.substr(0, _mtlScope.length() - 1);
}

}
const std::string &GetStripHierarchy() const {return _stripHierarchy;}
void SetStripHierarchy(const std::string &s) {
Expand Down Expand Up @@ -201,6 +215,7 @@ class UsdArnoldWriter {
std::unordered_set<std::string> _exportedNodes; // List of node names that were exported (including material scope)
std::unordered_set<const AtNode *> _exportedShaders; // list of shader nodes that were exported
std::string _scope; // scope in which the primitives must be written
std::string _mtlScope; // specific scope for materials (on top of the eventual generic scope)
std::string _stripHierarchy; // When writing out a primitive, strip a given hierarchy from the arnold node name
bool _allAttributes; // write all attributes to usd prims, even if they're left to default
UsdTimeCode _time; // current time required by client code
Expand Down

0 comments on commit 777f0f1

Please sign in to comment.