2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * Contributor(s): Esteban Tovagliari, Cedric Paille, Kevin Dietrich
20 * ***** END GPL LICENSE BLOCK *****
25 #include "abc_camera.h"
26 #include "abc_curves.h"
28 #include "abc_nurbs.h"
29 #include "abc_points.h"
30 #include "abc_transform.h"
32 #include <Alembic/AbcMaterial/IMaterial.h>
37 #include "DNA_object_types.h"
44 std::string get_id_name(const Object * const ob)
50 return get_id_name(&ob->id);
53 std::string get_id_name(const ID * const id)
55 std::string name(id->name + 2);
56 std::replace(name.begin(), name.end(), ' ', '_');
57 std::replace(name.begin(), name.end(), '.', '_');
58 std::replace(name.begin(), name.end(), ':', '_');
63 std::string get_object_dag_path_name(const Object * const ob, Object *dupli_parent)
65 std::string name = get_id_name(ob);
67 Object *p = ob->parent;
70 name = get_id_name(p) + "/" + name;
74 if (dupli_parent && (ob != dupli_parent)) {
75 name = get_id_name(dupli_parent) + "/" + name;
81 bool object_selected(Object *ob)
83 return ob->flag & SELECT;
86 bool parent_selected(Object *ob)
88 if (object_selected(ob)) {
92 bool do_export = false;
94 Object *parent = ob->parent;
96 while (parent != NULL) {
97 if (object_selected(parent)) {
102 parent = parent->parent;
108 Imath::M44d convert_matrix(float mat[4][4])
112 for (int i = 0; i < 4; ++i) {
113 for (int j = 0; j < 4; ++j) {
121 void split(const std::string &s, const char delim, std::vector<std::string> &tokens)
125 std::stringstream ss(s);
128 while (std::getline(ss, item, delim)) {
130 tokens.push_back(item);
135 void create_swapped_rotation_matrix(
136 float rot_x_mat[3][3], float rot_y_mat[3][3],
137 float rot_z_mat[3][3], const float euler[3],
138 AbcAxisSwapMode mode)
140 const float rx = euler[0];
144 /* Apply transformation */
146 case ABC_ZUP_FROM_YUP:
150 case ABC_YUP_FROM_ZUP:
162 rot_x_mat[1][1] = cos(rx);
163 rot_x_mat[2][1] = -sin(rx);
164 rot_x_mat[1][2] = sin(rx);
165 rot_x_mat[2][2] = cos(rx);
167 rot_y_mat[2][2] = cos(ry);
168 rot_y_mat[0][2] = -sin(ry);
169 rot_y_mat[2][0] = sin(ry);
170 rot_y_mat[0][0] = cos(ry);
172 rot_z_mat[0][0] = cos(rz);
173 rot_z_mat[1][0] = -sin(rz);
174 rot_z_mat[0][1] = sin(rz);
175 rot_z_mat[1][1] = cos(rz);
178 /* Convert matrix from Z=up to Y=up or vice versa. Use yup_mat = zup_mat for in-place conversion. */
179 void copy_m44_axis_swap(float dst_mat[4][4], float src_mat[4][4], AbcAxisSwapMode mode)
181 float dst_rot[3][3], src_rot[3][3], dst_scale_mat[4][4];
182 float rot_x_mat[3][3], rot_y_mat[3][3], rot_z_mat[3][3];
183 float src_trans[3], dst_scale[3], src_scale[3], euler[3];
191 unit_m4(dst_scale_mat);
193 /* We assume there is no sheer component and no homogeneous scaling component. */
194 BLI_assert(fabs(src_mat[0][3]) < 2 * FLT_EPSILON);
195 BLI_assert(fabs(src_mat[1][3]) < 2 * FLT_EPSILON);
196 BLI_assert(fabs(src_mat[2][3]) < 2 * FLT_EPSILON);
197 BLI_assert(fabs(src_mat[3][3] - 1.0f) < 2 * FLT_EPSILON);
199 /* Extract translation, rotation, and scale form matrix. */
200 mat4_to_loc_rot_size(src_trans, src_rot, src_scale, src_mat);
202 /* Get euler angles from rotation matrix. */
203 mat3_to_eulO(euler, ROT_MODE_XZY, src_rot);
205 /* Create X, Y, Z rotation matrices from euler angles. */
206 create_swapped_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler, mode);
208 /* Concatenate rotation matrices. */
209 mul_m3_m3m3(dst_rot, dst_rot, rot_z_mat);
210 mul_m3_m3m3(dst_rot, dst_rot, rot_y_mat);
211 mul_m3_m3m3(dst_rot, dst_rot, rot_x_mat);
213 mat3_to_eulO(euler, ROT_MODE_XZY, dst_rot);
215 /* Start construction of dst_mat from rotation matrix */
217 copy_m4_m3(dst_mat, dst_rot);
219 /* Apply translation */
221 case ABC_ZUP_FROM_YUP:
222 copy_zup_from_yup(dst_mat[3], src_trans);
224 case ABC_YUP_FROM_ZUP:
225 copy_yup_from_zup(dst_mat[3], src_trans);
231 /* Apply scale matrix. Swaps y and z, but does not
232 * negate like translation does. */
233 dst_scale[0] = src_scale[0];
234 dst_scale[1] = src_scale[2];
235 dst_scale[2] = src_scale[1];
237 size_to_mat4(dst_scale_mat, dst_scale);
238 mul_m4_m4m4(dst_mat, dst_mat, dst_scale_mat);
241 void convert_matrix(const Imath::M44d &xform, Object *ob, float r_mat[4][4])
243 for (int i = 0; i < 4; ++i) {
244 for (int j = 0; j < 4; ++j) {
245 r_mat[i][j] = static_cast<float>(xform[i][j]);
249 if (ob->type == OB_CAMERA) {
250 float cam_to_yup[4][4];
251 axis_angle_to_mat4_single(cam_to_yup, 'X', M_PI_2);
252 mul_m4_m4m4(r_mat, r_mat, cam_to_yup);
255 copy_m44_axis_swap(r_mat, r_mat, ABC_ZUP_FROM_YUP);
258 /* Recompute transform matrix of object in new coordinate system
259 * (from Z-Up to Y-Up). */
260 void create_transform_matrix(Object *obj, float r_yup_mat[4][4])
264 /* get local matrix. */
265 /* TODO Sybren: when we're exporting as "flat", i.e. non-hierarchial,
266 * we should export the world matrix even when the object has a parent
269 /* Note that this produces another matrix than the local matrix, due to
270 * constraints and modifiers as well as the obj->parentinv matrix. */
271 invert_m4_m4(obj->parent->imat, obj->parent->obmat);
272 mul_m4_m4m4(zup_mat, obj->parent->imat, obj->obmat);
273 copy_m44_axis_swap(r_yup_mat, zup_mat, ABC_YUP_FROM_ZUP);
276 copy_m44_axis_swap(r_yup_mat, obj->obmat, ABC_YUP_FROM_ZUP);
280 bool has_property(const Alembic::Abc::ICompoundProperty &prop, const std::string &name)
286 return prop.getPropertyHeader(name) != NULL;
289 typedef std::pair<Alembic::AbcCoreAbstract::index_t, float> index_time_pair_t;
291 float get_weight_and_index(float time,
292 const Alembic::AbcCoreAbstract::TimeSamplingPtr &time_sampling,
294 Alembic::AbcGeom::index_t &i0,
295 Alembic::AbcGeom::index_t &i1)
297 samples_number = std::max(samples_number, 1);
299 index_time_pair_t t0 = time_sampling->getFloorIndex(time, samples_number);
302 if (samples_number == 1 || (fabs(time - t0.second) < 0.0001f)) {
306 index_time_pair_t t1 = time_sampling->getCeilIndex(time, samples_number);
313 const float bias = (time - t0.second) / (t1.second - t0.second);
315 if (fabs(1.0f - bias) < 0.0001f) {
325 AbcObjectReader *create_reader(const Alembic::AbcGeom::IObject &object, ImportSettings &settings)
327 AbcObjectReader *reader = NULL;
329 const Alembic::AbcGeom::MetaData &md = object.getMetaData();
331 if (Alembic::AbcGeom::IXform::matches(md)) {
332 reader = new AbcEmptyReader(object, settings);
334 else if (Alembic::AbcGeom::IPolyMesh::matches(md)) {
335 reader = new AbcMeshReader(object, settings);
337 else if (Alembic::AbcGeom::ISubD::matches(md)) {
338 reader = new AbcSubDReader(object, settings);
340 else if (Alembic::AbcGeom::INuPatch::matches(md)) {
342 /* TODO(kevin): importing cyclic NURBS from other software crashes
343 * at the moment. This is due to the fact that NURBS in other
344 * software have duplicated points which causes buffer overflows in
345 * Blender. Need to figure out exactly how these points are
346 * duplicated, in all cases (cyclic U, cyclic V, and cyclic UV).
347 * Until this is fixed, disabling NURBS reading. */
348 reader = new AbcNurbsReader(child, settings);
351 else if (Alembic::AbcGeom::ICamera::matches(md)) {
352 reader = new AbcCameraReader(object, settings);
354 else if (Alembic::AbcGeom::IPoints::matches(md)) {
355 reader = new AbcPointsReader(object, settings);
357 else if (Alembic::AbcMaterial::IMaterial::matches(md)) {
360 else if (Alembic::AbcGeom::ILight::matches(md)) {
363 else if (Alembic::AbcGeom::IFaceSet::matches(md)) {
364 /* Pass, those are handled in the mesh reader. */
366 else if (Alembic::AbcGeom::ICurves::matches(md)) {
367 reader = new AbcCurveReader(object, settings);
370 std::cerr << "Alembic: unknown how to handle objects of schema "
371 << md.get("schemaObjTitle")
372 << ", skipping object "
373 << object.getFullName() << std::endl;
379 /* ********************** */
381 ScopeTimer::ScopeTimer(const char *message)
383 , m_start(PIL_check_seconds_timer())
386 ScopeTimer::~ScopeTimer()
388 fprintf(stderr, "%s: %fs\n", m_message, PIL_check_seconds_timer() - m_start);
391 /* ********************** */
393 bool SimpleLogger::empty()
395 return ((size_t)m_stream.tellp()) == 0ul;
398 std::string SimpleLogger::str() const
400 return m_stream.str();
403 void SimpleLogger::clear()
409 std::ostringstream &SimpleLogger::stream()
414 std::ostream &operator<<(std::ostream &os, const SimpleLogger &logger)