From d1663b7f7b4f57d1db6010932c97c9aaf6a335be Mon Sep 17 00:00:00 2001 From: SideFX Date: Sat, 16 Nov 2019 01:00:16 -0500 Subject: [PATCH] Matching to Houdini build 18.5.31. --- src/houdini/custom/USDOP/CMakeSources.cmake | 1 + src/houdini/custom/USDOP/HUSD_FieldWrapper.C | 171 ++++++++++++++++++ src/houdini/custom/USDOP/HUSD_FieldWrapper.h | 61 +++++++ src/houdini/custom/USDOP/plugin.C | 5 + .../lib/H_USD/HUSD/HUSD_EditReferences.C | 10 +- 5 files changed, 245 insertions(+), 3 deletions(-) create mode 100644 src/houdini/custom/USDOP/HUSD_FieldWrapper.C create mode 100644 src/houdini/custom/USDOP/HUSD_FieldWrapper.h diff --git a/src/houdini/custom/USDOP/CMakeSources.cmake b/src/houdini/custom/USDOP/CMakeSources.cmake index c4e85317..269d2dd0 100644 --- a/src/houdini/custom/USDOP/CMakeSources.cmake +++ b/src/houdini/custom/USDOP/CMakeSources.cmake @@ -1,5 +1,6 @@ set( sources plugin.C + HUSD_FieldWrapper.C HUSD_LopStageFactory.C OBJ_LOP.C OBJ_LOPCamera.C diff --git a/src/houdini/custom/USDOP/HUSD_FieldWrapper.C b/src/houdini/custom/USDOP/HUSD_FieldWrapper.C new file mode 100644 index 00000000..c8d39d38 --- /dev/null +++ b/src/houdini/custom/USDOP/HUSD_FieldWrapper.C @@ -0,0 +1,171 @@ +/* + * PROPRIETARY INFORMATION. This software is proprietary to + * Side Effects Software Inc., and is not to be reproduced, + * transmitted, or disclosed in any way without written permission. +*/ + +#include "HUSD_FieldWrapper.h" + +#include +#include +#include +#include +#include + +PXR_NAMESPACE_OPEN_SCOPE + +TF_DEFINE_PRIVATE_TOKENS(_tokens, + ((vdbFieldPrimType, "OpenVDBAsset")) + ((houdiniFieldPrimType, "HoudiniFieldAsset")) +); + +void +HUSD_FieldWrapper::registerForRead() +{ + static std::once_flag registered; + + std::call_once(registered, []() { + // Register for both VDB and Houdini volumes. + GusdPrimWrapper::registerPrimDefinitionFuncForRead( + _tokens->vdbFieldPrimType, &HUSD_FieldWrapper::defineForRead); + GusdPrimWrapper::registerPrimDefinitionFuncForRead( + _tokens->houdiniFieldPrimType, &HUSD_FieldWrapper::defineForRead); + }); +} + +HUSD_FieldWrapper::HUSD_FieldWrapper(const UsdVolFieldAsset &usd_field, + UsdTimeCode time, GusdPurposeSet purposes) + : GusdPrimWrapper(time, purposes), myUsdField(usd_field) +{ +} + +HUSD_FieldWrapper::~HUSD_FieldWrapper() {} + +const char * +HUSD_FieldWrapper::className() const +{ + return "HUSD_FieldWrapper"; +} + +void +HUSD_FieldWrapper::enlargeBounds(UT_BoundingBox boxes[], int nsegments) const +{ + UT_ASSERT_MSG(false, "HUSD_FieldWrapper::enlargeBounds not implemented"); +} + +int +HUSD_FieldWrapper::getMotionSegments() const +{ + return 1; +} + +int64 +HUSD_FieldWrapper::getMemoryUsage() const +{ + return sizeof(*this); +} + +GT_PrimitiveHandle +HUSD_FieldWrapper::doSoftCopy() const +{ + return GT_PrimitiveHandle(new HUSD_FieldWrapper(*this)); +} + +bool +HUSD_FieldWrapper::isValid() const +{ + return static_cast(myUsdField); +} + +bool +HUSD_FieldWrapper::refine(GT_Refine &refiner, + const GT_RefineParms *parms) const +{ + if (!isValid()) + { + TF_WARN("Invalid prim"); + return false; + } + + UsdAttribute file_path_attr = myUsdField.GetFilePathAttr(); + SdfAssetPath file_path; + if (file_path_attr) + TF_VERIFY(file_path_attr.Get(&file_path, m_time)); + + UsdAttribute field_name_attr = + myUsdField.GetPrim().GetAttribute(UsdVolTokens->fieldName); + TfToken field_name; + if (field_name_attr) + TF_VERIFY(field_name_attr.Get(&field_name, m_time)); + + UsdAttribute field_index_attr = + myUsdField.GetPrim().GetAttribute(UsdVolTokens->fieldIndex); + int field_index = -1; + if (field_index_attr) + TF_VERIFY(field_index_attr.Get(&field_index, m_time)); + + bool is_vdb = false; + bool is_houdini_vol = false; + TfToken field_type; + + // Determine the field type, and the correct type to pass to + // HUSD_HydraField::getVolumePrimitive (which expects Hydra prim types). + const TfToken &src_field_type = myUsdField.GetPrim().GetTypeName(); + if (src_field_type == _tokens->vdbFieldPrimType) + { + is_vdb = true; + field_type = HusdHdPrimTypeTokens()->openvdbAsset; + } + else if (src_field_type == _tokens->houdiniFieldPrimType) + { + is_houdini_vol = true; + field_type = HusdHdPrimTypeTokens()->bprimHoudiniFieldAsset; + } + else + { + UT_ASSERT_MSG(false, "Unknown volume primitive type"); + return false; + } + + // Attempt to load the volume from disk or a SOP network. + GT_PrimitiveHandle volume = HUSD_HydraField::getVolumePrimitive( + file_path.GetAssetPath(), field_name.GetString(), field_index, + field_type.GetString()); + if (!volume) + { + UT_ASSERT_MSG(false, "Could not load volume"); + return false; + } + + // Since we may have loaded the volume from SOPs, replace the attribute + // list with the field's primvars so that extra attributes won't + // unexpectedly appear. + GT_AttributeListHandle attribs = + new GT_AttributeList(new GT_AttributeMap()); + loadPrimvars(m_time, parms, 1, 0, 0, myUsdField.GetPath().GetString(), + nullptr, nullptr, &attribs, nullptr); + + if (is_vdb) + { + auto vol = UTverify_cast(volume.get()); + vol->setUniformAttributes(attribs); + } + else if (is_houdini_vol) + { + auto vol = UTverify_cast(volume.get()); + vol->setUniformAttributes(attribs); + } + + refiner.addPrimitive(volume); + return true; +} + +GT_PrimitiveHandle +HUSD_FieldWrapper::defineForRead(const UsdGeomImageable &source_prim, + UsdTimeCode time, GusdPurposeSet purposes) +{ + return new HUSD_FieldWrapper(UsdVolFieldAsset(source_prim.GetPrim()), time, + purposes); +} + +PXR_NAMESPACE_CLOSE_SCOPE diff --git a/src/houdini/custom/USDOP/HUSD_FieldWrapper.h b/src/houdini/custom/USDOP/HUSD_FieldWrapper.h new file mode 100644 index 00000000..5a367157 --- /dev/null +++ b/src/houdini/custom/USDOP/HUSD_FieldWrapper.h @@ -0,0 +1,61 @@ +/* + * PROPRIETARY INFORMATION. This software is proprietary to + * Side Effects Software Inc., and is not to be reproduced, + * transmitted, or disclosed in any way without written permission. +*/ + +#ifndef __HUSD_FieldWrapper__ +#define __HUSD_FieldWrapper__ + +#include +#include + +PXR_NAMESPACE_OPEN_SCOPE + +/// GusdPrimWrapper implementation for converting USD fields back to +/// GT_PrimVolume or GT_PrimVDB primitives. +class HUSD_FieldWrapper : public GusdPrimWrapper +{ +public: + HUSD_FieldWrapper(const UsdVolFieldAsset &usd_field, UsdTimeCode t, + GusdPurposeSet purposes); + virtual ~HUSD_FieldWrapper(); + + static void registerForRead(); + + virtual const UsdGeomImageable getUsdPrim() const override + { + return myUsdField; + } + + virtual const char *className() const override; + + virtual void + enlargeBounds(UT_BoundingBox boxes[], int nsegments) const override; + + virtual int getMotionSegments() const override; + + virtual int64 getMemoryUsage() const override; + + virtual GT_PrimitiveHandle doSoftCopy() const override; + + virtual bool isValid() const override; + + virtual bool refine(GT_Refine &refiner, + const GT_RefineParms *parms = nullptr) const override; + +public: + static GT_PrimitiveHandle + defineForRead(const UsdGeomImageable &source_prim, UsdTimeCode time, + GusdPurposeSet purposes); + +private: + bool initUsdPrim(const UsdStagePtr &stage, const SdfPath &path, + bool as_override); + + UsdVolFieldAsset myUsdField; +}; + +PXR_NAMESPACE_CLOSE_SCOPE + +#endif diff --git a/src/houdini/custom/USDOP/plugin.C b/src/houdini/custom/USDOP/plugin.C index 219b84bd..6c3dbeeb 100644 --- a/src/houdini/custom/USDOP/plugin.C +++ b/src/houdini/custom/USDOP/plugin.C @@ -24,6 +24,7 @@ #include "pxr/base/arch/export.h" #include +#include "HUSD_FieldWrapper.h" #include "OBJ_LOP.h" #include "OBJ_LOPCamera.h" #include "SOP_LOP.h" @@ -45,6 +46,7 @@ newSopOperator(OP_OperatorTable* operators) if (SOLARIS_ENABLED) { PXR_NS::GusdInit(); + PXR_NS::HUSD_FieldWrapper::registerForRead(); PXR_NS::SOP_LOP::Register(operators); PXR_NS::SOP_UnpackUSD::Register(operators); } @@ -57,6 +59,7 @@ newObjectOperator(OP_OperatorTable* operators) if (SOLARIS_ENABLED) { PXR_NS::GusdInit(); + PXR_NS::HUSD_FieldWrapper::registerForRead(); PXR_NS::OBJ_LOP::Register(operators); PXR_NS::OBJ_LOPCamera::Register(operators); } @@ -69,6 +72,7 @@ newGeometryPrim(GA_PrimitiveFactory *f) if (SOLARIS_ENABLED) { PXR_NS::GusdInit(); + PXR_NS::HUSD_FieldWrapper::registerForRead(); PXR_NS::GusdNewGeometryPrim(f); } } @@ -80,6 +84,7 @@ newGeometryIO(void *) if (SOLARIS_ENABLED) { PXR_NS::GusdInit(); + PXR_NS::HUSD_FieldWrapper::registerForRead(); PXR_NS::GusdNewGeometryIO(); } } diff --git a/src/houdini/lib/H_USD/HUSD/HUSD_EditReferences.C b/src/houdini/lib/H_USD/HUSD/HUSD_EditReferences.C index c741096e..33eac21b 100644 --- a/src/houdini/lib/H_USD/HUSD/HUSD_EditReferences.C +++ b/src/houdini/lib/H_USD/HUSD/HUSD_EditReferences.C @@ -207,17 +207,21 @@ HUSD_EditReferences::removeReference(const UT_StringRef &primpath, if (prim) { SdfFileFormat::FileFormatArguments args; + SdfPath bestrefprimpath; for (auto &&it : refargs) args[it.first.toStdString()] = it.second.toStdString(); + bestrefprimpath = HUSDgetBestRefPrimPath( + reffilepath, args, refprimpath, stage); + if (myRefType == HUSD_Constants::getReferenceTypeFile()) { auto refs = prim.GetReferences(); SdfReference sdfreference( SdfLayer::CreateIdentifier( reffilepath.toStdString(), args), - HUSDgetSdfPath(refprimpath), + bestrefprimpath, HUSDgetSdfLayerOffset(offset)); success = refs.RemoveReference(sdfreference); @@ -227,7 +231,7 @@ HUSD_EditReferences::removeReference(const UT_StringRef &primpath, auto refs = prim.GetReferences(); SdfReference sdfreference( std::string(), - HUSDgetSdfPath(refprimpath), + bestrefprimpath, HUSDgetSdfLayerOffset(offset)); success = refs.RemoveReference(sdfreference); @@ -238,7 +242,7 @@ HUSD_EditReferences::removeReference(const UT_StringRef &primpath, SdfPayload sdfpayload( SdfLayer::CreateIdentifier( reffilepath.toStdString(), args), - HUSDgetSdfPath(refprimpath), + bestrefprimpath, HUSDgetSdfLayerOffset(offset)); success = payloads.RemovePayload(sdfpayload);