[Bf-blender-cvs] [553c6bb3d0f] temp-T50725-alembic-export-custom-properties: Alembic export: write scalar custom properties

Sybren A. Stüvel noreply at git.blender.org
Thu Sep 10 19:45:39 CEST 2020


Commit: 553c6bb3d0f85a76177f7ed03ec8a3ba82816bdc
Author: Sybren A. Stüvel
Date:   Fri Aug 21 10:28:02 2020 +0200
Branches: temp-T50725-alembic-export-custom-properties
https://developer.blender.org/rB553c6bb3d0f85a76177f7ed03ec8a3ba82816bdc

Alembic export: write scalar custom properties

Write scalar custom properties as 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.

Seems to work ok'ish if I look at it via `usdcat`.

===================================================================

M	source/blender/blenkernel/intern/idprop.c
M	source/blender/io/alembic/CMakeLists.txt
A	source/blender/io/alembic/exporter/abc_custom_props.cc
A	source/blender/io/alembic/exporter/abc_custom_props.h
M	source/blender/io/alembic/exporter/abc_writer_transform.cc
M	source/blender/io/alembic/exporter/abc_writer_transform.h

===================================================================

diff --git a/source/blender/blenkernel/intern/idprop.c b/source/blender/blenkernel/intern/idprop.c
index 189eef5e175..5a9c953b302 100644
--- a/source/blender/blenkernel/intern/idprop.c
+++ b/source/blender/blenkernel/intern/idprop.c
@@ -1151,6 +1151,9 @@ void IDP_foreach_property(IDProperty *id_property_root,
   switch (id_property_root->type) {
     case IDP_GROUP: {
       LISTBASE_FOREACH (IDProperty *, loop, &id_property_root->data.group) {
+        if (STREQ(loop->name, "_RNA_UI")) {
+          continue;
+        }
         IDP_foreach_property(loop, type_filter, callback, user_data);
       }
       break;
diff --git a/source/blender/io/alembic/CMakeLists.txt b/source/blender/io/alembic/CMakeLists.txt
index 2b44146e475..d55f2382a9b 100644
--- a/source/blender/io/alembic/CMakeLists.txt
+++ b/source/blender/io/alembic/CMakeLists.txt
@@ -56,6 +56,7 @@ set(SRC
   intern/alembic_capi.cc
 
   exporter/abc_archive.cc
+  exporter/abc_custom_props.cc
   exporter/abc_export_capi.cc
   exporter/abc_hierarchy_iterator.cc
   exporter/abc_subdiv_disabler.cc
@@ -84,6 +85,7 @@ set(SRC
   intern/abc_util.h
 
   exporter/abc_archive.h
+  exporter/abc_custom_props.h
   exporter/abc_hierarchy_iterator.h
   exporter/abc_subdiv_disabler.h
   exporter/abc_writer_abstract.h
diff --git a/source/blender/io/alembic/exporter/abc_custom_props.cc b/source/blender/io/alembic/exporter/abc_custom_props.cc
new file mode 100644
index 00000000000..a9d69db64c7
--- /dev/null
+++ b/source/blender/io/alembic/exporter/abc_custom_props.cc
@@ -0,0 +1,134 @@
+/*
+ * 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
+ */
+
+#include "abc_custom_props.h"
+
+#include <functional>
+#include <iostream>
+#include <string>
+
+#include <Alembic/Abc/OTypedArrayProperty.h>
+#include <Alembic/Abc/OTypedScalarProperty.h>
+
+#include "BKE_idprop.h"
+#include "DNA_ID.h"
+
+using Alembic::Abc::ArraySample;
+using Alembic::Abc::OArrayProperty;
+using Alembic::Abc::OBaseProperty;
+using Alembic::Abc::OBoolArrayProperty;
+using Alembic::Abc::OBoolProperty;
+using Alembic::Abc::ODoubleArrayProperty;
+using Alembic::Abc::ODoubleProperty;
+using Alembic::Abc::OFloatArrayProperty;
+using Alembic::Abc::OFloatProperty;
+using Alembic::Abc::OInt64ArrayProperty;
+using Alembic::Abc::OInt64Property;
+using Alembic::Abc::OScalarProperty;
+using Alembic::Abc::OStringArrayProperty;
+using Alembic::Abc::OStringProperty;
+
+namespace blender::io::alembic {
+
+CustomPropertiesExporter::CustomPropertiesExporter(
+    Alembic::Abc::OCompoundProperty abc_compound_prop)
+    : abc_compound_prop_(abc_compound_prop)
+{
+}
+
+CustomPropertiesExporter::~CustomPropertiesExporter()
+{
+}
+
+namespace {
+
+/* Callback for IDP_foreach_property() that just calls CustomPropertiesExporter::write(). */
+void customPropertiesExporter_write(IDProperty *id_property, void *user_data)
+{
+  CustomPropertiesExporter *exporter = reinterpret_cast<CustomPropertiesExporter *>(user_data);
+  exporter->write(id_property);
+};
+
+}  // namespace
+
+void CustomPropertiesExporter::write_all(IDProperty *group)
+{
+  if (group == nullptr) {
+    std::cout << "CustomPropertiesExporter::write_all(nullptr)\n";
+    return;
+  }
+  std::cout << "CustomPropertiesExporter::write_all(" << group->name << ")\n";
+  IDP_foreach_property(group,
+                       IDP_TYPE_FILTER_STRING | IDP_TYPE_FILTER_INT | IDP_TYPE_FILTER_FLOAT |
+                           IDP_TYPE_FILTER_ARRAY | IDP_TYPE_FILTER_DOUBLE,
+                       customPropertiesExporter_write,
+                       this);
+}
+
+template<typename ABCPropertyType, typename BlenderValueType>
+void CustomPropertiesExporter::set_scalar_property(const StringRef property_name,
+                                                   const BlenderValueType property_value)
+{
+  auto create_callback = [this, property_name]() -> OArrayProperty {
+    ABCPropertyType abc_property(abc_compound_prop_, property_name);
+    return abc_property;
+  };
+
+  OArrayProperty array_prop = abc_properties_.lookup_or_add_cb(property_name, create_callback);
+  ArraySample sample(&property_value, array_prop.getDataType(), Alembic::Util::Dimensions(1));
+  array_prop.set(sample);
+}
+
+void CustomPropertiesExporter::write(IDProperty *id_property)
+{
+  std::cout << "    CustomPropertiesExporter::write(" << id_property->name << ")\n";
+  switch (id_property->type) {
+    case IDP_STRING: {
+      /* The Alembic library doesn't accept NULL-terminated character arrays. */
+      const std::string prop_value = IDP_String(id_property);
+      set_scalar_property<OStringArrayProperty, std::string>(id_property->name, prop_value);
+      break;
+    }
+    case IDP_INT: {
+      set_scalar_property<OInt64ArrayProperty, int>(id_property->name, IDP_Int(id_property));
+      break;
+    }
+    case IDP_FLOAT: {
+      set_scalar_property<OFloatArrayProperty, float>(id_property->name, IDP_Float(id_property));
+      break;
+    }
+    case IDP_DOUBLE: {
+      set_scalar_property<ODoubleArrayProperty, double>(id_property->name,
+                                                        IDP_Double(id_property));
+      break;
+    }
+    case IDP_ARRAY:
+    case IDP_GROUP:
+    case IDP_ID:
+    case IDP_IDPARRAY:
+      /* Unsupported. */
+      break;
+  }
+}
+
+}  // namespace blender::io::alembic
diff --git a/source/blender/io/alembic/exporter/abc_custom_props.h b/source/blender/io/alembic/exporter/abc_custom_props.h
new file mode 100644
index 00000000000..9a86b61c2c8
--- /dev/null
+++ b/source/blender/io/alembic/exporter/abc_custom_props.h
@@ -0,0 +1,65 @@
+/*
+ * 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 {
+
+/* Write values of Custom Properties (a.k.a. ID Properties) to Alembic.
+ * Create the appropriate Alembic objects for the property types. */
+class CustomPropertiesExporter {
+ private:
+  /* The Compound Property that will contain the exported custom properties.
+   *
+   * Typically this the return value of Abc::OSchema::getArbGeomParams() or
+   * Abc::OSchema::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(Alembic::Abc::OCompoundProperty abc_compound_prop);
+  virtual ~CustomPropertiesExporter();
+
+  void write_all(IDProperty *group);
+  void write(IDProperty *id_property);
+
+ private:
+  /* Write a single scalar (i.e. non-array) property as single-value array. */
+  template<typename ABCPropertyType, typename BlenderValueType>
+  void set_scalar_property(const StringRef property_name, const BlenderValueType property_value);
+};
+
+}  // namespace blender::io::alembic
diff --git a/source/blender/io/alembic/exporter/abc_writer_transform.cc b/source/blender/io/alembic/exporter/abc_writer_transform.cc
index a72a6b47aa9..b4db3e78b07 100644
--- a/source/blender/io/alembic/exporter/abc_writer_transform.cc
+++ b/source/blender/io/alembic/exporter/abc_writer_transform.cc
@@ -51,6 +51,9 @@ void ABCTransformWriter::create_alembic_objects(const HierarchyContext * /*conte
   CLOG_INFO(&LOG, 2, "exporting %s", args_.abc_path.c_str());
   abc_xform_ = OXform(args_.abc_parent, args_.abc_name, timesample_index_);
   abc_xform_schema_ = abc_xform_.getSchema();
+
+  custom_props_ = std::make_unique<CustomPropertiesExporter>(
+      abc_xform_schema_.getUserProperties());
 }
 
 void ABCTransformWriter::do_write(HierarchyContext &context)
@@ -92,6 +95,8 @@ void ABCTransformWriter::do_write(HierarchyContext &context)
   abc_xform_schema_.set(xform_sample);
 
   write_visibility(context);
+
+  custom_props_->write_all(context.object->id.properties);
 }
 
 OObject ABCTransformWriter::get_alembic_object() const
diff --git a/source/blender/io/alembic/exporter/abc_writer_transform.h b/source/blender/io/alembic/exporter/abc_writer_transform.h
index a334fe610ee..a4bf0288782 100644
--- a/source/blender/io/alembic/exporter/abc_writer_transform.h
+++ b/source/blender/io/alembic/exporter/abc_writer_transform.h
@@ -19,8 +19,11 @@
  * \ingroup balembic
  */
 
+#include "abc_custom_props.h"
 #include "abc_writer_abstract.h"
 
+#include <memory>
+
 #include <Alembic/AbcGeom/OXform.h>
 
 namespace blender::io::

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list