Blender V4.3
IO_abstract_hierarchy_iterator.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5/*
6 * This file contains the AbstractHierarchyIterator. It is intended for exporters for file
7 * formats that concern an entire hierarchy of objects (rather than, for example, an OBJ file that
8 * contains only a single mesh). Examples are Universal Scene Description (USD) and Alembic.
9 * AbstractHierarchyIterator is intended to be subclassed to support concrete file formats.
10 *
11 * The AbstractHierarchyIterator makes a distinction between the actual object hierarchy and the
12 * export hierarchy. The former is the parent/child structure in Blender, which can have multiple
13 * parent-like objects. For example, a duplicated object can have both a duplicator and a parent,
14 * both determining the final transform. The export hierarchy is the hierarchy as written to the
15 * file, and every object has only one export-parent.
16 *
17 * Currently the AbstractHierarchyIterator does not make any decisions about *what* to export.
18 * Selections like "selected only" or "no hair systems" are left to concrete subclasses.
19 */
20
21#pragma once
22
24
25#include "DEG_depsgraph.hh"
26
27#include <map>
28#include <set>
29#include <string>
30
31struct Depsgraph;
32struct DupliObject;
33struct ID;
34struct Main;
35struct Object;
36struct ParticleSystem;
37
38namespace blender::io {
39
40class AbstractHierarchyWriter;
41class DupliParentFinder;
42
43/* HierarchyContext structs are created by the AbstractHierarchyIterator. Each HierarchyContext
44 * struct contains everything necessary to export a single object to a file. */
46 /*********** Determined during hierarchy iteration: ***************/
47 Object *object; /* Evaluated object. */
51 float matrix_world[4][4];
52 std::string export_name;
53
54 /* When weak_export=true, the object will be exported only as transform, and only if is an
55 * ancestor of an object with weak_export=false.
56 *
57 * In other words: when weak_export=true but this object has no children, or all descendants also
58 * have weak_export=true, this object (and by recursive reasoning all its descendants) will be
59 * excluded from the export.
60 *
61 * The export hierarchy is kept as close to the hierarchy in Blender as possible. As such, an
62 * object that serves as a parent for another object, but which should NOT be exported itself, is
63 * exported only as transform (i.e. as empty). This happens with objects that are invisible when
64 * exporting with "Visible Only" enabled, for example. */
66
67 /* When true, this object should check its parents for animation data when determining whether
68 * it's animated. This is necessary when a parent object in Blender is not part of the export. */
70
71 /*********** Determined during writer creation: ***************/
72 float parent_matrix_inv_world[4][4]; /* Inverse of the parent's world matrix. */
73 std::string export_path; /* Hierarchical path, such as "/grandparent/parent/object_name". */
74 ParticleSystem *particle_system; /* Only set for particle/hair writers. */
75
76 /* Hierarchical path of the object this object is duplicating; only set when this object should
77 * be stored as a reference to its original. It can happen that the original is not part of the
78 * exported objects, in which case this string is empty even though 'duplicator' is set. */
80
81 /* Export path of the higher-up exported data. For transforms, this is the export path of the
82 * parent object. For object data, this is the export path of that object's transform.
83 *
84 * From the exported file's point of view, this is the path to the parent in that file. The term
85 * "parent" is not used here to avoid confusion with Blender's meaning of the word (which always
86 * refers to a different object). */
88
89 bool operator<(const HierarchyContext &other) const;
90
91 /* Return a HierarchyContext representing the root of the export hierarchy. */
92 static const HierarchyContext *root();
93
94 /* For handling instanced collections, instances created by particles, etc. */
95 bool is_instance() const;
96 void mark_as_instance_of(const std::string &reference_export_path);
98
99 bool is_object_visible(enum eEvaluationMode evaluation_mode) const;
100};
101
102/* Abstract writer for objects. Create concrete subclasses to write to USD, Alembic, etc.
103 *
104 * Instantiated by the AbstractHierarchyIterator on the first frame an object exists. Generally
105 * that's the first frame to be exported, but can be later, for example when objects are
106 * instantiated by particles. The AbstractHierarchyWriter::write() function is called on every
107 * frame the object exists in the dependency graph and should be exported.
108 */
110 public:
111 virtual ~AbstractHierarchyWriter() = default;
112 virtual void write(HierarchyContext &context) = 0;
113 /* TODO(Sybren): add function like absent() that's called when a writer was previously created,
114 * but wasn't used while exporting the current frame (for example, a particle-instanced mesh of
115 * which the particle is no longer alive). */
116 protected:
117 /* Return true if the data written by this writer changes over time.
118 * Note that this function assumes this is an object data writer. Transform writers should not
119 * call this but implement their own logic. */
120 virtual bool check_is_animated(const HierarchyContext &context) const;
121
122 /* Helper functions for animation checks. */
123 static bool check_has_physics(const HierarchyContext &context);
124 static bool check_has_deforming_physics(const HierarchyContext &context);
125};
126
127/* Determines which subset of the writers actually gets to write. */
129 bool transforms : 1;
130 bool shapes : 1;
131};
132
133/* EnsuredWriter represents an AbstractHierarchyWriter* combined with information whether it was
134 * newly created or not. It's returned by AbstractHierarchyIterator::ensure_writer(). */
136 private:
138
139 /* Is set to truth when ensure_writer() did not find existing writer and created a new one.
140 * Is set to false when writer has been re-used or when allocation of the new one has failed
141 * (`writer` will be `nullptr` in that case and bool(ensured_writer) will be false). */
142 bool newly_created_;
143
145
146 public:
148
149 static EnsuredWriter empty();
152
153 bool is_newly_created() const;
154
155 /* These operators make an EnsuredWriter* act as an AbstractHierarchyWriter* */
156 operator bool() const;
158};
159
160/* Unique identifier for a (potentially duplicated) object.
161 *
162 * Instances of this class serve as key in the export graph of the
163 * AbstractHierarchyIterator. */
165 public:
167 Object *duplicated_by; /* nullptr for real objects. */
169
170 protected:
172
173 public:
177 static ObjectIdentifier for_duplicated_object(const DupliObject *dupli_object,
179
180 bool is_root() const;
181};
182
183bool operator<(const ObjectIdentifier &obj_ident_a, const ObjectIdentifier &obj_ident_b);
184bool operator==(const ObjectIdentifier &obj_ident_a, const ObjectIdentifier &obj_ident_b);
185
186/* AbstractHierarchyIterator iterates over objects in a dependency graph, and constructs export
187 * writers. These writers are then called to perform the actual writing to a USD or Alembic file.
188 *
189 * Dealing with file- and scene-level data (for example, creating a USD scene, setting the frame
190 * rate, etc.) is not part of the AbstractHierarchyIterator class structure, and should be done
191 * in separate code.
192 */
194 public:
195 /* Mapping from export path to writer. */
196 typedef std::map<std::string, AbstractHierarchyWriter *> WriterMap;
197 /* All the children of some object, as per the export hierarchy. */
198 typedef std::set<HierarchyContext *> ExportChildren;
199 /* Mapping from an object and its duplicator to the object's export-children. */
200 typedef std::map<ObjectIdentifier, ExportChildren> ExportGraph;
201 /* Mapping from ID to its export path. This is used for instancing; given an
202 * instanced datablock, the export path of the original can be looked up. */
203 typedef std::map<ID *, std::string> ExportPathMap;
204
205 protected:
209 Depsgraph *depsgraph_;
212
213 public:
214 explicit AbstractHierarchyIterator(Main *bmain, Depsgraph *depsgraph);
216
217 /* Iterate over the depsgraph, create writers, and tell the writers to write.
218 * Main entry point for the AbstractHierarchyIterator, must be called for every to-be-exported
219 * (sub)frame. */
220 virtual void iterate_and_write();
221
222 /* Release all writers. Call after all frames have been exported. */
223 void release_writers();
224
225 /* Determine which subset of writers is used for exporting.
226 * Set this before calling iterate_and_write().
227 *
228 * Note that writers are created for each iterated object, regardless of this option. When a
229 * writer is created it will also write the current iteration, to ensure the hierarchy is
230 * complete. The `export_subset` option is only in effect when the writer already existed from a
231 * previous iteration. */
232 void set_export_subset(ExportSubset export_subset);
233
234 /* Convert the given name to something that is valid for the exported file format.
235 * This base implementation is a no-op; override in a concrete subclass. */
236 virtual std::string make_valid_name(const std::string &name) const;
237
238 /* Return the name of this ID datablock that is valid for the exported file format. Overriding is
239 * only necessary if make_valid_name(id->name+2) is not suitable for the exported file format.
240 * NULL-safe: when `id == nullptr` this returns an empty string. */
241 virtual std::string get_id_name(const ID *id) const;
242
243 /* Given a HierarchyContext of some Object *, return an export path that is valid for its
244 * object->data. Overriding is necessary when the exported format does NOT expect the object's
245 * data to be a child of the object. */
246 virtual std::string get_object_data_path(const HierarchyContext *context) const;
247
248 private:
249 void debug_print_export_graph(const ExportGraph &graph) const;
250
251 void export_graph_construct();
252 void connect_loose_objects();
253 void export_graph_prune();
254 void export_graph_clear();
255
256 void visit_object(Object *object, Object *export_parent, bool weak_export);
257 void visit_dupli_object(DupliObject *dupli_object,
258 Object *duplicator,
259 const DupliParentFinder &dupli_parent_finder);
260
261 void context_update_for_graph_index(HierarchyContext *context,
262 const ExportGraph::key_type &graph_index) const;
263
264 void determine_export_paths(const HierarchyContext *parent_context);
265 void determine_duplication_references(const HierarchyContext *parent_context,
266 const std::string &indent);
267
268 /* These three functions create writers and call their write() method. */
269 void make_writers(const HierarchyContext *parent_context);
270 void make_writer_object_data(const HierarchyContext *context);
271 void make_writers_particle_systems(const HierarchyContext *transform_context);
272
273 /* Return the appropriate HierarchyContext for the data of the object represented by
274 * object_context. */
275 HierarchyContext context_for_object_data(const HierarchyContext *object_context) const;
276
277 /* Convenience wrappers around get_id_name(). */
278 std::string get_object_name(const Object *object) const;
279 std::string get_object_data_name(const Object *object) const;
280
281 typedef AbstractHierarchyWriter *(AbstractHierarchyIterator::*create_writer_func)(
282 const HierarchyContext *);
283 /* Ensure that a writer exists; if it doesn't, call create_func(context).
284 *
285 * The create_func function should be one of the create_XXXX_writer(context) functions declared
286 * below. */
287 EnsuredWriter ensure_writer(HierarchyContext *context, create_writer_func create_func);
288
289 protected:
290 /* Construct a valid path for the export file format. This class concatenates by using '/' as a
291 * path separator, which is valid for both Alembic and USD. */
292 virtual std::string path_concatenate(const std::string &parent_path,
293 const std::string &child_path) const;
294
295 /* Return whether this object should be marked as 'weak export' or not.
296 *
297 * When this returns false, writers for the transform and data are created,
298 * and dupli-objects dupli-object generated from this object will be passed to
299 * should_visit_dupli_object().
300 *
301 * When this returns true, only a transform writer is created and marked as
302 * 'weak export'. In this case, the transform writer will be removed before
303 * exporting starts, unless a descendant of this object is to be exported.
304 * Dupli-object generated from this object will also be skipped.
305 *
306 * See HierarchyContext::weak_export.
307 */
308 virtual bool mark_as_weak_export(const Object *object) const;
309
310 virtual bool should_visit_dupli_object(const DupliObject *dupli_object) const;
311
312 virtual ExportGraph::key_type determine_graph_index_object(const HierarchyContext *context);
313 virtual ExportGraph::key_type determine_graph_index_dupli(
314 const HierarchyContext *context,
315 const DupliObject *dupli_object,
316 const DupliParentFinder &dupli_parent_finder);
317
318 /* These functions should create an AbstractHierarchyWriter subclass instance, or return
319 * nullptr if the object or its data should not be exported. Returning a nullptr for
320 * data/hair/particle will NOT prevent the transform to be written.
321 *
322 * The returned writer is owned by the AbstractHierarchyWriter, and should be freed in
323 * delete_object_writer().
324 *
325 * The created AbstractHierarchyWriter instances should NOT keep a copy of the context pointer.
326 * The context can be stack-allocated and go out of scope. */
331
332 /* Called by release_writers() to free what the create_XXX_writer() functions allocated. */
333 virtual void release_writer(AbstractHierarchyWriter *writer) = 0;
334
335 AbstractHierarchyWriter *get_writer(const std::string &export_path) const;
337};
338
339} // namespace blender::io
eEvaluationMode
std::map< ObjectIdentifier, ExportChildren > ExportGraph
virtual ExportGraph::key_type determine_graph_index_object(const HierarchyContext *context)
virtual bool should_visit_dupli_object(const DupliObject *dupli_object) const
virtual void release_writer(AbstractHierarchyWriter *writer)=0
virtual bool mark_as_weak_export(const Object *object) const
void set_export_subset(ExportSubset export_subset)
virtual std::string get_id_name(const ID *id) const
virtual AbstractHierarchyWriter * create_particle_writer(const HierarchyContext *context)=0
ExportChildren & graph_children(const HierarchyContext *context)
std::map< std::string, AbstractHierarchyWriter * > WriterMap
virtual AbstractHierarchyWriter * create_transform_writer(const HierarchyContext *context)=0
AbstractHierarchyIterator(Main *bmain, Depsgraph *depsgraph)
virtual std::string path_concatenate(const std::string &parent_path, const std::string &child_path) const
AbstractHierarchyWriter * get_writer(const std::string &export_path) const
virtual AbstractHierarchyWriter * create_data_writer(const HierarchyContext *context)=0
virtual AbstractHierarchyWriter * create_hair_writer(const HierarchyContext *context)=0
virtual std::string make_valid_name(const std::string &name) const
virtual ExportGraph::key_type determine_graph_index_dupli(const HierarchyContext *context, const DupliObject *dupli_object, const DupliParentFinder &dupli_parent_finder)
virtual std::string get_object_data_path(const HierarchyContext *context) const
virtual bool check_is_animated(const HierarchyContext &context) const
virtual void write(HierarchyContext &context)=0
static bool check_has_deforming_physics(const HierarchyContext &context)
virtual ~AbstractHierarchyWriter()=default
static bool check_has_physics(const HierarchyContext &context)
static EnsuredWriter existing(AbstractHierarchyWriter *writer)
static EnsuredWriter newly_created(AbstractHierarchyWriter *writer)
AbstractHierarchyWriter * operator->()
static ObjectIdentifier for_graph_root()
static ObjectIdentifier for_duplicated_object(const DupliObject *dupli_object, Object *duplicated_by)
static ObjectIdentifier for_real_object(Object *object)
static ObjectIdentifier for_hierarchy_context(const HierarchyContext *context)
ObjectIdentifier(Object *object, Object *duplicated_by, const PersistentID &persistent_id)
const Depsgraph * depsgraph
bool operator<(const PersistentID &persistent_id_a, const PersistentID &persistent_id_b)
bool operator==(const PersistentID &persistent_id_a, const PersistentID &persistent_id_b)
static PyObject * create_func(PyObject *, PyObject *args)
Definition python.cpp:161
Definition DNA_ID.h:413
bool is_object_visible(enum eEvaluationMode evaluation_mode) const
static const HierarchyContext * root()
bool operator<(const HierarchyContext &other) const
void mark_as_instance_of(const std::string &reference_export_path)