Blender  V2.93
abc_custom_props.h
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) 2020 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #pragma once
25 
26 #include <Alembic/Abc/OArrayProperty.h>
27 #include <Alembic/Abc/OCompoundProperty.h>
28 
29 #include "BLI_map.hh"
30 
31 #include <memory>
32 
33 struct IDProperty;
34 
35 namespace blender::io::alembic {
36 
37 class ABCAbstractWriter;
38 
39 /* Write values of Custom Properties (a.k.a. ID Properties) to Alembic.
40  *
41  * Each Alembic Writer instance optionally has one CustomPropertiesExporter (CPE). This CPE not
42  * only writes the custom properties to Alembic, but also keeps references in memory so that the
43  * Alembic library doesn't prematurely finalize the data. */
45  private:
46  /* Owner is used to get the OCompoundProperty and time sample index. The former should only be
47  * requested from the Alembic library when it's actually going to be used to add custom
48  * properties (otherwise an invalid Alembic file is written). */
49  ABCAbstractWriter *owner_;
50 
51  /* The Compound Property that will contain the exported custom properties.
52  *
53  * Typically this the return value of abc_schema.getArbGeomParams() or
54  * abc_schema.getUserProperties(). */
55  Alembic::Abc::OCompoundProperty abc_compound_prop_;
56 
57  /* Mapping from property name in Blender to property in Alembic.
58  * Here Blender does the same as other software (Maya, Houdini), and writes
59  * scalar properties as single-element arrays. */
61 
62  public:
64  virtual ~CustomPropertiesExporter() = default;
65 
66  void write_all(const IDProperty *group);
67 
68  private:
69  void write(const IDProperty *id_property);
70  void write_array(const IDProperty *id_property);
71 
72  /* IDProperty arrays are used to store arrays-of-arrays or arrays-of-strings. */
73  void write_idparray(const IDProperty *idp_array);
74  void write_idparray_of_strings(const IDProperty *idp_array);
75  void write_idparray_of_numbers(const IDProperty *idp_array);
76 
77  /* Flatten an array-of-arrays into one long array, then write that.
78  * It is tempting to write an array of NxM numbers as a matrix, but there is
79  * no guarantee that the data actually represents a matrix. */
80  template<typename ABCPropertyType, typename BlenderValueType>
81  void write_idparray_flattened_typed(const IDProperty *idp_array);
82 
83  /* Write a single scalar (i.e. non-array) property as single-value array. */
84  template<typename ABCPropertyType, typename BlenderValueType>
85  void set_scalar_property(StringRef property_name, const BlenderValueType property_value);
86 
87  template<typename ABCPropertyType, typename BlenderValueType>
88  void set_array_property(StringRef property_name,
89  const BlenderValueType *array_values,
90  size_t num_array_items);
91 
92  template<typename ABCPropertyType>
93  Alembic::Abc::OArrayProperty create_abc_property(StringRef property_name);
94 };
95 
96 } // namespace blender::io::alembic