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.
master
Sybren A. Stüvel 4 years ago
parent b8d4a2aff8
commit 63dc72c352

@ -21,6 +21,8 @@
#include <pxr/base/tf/stringUtils.h>
#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

@ -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

@ -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 </_materials/{material name}>, 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;
}

Loading…
Cancel
Save