Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LOOKDEVX-2672 - Add default value concept to USD datamodel for UFE #3721

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cmake/modules/FindUFE.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# UFE_LIGHTS_SUPPORT Presence of UFE lights support
# UFE_MATERIALS_SUPPORT Presence of UFE materials support
# UFE_SCENE_SEGMENT_SUPPORT Presence of UFE scene segment support
# UFE_DEFAULT_VALUE_SUPPORT Presence of UFE default value support
# UFE_PREVIEW_FEATURES List of all features introduced gradually in the UFE preview version
#

Expand Down Expand Up @@ -163,6 +164,15 @@ if(UFE_INCLUDE_DIR AND EXISTS "${UFE_INCLUDE_DIR}/ufe/attribute.h")
endif()
endif()

set(UFE_DEFAULT_VALUE_SUPPORT FALSE CACHE INTERNAL "ufeHasDefaultValue")
if(UFE_INCLUDE_DIR AND EXISTS "${UFE_INCLUDE_DIR}/ufe/attribute.h")
file(STRINGS ${UFE_INCLUDE_DIR}/ufe/attribute.h UFE_HAS_API REGEX "isDefault")
if(UFE_HAS_API)
set(UFE_DEFAULT_VALUE_SUPPORT TRUE CACHE INTERNAL "ufeHasDefaultValue")
message(STATUS "Maya has UFE Attribute default value support")
endif()
endif()

set(UFE_HAS_NATIVE_TYPE_METADATA FALSE CACHE INTERNAL "ufeHasNativeTypeMetadata")
if(UFE_INCLUDE_DIR AND EXISTS "${UFE_INCLUDE_DIR}/ufe/attributeDef.h")
file(STRINGS ${UFE_INCLUDE_DIR}/ufe/attributeDef.h UFE_HAS_API REGEX "NativeType")
Expand Down
7 changes: 7 additions & 0 deletions lib/mayaUsd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ if (UFE_HAS_DISPLAY_NAME)
)
endif()

if (UFE_DEFAULT_VALUE_SUPPORT)
target_compile_definitions(${PROJECT_NAME}
PRIVATE
UFE_DEFAULT_VALUE_SUPPORT=1
)
endif()

if (UFE_HAS_NATIVE_TYPE_METADATA)
target_compile_definitions(${PROJECT_NAME}
PRIVATE
Expand Down
6 changes: 6 additions & 0 deletions lib/mayaUsd/ufe/UsdAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ bool UsdAttribute::set(const PXR_NS::VtValue& value, PXR_NS::UsdTimeCode time)
return _attrHolder->set(value, time);
}

#ifdef UFE_DEFAULT_VALUE_SUPPORT
bool UsdAttribute::_isDefault() const { return _attrHolder->isDefault(); }

void UsdAttribute::_reset() { _attrHolder->reset(); }
#endif

#ifdef UFE_V4_FEATURES_AVAILABLE
bool UsdAttribute::_hasValue() const
#else
Expand Down
30 changes: 30 additions & 0 deletions lib/mayaUsd/ufe/UsdAttribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ class MAYAUSD_CORE_PUBLIC UsdAttribute
bool get(PXR_NS::VtValue& value, PXR_NS::UsdTimeCode time) const;
bool set(const PXR_NS::VtValue& value, PXR_NS::UsdTimeCode time);

#ifdef UFE_DEFAULT_VALUE_SUPPORT
bool _isDefault() const;
void _reset();
#endif
#ifdef UFE_V4_FEATURES_AVAILABLE
bool _hasValue() const;
std::string _name() const;
Expand Down Expand Up @@ -198,6 +202,14 @@ class UsdAttributeGeneric
static const std::string& nativeSdrTypeMetadata();
}; // UsdAttributeGeneric

#ifdef UFE_DEFAULT_VALUE_SUPPORT
#define UFE_DEFAULT_VALUE_OVERRIDES \
bool isDefault() const override { return UsdAttribute::_isDefault(); } \
void reset() override { UsdAttribute::_reset(); }
#else
#define UFE_DEFAULT_VALUE_OVERRIDES
#endif

#ifdef UFE_V4_FEATURES_AVAILABLE
//! \brief Interface for USD token attributes.
class UsdAttributeFilename
Expand All @@ -216,6 +228,9 @@ class UsdAttributeFilename
// Ufe::Attribute overrides
UFE_ATTRIBUTE_OVERRIDES

// Ufe default value overrides
UFE_DEFAULT_VALUE_OVERRIDES

// Ufe::AttributeFilename overrides
std::string get() const override;
void set(const std::string& value) override;
Expand Down Expand Up @@ -244,6 +259,9 @@ class UsdAttributeEnumString
// Ufe::Attribute overrides
UFE_ATTRIBUTE_OVERRIDES

// Ufe default value overrides
UFE_DEFAULT_VALUE_OVERRIDES

// Ufe::AttributeEnumString overrides
std::string get() const override;
void set(const std::string& value) override;
Expand Down Expand Up @@ -272,6 +290,9 @@ class UsdAttributeEnumToken
// Ufe::Attribute overrides
UFE_ATTRIBUTE_OVERRIDES

