Blender V5.3
usd_hierarchy_iterator.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4#include "usd.hh"
5
9#include "usd_skel_convert.hh"
11#include "usd_utils.hh"
14#include "usd_writer_camera.hh"
15#include "usd_writer_curves.hh"
16#include "usd_writer_hair.hh"
17#include "usd_writer_light.hh"
18#include "usd_writer_mesh.hh"
21#include "usd_writer_points.hh"
22#include "usd_writer_text.hh"
24#include "usd_writer_volume.hh"
25
26#include <string>
27
28#include "BKE_lib_id.hh"
29#include "BKE_main.hh"
30#include "BKE_report.hh"
31
32#include "BLI_assert.hh"
33
34#include "DNA_layer_types.h"
35#include "DNA_object_types.h"
36
37#include "WM_types.hh"
38
39namespace blender::io::usd {
40
42 Depsgraph *depsgraph,
43 pxr::UsdStageRefPtr stage,
44 const USDExportParams &params)
45 : AbstractHierarchyIterator(bmain, depsgraph), stage_(stage), params_(params)
46{
47}
48
50{
51 if (params_.selected_objects_only && (object->base_flag & BASE_SELECTED) == 0) {
52 return true;
53 }
54
55 switch (object->type) {
56 case OB_EMPTY:
57 /* Always assume empties are being exported intentionally. */
58 return false;
59 case OB_MESH:
60 case OB_MBALL:
61 case OB_FONT:
62 case OB_SURF:
63 return !params_.export_meshes;
64 case OB_CAMERA:
65 return !params_.export_cameras;
66 case OB_LAMP:
67 return !params_.export_lights;
69 case OB_CURVES:
70 return !params_.export_curves;
71 case OB_VOLUME:
72 return !params_.export_volumes;
73 case OB_ARMATURE:
74 return !params_.export_armatures;
75 case OB_POINTCLOUD:
76 return !params_.export_points;
77
78 default:
79 /* Assume weak for all other types. */
80 return true;
81 }
82}
83
85{
86 delete static_cast<USDAbstractWriter *>(writer);
87}
88
89std::string USDHierarchyIterator::make_valid_name(const std::string &name) const
90{
91 return make_safe_name(name, params_.allow_unicode);
92}
93
95{
96 skel_export_chaser(stage_,
97 armature_export_map_,
98 skinned_mesh_export_map_,
99 shape_key_mesh_export_map_,
100 depsgraph_);
101
102 create_skel_roots(stage_, params_);
103}
104
106{
107 /* The USD stage is already set up to have FPS time-codes per frame. */
108 export_time_ = pxr::UsdTimeCode(frame_nr);
109}
110
111USDExporterContext USDHierarchyIterator::create_usd_export_context(const HierarchyContext *context)
112{
113 pxr::SdfPath path;
114 if (!params_.root_prim_path.empty()) {
115 path = pxr::SdfPath(params_.root_prim_path + context->export_path);
116 }
117 else {
118 path = pxr::SdfPath(context->export_path);
119 }
120
121 if (params_.merge_parent_xform && context->is_object_data_context && !context->is_parent) {
122 bool can_merge_with_xform = true;
123 if (params_.export_shapekeys && is_mesh_with_shape_keys(context->object)) {
124 can_merge_with_xform = false;
125 }
126
127 if (params_.use_instancing && (context->is_prototype() || context->is_instance())) {
128 can_merge_with_xform = false;
129 }
130
131 if (can_merge_with_xform) {
132 path = path.GetParentPath();
133 }
134 }
135
136 /* Returns the same path that was passed to `stage_` object during it's creation (via
137 * `pxr::UsdStage::CreateNew` function). */
138 const pxr::SdfLayerHandle root_layer = stage_->GetRootLayer();
139 const std::string export_file_path = root_layer->GetRealPath();
140 auto get_time_code = [this]() { return this->export_time_; };
141
142 USDExporterContext exporter_context = USDExporterContext{bmain_,
144 stage_,
145 path,
146 get_time_code,
147 params_,
148 export_file_path,
149 nullptr,
150 nullptr,
151 this};
152
153 /* Provides optional skel mapping hook. Now it's been used in USDPointInstancerWriter for write
154 * base layer. */
155 exporter_context.add_skel_mapping_fn = [this](const Object *obj, const pxr::SdfPath &usd_path) {
156 this->add_usd_skel_export_mapping(obj, usd_path);
157 };
158
159 return exporter_context;
160}
161
163{
164 if (!context) {
165 return true;
166 }
167
168 if (context->object->type == OB_ARMATURE) {
169 return true;
170 }
171
172 bool is_referencing_self = false;
173 if (context->is_point_instancer()) {
174 /* Mark the point instancer's children as a point instance. */
175 USDExporterContext usd_export_context = create_usd_export_context(context);
176 const ExportChildren *children = graph_children(context);
177
178 pxr::SdfPath instancer_path;
179 if (!params_.root_prim_path.empty()) {
180 instancer_path = pxr::SdfPath(params_.root_prim_path + context->export_path);
181 }
182 else {
183 instancer_path = pxr::SdfPath(context->export_path);
184 }
185
186 if (children != nullptr) {
187 for (HierarchyContext *child_context : *children) {
188 if (!child_context->original_export_path.empty()) {
189 const pxr::SdfPath parent_export_path(context->export_path);
190 const pxr::SdfPath children_original_export_path(child_context->original_export_path);
191
192 /* Detect if the parent is referencing itself via a prototype. */
193 if (parent_export_path.HasPrefix(children_original_export_path)) {
194 is_referencing_self = true;
195 break;
196 }
197 }
198
199 pxr::SdfPath prototype_path;
200 if (child_context->is_instance() && child_context->duplicator != nullptr) {
201 /* When the current child context is point instancer's instance, use reference path
202 * (original_export_path) as the prototype path. */
203 if (!params_.root_prim_path.empty()) {
204 prototype_path = pxr::SdfPath(params_.root_prim_path +
205 child_context->original_export_path);
206 }
207 else {
208 prototype_path = pxr::SdfPath(child_context->original_export_path);
209 }
210
211 prototype_paths_.lookup_or_add(instancer_path, {})
212 .add(std::make_pair(prototype_path, child_context->object));
213 child_context->is_point_instance = true;
214 }
215 else {
216 /* When the current child context is point instancer's prototype, use its own export path
217 * (export_path) as the prototype path. */
218 if (!params_.root_prim_path.empty()) {
219 prototype_path = pxr::SdfPath(params_.root_prim_path + child_context->export_path);
220 }
221 else {
222 prototype_path = pxr::SdfPath(child_context->export_path);
223 }
224
225 prototype_paths_.lookup_or_add(instancer_path, {})
226 .add(std::make_pair(prototype_path, child_context->object));
227 child_context->is_point_proto = true;
228 }
229 }
230 }
231
232 /* MARK: If the "Instance on Points" node uses an Object as a prototype,
233 * but the "Object Info" node has not enabled the "As Instance" option,
234 * then the generated reference path is incorrect and refers to itself. */
235 if (is_referencing_self) {
237 params_.worker_status->reports,
239 "One or more objects used as prototypes in 'Instance on Points' nodes either do not "
240 "have 'As Instance' enabled in their 'Object Info' nodes, or the prototype is the "
241 "base geometry input itself. Both cases prevent valid point instancer export. If it's "
242 "the former, enable 'As Instance' to avoid incorrect self-referencing.");
243
244 /* Clear any paths which had already been accumulated. */
245 Set<std::pair<pxr::SdfPath, Object *>> *paths = prototype_paths_.lookup_ptr(instancer_path);
246 if (paths) {
247 paths->clear();
248 }
249 for (HierarchyContext *child_context : *children) {
250 child_context->is_point_instance = false;
251 child_context->is_point_proto = false;
252 }
253 }
254 }
255
256 return !is_referencing_self;
257}
258
260 const HierarchyContext *context)
261{
262 /* The transform writer is always called before data writers,
263 * so determine if the #Xform's children is a point instancer before writing data. */
264 if (params_.use_instancing) {
265 if (!determine_point_instancers(context)) {
266 /* If we could not determine that our point instancing setup is safe, we should not continue
267 * writing. Continuing would result in enormous amounts of USD warnings about cyclic
268 * references. */
269 return nullptr;
270 }
271 }
272
273 return new USDTransformWriter(create_usd_export_context(context));
274}
275
277{
278 USDExporterContext usd_export_context = create_usd_export_context(context);
279 USDAbstractWriter *data_writer = nullptr;
280 const Set<std::pair<pxr::SdfPath, Object *>> *proto_paths = prototype_paths_.lookup_ptr(
281 usd_export_context.usd_path.GetParentPath());
282 const bool use_point_instancing = context->is_point_instancer() &&
283 (proto_paths && !proto_paths->is_empty());
284
285 switch (context->object->type) {
286 case OB_MESH:
287 case OB_SURF:
288 /* NURBS surfaces are tessellated to a mesh during depsgraph evaluation,
289 * so they can be written through the standard mesh writer. */
290 if (usd_export_context.export_params.export_meshes) {
291 if (params_.use_instancing && use_point_instancing) {
292 USDExporterContext mesh_context = create_point_instancer_context(context,
293 usd_export_context);
294 std::unique_ptr<USDMeshWriter> mesh_writer = std::make_unique<USDMeshWriter>(
295 mesh_context);
296
297 data_writer = new USDPointInstancerWriter(
298 usd_export_context, *proto_paths, std::move(mesh_writer));
299 }
300 else {
301 data_writer = new USDMeshWriter(usd_export_context);
302 }
303 }
304 else {
305 return nullptr;
306 }
307 break;
308 case OB_CAMERA:
309 if (usd_export_context.export_params.export_cameras) {
310 data_writer = new USDCameraWriter(usd_export_context);
311 }
312 else {
313 return nullptr;
314 }
315 break;
316 case OB_LAMP:
317 if (usd_export_context.export_params.export_lights) {
318 data_writer = new USDLightWriter(usd_export_context);
319 }
320 else {
321 return nullptr;
322 }
323 break;
324 case OB_MBALL:
325 data_writer = new USDMetaballWriter(usd_export_context);
326 break;
327 case OB_FONT:
328 data_writer = new USDTextWriter(usd_export_context);
329 break;
330 case OB_CURVES_LEGACY:
331 case OB_CURVES:
332 if (usd_export_context.export_params.export_curves) {
333 if (params_.use_instancing && use_point_instancing) {
334 USDExporterContext curves_context = create_point_instancer_context(context,
335 usd_export_context);
336 std::unique_ptr<USDCurvesWriter> curves_writer = std::make_unique<USDCurvesWriter>(
337 curves_context);
338
339 data_writer = new USDPointInstancerWriter(
340 usd_export_context, *proto_paths, std::move(curves_writer));
341 }
342 else {
343 data_writer = new USDCurvesWriter(usd_export_context);
344 }
345 }
346 else {
347 return nullptr;
348 }
349 break;
350 case OB_VOLUME:
351 if (usd_export_context.export_params.export_volumes) {
352 data_writer = new USDVolumeWriter(usd_export_context);
353 }
354 else {
355 return nullptr;
356 }
357 break;
358 case OB_ARMATURE:
359 if (usd_export_context.export_params.export_armatures) {
360 data_writer = new USDArmatureWriter(usd_export_context);
361 }
362 else {
363 return nullptr;
364 }
365 break;
366 case OB_POINTCLOUD:
367 if (usd_export_context.export_params.export_points) {
368 if (params_.use_instancing && use_point_instancing) {
369 USDExporterContext point_cloud_context = create_point_instancer_context(
370 context, usd_export_context);
371 std::unique_ptr<USDPointsWriter> point_cloud_writer = std::make_unique<USDPointsWriter>(
372 point_cloud_context);
373
374 data_writer = new USDPointInstancerWriter(
375 usd_export_context, *proto_paths, std::move(point_cloud_writer));
376 }
377 else {
378 data_writer = new USDPointsWriter(usd_export_context);
379 }
380 }
381 else {
382 return nullptr;
383 }
384 break;
385
386 case OB_EMPTY:
387 case OB_SPEAKER:
388 case OB_LIGHTPROBE:
389 case OB_LATTICE:
390 case OB_GREASE_PENCIL:
391 return nullptr;
392
393 case OB_TYPE_MAX:
394 BLI_assert_msg(0, "OB_TYPE_MAX should not be used");
395 return nullptr;
396 default:
398 return nullptr;
399 }
400
401 if (data_writer && !data_writer->is_supported(context)) {
402 delete data_writer;
403 return nullptr;
404 }
405
406 if (data_writer && (params_.export_armatures || params_.export_shapekeys)) {
407 add_usd_skel_export_mapping(context->object, data_writer->usd_path());
408 }
409
410 return data_writer;
411}
412
414{
415 if (!params_.export_hair) {
416 return nullptr;
417 }
418 return new USDHairWriter(create_usd_export_context(context));
419}
420
422 const HierarchyContext * /*context*/)
423{
424 return nullptr;
425}
426
428{
429 /* Don't generate data writers for instances. */
430
431 return !(params_.use_instancing && context->is_instance());
432}
433
435{
436 /* Don't generate writers for children of instances. */
437
438 return !(params_.use_instancing && context->is_instance());
439}
440
441void USDHierarchyIterator::add_usd_skel_export_mapping(const Object *obj, const pxr::SdfPath &path)
442{
444 shape_key_mesh_export_map_.add(obj, path);
445 }
446
447 if (params_.export_armatures && obj->type == OB_ARMATURE) {
448 armature_export_map_.add(obj, path);
449 }
450
451 if (params_.export_armatures && obj->type == OB_MESH &&
453 {
454 skinned_mesh_export_map_.add(obj, path);
455 }
456}
457
459{
460 return exported_prim_map_;
461}
462
463pxr::UsdStageRefPtr USDHierarchyIterator::get_stage() const
464{
465 return stage_;
466}
467
468void USDHierarchyIterator::add_to_prim_map(const pxr::SdfPath &usd_path, const ID *id) const
469{
470 if (!id) {
471 return;
472 }
473 ID *local_id = BKE_libblock_find_name(bmain_, GS(id->name), id->name + 2);
474 if (local_id) {
475 Vector<ID *> &id_list = exported_prim_map_.lookup_or_add_default(usd_path);
476 if (!id_list.contains(local_id)) {
477 id_list.append(local_id);
478 }
479 }
480}
481
482USDExporterContext USDHierarchyIterator::create_point_instancer_context(
483 const HierarchyContext *context, const USDExporterContext &export_context) const
484{
485 BLI_assert(context && context->object);
486 std::string base_name = std::string(BKE_id_name(context->object->id)).append("_base");
487 std::string safe_name = make_safe_name(base_name, export_context.export_params.allow_unicode);
488
489 pxr::SdfPath base_path = export_context.usd_path.GetParentPath().AppendChild(
490 pxr::TfToken(safe_name));
491
492 return {export_context.bmain,
493 export_context.depsgraph,
494 export_context.stage,
495 base_path,
496 export_context.get_time_code,
497 export_context.export_params,
498 export_context.export_file_path,
499 export_context.export_image_fn,
500 export_context.add_skel_mapping_fn,
501 export_context.hierarchy_iterator};
502}
503
504} // namespace blender::io::usd
Object is a sort of wrapper for general info.
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
bool is_empty() const
Definition BLI_set.hh:614
void clear()
Definition BLI_set.hh:570
bool contains(const T &value) const
void append(const T &value)
ExportChildren * graph_children(const HierarchyContext *context)
AbstractHierarchyIterator(Main *bmain, Depsgraph *depsgraph)
const pxr::SdfPath & usd_path() const
virtual bool is_supported(const HierarchyContext *context) const
USDArmatureWriter(const USDExporterContext &ctx)
USDCameraWriter(const USDExporterContext &ctx)
USDCurvesWriter(const USDExporterContext &ctx)
USDHairWriter(const USDExporterContext &ctx)
bool mark_as_weak_export(const Object *object) const override
AbstractHierarchyWriter * create_data_writer(const HierarchyContext *context) override
void add_to_prim_map(const pxr::SdfPath &usd_path, const ID *id) const
void release_writer(AbstractHierarchyWriter *writer) override
bool determine_point_instancers(const HierarchyContext *context)
AbstractHierarchyWriter * create_particle_writer(const HierarchyContext *context) override
USDHierarchyIterator(Main *bmain, Depsgraph *depsgraph, pxr::UsdStageRefPtr stage, const USDExportParams &params)
const Map< pxr::SdfPath, Vector< ID * > > & get_exported_prim_map() const
AbstractHierarchyWriter * create_hair_writer(const HierarchyContext *context) override
bool include_child_writers(const HierarchyContext *context) const override
bool include_data_writers(const HierarchyContext *context) const override
std::string make_valid_name(const std::string &name) const override
AbstractHierarchyWriter * create_transform_writer(const HierarchyContext *context) override
USDLightWriter(const USDExporterContext &ctx)
USDMeshWriter(const USDExporterContext &ctx)
USDMetaballWriter(const USDExporterContext &ctx)
USDPointInstancerWriter(const USDExporterContext &ctx, const Set< std::pair< pxr::SdfPath, Object * > > &prototype_paths, std::unique_ptr< USDAbstractWriter > base_writer)
USDPointsWriter(const USDExporterContext &ctx)
USDTextWriter(const USDExporterContext &ctx)
USDTransformWriter(const USDExporterContext &ctx)
USDVolumeWriter(const USDExporterContext &ctx)
void skel_export_chaser(pxr::UsdStageRefPtr stage, const ObjExportMap &armature_export_map, const ObjExportMap &skinned_mesh_export_map, const ObjExportMap &shape_key_mesh_export_map, const Depsgraph *depsgraph)
std::string make_safe_name(const StringRef name, bool allow_unicode)
Definition usd_utils.cc:83
void create_skel_roots(pxr::UsdStageRefPtr stage, const USDExportParams &params)
bool can_export_skinned_mesh(const Object &obj, const Depsgraph *depsgraph)
bool is_mesh_with_shape_keys(const Object *obj)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ID * BKE_libblock_find_name(Main *bmain, short type, const char *name, const std::optional< Library * > lib=std::nullopt) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition lib_id.cc:1808
@ RPT_WARNING
Definition BKE_report.hh:40
const char * BKE_id_name(const ID &id)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
ID_Type GS(const char *name)
Definition DNA_ID.h:410
std::function< pxr::UsdTimeCode()> get_time_code
std::function< std::string(Main *, Scene *, Image *, ImageUser *)> export_image_fn
std::function< void(const Object *, const pxr::SdfPath &)> add_skel_mapping_fn