USD: Allow exporting of invisible objects

The fix for T75936 made it possible to export invisible objects to
Alembic. This commit applies the same approach to the USD exporter.

The USD and Alembic code is slightly different in terms of where in the
exported file the visibility attribute is stored. In USD the visibility
is used to prune the scene graph, and thus there are only two options:
"hidden" and "inherited". Setting the visiblity of a node in the scene
graph to "hidden" immediately hides all its children. To allow hidden
parents with visible children, the visibility is stored on the object
data (so the geometry/camera/lamp/etc) instead.
This commit is contained in:
Sybren A. Stüvel 2020-08-14 16:48:39 +02:00
parent 271361e31f
commit 108f3284a7
6 changed files with 43 additions and 2 deletions

View File

@ -113,6 +113,7 @@ static int wm_usd_export_exec(bContext *C, wmOperator *op)
MEM_SAFE_FREE(op->customdata);
const bool selected_objects_only = RNA_boolean_get(op->ptr, "selected_objects_only");
const bool visible_objects_only = RNA_boolean_get(op->ptr, "visible_objects_only");
const bool export_animation = RNA_boolean_get(op->ptr, "export_animation");
const bool export_hair = RNA_boolean_get(op->ptr, "export_hair");
const bool export_uvmaps = RNA_boolean_get(op->ptr, "export_uvmaps");
@ -128,6 +129,7 @@ static int wm_usd_export_exec(bContext *C, wmOperator *op)
export_normals,
export_materials,
selected_objects_only,
visible_objects_only,
use_instancing,
evaluation_mode,
};
@ -149,6 +151,7 @@ static void wm_usd_export_draw(bContext *UNUSED(C), wmOperator *op)
col = uiLayoutColumn(box, true);
uiItemR(col, ptr, "selected_objects_only", 0, NULL, ICON_NONE);
uiItemR(col, ptr, "visible_objects_only", 0, NULL, ICON_NONE);
col = uiLayoutColumn(box, true);
uiItemR(col, ptr, "export_animation", 0, NULL, ICON_NONE);
@ -191,6 +194,13 @@ void WM_OT_usd_export(struct wmOperatorType *ot)
"Only selected objects are exported. Unselected parents of selected objects are "
"exported as empty transform");
RNA_def_boolean(ot->srna,
"visible_objects_only",
true,
"Visible Only",
"Only visible objects are exported. Invisible parents of exported objects are "
"exported as empty transform");
RNA_def_boolean(ot->srna,
"export_animation",
false,

View File

@ -76,7 +76,12 @@ static void export_startjob(void *customdata,
// Construct the depsgraph for exporting.
Scene *scene = DEG_get_input_scene(data->depsgraph);
ViewLayer *view_layer = DEG_get_input_view_layer(data->depsgraph);
DEG_graph_build_from_view_layer(data->depsgraph, data->bmain, scene, view_layer);
if (data->params.visible_objects_only) {
DEG_graph_build_from_view_layer(data->depsgraph, data->bmain, scene, view_layer);
}
else {
DEG_graph_build_for_all_objects(data->depsgraph, data->bmain, scene, view_layer);
}
BKE_scene_graph_update_tagged(data->depsgraph, data->bmain);
*progress = 0.0f;

View File

@ -114,6 +114,20 @@ pxr::UsdShadeMaterial USDAbstractWriter::ensure_usd_material(Material *material)
return usd_material;
}
void USDAbstractWriter::write_visibility(const HierarchyContext &context,
const pxr::UsdTimeCode timecode,
pxr::UsdGeomImageable &usd_geometry)
{
pxr::UsdAttribute attr_visibility = usd_geometry.CreateVisibilityAttr(pxr::VtValue(), true);
const bool is_visible = context.is_object_visible(
usd_export_context_.export_params.evaluation_mode);
const pxr::TfToken visibility = is_visible ? pxr::UsdGeomTokens->inherited :
pxr::UsdGeomTokens->invisible;
usd_value_writer_.SetAttribute(attr_visibility, pxr::VtValue(visibility), timecode);
}
} // namespace usd
} // namespace io
} // namespace blender

View File

@ -72,6 +72,10 @@ class USDAbstractWriter : public AbstractHierarchyWriter {
pxr::UsdTimeCode get_export_time_code() const;
pxr::UsdShadeMaterial ensure_usd_material(Material *material);
void write_visibility(const HierarchyContext &context,
const pxr::UsdTimeCode timecode,
pxr::UsdGeomImageable &usd_geometry);
};
} // namespace usd

View File

@ -42,6 +42,8 @@
#include "DNA_object_fluidsim_types.h"
#include "DNA_particle_types.h"
#include <iostream>
namespace blender {
namespace io {
namespace usd {
@ -52,7 +54,10 @@ USDGenericMeshWriter::USDGenericMeshWriter(const USDExporterContext &ctx) : USDA
bool USDGenericMeshWriter::is_supported(const HierarchyContext *context) const
{
return context->is_object_visible(usd_export_context_.export_params.evaluation_mode);
if (usd_export_context_.export_params.visible_objects_only) {
return context->is_object_visible(usd_export_context_.export_params.evaluation_mode);
}
return true;
}
void USDGenericMeshWriter::do_write(HierarchyContext &context)
@ -149,6 +154,8 @@ void USDGenericMeshWriter::write_mesh(HierarchyContext &context, Mesh *mesh)
const pxr::SdfPath &usd_path = usd_export_context_.usd_path;
pxr::UsdGeomMesh usd_mesh = pxr::UsdGeomMesh::Define(stage, usd_path);
write_visibility(context, timecode, usd_mesh);
USDMeshData usd_mesh_data;
get_geometry_data(mesh, usd_mesh_data);

View File

@ -35,6 +35,7 @@ struct USDExportParams {
bool export_normals;
bool export_materials;
bool selected_objects_only;
bool visible_objects_only;
bool use_instancing;
enum eEvaluationMode evaluation_mode;
};