Welcome to mirror list, hosted at ThFree Co, Russian Federation.

abc_hierarchy_iterator.cc « exporter « alembic « io « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e2be241c144355631d6bf7cddb1c87860cdde964 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2020 Blender Foundation.
 * All rights reserved.
 */

#include "abc_hierarchy_iterator.h"
#include "abc_writer_abstract.h"
#include "abc_writer_camera.h"
#include "abc_writer_curves.h"
#include "abc_writer_hair.h"
#include "abc_writer_instance.h"
#include "abc_writer_mball.h"
#include "abc_writer_mesh.h"
#include "abc_writer_nurbs.h"
#include "abc_writer_points.h"
#include "abc_writer_transform.h"

#include <memory>
#include <string>

#include "BLI_assert.h"

#include "DEG_depsgraph_query.h"

#include "DNA_ID.h"
#include "DNA_layer_types.h"
#include "DNA_object_types.h"

namespace blender::io::alembic {

ABCHierarchyIterator::ABCHierarchyIterator(Depsgraph *depsgraph,
                                           ABCArchive *abc_archive,
                                           const AlembicExportParams &params)
    : AbstractHierarchyIterator(depsgraph), abc_archive_(abc_archive), params_(params)
{
}

void ABCHierarchyIterator::iterate_and_write()
{
  AbstractHierarchyIterator::iterate_and_write();
  update_archive_bounding_box();
}

void ABCHierarchyIterator::update_archive_bounding_box()
{
  Imath::Box3d bounds;
  update_bounding_box_recursive(bounds, HierarchyContext::root());
  abc_archive_->update_bounding_box(bounds);
}

void ABCHierarchyIterator::update_bounding_box_recursive(Imath::Box3d &bounds,
                                                         const HierarchyContext *context)
{
  if (context != nullptr) {
    AbstractHierarchyWriter *abstract_writer = writers_[context->export_path];
    ABCAbstractWriter *abc_writer = static_cast<ABCAbstractWriter *>(abstract_writer);

    if (abc_writer != nullptr) {
      bounds.extendBy(abc_writer->bounding_box());
    }
  }

  for (HierarchyContext *child_context : graph_children(context)) {
    update_bounding_box_recursive(bounds, child_context);
  }
}

bool ABCHierarchyIterator::mark_as_weak_export(const Object *object) const
{
  if (params_.selected_only && (object->base_flag & BASE_SELECTED) == 0) {
    return true;
  }
  /* TODO(Sybren): handle other flags too? */
  return false;
}

void ABCHierarchyIterator::release_writer(AbstractHierarchyWriter *writer)
{
  delete writer;
}

std::string ABCHierarchyIterator::make_valid_name(const std::string &name) const
{
  std::string abc_name(name);
  std::replace(abc_name.begin(), abc_name.end(), ' ', '_');
  std::replace(abc_name.begin(), abc_name.end(), '.', '_');
  std::replace(abc_name.begin(), abc_name.end(), ':', '_');
  return abc_name;
}

AbstractHierarchyIterator::ExportGraph::key_type ABCHierarchyIterator::
    determine_graph_index_object(const HierarchyContext *context)
{
  if (params_.flatten_hierarchy) {
    return ObjectIdentifier::for_graph_root();
  }

  return AbstractHierarchyIterator::determine_graph_index_object(context);
}

AbstractHierarchyIterator::ExportGraph::key_type ABCHierarchyIterator::determine_graph_index_dupli(
    const HierarchyContext *context,
    const DupliObject *dupli_object,
    const DupliParentFinder &dupli_parent_finder)
{
  if (params_.flatten_hierarchy) {
    return ObjectIdentifier::for_graph_root();
  }

  return AbstractHierarchyIterator::determine_graph_index_dupli(
      context, dupli_object, dupli_parent_finder);
}

Alembic::Abc::OObject ABCHierarchyIterator::get_alembic_object(
    const std::string &export_path) const
{
  if (export_path.empty()) {
    return Alembic::Abc::OObject();
  }

  AbstractHierarchyWriter *writer = get_writer(export_path);
  if (writer == nullptr) {
    return Alembic::Abc::OObject();
  }

  ABCAbstractWriter *abc_writer = static_cast<ABCAbstractWriter *>(writer);
  return abc_writer->get_alembic_object();
}

Alembic::Abc::OObject ABCHierarchyIterator::get_alembic_parent(
    const HierarchyContext *context) const
{
  Alembic::Abc::OObject parent = get_alembic_object(context->higher_up_export_path);

  if (!parent.valid()) {
    /* An invalid parent object means "no parent", which should be translated to Alembic's top
     * archive object. */
    return abc_archive_->archive->getTop();
  }

  return parent;
}

ABCWriterConstructorArgs ABCHierarchyIterator::writer_constructor_args(
    const HierarchyContext *context) const
{
  ABCWriterConstructorArgs constructor_args;
  constructor_args.depsgraph = depsgraph_;
  constructor_args.abc_archive = abc_archive_;
  constructor_args.abc_parent = get_alembic_parent(context);
  constructor_args.abc_name = context->export_name;
  constructor_args.abc_path = context->export_path;
  constructor_args.hierarchy_iterator = this;
  constructor_args.export_params = &params_;
  return constructor_args;
}

AbstractHierarchyWriter *ABCHierarchyIterator::create_transform_writer(
    const HierarchyContext *context)
{
  ABCAbstractWriter *transform_writer = new ABCTransformWriter(writer_constructor_args(context));
  transform_writer->create_alembic_objects(context);
  return transform_writer;
}

AbstractHierarchyWriter *ABCHierarchyIterator::create_data_writer(const HierarchyContext *context)
{
  const ABCWriterConstructorArgs writer_args = writer_constructor_args(context);
  ABCAbstractWriter *data_writer = nullptr;

  if (params_.use_instancing && context->is_instance()) {
    data_writer = new ABCInstanceWriter(writer_args);
  }
  else {
    data_writer = create_data_writer_for_object_type(context, writer_args);
  }

  if (data_writer == nullptr || !data_writer->is_supported(context)) {
    delete data_writer;
    return nullptr;
  }

  data_writer->create_alembic_objects(context);
  return data_writer;
}

ABCAbstractWriter *ABCHierarchyIterator::create_data_writer_for_object_type(
    const HierarchyContext *context, const ABCWriterConstructorArgs &writer_args)
{
  switch (context->object->type) {
    case OB_MESH:
      return new ABCMeshWriter(writer_args);
    case OB_CAMERA:
      return new ABCCameraWriter(writer_args);
    case OB_CURVE:
      if (params_.curves_as_mesh) {
        return new ABCCurveMeshWriter(writer_args);
      }
      return new ABCCurveWriter(writer_args);
    case OB_SURF:
      if (params_.curves_as_mesh) {
        return new ABCCurveMeshWriter(writer_args);
      }
      return new ABCNurbsWriter(writer_args);
    case OB_MBALL:
      return new ABCMetaballWriter(writer_args);

    case OB_EMPTY:
    case OB_LAMP:
    case OB_FONT:
    case OB_SPEAKER:
    case OB_LIGHTPROBE:
    case OB_LATTICE:
    case OB_ARMATURE:
    case OB_GPENCIL:
      return nullptr;
    case OB_TYPE_MAX:
      BLI_assert(!"OB_TYPE_MAX should not be used");
      return nullptr;
  }

  /* Just to please the compiler, all cases should be handled by the above switch. */
  return nullptr;
}

AbstractHierarchyWriter *ABCHierarchyIterator::create_hair_writer(const HierarchyContext *context)
{
  if (!params_.export_hair) {
    return nullptr;
  }

  const ABCWriterConstructorArgs writer_args = writer_constructor_args(context);
  ABCAbstractWriter *hair_writer = new ABCHairWriter(writer_args);

  if (!hair_writer->is_supported(context)) {
    delete hair_writer;
    return nullptr;
  }

  hair_writer->create_alembic_objects(context);
  return hair_writer;
}

AbstractHierarchyWriter *ABCHierarchyIterator::create_particle_writer(
    const HierarchyContext *context)
{
  if (!params_.export_particles) {
    return nullptr;
  }

  const ABCWriterConstructorArgs writer_args = writer_constructor_args(context);
  std::unique_ptr<ABCPointsWriter> particle_writer(std::make_unique<ABCPointsWriter>(writer_args));

  if (!particle_writer->is_supported(context)) {
    return nullptr;
  }

  particle_writer->create_alembic_objects(context);
  return particle_writer.release();
}

}  // namespace blender::io::alembic