This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/io/alembic/exporter/abc_custom_props.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
3.6 KiB
C++
Raw Normal View History

Alembic export: write custom properties Write custom properties (aka ID properties) to Alembic, to the `.userProperties` compound property. Manifest Task: https://developer.blender.org/T50725 Scalar properties (so single-value/non-array properties) are written as single-element array properties to Alembic. This is also what's done by Houdini and Maya exporters, so it seems to be the standard way of doing things. It also simplifies the implementation. Two-dimensional arrays are flattened by concatenating all the numbers into a single array. This is because ID properties have a limited type system. This means that a 3x3 "matrix" could just as well be a list of three 3D vectors. Alembic has two container properties to store custom data: - `.userProperties`, which is meant for properties that aren't necessarily understood by other software packages, and - `.arbGeomParams`, which can contain the same kind of data as `.userProperties`, but can also specify that these vary per face of a mesh. This property is mostly intended for renderers. Most industry packages write their custom data to `.arbGeomParams`. However, given their goals I feel that `.userProperties` is the more appropriate one for Blender's ID Properties. The code is a bit more involved than I would have liked. An `ABCAbstractWriter` has a `uniqueptr` to its `CustomPropertiesExporter`, but the `CustomPropertiesExporter` also has a pointer back to its owning `ABCAbstractWriter`. It's the latter pointer that I'm not too happy with, but it has a reason. Getting the aforementioned `.userProperties` from the Alembic library will automatically create it if it doesn't exist already. If it's not used to actually add custom properties to, it will crash the Alembic CLI tools (and maybe others too). This is what the pointer back to the `ABCAbstractWriter` is used for: to get the `.userProperties` at the last moment, when it's 100% sure at least one custom property will be written. Differential Revision: https://developer.blender.org/D8869 Reviewed by: sergey, dbystedt
2020-09-11 14:06:13 +02:00
/*
* 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.
*/
/** \file
* \ingroup Alembic
*/
#pragma once
#include <Alembic/Abc/OArrayProperty.h>
#include <Alembic/Abc/OCompoundProperty.h>
#include "BLI_map.hh"
#include <memory>
struct IDProperty;
namespace blender::io::alembic {
class ABCAbstractWriter;
/* Write values of Custom Properties (a.k.a. ID Properties) to Alembic.
*
* Each Alembic Writer instance optionally has one CustomPropertiesExporter (CPE). This CPE not
* only writes the custom properties to Alembic, but also keeps references in memory so that the
* Alembic library doesn't prematurely finalize the data. */
class CustomPropertiesExporter {
private:
/* Owner is used to get the OCompoundProperty and time sample index. The former should only be
* requested from the Alembic library when it's actually going to be used to add custom
* properties (otherwise an invalid Alembic file is written). */
ABCAbstractWriter *owner_;
/* The Compound Property that will contain the exported custom properties.
*
* Typically this the return value of abc_schema.getArbGeomParams() or
* abc_schema.getUserProperties(). */
Alembic::Abc::OCompoundProperty abc_compound_prop_;
/* Mapping from property name in Blender to property in Alembic.
* Here Blender does the same as other software (Maya, Houdini), and writes
* scalar properties as single-element arrays. */
Map<std::string, Alembic::Abc::OArrayProperty> abc_properties_;
public:
CustomPropertiesExporter(ABCAbstractWriter *owner);
virtual ~CustomPropertiesExporter();
void write_all(const IDProperty *group);
private:
void write(const IDProperty *id_property);
void write_array(const IDProperty *id_property);
/* IDProperty arrays are used to store arrays-of-arrays or arrays-of-strings. */
void write_idparray(const IDProperty *idp_array);
void write_idparray_of_strings(const IDProperty *idp_array);
void write_idparray_of_numbers(const IDProperty *idp_array);
/* Flatten an array-of-arrays into one long array, then write that.
* It is tempting to write an array of NxM numbers as a matrix, but there is
* no guarantee that the data actually represents a matrix. */
template<typename ABCPropertyType, typename BlenderValueType>
void write_idparray_flattened_typed(const IDProperty *idp_array);
/* Write a single scalar (i.e. non-array) property as single-value array. */
template<typename ABCPropertyType, typename BlenderValueType>
void set_scalar_property(StringRef property_name, const BlenderValueType property_value);
template<typename ABCPropertyType, typename BlenderValueType>
void set_array_property(StringRef property_name,
const BlenderValueType *array_values,
size_t num_array_items);
template<typename ABCPropertyType>
Alembic::Abc::OArrayProperty create_abc_property(StringRef property_name);
};
} // namespace blender::io::alembic