Blender V4.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"
20#include "usd_writer_points.hh"
22#include "usd_writer_volume.hh"
23
24#include <string>
25
26#include "BKE_main.hh"
27
28#include "BLI_assert.h"
29
30#include "DNA_layer_types.h"
31#include "DNA_object_types.h"
32
33namespace blender::io::usd {
34
36 Depsgraph *depsgraph,
37 pxr::UsdStageRefPtr stage,
39 : AbstractHierarchyIterator(bmain, depsgraph), stage_(stage), params_(params)
40{
41}
42
44{
45 if (params_.selected_objects_only && (object->base_flag & BASE_SELECTED) == 0) {
46 return true;
47 }
48
49 switch (object->type) {
50 case OB_EMPTY:
51 /* Always assume empties are being exported intentionally. */
52 return false;
53 case OB_MESH:
54 case OB_MBALL:
55 return !params_.export_meshes;
56 case OB_CAMERA:
57 return !params_.export_cameras;
58 case OB_LAMP:
59 return !params_.export_lights;
61 case OB_CURVES:
62 return !params_.export_curves;
63 case OB_VOLUME:
64 return !params_.export_volumes;
65 case OB_ARMATURE:
66 return !params_.export_armatures;
67 case OB_POINTCLOUD:
68 return !params_.export_points;
69
70 default:
71 /* Assume weak for all other types. */
72 return true;
73 }
74}
75
77{
78 delete static_cast<USDAbstractWriter *>(writer);
79}
80
81std::string USDHierarchyIterator::make_valid_name(const std::string &name) const
82{
83 return make_safe_name(name, params_.allow_unicode);
84}
85
87{
88 skel_export_chaser(stage_,
89 armature_export_map_,
90 skinned_mesh_export_map_,
91 shape_key_mesh_export_map_,
93
94 create_skel_roots(stage_, params_);
95}
96
98{
99 /* The USD stage is already set up to have FPS time-codes per frame. */
100 export_time_ = pxr::UsdTimeCode(frame_nr);
101}
102
103USDExporterContext USDHierarchyIterator::create_usd_export_context(const HierarchyContext *context)
104{
105 pxr::SdfPath path;
106 if (params_.root_prim_path[0] != '\0') {
107 path = pxr::SdfPath(params_.root_prim_path + context->export_path);
108 }
109 else {
110 path = pxr::SdfPath(context->export_path);
111 }
112
113 /* Returns the same path that was passed to `stage_` object during it's creation (via
114 * `pxr::UsdStage::CreateNew` function). */
115 const pxr::SdfLayerHandle root_layer = stage_->GetRootLayer();
116 const std::string export_file_path = root_layer->GetRealPath();
117 auto get_time_code = [this]() { return this->export_time_; };
118
119 return USDExporterContext{
120 bmain_, depsgraph_, stage_, path, get_time_code, params_, export_file_path};
121}
122
124 const HierarchyContext *context)
125{
126 return new USDTransformWriter(create_usd_export_context(context));
127}
128
130{
131 USDExporterContext usd_export_context = create_usd_export_context(context);
132 USDAbstractWriter *data_writer = nullptr;
133
134 switch (context->object->type) {
135 case OB_MESH:
136 if (usd_export_context.export_params.export_meshes) {
137 data_writer = new USDMeshWriter(usd_export_context);
138 }
139 else {
140 return nullptr;
141 }
142 break;
143 case OB_CAMERA:
144 if (usd_export_context.export_params.export_cameras) {
145 data_writer = new USDCameraWriter(usd_export_context);
146 }
147 else {
148 return nullptr;
149 }
150 break;
151 case OB_LAMP:
152 if (usd_export_context.export_params.export_lights) {
153 data_writer = new USDLightWriter(usd_export_context);
154 }
155 else {
156 return nullptr;
157 }
158 break;
159 case OB_MBALL:
160 data_writer = new USDMetaballWriter(usd_export_context);
161 break;
162 case OB_CURVES_LEGACY:
163 case OB_CURVES:
164 if (usd_export_context.export_params.export_curves) {
165 data_writer = new USDCurvesWriter(usd_export_context);
166 }
167 else {
168 return nullptr;
169 }
170 break;
171 case OB_VOLUME:
172 if (usd_export_context.export_params.export_volumes) {
173 data_writer = new USDVolumeWriter(usd_export_context);
174 }
175 else {
176 return nullptr;
177 }
178 break;
179 case OB_ARMATURE:
180 if (usd_export_context.export_params.export_armatures) {
181 data_writer = new USDArmatureWriter(usd_export_context);
182 }
183 else {
184 return nullptr;
185 }
186 break;
187 case OB_POINTCLOUD:
188 if (usd_export_context.export_params.export_points) {
189 data_writer = new USDPointsWriter(usd_export_context);
190 }
191 else {
192 return nullptr;
193 }
194 break;
195
196 case OB_EMPTY:
197 case OB_SURF:
198 case OB_FONT:
199 case OB_SPEAKER:
200 case OB_LIGHTPROBE:
201 case OB_LATTICE:
203 case OB_GREASE_PENCIL:
204 return nullptr;
205
206 case OB_TYPE_MAX:
207 BLI_assert_msg(0, "OB_TYPE_MAX should not be used");
208 return nullptr;
209 default:
211 return nullptr;
212 }
213
214 if (!data_writer->is_supported(context)) {
215 delete data_writer;
216 return nullptr;
217 }
218
219 if (data_writer && (params_.export_armatures || params_.export_shapekeys)) {
220 add_usd_skel_export_mapping(context->object, data_writer->usd_path());
221 }
222
223 return data_writer;
224}
225
227{
228 if (!params_.export_hair) {
229 return nullptr;
230 }
231 return new USDHairWriter(create_usd_export_context(context));
232}
233
235 const HierarchyContext * /*context*/)
236{
237 return nullptr;
238}
239
240void USDHierarchyIterator::add_usd_skel_export_mapping(const Object *obj, const pxr::SdfPath &path)
241{
242 if (params_.export_shapekeys && is_mesh_with_shape_keys(obj)) {
243 shape_key_mesh_export_map_.add(obj, path);
244 }
245
246 if (params_.export_armatures && obj->type == OB_ARMATURE) {
247 armature_export_map_.add(obj, path);
248 }
249
250 if (params_.export_armatures && obj->type == OB_MESH &&
252 {
253 skinned_mesh_export_map_.add(obj, path);
254 }
255}
256
257} // namespace blender::io::usd
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:57
Object is a sort of wrapper for general info.
@ OB_SPEAKER
@ OB_LATTICE
@ OB_MBALL
@ OB_EMPTY
@ OB_SURF
@ OB_CAMERA
@ OB_FONT
@ OB_GREASE_PENCIL
@ OB_TYPE_MAX
@ OB_ARMATURE
@ OB_LAMP
@ OB_MESH
@ OB_POINTCLOUD
@ OB_VOLUME
@ OB_CURVES_LEGACY
@ OB_GPENCIL_LEGACY
@ OB_CURVES
@ OB_LIGHTPROBE
#define BASE_SELECTED(v3d, base)
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:271
const pxr::SdfPath & usd_path() const
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
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:83
const Depsgraph * depsgraph
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
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 std::string &name, bool allow_unicode)
Definition usd_utils.cc:16
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)