Blender  V3.3
usd_hierarchy_iterator.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2019 Blender Foundation. All rights reserved. */
3 #include "usd.h"
4 
6 #include "usd_writer_abstract.h"
7 #include "usd_writer_camera.h"
8 #include "usd_writer_hair.h"
9 #include "usd_writer_light.h"
10 #include "usd_writer_mesh.h"
11 #include "usd_writer_metaball.h"
12 #include "usd_writer_transform.h"
13 #include "usd_writer_volume.h"
14 
15 #include <string>
16 
17 #include <pxr/base/tf/stringUtils.h>
18 
19 #include "BKE_duplilist.h"
20 
21 #include "BLI_assert.h"
22 #include "BLI_utildefines.h"
23 
24 #include "DEG_depsgraph_query.h"
25 
26 #include "DNA_ID.h"
27 #include "DNA_layer_types.h"
28 #include "DNA_object_types.h"
29 
30 namespace blender::io::usd {
31 
34  pxr::UsdStageRefPtr stage,
35  const USDExportParams &params)
36  : AbstractHierarchyIterator(bmain, depsgraph), stage_(stage), params_(params)
37 {
38 }
39 
41 {
42  if (params_.selected_objects_only && (object->base_flag & BASE_SELECTED) == 0) {
43  return true;
44  }
45  return false;
46 }
47 
49 {
50  delete static_cast<USDAbstractWriter *>(writer);
51 }
52 
53 std::string USDHierarchyIterator::make_valid_name(const std::string &name) const
54 {
55  return pxr::TfMakeValidIdentifier(name);
56 }
57 
59 {
60  /* The USD stage is already set up to have FPS time-codes per frame. */
61  export_time_ = pxr::UsdTimeCode(frame_nr);
62 }
63 
65 {
66  /* Returns the same path that was passed to `stage_` object during it's creation (via
67  * `pxr::UsdStage::CreateNew` function). */
68  const pxr::SdfLayerHandle root_layer = stage_->GetRootLayer();
69  const std::string usd_export_file_path = root_layer->GetRealPath();
70  return usd_export_file_path;
71 }
72 
73 const pxr::UsdTimeCode &USDHierarchyIterator::get_export_time_code() const
74 {
75  return export_time_;
76 }
77 
78 USDExporterContext USDHierarchyIterator::create_usd_export_context(const HierarchyContext *context)
79 {
80  return USDExporterContext{
81  bmain_, depsgraph_, stage_, pxr::SdfPath(context->export_path), this, params_};
82 }
83 
86 {
87  return new USDTransformWriter(create_usd_export_context(context));
88 }
89 
91 {
92  USDExporterContext usd_export_context = create_usd_export_context(context);
93  USDAbstractWriter *data_writer = nullptr;
94 
95  switch (context->object->type) {
96  case OB_MESH:
97  data_writer = new USDMeshWriter(usd_export_context);
98  break;
99  case OB_CAMERA:
100  data_writer = new USDCameraWriter(usd_export_context);
101  break;
102  case OB_LAMP:
103  data_writer = new USDLightWriter(usd_export_context);
104  break;
105  case OB_MBALL:
106  data_writer = new USDMetaballWriter(usd_export_context);
107  break;
108  case OB_VOLUME:
109  data_writer = new USDVolumeWriter(usd_export_context);
110  break;
111 
112  case OB_EMPTY:
113  case OB_CURVES_LEGACY:
114  case OB_SURF:
115  case OB_FONT:
116  case OB_SPEAKER:
117  case OB_LIGHTPROBE:
118  case OB_LATTICE:
119  case OB_ARMATURE:
120  case OB_GPENCIL:
121  return nullptr;
122  case OB_TYPE_MAX:
123  BLI_assert_msg(0, "OB_TYPE_MAX should not be used");
124  return nullptr;
125  }
126 
127  if (!data_writer->is_supported(context)) {
128  delete data_writer;
129  return nullptr;
130  }
131 
132  return data_writer;
133 }
134 
136 {
137  if (!params_.export_hair) {
138  return nullptr;
139  }
140  return new USDHairWriter(create_usd_export_context(context));
141 }
142 
145 {
146  return nullptr;
147 }
148 
149 } // namespace blender::io::usd
#define BLI_assert_msg(a, msg)
Definition: BLI_assert.h:53
#define UNUSED(x)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
ID and Library types, which are fundamental for sdna.
@ BASE_SELECTED
Object is a sort of wrapper for general info.
@ OB_SPEAKER
@ OB_LATTICE
@ OB_MBALL
@ OB_EMPTY
@ OB_SURF
@ OB_CAMERA
@ OB_FONT
@ OB_TYPE_MAX
@ OB_ARMATURE
@ OB_LAMP
@ OB_MESH
@ OB_VOLUME
@ OB_CURVES_LEGACY
@ OB_GPENCIL
@ OB_LIGHTPROBE
virtual bool is_supported(const HierarchyContext *context) const
virtual bool mark_as_weak_export(const Object *object) const override
virtual AbstractHierarchyWriter * create_data_writer(const HierarchyContext *context) override
virtual void release_writer(AbstractHierarchyWriter *writer) override
virtual AbstractHierarchyWriter * create_particle_writer(const HierarchyContext *context) override
const pxr::UsdTimeCode & get_export_time_code() const
USDHierarchyIterator(Main *bmain, Depsgraph *depsgraph, pxr::UsdStageRefPtr stage, const USDExportParams &params)
virtual AbstractHierarchyWriter * create_hair_writer(const HierarchyContext *context) override
virtual std::string make_valid_name(const std::string &name) const override
virtual AbstractHierarchyWriter * create_transform_writer(const HierarchyContext *context) override
EvaluationStage stage
Definition: deg_eval.cc:89
const Depsgraph * depsgraph
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
Definition: BKE_main.h:121
short base_flag
bool selected_objects_only
Definition: usd.h:31
bool export_hair
Definition: usd.h:27