Blender  V2.93
usd_hierarchy_iterator.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2019 Blender Foundation.
17  * All rights reserved.
18  */
19 #include "usd.h"
20 
21 #include "usd_hierarchy_iterator.h"
22 #include "usd_writer_abstract.h"
23 #include "usd_writer_camera.h"
24 #include "usd_writer_hair.h"
25 #include "usd_writer_light.h"
26 #include "usd_writer_mesh.h"
27 #include "usd_writer_metaball.h"
28 #include "usd_writer_transform.h"
29 
30 #include <string>
31 
32 #include <pxr/base/tf/stringUtils.h>
33 
34 #include "BKE_duplilist.h"
35 
36 #include "BLI_assert.h"
37 #include "BLI_utildefines.h"
38 
39 #include "DEG_depsgraph_query.h"
40 
41 #include "DNA_ID.h"
42 #include "DNA_layer_types.h"
43 #include "DNA_object_types.h"
44 
45 namespace blender::io::usd {
46 
48  pxr::UsdStageRefPtr stage,
49  const USDExportParams &params)
50  : AbstractHierarchyIterator(depsgraph), stage_(stage), params_(params)
51 {
52 }
53 
55 {
56  if (params_.selected_objects_only && (object->base_flag & BASE_SELECTED) == 0) {
57  return true;
58  }
59  return false;
60 }
61 
63 {
64  delete static_cast<USDAbstractWriter *>(writer);
65 }
66 
67 std::string USDHierarchyIterator::make_valid_name(const std::string &name) const
68 {
69  return pxr::TfMakeValidIdentifier(name);
70 }
71 
73 {
74  /* The USD stage is already set up to have FPS time-codes per frame. */
75  export_time_ = pxr::UsdTimeCode(frame_nr);
76 }
77 
78 const pxr::UsdTimeCode &USDHierarchyIterator::get_export_time_code() const
79 {
80  return export_time_;
81 }
82 
83 USDExporterContext USDHierarchyIterator::create_usd_export_context(const HierarchyContext *context)
84 {
85  return USDExporterContext{depsgraph_, stage_, pxr::SdfPath(context->export_path), this, params_};
86 }
87 
90 {
91  return new USDTransformWriter(create_usd_export_context(context));
92 }
93 
95 {
96  USDExporterContext usd_export_context = create_usd_export_context(context);
97  USDAbstractWriter *data_writer = nullptr;
98 
99  switch (context->object->type) {
100  case OB_MESH:
101  data_writer = new USDMeshWriter(usd_export_context);
102  break;
103  case OB_CAMERA:
104  data_writer = new USDCameraWriter(usd_export_context);
105  break;
106  case OB_LAMP:
107  data_writer = new USDLightWriter(usd_export_context);
108  break;
109  case OB_MBALL:
110  data_writer = new USDMetaballWriter(usd_export_context);
111  break;
112 
113  case OB_EMPTY:
114  case OB_CURVE:
115  case OB_SURF:
116  case OB_FONT:
117  case OB_SPEAKER:
118  case OB_LIGHTPROBE:
119  case OB_LATTICE:
120  case OB_ARMATURE:
121  case OB_GPENCIL:
122  return nullptr;
123  case OB_TYPE_MAX:
124  BLI_assert(!"OB_TYPE_MAX should not be used");
125  return nullptr;
126  }
127 
128  if (!data_writer->is_supported(context)) {
129  delete data_writer;
130  return nullptr;
131  }
132 
133  return data_writer;
134 }
135 
137 {
138  if (!params_.export_hair) {
139  return nullptr;
140  }
141  return new USDHairWriter(create_usd_export_context(context));
142 }
143 
146 {
147  return nullptr;
148 }
149 
150 } // namespace blender::io::usd
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define UNUSED(x)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
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_CURVE
@ 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
USDHierarchyIterator(Depsgraph *depsgraph, pxr::UsdStageRefPtr stage, const USDExportParams &params)
virtual void release_writer(AbstractHierarchyWriter *writer) override
virtual AbstractHierarchyWriter * create_particle_writer(const HierarchyContext *context) override
const pxr::UsdTimeCode & get_export_time_code() const
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:96
const Depsgraph * depsgraph
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
struct SELECTID_Context context
Definition: select_engine.c:47
short base_flag
bool selected_objects_only
Definition: usd.h:36
bool export_hair
Definition: usd.h:32