Alembic: Construct ISampleSelector once and pass along
[blender.git] / source / blender / alembic / intern / abc_transform.cc
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Esteban Tovagliari, Cedric Paille, Kevin Dietrich
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 #include "abc_transform.h"
24
25 #include <OpenEXR/ImathBoxAlgo.h>
26
27 #include "abc_util.h"
28
29 extern "C" {
30 #include "DNA_object_types.h"
31
32 #include "BLI_math.h"
33
34 #include "BKE_object.h"
35 }
36
37 using Alembic::AbcGeom::OObject;
38 using Alembic::AbcGeom::OXform;
39 using Alembic::Abc::ISampleSelector;
40
41 /* ************************************************************************** */
42
43 static bool has_parent_camera(Object *ob)
44 {
45         if (!ob->parent) {
46                 return false;
47         }
48
49         Object *parent = ob->parent;
50
51         if (parent->type == OB_CAMERA) {
52                 return true;
53         }
54
55         return has_parent_camera(parent);
56 }
57
58 /* ************************************************************************** */
59
60 AbcTransformWriter::AbcTransformWriter(Object *ob,
61                                        const OObject &abc_parent,
62                                        AbcTransformWriter *parent,
63                                        unsigned int time_sampling,
64                                        ExportSettings &settings)
65     : AbcObjectWriter(NULL, ob, time_sampling, settings, parent)
66     , m_proxy_from(NULL)
67 {
68         m_is_animated = hasAnimation(m_object);
69
70         if (!m_is_animated) {
71                 time_sampling = 0;
72         }
73
74         m_xform = OXform(abc_parent, get_id_name(m_object), time_sampling);
75         m_schema = m_xform.getSchema();
76
77         /* Blender objects can't have a parent without inheriting the transform. */
78         m_inherits_xform = parent != NULL;
79 }
80
81 void AbcTransformWriter::do_write()
82 {
83         if (m_first_frame) {
84                 m_visibility = Alembic::AbcGeom::CreateVisibilityProperty(m_xform, m_xform.getSchema().getTimeSampling());
85         }
86
87         m_visibility.set(!(m_object->restrictflag & OB_RESTRICT_VIEW));
88
89         if (!m_first_frame && !m_is_animated) {
90                 return;
91         }
92
93         float yup_mat[4][4];
94         create_transform_matrix(m_object, yup_mat,
95                                 m_inherits_xform ? ABC_MATRIX_LOCAL : ABC_MATRIX_WORLD,
96                                 m_proxy_from);
97
98         /* Only apply rotation to root camera, parenting will propagate it. */
99         if (m_object->type == OB_CAMERA && (!m_inherits_xform || !has_parent_camera(m_object))) {
100                 float rot_mat[4][4];
101                 axis_angle_to_mat4_single(rot_mat, 'X', -M_PI_2);
102                 mul_m4_m4m4(yup_mat, yup_mat, rot_mat);
103         }
104
105         if (!m_object->parent || !m_inherits_xform) {
106                 /* Only apply scaling to root objects, parenting will propagate it. */
107                 float scale_mat[4][4];
108                 scale_m4_fl(scale_mat, m_settings.global_scale);
109                 scale_mat[3][3] = m_settings.global_scale;  /* also scale translation */
110                 mul_m4_m4m4(yup_mat, yup_mat, scale_mat);
111         }
112
113         m_matrix = convert_matrix(yup_mat);
114         m_sample.setMatrix(m_matrix);
115         m_sample.setInheritsXforms(m_inherits_xform);
116         m_schema.set(m_sample);
117 }
118
119 Imath::Box3d AbcTransformWriter::bounds()
120 {
121         Imath::Box3d bounds;
122
123         for (int i = 0; i < m_children.size(); ++i) {
124                 Imath::Box3d box(m_children[i]->bounds());
125                 bounds.extendBy(box);
126         }
127
128         return Imath::transform(bounds, m_matrix);
129 }
130
131 bool AbcTransformWriter::hasAnimation(Object * /*ob*/) const
132 {
133         /* TODO(kevin): implement this. */
134         return true;
135 }
136
137 /* ************************************************************************** */
138
139 AbcEmptyReader::AbcEmptyReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
140     : AbcObjectReader(object, settings)
141 {
142         /* Empties have no data. It makes the import of Alembic files easier to
143          * understand when we name the empty after its name in Alembic. */
144         m_object_name = object.getName();
145
146         Alembic::AbcGeom::IXform xform(object, Alembic::AbcGeom::kWrapExisting);
147         m_schema = xform.getSchema();
148
149         get_min_max_time(m_iobject, m_schema, m_min_time, m_max_time);
150 }
151
152 bool AbcEmptyReader::valid() const
153 {
154         return m_schema.valid();
155 }
156
157 void AbcEmptyReader::readObjectData(Main *bmain, const ISampleSelector &UNUSED(sample_sel))
158 {
159         m_object = BKE_object_add_only_object(bmain, OB_EMPTY,
160                                               m_object_name.c_str());
161         m_object->data = NULL;
162 }