From 63dc72c35215d38c437a5d20a8dc8f6e25e1fc8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Tue, 8 Sep 2020 11:33:35 +0200 Subject: Cleanup: USD export, refactor mesh instancing Extract the mesh instancing code from the mesh writing function into a generic 'mark as instance' function on the abstract USD writer. This will help in supporting non-mesh instances. No functional changes. --- .../blender/io/usd/intern/usd_writer_abstract.cc | 27 ++++++++++++++++++++++ source/blender/io/usd/intern/usd_writer_abstract.h | 4 ++++ source/blender/io/usd/intern/usd_writer_mesh.cc | 23 +++++------------- 3 files changed, 37 insertions(+), 17 deletions(-) (limited to 'source/blender/io') diff --git a/source/blender/io/usd/intern/usd_writer_abstract.cc b/source/blender/io/usd/intern/usd_writer_abstract.cc index 4910b7f11dd..082a60d102e 100644 --- a/source/blender/io/usd/intern/usd_writer_abstract.cc +++ b/source/blender/io/usd/intern/usd_writer_abstract.cc @@ -21,6 +21,8 @@ #include +#include "BLI_assert.h" + /* TfToken objects are not cheap to construct, so we do it once. */ namespace usdtokens { // Materials @@ -128,6 +130,31 @@ void USDAbstractWriter::write_visibility(const HierarchyContext &context, usd_value_writer_.SetAttribute(attr_visibility, pxr::VtValue(visibility), timecode); } +/* Reference the original data instead of writing a copy. */ +bool USDAbstractWriter::mark_as_instance(HierarchyContext &context, const pxr::UsdPrim &prim) +{ + BLI_assert(context.is_instance()); + + if (context.export_path == context.original_export_path) { + printf("USD ref error: export path is reference path: %s\n", context.export_path.c_str()); + BLI_assert(!"USD reference error"); + return false; + } + + pxr::SdfPath ref_path(context.original_export_path); + if (!prim.GetReferences().AddInternalReference(ref_path)) { + /* See this URL for a description fo why referencing may fail" + * https://graphics.pixar.com/usd/docs/api/class_usd_references.html#Usd_Failing_References + */ + printf("USD Export warning: unable to add reference from %s to %s, not instancing object\n", + context.export_path.c_str(), + context.original_export_path.c_str()); + return false; + } + + return true; +} + } // namespace usd } // namespace io } // namespace blender diff --git a/source/blender/io/usd/intern/usd_writer_abstract.h b/source/blender/io/usd/intern/usd_writer_abstract.h index 248bdd22a3b..f702768f734 100644 --- a/source/blender/io/usd/intern/usd_writer_abstract.h +++ b/source/blender/io/usd/intern/usd_writer_abstract.h @@ -76,6 +76,10 @@ class USDAbstractWriter : public AbstractHierarchyWriter { void write_visibility(const HierarchyContext &context, const pxr::UsdTimeCode timecode, pxr::UsdGeomImageable &usd_geometry); + + /* Turn `prim` into an instance referencing `context.original_export_path`. + * Return true when the instancing was succesful, false otherwise. */ + virtual bool mark_as_instance(HierarchyContext &context, const pxr::UsdPrim &prim); }; } // namespace usd diff --git a/source/blender/io/usd/intern/usd_writer_mesh.cc b/source/blender/io/usd/intern/usd_writer_mesh.cc index 7d3ea911a65..2073d4cbe87 100644 --- a/source/blender/io/usd/intern/usd_writer_mesh.cc +++ b/source/blender/io/usd/intern/usd_writer_mesh.cc @@ -160,29 +160,18 @@ void USDGenericMeshWriter::write_mesh(HierarchyContext &context, Mesh *mesh) get_geometry_data(mesh, usd_mesh_data); if (usd_export_context_.export_params.use_instancing && context.is_instance()) { - // This object data is instanced, just reference the original instead of writing a copy. - if (context.export_path == context.original_export_path) { - printf("USD ref error: export path is reference path: %s\n", context.export_path.c_str()); - BLI_assert(!"USD reference error"); - return; - } - pxr::SdfPath ref_path(context.original_export_path); - if (!usd_mesh.GetPrim().GetReferences().AddInternalReference(ref_path)) { - /* See this URL for a description fo why referencing may fail" - * https://graphics.pixar.com/usd/docs/api/class_usd_references.html#Usd_Failing_References - */ - printf("USD Export warning: unable to add reference from %s to %s, not instancing object\n", - context.export_path.c_str(), - context.original_export_path.c_str()); + if (!mark_as_instance(context, usd_mesh.GetPrim())) { return; } + /* The material path will be of the form , which is outside the - sub-tree pointed to by ref_path. As a result, the referenced data is not allowed to point out - of its own sub-tree. It does work when we override the material with exactly the same path, - though.*/ + * sub-tree pointed to by ref_path. As a result, the referenced data is not allowed to point + * out of its own sub-tree. It does work when we override the material with exactly the same + * path, though.*/ if (usd_export_context_.export_params.export_materials) { assign_materials(context, usd_mesh, usd_mesh_data.face_groups); } + return; } -- cgit v1.2.3