Pass EvaluationContext argument everywhere
[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(EvaluationContext *eval_ctx,
61                                        Object *ob,
62                                        const OObject &abc_parent,
63                                        AbcTransformWriter *parent,
64                                        unsigned int time_sampling,
65                                        ExportSettings &settings)
66     : AbcObjectWriter(eval_ctx, NULL, ob, time_sampling, settings, parent)
67     , m_proxy_from(NULL)
68 {
69         m_is_animated = hasAnimation(m_object);
70
71         if (!m_is_animated) {
72                 time_sampling = 0;
73         }
74
75         m_xform = OXform(abc_parent, get_id_name(m_object), time_sampling);
76         m_schema = m_xform.getSchema();
77
78         /* Blender objects can't have a parent without inheriting the transform. */
79         m_inherits_xform = parent != NULL;
80 }
81
82 void AbcTransformWriter::do_write()
83 {
84         if (m_first_frame) {
85                 m_visibility = Alembic::AbcGeom::CreateVisibilityProperty(m_xform, m_xform.getSchema().getTimeSampling());
86         }
87
88         m_visibility.set(!(m_object->restrictflag & OB_RESTRICT_VIEW));
89
90         if (!m_first_frame && !m_is_animated) {
91                 return;
92         }
93
94         float yup_mat[4][4];
95         create_transform_matrix(m_object, yup_mat,
96                                 m_inherits_xform ? ABC_MATRIX_LOCAL : ABC_MATRIX_WORLD,
97                                 m_proxy_from);
98
99         /* Only apply rotation to root camera, parenting will propagate it. */
100         if (m_object->type == OB_CAMERA && (!m_inherits_xform || !has_parent_camera(m_object))) {
101                 float rot_mat[4][4];
102                 axis_angle_to_mat4_single(rot_mat, 'X', -M_PI_2);
103                 mul_m4_m4m4(yup_mat, yup_mat, rot_mat);
104         }
105
106         if (!m_object->parent || !m_inherits_xform) {
107                 /* Only apply scaling to root objects, parenting will propagate it. */
108                 float scale_mat[4][4];
109                 scale_m4_fl(scale_mat, m_settings.global_scale);
110                 scale_mat[3][3] = m_settings.global_scale;  /* also scale translation */
111                 mul_m4_m4m4(yup_mat, yup_mat, scale_mat);
112                 yup_mat[3][3] /= m_settings.global_scale;  /* normalise the homogeneous component */
113         }
114
115         m_matrix = convert_matrix(yup_mat);
116         m_sample.setMatrix(m_matrix);
117         m_sample.setInheritsXforms(m_inherits_xform);
118         m_schema.set(m_sample);
119 }
120
121 Imath::Box3d AbcTransformWriter::bounds()
122 {
123         Imath::Box3d bounds;
124
125         for (int i = 0; i < m_children.size(); ++i) {
126                 Imath::Box3d box(m_children[i]->bounds());
127                 bounds.extendBy(box);
128         }
129
130         return Imath::transform(bounds, m_matrix);
131 }
132
133 bool AbcTransformWriter::hasAnimation(Object * /*ob*/) const
134 {
135         /* TODO(kevin): implement this. */
136         return true;
137 }
138
139 /* ************************************************************************** */
140
141 AbcEmptyReader::AbcEmptyReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
142     : AbcObjectReader(object, settings)
143 {
144         /* Empties have no data. It makes the import of Alembic files easier to
145          * understand when we name the empty after its name in Alembic. */
146         m_object_name = object.getName();
147
148         Alembic::AbcGeom::IXform xform(object, Alembic::AbcGeom::kWrapExisting);
149         m_schema = xform.getSchema();
150
151         get_min_max_time(m_iobject, m_schema, m_min_time, m_max_time);
152 }
153
154 bool AbcEmptyReader::valid() const
155 {
156         return m_schema.valid();
157 }
158
159 bool AbcEmptyReader::accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
160                                          const Object *const ob,
161                                          const char **err_str) const
162 {
163         if (!Alembic::AbcGeom::IXform::matches(alembic_header)) {
164                 *err_str = "Object type mismatch, Alembic object path pointed to XForm when importing, but not any more.";
165                 return false;
166         }
167
168         if (ob->type != OB_EMPTY) {
169                 *err_str = "Object type mismatch, Alembic object path points to XForm.";
170                 return false;
171         }
172
173         return true;
174 }
175
176 void AbcEmptyReader::readObjectData(Main *bmain, const ISampleSelector &UNUSED(sample_sel))
177 {
178         m_object = BKE_object_add_only_object(bmain, OB_EMPTY,
179                                               m_object_name.c_str());
180         m_object->data = NULL;
181 }