[Bf-blender-cvs] [da9bf46be3b] temp-alembic-exporter-T73363-ms2: Alembic: added camera writer

Sybren A. Stüvel noreply at git.blender.org
Tue Apr 28 11:51:07 CEST 2020


Commit: da9bf46be3b895f10f2de70e584367b43875628a
Author: Sybren A. Stüvel
Date:   Fri Apr 24 12:57:32 2020 +0200
Branches: temp-alembic-exporter-T73363-ms2
https://developer.blender.org/rBda9bf46be3b895f10f2de70e584367b43875628a

Alembic: added camera writer

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

M	source/blender/io/alembic/CMakeLists.txt
M	source/blender/io/alembic/intern/export/abc_hierarchy_iterator.cc
A	source/blender/io/alembic/intern/export/abc_writer_camera.cc
A	source/blender/io/alembic/intern/export/abc_writer_camera.h
M	source/blender/io/alembic/intern/export/abc_writer_transform.cc

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

diff --git a/source/blender/io/alembic/CMakeLists.txt b/source/blender/io/alembic/CMakeLists.txt
index cd796cafb01..61ba4c4b009 100644
--- a/source/blender/io/alembic/CMakeLists.txt
+++ b/source/blender/io/alembic/CMakeLists.txt
@@ -96,6 +96,7 @@ set(SRC
   intern/export/abc_hierarchy_iterator.cc
   intern/export/abc_subdiv_disabler.cc
   intern/export/abc_writer_abstract.cc
+  intern/export/abc_writer_camera.cc
   intern/export/abc_writer_mesh.cc
   intern/export/abc_writer_metaball.cc
   intern/export/abc_writer_transform.cc
@@ -104,6 +105,7 @@ set(SRC
   intern/export/abc_hierarchy_iterator.h
   intern/export/abc_subdiv_disabler.h
   intern/export/abc_writer_abstract.h
+  intern/export/abc_writer_camera.h
   intern/export/abc_writer_mesh.h
   intern/export/abc_writer_metaball.h
   intern/export/abc_writer_transform.h
diff --git a/source/blender/io/alembic/intern/export/abc_hierarchy_iterator.cc b/source/blender/io/alembic/intern/export/abc_hierarchy_iterator.cc
index e0d6146386c..087d9995cfd 100644
--- a/source/blender/io/alembic/intern/export/abc_hierarchy_iterator.cc
+++ b/source/blender/io/alembic/intern/export/abc_hierarchy_iterator.cc
@@ -19,7 +19,7 @@
 
 #include "abc_hierarchy_iterator.h"
 #include "abc_writer_abstract.h"
-// #include "abc_writer_camera.h"
+#include "abc_writer_camera.h"
 // #include "abc_writer_hair.h"
 // #include "abc_writer_light.h"
 #include "abc_writer_mesh.h"
@@ -122,11 +122,10 @@ AbstractHierarchyWriter *ABCHierarchyIterator::create_data_writer(const Hierarch
       data_writer = new ABCMeshWriter(writer_args);
       break;
     case OB_CAMERA:
-      // data_writer = new USDCameraWriter(writer_args);
-      return nullptr;
+      data_writer = new ABCCameraWriter(writer_args);
       break;
     case OB_LAMP:
-      // data_writer = new USDLightWriter(writer_args);
+      // data_writer = new ABCLightWriter(writer_args);
       return nullptr;
       break;
     case OB_MBALL:
diff --git a/source/blender/io/alembic/intern/export/abc_writer_camera.cc b/source/blender/io/alembic/intern/export/abc_writer_camera.cc
new file mode 100644
index 00000000000..f69dc0e54d2
--- /dev/null
+++ b/source/blender/io/alembic/intern/export/abc_writer_camera.cc
@@ -0,0 +1,109 @@
+/*
+ * 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_writer_camera.h"
+#include "abc_hierarchy_iterator.h"
+
+extern "C" {
+#include "BKE_camera.h"
+
+#include "BLI_assert.h"
+
+#include "DNA_camera_types.h"
+#include "DNA_scene_types.h"
+}
+
+#include "CLG_log.h"
+static CLG_LogRef LOG = {"io.alembic"};
+
+namespace ABC {
+
+using Alembic::AbcGeom::CameraSample;
+using Alembic::AbcGeom::OCamera;
+using Alembic::AbcGeom::OFloatProperty;
+
+ABCCameraWriter::ABCCameraWriter(const ABCWriterConstructorArgs &args) : ABCAbstractWriter(args)
+{
+}
+
+bool ABCCameraWriter::is_supported(const HierarchyContext *context) const
+{
+  Camera *camera = static_cast<Camera *>(context->object->data);
+  return camera->type == CAM_PERSP;
+}
+
+void ABCCameraWriter::create_alembic_objects()
+{
+  /* If the object is static, use the default static time sampling. */
+  uint32_t timesample_index = is_animated_ ? timesample_index_geometry_ : 0;
+
+  CLOG_INFO(&LOG, 2, "exporting %s", args_.abc_path.c_str());
+  abc_camera_ = OCamera(args_.abc_parent, args_.abc_name, timesample_index);
+  abc_camera_schema_ = abc_camera_.getSchema();
+
+  abc_custom_data_container_ = abc_camera_schema_.getUserProperties();
+  abc_stereo_distance_ = OFloatProperty(
+      abc_custom_data_container_, "stereoDistance", timesample_index);
+  abc_eye_separation_ = OFloatProperty(
+      abc_custom_data_container_, "eyeSeparation", timesample_index);
+}
+
+const Alembic::Abc::OObject ABCCameraWriter::get_alembic_object() const
+{
+  return abc_camera_;
+}
+
+void ABCCameraWriter::do_write(HierarchyContext &context)
+{
+  Camera *cam = static_cast<Camera *>(context.object->data);
+
+  abc_stereo_distance_.set(cam->stereo.convergence_distance);
+  abc_eye_separation_.set(cam->stereo.interocular_distance);
+
+  const double apperture_x = cam->sensor_x / 10.0;
+  const double apperture_y = cam->sensor_y / 10.0;
+  const double film_aspect = apperture_x / apperture_y;
+
+  CameraSample camera_sample;
+  camera_sample.setFocalLength(cam->lens);
+  camera_sample.setHorizontalAperture(apperture_x);
+  camera_sample.setVerticalAperture(apperture_y);
+  camera_sample.setHorizontalFilmOffset(apperture_x * cam->shiftx);
+  camera_sample.setVerticalFilmOffset(apperture_y * cam->shifty * film_aspect);
+  camera_sample.setNearClippingPlane(cam->clip_start);
+  camera_sample.setFarClippingPlane(cam->clip_end);
+
+  if (cam->dof.focus_object) {
+    Imath::V3f v(context.object->loc[0] - cam->dof.focus_object->loc[0],
+                 context.object->loc[1] - cam->dof.focus_object->loc[1],
+                 context.object->loc[2] - cam->dof.focus_object->loc[2]);
+    camera_sample.setFocusDistance(v.length());
+  }
+  else {
+    camera_sample.setFocusDistance(cam->dof.focus_distance);
+  }
+
+  /* Blender camera does not have an fstop param, so try to find a custom prop
+   * instead. */
+  camera_sample.setFStop(cam->dof.aperture_fstop);
+
+  camera_sample.setLensSqueezeRatio(1.0);
+  abc_camera_schema_.set(camera_sample);
+}
+
+}  // namespace ABC
diff --git a/source/blender/io/alembic/intern/export/abc_writer_camera.h b/source/blender/io/alembic/intern/export/abc_writer_camera.h
new file mode 100644
index 00000000000..bc7fe4b0a31
--- /dev/null
+++ b/source/blender/io/alembic/intern/export/abc_writer_camera.h
@@ -0,0 +1,47 @@
+/*
+ * 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.
+ */
+#pragma once
+
+#include "abc_writer_abstract.h"
+
+#include <Alembic/AbcGeom/OCamera.h>
+
+namespace ABC {
+
+class ABCCameraWriter : public ABCAbstractWriter {
+ private:
+  Alembic::AbcGeom::OCamera abc_camera_;
+  Alembic::AbcGeom::OCameraSchema abc_camera_schema_;
+
+  Alembic::AbcGeom::OCompoundProperty abc_custom_data_container_;
+  Alembic::AbcGeom::OFloatProperty abc_stereo_distance_;
+  Alembic::AbcGeom::OFloatProperty abc_eye_separation_;
+
+ public:
+  ABCCameraWriter(const ABCWriterConstructorArgs &args);
+
+  virtual void create_alembic_objects() override;
+  virtual const Alembic::Abc::OObject get_alembic_object() const override;
+
+ protected:
+  virtual bool is_supported(const HierarchyContext *context) const override;
+  virtual void do_write(HierarchyContext &context) override;
+};
+
+}  // namespace ABC
diff --git a/source/blender/io/alembic/intern/export/abc_writer_transform.cc b/source/blender/io/alembic/intern/export/abc_writer_transform.cc
index 9a0af922679..c0616aa03db 100644
--- a/source/blender/io/alembic/intern/export/abc_writer_transform.cc
+++ b/source/blender/io/alembic/intern/export/abc_writer_transform.cc
@@ -25,6 +25,7 @@ extern "C" {
 #include "BKE_object.h"
 
 #include "BLI_math_matrix.h"
+#include "BLI_math_rotation.h"
 
 #include "DNA_layer_types.h"
 }
@@ -60,6 +61,31 @@ void ABCTransformWriter::do_write(HierarchyContext &context)
   // After this, parent_relative_matrix uses Y=up.
   copy_m44_axis_swap(parent_relative_matrix, parent_relative_matrix, ABC_YUP_FROM_ZUP);
 
+  /* If the parent is a camera, undo its to-Maya rotation (see below). */
+  bool is_root_object = context.export_parent == nullptr;
+  if (!is_root_object && context.export_parent->type == OB_CAMERA) {
+    float rot_mat[4][4];
+    axis_angle_to_mat4_single(rot_mat, 'X', M_PI_2);
+    mul_m4_m4m4(parent_relative_matrix, rot_mat, parent_relative_matrix);
+  }
+
+  /* If the object is a camera, apply an extra rotation to Maya camera orientation. */
+  if (context.object->type == OB_CAMERA) {
+    float rot_mat[4][4];
+    axis_angle_to_mat4_single(rot_mat, 'X', -M_PI_2);
+    mul_m4_m4m4(parent_relative_matrix, parent_relative_matrix, rot_mat);
+  }
+
+  if (is_root_object) {
+    /* Only apply scaling to root objects, parenting will propagate it. */
+    float scale_mat[4][4];
+    scale_m4_fl(scale_mat, args_.export_params.global_scale);
+    scale_mat[3][3] = args_.export_params.global_scale; /* also scale translation */
+    mul_m4_m4m4(parent_relative_matrix, parent_relative_matrix, scale_mat);
+    parent_relative_matrix[3][3] /=
+        args_.export_params.global_scale; /* normalise the homogeneous component */
+  }
+
   XformSample xform_sample;
   xform_sample.setMatrix(convert_matrix_datatype(parent_relative_matrix));
   xform_sample.setInheritsXforms(true);



More information about the Bf-blender-cvs mailing list