Skip to content

Commit 773e674

Browse files
committed
Support enums custom attributes as labels using a maya OptionVar
1 parent 59b610e commit 773e674

File tree

2 files changed

+96
-6
lines changed

2 files changed

+96
-6
lines changed

lib/mayaHydra/hydraExtensions/mixedUtils.cpp

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ PXR_NAMESPACE_USING_DIRECTIVE
7878

7979
namespace MAYAHYDRA_NS_DEF {
8080

81+
// OptionVar to emit enum primvars as label tokens instead of integer values.
82+
constexpr const char* kExtEnumUseLabelsOptionVar = "mayaHydraExtensionEnumUseLabels";
83+
84+
bool UseEnumLabelsForExtensionAttrs()
85+
{
86+
if (MGlobal::optionVarExists(kExtEnumUseLabelsOptionVar)) {
87+
return MGlobal::optionVarIntValue(kExtEnumUseLabelsOptionVar) != 0;
88+
}
89+
return false;
90+
}
91+
8192
// Store value only when it differs from the default.
8293
template<typename T>
8394
void UpdateAttrs(const char* attrName, const T& val, const T& defaultVal, VtDictionary& attrs)
@@ -1057,12 +1068,32 @@ void GetExtensionAttributesFromNode(
10571068
short value = attrPlug.asShort();
10581069
short defaultVal = 0;
10591070
enumAttr.getDefault(defaultVal);
1060-
UpdateAttrsValue(
1061-
attrName,
1062-
VtValue(value),
1063-
VtValue(defaultVal),
1064-
!ignoreDefault,
1065-
attrs);
1071+
if (UseEnumLabelsForExtensionAttrs()) {
1072+
const MString label = enumAttr.fieldName(value);
1073+
const MString defaultLabel = enumAttr.fieldName(defaultVal);
1074+
if (!label.length() || !defaultLabel.length()) {
1075+
UpdateAttrsValue(
1076+
attrName,
1077+
VtValue(value),
1078+
VtValue(defaultVal),
1079+
!ignoreDefault,
1080+
attrs);
1081+
} else {
1082+
UpdateAttrsValue(
1083+
attrName,
1084+
VtValue(TfToken(label.asChar())),
1085+
VtValue(TfToken(defaultLabel.asChar())),
1086+
!ignoreDefault,
1087+
attrs);
1088+
}
1089+
} else {
1090+
UpdateAttrsValue(
1091+
attrName,
1092+
VtValue(value),
1093+
VtValue(defaultVal),
1094+
!ignoreDefault,
1095+
attrs);
1096+
}
10661097
} break;
10671098
case MFn::kTypedAttribute: {
10681099
MFnTypedAttribute typeAttr(attrObj);

test/lib/mayaUsd/render/mayaToHydra/cpp/testCustomAttributeTypes.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,35 @@ using namespace MayaHydra;
7575
// Unit test: verifies custom Maya extension attributes are translated into Hydra primvars.
7676
namespace {
7777

78+
// OptionVar to emit enum primvars as label tokens.
79+
const char* kEnumLabelOptionVar = "mayaHydraExtensionEnumUseLabels";
80+
81+
class ScopedOptionVarInt
82+
{
83+
public:
84+
ScopedOptionVarInt(const char* name, int value)
85+
: _name(name)
86+
, _hadValue(MGlobal::optionVarExists(name))
87+
, _oldValue(_hadValue ? MGlobal::optionVarIntValue(name) : 0)
88+
{
89+
MGlobal::setOptionVarValue(name, value);
90+
}
91+
92+
~ScopedOptionVarInt()
93+
{
94+
if (_hadValue) {
95+
MGlobal::setOptionVarValue(_name.c_str(), _oldValue);
96+
} else {
97+
MGlobal::removeOptionVar(_name.c_str());
98+
}
99+
}
100+
101+
private:
102+
std::string _name;
103+
bool _hadValue;
104+
int _oldValue;
105+
};
106+
78107
// Build a predicate to find a prim by name and type.
79108
FindPrimPredicate getPrimPredicate(const std::string& primName, const TfToken& primType)
80109
{
@@ -264,6 +293,36 @@ TEST(CustomAttributes, extensionAttributeTypes)
264293
ExpectPrimvarValue(prim, TfToken("extMatrixAttr"), ToGfMatrix(matrixAttrValue));
265294
}
266295

296+
// Validate enum primvars can emit labels when configured.
297+
TEST(CustomAttributes, extensionAttributeEnumLabels)
298+
{
299+
ScopedOptionVarInt enumLabels(kEnumLabelOptionVar, 1);
300+
MGlobal::executeCommand("setAttr \"pCubeShape1.extEnum\" 2");
301+
MGlobal::executeCommand("dgdirty -a");
302+
MGlobal::executeCommand("refresh");
303+
304+
const SceneIndicesVector& sceneIndices = GetTerminalSceneIndices();
305+
ASSERT_GT(sceneIndices.size(), 0u);
306+
307+
HdSceneIndexPrim prim;
308+
bool testPassed = false;
309+
for (const HdSceneIndexBaseRefPtr& sceneIndex : sceneIndices) {
310+
SceneIndexInspector inspector(sceneIndex);
311+
PrimEntriesVector foundPrims
312+
= inspector.FindPrims(getPrimPredicate("pCube1", HdPrimTypeTokens->mesh));
313+
if (foundPrims.size() == 1u) {
314+
prim = foundPrims.front().prim;
315+
ASSERT_EQ(prim.primType, HdPrimTypeTokens->mesh);
316+
ASSERT_NE(prim.dataSource, nullptr);
317+
testPassed = true;
318+
break;
319+
}
320+
}
321+
ASSERT_TRUE(testPassed);
322+
323+
ExpectPrimvarValue(prim, TfToken("extEnum"), TfToken("two"));
324+
}
325+
267326
// Validate typed numeric attributes are translated into primvars.
268327
TEST(CustomAttributes, extensionAttributeTypedNumeric)
269328
{

0 commit comments

Comments
 (0)