// Ufe default value overrides
UFE_DEFAULT_VALUE_OVERRIDES

// Ufe::AttributeEnumString overrides
std::string get() const override;
void set(const std::string& value) override;
Expand All @@ -296,6 +317,9 @@ class TypedUsdAttribute
// Ufe::Attribute overrides
UFE_ATTRIBUTE_OVERRIDES

// Ufe default value overrides
UFE_DEFAULT_VALUE_OVERRIDES

// Ufe::TypedAttribute overrides
T get() const override;
void set(const T& value) override;
Expand Down Expand Up @@ -376,6 +400,9 @@ class UsdAttributeString
// Ufe::Attribute overrides
UFE_ATTRIBUTE_OVERRIDES

// Ufe default value overrides
UFE_DEFAULT_VALUE_OVERRIDES

// Ufe::TypedAttribute overrides
std::string get() const override;
void set(const std::string& value) override;
Expand Down Expand Up @@ -403,6 +430,9 @@ class UsdAttributeToken
// Ufe::Attribute overrides
UFE_ATTRIBUTE_OVERRIDES

// Ufe default value overrides
UFE_DEFAULT_VALUE_OVERRIDES

// Ufe::TypedAttribute overrides
std::string get() const override;
void set(const std::string& value) override;
Expand Down
13 changes: 13 additions & 0 deletions lib/mayaUsd/ufe/UsdAttributeHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ bool UsdAttributeHolder::set(const PXR_NS::VtValue& value, PXR_NS::UsdTimeCode t
return _usdAttr.Set(value, time);
}

bool UsdAttributeHolder::isDefault()
{
// Checks both authored default value and authored time samples
return !_usdAttr.HasAuthoredValue();
}

void UsdAttributeHolder::reset()
{
// Will clear all values, including time samples.
_usdAttr.Clear();
_usdAttr.GetPrim().RemoveProperty(_usdAttr.GetName());
}

bool UsdAttributeHolder::hasValue() const { return isValid() ? _usdAttr.HasValue() : false; }

std::string UsdAttributeHolder::name() const
Expand Down
2 changes: 2 additions & 0 deletions lib/mayaUsd/ufe/UsdAttributeHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class UsdAttributeHolder
virtual std::string nativeType() const;
virtual bool get(PXR_NS::VtValue& value, PXR_NS::UsdTimeCode time) const;
virtual bool set(const PXR_NS::VtValue& value, PXR_NS::UsdTimeCode time);
virtual bool isDefault();
virtual void reset();

virtual bool hasValue() const;
virtual std::string name() const;
Expand Down
32 changes: 32 additions & 0 deletions test/lib/ufe/testAttribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -2067,6 +2067,38 @@ def testAttributeNiceNames(self):
self.assertIsNotNone(attr)
self.assertEqual(attr.getMetadata("uiname"), niceName)

@unittest.skipUnless(hasattr(ufe.AttributeFloat, "isDefault"), 'Requires default value support')
def testDefaultValue(self):
cmds.file(new=True, force=True)
testFile = testUtils.getTestScene("UsdPreviewSurface", "DisplayColorCube.usda")
testDagPath, testStage = mayaUtils.createProxyFromFile(testFile)
mayaPathSegment = mayaUtils.createUfePathSegment(testDagPath)
usdPathSegment = usdUtils.createUfePathSegment("/DisplayColorCube/Looks/usdPreviewSurface1SG/usdPreviewSurface1")
shaderPath = ufe.Path([mayaPathSegment, usdPathSegment])
shaderItem = ufe.Hierarchy.createItem(shaderPath)
shaderAttrs = ufe.Attributes.attributes(shaderItem)

self.assertTrue(shaderAttrs.hasAttribute("inputs:roughness"))
shaderAttr = shaderAttrs.attribute("inputs:roughness")
self.assertAlmostEqual(shaderAttr.get(), 0.5)
self.assertTrue(shaderAttr.isDefault())
shaderAttr.set(0.75)
self.assertAlmostEqual(shaderAttr.get(), 0.75)
self.assertFalse(shaderAttr.isDefault())
shaderAttr.reset()
self.assertAlmostEqual(shaderAttr.get(), 0.5)
self.assertTrue(shaderAttr.isDefault())

self.assertTrue(shaderAttrs.hasAttribute("inputs:emissiveColor"))
shaderAttr = shaderAttrs.attribute("inputs:emissiveColor")
self.assertAlmostEqual(shaderAttr.get().r(), 0)
self.assertTrue(shaderAttr.isDefault())
shaderAttr.set(ufe.Color3f(0.75, 0.75, 0.75))
self.assertAlmostEqual(shaderAttr.get().r(), 0.75)
self.assertFalse(shaderAttr.isDefault())
shaderAttr.reset()
self.assertAlmostEqual(shaderAttr.get().r(), 0)
self.assertTrue(shaderAttr.isDefault())

if __name__ == '__main__':
unittest.main(verbosity=2)