Skip to content

Commit

Permalink
Merge pull request #2364 from Autodesk/t_gamaj/LOOKDEVX-670/translate…
Browse files Browse the repository at this point in the history
…_new_types_from_usd

LOOKDEVX-670 Improved USD type support
  • Loading branch information
seando-adsk authored May 19, 2022
2 parents 78739ff + 9d32752 commit dd195b4
Show file tree
Hide file tree
Showing 9 changed files with 977 additions and 66 deletions.
273 changes: 265 additions & 8 deletions lib/mayaUsd/ufe/UsdAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ U getUsdAttributeVectorAsUfe(const PXR_NS::UsdAttribute& attr, const PXR_NS::Usd

PXR_NS::VtValue vt;
if (attr.Get(&vt, time) && vt.IsHolding<T>()) {
T gfVec = vt.UncheckedGet<T>();
U ret(gfVec[0], gfVec[1], gfVec[2]);
T gfVec = vt.UncheckedGet<T>();
U ret;
constexpr size_t num = ret.vector.size();
std::copy(gfVec.data(), gfVec.data() + num, ret.vector.data());
return ret;
}

Expand All @@ -192,10 +194,74 @@ void setUsdAttributeVectorFromUfe(
const PXR_NS::UsdTimeCode& time)
{
T vec;
vec.Set(value.x(), value.y(), value.z());
std::copy(value.vector.data(), value.vector.data() + value.vector.size(), vec.data());
setUsdAttr<T>(attr, vec);
}

template <typename T, typename U>
U getUsdAttributeColorAsUfe(const PXR_NS::UsdAttribute& attr, const PXR_NS::UsdTimeCode& time)
{
if (!attr.IsValid() || !attr.HasValue())
return U();

PXR_NS::VtValue vt;
if (attr.Get(&vt, time) && vt.IsHolding<T>()) {
T gfVec = vt.UncheckedGet<T>();
U ret;
std::copy(gfVec.data(), gfVec.data() + ret.color.size(), ret.color.data());
return ret;
}

return U();
}

template <typename T, typename U>
void setUsdAttributeColorFromUfe(
PXR_NS::UsdAttribute& attr,
const U& value,
const PXR_NS::UsdTimeCode& time)
{
T vec;
std::copy(value.color.data(), value.color.data() + value.color.size(), vec.data());
setUsdAttr<T>(attr, vec);
}

#if (UFE_PREVIEW_VERSION_NUM >= 4015)
template <typename T, typename U>
U getUsdAttributeMatrixAsUfe(const PXR_NS::UsdAttribute& attr, const PXR_NS::UsdTimeCode& time)
{
if (!attr.IsValid() || !attr.HasValue())
return U();

PXR_NS::VtValue vt;
if (attr.Get(&vt, time) && vt.IsHolding<T>()) {
T gfMat = vt.UncheckedGet<T>();
U ret;
std::copy(
gfMat.data(),
gfMat.data() + ret.matrix.size() * ret.matrix.size(),
ret.matrix[0].data());
return ret;
}

return U();
}

template <typename T, typename U>
void setUsdAttributeMatrixFromUfe(
PXR_NS::UsdAttribute& attr,
const U& value,
const PXR_NS::UsdTimeCode& time)
{
T mat;
std::copy(
value.matrix[0].data(),
value.matrix[0].data() + value.matrix.size() * value.matrix.size(),
mat.data());
setUsdAttr<T>(attr, mat);
}
#endif

class UsdUndoableCommand : public Ufe::UndoableCommand
{
public:
Expand Down Expand Up @@ -384,6 +450,64 @@ std::string UsdAttributeGeneric::nativeType() const
return fUsdAttr.GetTypeName().GetType().GetTypeName();
}

#if (UFE_PREVIEW_VERSION_NUM >= 4015)
//------------------------------------------------------------------------------
// UsdAttributeFilename:
//------------------------------------------------------------------------------

UsdAttributeFilename::UsdAttributeFilename(
const UsdSceneItem::Ptr& item,
const PXR_NS::UsdAttribute& usdAttr)
: Ufe::AttributeFilename(item)
, UsdAttribute(item, usdAttr)
{
}

/*static*/
UsdAttributeFilename::Ptr
UsdAttributeFilename::create(const UsdSceneItem::Ptr& item, const PXR_NS::UsdAttribute& usdAttr)
{
auto attr = std::make_shared<UsdAttributeFilename>(item, usdAttr);
return attr;
}

//------------------------------------------------------------------------------
// UsdAttributeFilename - Ufe::AttributeFilename overrides
//------------------------------------------------------------------------------

std::string UsdAttributeFilename::get() const
{
PXR_NS::VtValue vt;
if (fUsdAttr.Get(&vt, getCurrentTime(sceneItem())) && vt.IsHolding<SdfAssetPath>()) {
SdfAssetPath path = vt.UncheckedGet<SdfAssetPath>();
return path.GetAssetPath();
}

return std::string();
}

void UsdAttributeFilename::set(const std::string& value)
{
SdfAssetPath path(value);
setUsdAttr<PXR_NS::SdfAssetPath>(fUsdAttr, path);
}

Ufe::UndoableCommand::Ptr UsdAttributeFilename::setCmd(const std::string& value)
{
auto self = std::dynamic_pointer_cast<UsdAttributeFilename>(shared_from_this());
if (!TF_VERIFY(self, kErrorMsgInvalidType))
return nullptr;

std::string errMsg;
if (!MayaUsd::ufe::isAttributeEditAllowed(fUsdAttr, &errMsg)) {
MGlobal::displayError(errMsg.c_str());
return nullptr;
}

return std::make_shared<SetUndoableCommand<std::string, UsdAttributeFilename>>(self, value);
}
#endif

//------------------------------------------------------------------------------
// UsdAttributeEnumString:
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -524,17 +648,28 @@ template <> void TypedUsdAttribute<std::string>::set(const std::string& value)

template <> Ufe::Color3f TypedUsdAttribute<Ufe::Color3f>::get() const
{
return getUsdAttributeVectorAsUfe<GfVec3f, Ufe::Color3f>(fUsdAttr, getCurrentTime(sceneItem()));
return getUsdAttributeColorAsUfe<GfVec3f, Ufe::Color3f>(fUsdAttr, getCurrentTime(sceneItem()));
}

// Note: cannot use setUsdAttributeVectorFromUfe since it relies on x/y/z
template <> void TypedUsdAttribute<Ufe::Color3f>::set(const Ufe::Color3f& value)
{
GfVec3f vec;
vec.Set(value.r(), value.g(), value.b());
setUsdAttr<GfVec3f>(fUsdAttr, vec);
setUsdAttributeColorFromUfe<GfVec3f, Ufe::Color3f>(
fUsdAttr, value, getCurrentTime(sceneItem()));
}

#if (UFE_PREVIEW_VERSION_NUM >= 4015)
template <> Ufe::Color4f TypedUsdAttribute<Ufe::Color4f>::get() const
{
return getUsdAttributeColorAsUfe<GfVec4f, Ufe::Color4f>(fUsdAttr, getCurrentTime(sceneItem()));
}

template <> void TypedUsdAttribute<Ufe::Color4f>::set(const Ufe::Color4f& value)
{
setUsdAttributeColorFromUfe<GfVec4f, Ufe::Color4f>(
fUsdAttr, value, getCurrentTime(sceneItem()));
}
#endif

template <> Ufe::Vector3i TypedUsdAttribute<Ufe::Vector3i>::get() const
{
return getUsdAttributeVectorAsUfe<GfVec3i, Ufe::Vector3i>(
Expand All @@ -547,6 +682,20 @@ template <> void TypedUsdAttribute<Ufe::Vector3i>::set(const Ufe::Vector3i& valu
fUsdAttr, value, getCurrentTime(sceneItem()));
}

#if (UFE_PREVIEW_VERSION_NUM >= 4015)
template <> Ufe::Vector2f TypedUsdAttribute<Ufe::Vector2f>::get() const
{
return getUsdAttributeVectorAsUfe<GfVec2f, Ufe::Vector2f>(
fUsdAttr, getCurrentTime(sceneItem()));
}

template <> void TypedUsdAttribute<Ufe::Vector2f>::set(const Ufe::Vector2f& value)
{
setUsdAttributeVectorFromUfe<GfVec2f, Ufe::Vector2f>(
fUsdAttr, value, getCurrentTime(sceneItem()));
}
#endif

template <> Ufe::Vector3f TypedUsdAttribute<Ufe::Vector3f>::get() const
{
return getUsdAttributeVectorAsUfe<GfVec3f, Ufe::Vector3f>(
Expand All @@ -559,6 +708,20 @@ template <> void TypedUsdAttribute<Ufe::Vector3f>::set(const Ufe::Vector3f& valu
fUsdAttr, value, getCurrentTime(sceneItem()));
}

#if (UFE_PREVIEW_VERSION_NUM >= 4015)
template <> Ufe::Vector4f TypedUsdAttribute<Ufe::Vector4f>::get() const
{
return getUsdAttributeVectorAsUfe<GfVec4f, Ufe::Vector4f>(
fUsdAttr, getCurrentTime(sceneItem()));
}

template <> void TypedUsdAttribute<Ufe::Vector4f>::set(const Ufe::Vector4f& value)
{
setUsdAttributeVectorFromUfe<GfVec4f, Ufe::Vector4f>(
fUsdAttr, value, getCurrentTime(sceneItem()));
}
#endif

template <> Ufe::Vector3d TypedUsdAttribute<Ufe::Vector3d>::get() const
{
return getUsdAttributeVectorAsUfe<GfVec3d, Ufe::Vector3d>(
Expand All @@ -571,6 +734,32 @@ template <> void TypedUsdAttribute<Ufe::Vector3d>::set(const Ufe::Vector3d& valu
fUsdAttr, value, getCurrentTime(sceneItem()));
}

#if (UFE_PREVIEW_VERSION_NUM >= 4015)
template <> Ufe::Matrix3d TypedUsdAttribute<Ufe::Matrix3d>::get() const
{
return getUsdAttributeMatrixAsUfe<GfMatrix3d, Ufe::Matrix3d>(
fUsdAttr, getCurrentTime(sceneItem()));
}

template <> void TypedUsdAttribute<Ufe::Matrix3d>::set(const Ufe::Matrix3d& value)
{
setUsdAttributeMatrixFromUfe<GfMatrix3d, Ufe::Matrix3d>(
fUsdAttr, value, getCurrentTime(sceneItem()));
}

template <> Ufe::Matrix4d TypedUsdAttribute<Ufe::Matrix4d>::get() const
{
return getUsdAttributeMatrixAsUfe<GfMatrix4d, Ufe::Matrix4d>(
fUsdAttr, getCurrentTime(sceneItem()));
}

template <> void TypedUsdAttribute<Ufe::Matrix4d>::set(const Ufe::Matrix4d& value)
{
setUsdAttributeMatrixFromUfe<GfMatrix4d, Ufe::Matrix4d>(
fUsdAttr, value, getCurrentTime(sceneItem()));
}
#endif

template <typename T> T TypedUsdAttribute<T>::get() const
{
if (!hasValue())
Expand Down Expand Up @@ -661,6 +850,20 @@ UsdAttributeColorFloat3::create(const UsdSceneItem::Ptr& item, const PXR_NS::Usd
return attr;
}

#if (UFE_PREVIEW_VERSION_NUM >= 4015)
//------------------------------------------------------------------------------
// UsdAttributeColorFloat4:
//------------------------------------------------------------------------------

/*static*/
UsdAttributeColorFloat4::Ptr
UsdAttributeColorFloat4::create(const UsdSceneItem::Ptr& item, const PXR_NS::UsdAttribute& usdAttr)
{
auto attr = std::make_shared<UsdAttributeColorFloat4>(item, usdAttr);
return attr;
}
#endif

//------------------------------------------------------------------------------
// UsdAttributeInt3:
//------------------------------------------------------------------------------
Expand All @@ -673,6 +876,20 @@ UsdAttributeInt3::create(const UsdSceneItem::Ptr& item, const PXR_NS::UsdAttribu
return attr;
}

#if (UFE_PREVIEW_VERSION_NUM >= 4015)
//------------------------------------------------------------------------------
// UsdAttributeFloat2:
//------------------------------------------------------------------------------

/*static*/
UsdAttributeFloat2::Ptr
UsdAttributeFloat2::create(const UsdSceneItem::Ptr& item, const PXR_NS::UsdAttribute& usdAttr)
{
auto attr = std::make_shared<UsdAttributeFloat2>(item, usdAttr);
return attr;
}
#endif

//------------------------------------------------------------------------------
// UsdAttributeFloat3:
//------------------------------------------------------------------------------
Expand All @@ -685,6 +902,20 @@ UsdAttributeFloat3::create(const UsdSceneItem::Ptr& item, const PXR_NS::UsdAttri
return attr;
}

#if (UFE_PREVIEW_VERSION_NUM >= 4015)
//------------------------------------------------------------------------------
// UsdAttributeFloat4:
//------------------------------------------------------------------------------

/*static*/
UsdAttributeFloat4::Ptr
UsdAttributeFloat4::create(const UsdSceneItem::Ptr& item, const PXR_NS::UsdAttribute& usdAttr)
{
auto attr = std::make_shared<UsdAttributeFloat4>(item, usdAttr);
return attr;
}
#endif

//------------------------------------------------------------------------------
// UsdAttributeDouble3:
//------------------------------------------------------------------------------
Expand All @@ -697,6 +928,32 @@ UsdAttributeDouble3::create(const UsdSceneItem::Ptr& item, const PXR_NS::UsdAttr
return attr;
}

#if (UFE_PREVIEW_VERSION_NUM >= 4015)
//------------------------------------------------------------------------------
// UsdAttributeMatrix3d:
//------------------------------------------------------------------------------

/*static*/
UsdAttributeMatrix3d::Ptr
UsdAttributeMatrix3d::create(const UsdSceneItem::Ptr& item, const PXR_NS::UsdAttribute& usdAttr)
{
auto attr = std::make_shared<UsdAttributeMatrix3d>(item, usdAttr);
return attr;
}

//------------------------------------------------------------------------------
// UsdAttributeMatrix4d:
//------------------------------------------------------------------------------

/*static*/
UsdAttributeMatrix4d::Ptr
UsdAttributeMatrix4d::create(const UsdSceneItem::Ptr& item, const PXR_NS::UsdAttribute& usdAttr)
{
auto attr = std::make_shared<UsdAttributeMatrix4d>(item, usdAttr);
return attr;
}
#endif

#if 0
// Note: if we were to implement generic attribute setting (via string) this
// would be the way it could be done.
Expand Down
Loading

0 comments on commit dd195b4

Please sign in to comment.