Cleanup: triple quotes for docstrings
[blender.git] / source / blender / alembic / intern / abc_exporter.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_exporter.h"
24
25 #include <cmath>
26
27 #include "abc_archive.h"
28 #include "abc_camera.h"
29 #include "abc_curves.h"
30 #include "abc_hair.h"
31 #include "abc_mesh.h"
32 #include "abc_nurbs.h"
33 #include "abc_points.h"
34 #include "abc_transform.h"
35 #include "abc_util.h"
36
37 extern "C" {
38 #include "DNA_camera_types.h"
39 #include "DNA_curve_types.h"
40 #include "DNA_mesh_types.h"
41 #include "DNA_modifier_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_space_types.h"  /* for FILE_MAX */
45
46 #include "BLI_string.h"
47
48 #ifdef WIN32
49 /* needed for MSCV because of snprintf from BLI_string */
50 #  include "BLI_winstuff.h"
51 #endif
52
53 #include "BKE_anim.h"
54 #include "BKE_global.h"
55 #include "BKE_idprop.h"
56 #include "BKE_main.h"
57 #include "BKE_modifier.h"
58 #include "BKE_particle.h"
59 #include "BKE_scene.h"
60 }
61
62 using Alembic::Abc::TimeSamplingPtr;
63 using Alembic::Abc::OBox3dProperty;
64
65 /* ************************************************************************** */
66
67 ExportSettings::ExportSettings()
68     : scene(NULL)
69         , logger()
70     , selected_only(false)
71     , visible_layers_only(false)
72     , renderable_only(false)
73     , frame_start(1)
74     , frame_end(1)
75     , frame_step_xform(1)
76     , frame_step_shape(1)
77     , shutter_open(0.0)
78     , shutter_close(1.0)
79     , global_scale(1.0f)
80     , flatten_hierarchy(false)
81     , export_normals(false)
82     , export_uvs(false)
83     , export_vcols(false)
84     , export_face_sets(false)
85     , export_vweigths(false)
86     , apply_subdiv(false)
87     , use_subdiv_schema(false)
88     , export_child_hairs(true)
89     , export_ogawa(true)
90     , pack_uv(false)
91     , triangulate(false)
92     , quad_method(0)
93     , ngon_method(0)
94     , do_convert_axis(false)
95 {}
96
97 static bool object_is_smoke_sim(Object *ob)
98 {
99         ModifierData *md = modifiers_findByType(ob, eModifierType_Smoke);
100
101         if (md) {
102                 SmokeModifierData *smd = reinterpret_cast<SmokeModifierData *>(md);
103                 return (smd->type == MOD_SMOKE_TYPE_DOMAIN);
104         }
105
106         return false;
107 }
108
109 static bool object_is_shape(Object *ob)
110 {
111         switch (ob->type) {
112                 case OB_MESH:
113                         if (object_is_smoke_sim(ob)) {
114                                 return false;
115                         }
116
117                         return true;
118                 case OB_CURVE:
119                 case OB_SURF:
120                 case OB_CAMERA:
121                         return true;
122                 default:
123                         return false;
124         }
125 }
126
127
128 /**
129  * Returns whether this object should be exported into the Alembic file.
130  *
131  * @param settings export settings, used for options like 'selected only'.
132  * @param ob the object in question.
133  * @param is_duplicated normally false; true when the object is instanced
134  *                      into the scene by a dupli-object (e.g. part of a
135  *                      dupligroup). This ignores selection and layer
136  *                      visibility, and assumes that the dupli-object itself
137  *                      (e.g. the group-instantiating empty) is exported.
138  */
139 static bool export_object(const ExportSettings * const settings, Object *ob,
140                           bool is_duplicated)
141 {
142         if (!is_duplicated) {
143                 /* These two tests only make sense when the object isn't being instanced
144                  * into the scene. When it is, its exportability is determined by
145                  * its dupli-object and the DupliObject::no_draw property. */
146                 if (settings->selected_only && !parent_selected(ob)) {
147                         return false;
148                 }
149
150                 if (settings->visible_layers_only && !(settings->scene->lay & ob->lay)) {
151                         return false;
152                 }
153         }
154
155         if (settings->renderable_only && (ob->restrictflag & OB_RESTRICT_RENDER)) {
156                 return false;
157         }
158
159         return true;
160 }
161
162 /* ************************************************************************** */
163
164 AbcExporter::AbcExporter(Scene *scene, const char *filename, ExportSettings &settings)
165     : m_settings(settings)
166     , m_filename(filename)
167     , m_trans_sampling_index(0)
168     , m_shape_sampling_index(0)
169     , m_scene(scene)
170     , m_writer(NULL)
171 {}
172
173 AbcExporter::~AbcExporter()
174 {
175         /* Free xforms map */
176         m_xforms_type::iterator it_x, e_x;
177         for (it_x = m_xforms.begin(), e_x = m_xforms.end(); it_x != e_x; ++it_x) {
178                 delete it_x->second;
179         }
180
181         /* Free shapes vector */
182         for (int i = 0, e = m_shapes.size(); i != e; ++i) {
183                 delete m_shapes[i];
184         }
185
186         delete m_writer;
187 }
188
189 void AbcExporter::getShutterSamples(double step, bool time_relative,
190                                     std::vector<double> &samples)
191 {
192         samples.clear();
193
194         const double time_factor = time_relative ? m_scene->r.frs_sec : 1.0;
195         const double shutter_open = m_settings.shutter_open;
196         const double shutter_close = m_settings.shutter_close;
197
198         /* sample all frame */
199         if (shutter_open == 0.0 && shutter_close == 1.0) {
200                 for (double t = 0.0; t < 1.0; t += step) {
201                         samples.push_back((t + m_settings.frame_start) / time_factor);
202                 }
203         }
204         else {
205                 /* sample between shutter open & close */
206                 const int nsamples = static_cast<int>(std::max((1.0 / step) - 1.0, 1.0));
207                 const double time_inc = (shutter_close - shutter_open) / nsamples;
208
209                 for (double t = shutter_open; t <= shutter_close; t += time_inc) {
210                         samples.push_back((t + m_settings.frame_start) / time_factor);
211                 }
212         }
213 }
214
215 Alembic::Abc::TimeSamplingPtr AbcExporter::createTimeSampling(double step)
216 {
217         TimeSamplingPtr time_sampling;
218         std::vector<double> samples;
219
220         if (m_settings.frame_start == m_settings.frame_end) {
221                 time_sampling.reset(new Alembic::Abc::TimeSampling());
222                 return time_sampling;
223         }
224
225         getShutterSamples(step, true, samples);
226
227         Alembic::Abc::TimeSamplingType ts(static_cast<uint32_t>(samples.size()), 1.0 / m_scene->r.frs_sec);
228         time_sampling.reset(new Alembic::Abc::TimeSampling(ts, samples));
229
230         return time_sampling;
231 }
232
233 void AbcExporter::getFrameSet(double step, std::set<double> &frames)
234 {
235         frames.clear();
236
237         std::vector<double> shutter_samples;
238
239         getShutterSamples(step, false, shutter_samples);
240
241         for (double frame = m_settings.frame_start; frame <= m_settings.frame_end; frame += 1.0) {
242                 for (int j = 0, e = shutter_samples.size(); j < e; ++j) {
243                         frames.insert(frame + shutter_samples[j]);
244                 }
245         }
246 }
247
248 void AbcExporter::operator()(Main *bmain, float &progress, bool &was_canceled)
249 {
250         std::string scene_name;
251
252         if (bmain->name[0] != '\0') {
253                 char scene_file_name[FILE_MAX];
254                 BLI_strncpy(scene_file_name, bmain->name, FILE_MAX);
255                 scene_name = scene_file_name;
256         }
257         else {
258                 scene_name = "untitled";
259         }
260
261         Scene *scene = m_scene;
262         const double fps = FPS;
263         char buf[16];
264         snprintf(buf, 15, "%f", fps);
265         const std::string str_fps = buf;
266
267         Alembic::AbcCoreAbstract::MetaData md;
268         md.set("FramesPerTimeUnit", str_fps);
269
270         m_writer = new ArchiveWriter(m_filename, scene_name.c_str(), m_settings.export_ogawa, md);
271
272         /* Create time samplings for transforms and shapes. */
273
274         TimeSamplingPtr trans_time = createTimeSampling(m_settings.frame_step_xform);
275
276         m_trans_sampling_index = m_writer->archive().addTimeSampling(*trans_time);
277
278         TimeSamplingPtr shape_time;
279
280         if ((m_settings.frame_step_shape == m_settings.frame_step_xform) ||
281             (m_settings.frame_start == m_settings.frame_end))
282         {
283                 shape_time = trans_time;
284                 m_shape_sampling_index = m_trans_sampling_index;
285         }
286         else {
287                 shape_time = createTimeSampling(m_settings.frame_step_shape);
288                 m_shape_sampling_index = m_writer->archive().addTimeSampling(*shape_time);
289         }
290
291         OBox3dProperty archive_bounds_prop = Alembic::AbcGeom::CreateOArchiveBounds(m_writer->archive(), m_trans_sampling_index);
292
293         if (m_settings.flatten_hierarchy) {
294                 createTransformWritersFlat();
295         }
296         else {
297                 createTransformWritersHierarchy(bmain->eval_ctx);
298         }
299
300         createShapeWriters(bmain->eval_ctx);
301
302         /* Make a list of frames to export. */
303
304         std::set<double> xform_frames;
305         getFrameSet(m_settings.frame_step_xform, xform_frames);
306
307         std::set<double> shape_frames;
308         getFrameSet(m_settings.frame_step_shape, shape_frames);
309
310         /* Merge all frames needed. */
311
312         std::set<double> frames(xform_frames);
313         frames.insert(shape_frames.begin(), shape_frames.end());
314
315         /* Export all frames. */
316
317         std::set<double>::const_iterator begin = frames.begin();
318         std::set<double>::const_iterator end = frames.end();
319
320         const float size = static_cast<float>(frames.size());
321         size_t i = 0;
322
323         for (; begin != end; ++begin) {
324                 progress = (++i / size);
325
326                 if (G.is_break) {
327                         was_canceled = true;
328                         break;
329                 }
330
331                 const double frame = *begin;
332
333                 /* 'frame' is offset by start frame, so need to cancel the offset. */
334                 setCurrentFrame(bmain, frame - m_settings.frame_start);
335
336                 if (shape_frames.count(frame) != 0) {
337                         for (int i = 0, e = m_shapes.size(); i != e; ++i) {
338                                 m_shapes[i]->write();
339                         }
340                 }
341
342                 if (xform_frames.count(frame) == 0) {
343                         continue;
344                 }
345
346                 m_xforms_type::iterator xit, xe;
347                 for (xit = m_xforms.begin(), xe = m_xforms.end(); xit != xe; ++xit) {
348                         xit->second->write();
349                 }
350
351                 /* Save the archive 's bounding box. */
352                 Imath::Box3d bounds;
353
354                 for (xit = m_xforms.begin(), xe = m_xforms.end(); xit != xe; ++xit) {
355                         Imath::Box3d box = xit->second->bounds();
356                         bounds.extendBy(box);
357                 }
358
359                 archive_bounds_prop.set(bounds);
360         }
361 }
362
363 void AbcExporter::createTransformWritersHierarchy(EvaluationContext *eval_ctx)
364 {
365         Base *base = static_cast<Base *>(m_scene->base.first);
366
367         while (base) {
368                 Object *ob = base->object;
369
370                 switch (ob->type) {
371                         case OB_LAMP:
372                         case OB_LATTICE:
373                         case OB_MBALL:
374                         case OB_SPEAKER:
375                                 /* We do not export transforms for objects of these classes. */
376                                 break;
377
378                         default:
379                                 exploreTransform(eval_ctx, ob, ob->parent);
380                 }
381
382                 base = base->next;
383         }
384 }
385
386 void AbcExporter::createTransformWritersFlat()
387 {
388         Base *base = static_cast<Base *>(m_scene->base.first);
389
390         while (base) {
391                 Object *ob = base->object;
392
393                 if (export_object(&m_settings, ob, false) && object_is_shape(ob)) {
394                         std::string name = get_id_name(ob);
395                         m_xforms[name] = new AbcTransformWriter(
396                                              ob, m_writer->archive().getTop(), NULL,
397                                              m_trans_sampling_index, m_settings);
398                 }
399
400                 base = base->next;
401         }
402 }
403
404 void AbcExporter::exploreTransform(EvaluationContext *eval_ctx, Object *ob, Object *parent, Object *dupliObParent)
405 {
406         /* If an object isn't exported itself, its duplilist shouldn't be
407          * exported either. */
408         if (!export_object(&m_settings, ob, dupliObParent != NULL)) {
409                 return;
410         }
411
412         if (object_is_shape(ob)) {
413                 createTransformWriter(ob, parent, dupliObParent);
414         }
415
416         ListBase *lb = object_duplilist(eval_ctx, m_scene, ob);
417
418         if (lb) {
419                 DupliObject *link = static_cast<DupliObject *>(lb->first);
420                 Object *dupli_ob = NULL;
421                 Object *dupli_parent = NULL;
422                 
423                 for (; link; link = link->next) {
424                         /* This skips things like custom bone shapes. */
425                         if (m_settings.renderable_only && link->no_draw) {
426                                 continue;
427                         }
428
429                         if (link->type == OB_DUPLIGROUP) {
430                                 dupli_ob = link->ob;
431                                 dupli_parent = (dupli_ob->parent) ? dupli_ob->parent : ob;
432
433                                 exploreTransform(eval_ctx, dupli_ob, dupli_parent, ob);
434                         }
435                 }
436         }
437
438         free_object_duplilist(lb);
439 }
440
441 AbcTransformWriter * AbcExporter::createTransformWriter(Object *ob, Object *parent, Object *dupliObParent)
442 {
443         const std::string name = get_object_dag_path_name(ob, dupliObParent);
444
445         /* An object should not be its own parent, or we'll get infinite loops. */
446         BLI_assert(ob != parent);
447         BLI_assert(ob != dupliObParent);
448
449         /* check if we have already created a transform writer for this object */
450         AbcTransformWriter *my_writer = getXForm(name);
451         if (my_writer != NULL){
452                 return my_writer;
453         }
454
455         AbcTransformWriter *parent_writer = NULL;
456         Alembic::Abc::OObject alembic_parent;
457
458         if (parent) {
459                 /* Since there are so many different ways to find parents (as evident
460                  * in the number of conditions below), we can't really look up the
461                  * parent by name. We'll just call createTransformWriter(), which will
462                  * return the parent's AbcTransformWriter pointer. */
463                 if (parent->parent) {
464                         parent_writer = createTransformWriter(parent, parent->parent, dupliObParent);
465                 }
466                 else if (parent == dupliObParent) {
467                         if (dupliObParent->parent == NULL) {
468                                 parent_writer = createTransformWriter(parent, NULL, NULL);
469                         }
470                         else {
471                                 parent_writer = createTransformWriter(parent, dupliObParent->parent, dupliObParent->parent);
472                         }
473                 }
474                 else {
475                         parent_writer = createTransformWriter(parent, dupliObParent, dupliObParent);
476                 }
477
478                 BLI_assert(parent_writer);
479                 alembic_parent = parent_writer->alembicXform();
480         }
481         else {
482                 /* Parentless objects still have the "top object" as parent
483                  * in Alembic. */
484                 alembic_parent = m_writer->archive().getTop();
485         }
486
487         my_writer = new AbcTransformWriter(ob, alembic_parent, parent_writer,
488                                            m_trans_sampling_index, m_settings);
489         m_xforms[name] = my_writer;
490         return my_writer;
491 }
492
493 void AbcExporter::createShapeWriters(EvaluationContext *eval_ctx)
494 {
495         Base *base = static_cast<Base *>(m_scene->base.first);
496
497         while (base) {
498                 Object *ob = base->object;
499                 exploreObject(eval_ctx, ob, NULL);
500
501                 base = base->next;
502         }
503 }
504
505 void AbcExporter::exploreObject(EvaluationContext *eval_ctx, Object *ob, Object *dupliObParent)
506 {
507         /* If an object isn't exported itself, its duplilist shouldn't be
508          * exported either. */
509         if (!export_object(&m_settings, ob, dupliObParent != NULL)) {
510                 return;
511         }
512
513         createShapeWriter(ob, dupliObParent);
514         
515         ListBase *lb = object_duplilist(eval_ctx, m_scene, ob);
516
517         if (lb) {
518                 DupliObject *link = static_cast<DupliObject *>(lb->first);
519
520                 for (; link; link = link->next) {
521                         /* This skips things like custom bone shapes. */
522                         if (m_settings.renderable_only && link->no_draw) {
523                                 continue;
524                         }
525
526                         if (link->type == OB_DUPLIGROUP) {
527                                 exploreObject(eval_ctx, link->ob, ob);
528                         }
529                 }
530         }
531
532         free_object_duplilist(lb);
533 }
534
535 void AbcExporter::createShapeWriter(Object *ob, Object *dupliObParent)
536 {
537         if (!object_is_shape(ob)) {
538                 return;
539         }
540
541         std::string name;
542
543         if (m_settings.flatten_hierarchy) {
544                 name = get_id_name(ob);
545         }
546         else {
547                 name = get_object_dag_path_name(ob, dupliObParent);
548         }
549         
550         AbcTransformWriter *xform = getXForm(name);
551
552         if (!xform) {
553                 ABC_LOG(m_settings.logger) << __func__ << ": xform " << name << " is NULL\n";
554                 return;
555         }
556
557         ParticleSystem *psys = static_cast<ParticleSystem *>(ob->particlesystem.first);
558
559         for (; psys; psys = psys->next) {
560                 if (!psys_check_enabled(ob, psys, G.is_rendering) || !psys->part) {
561                         continue;
562                 }
563
564                 if (psys->part->type == PART_HAIR) {
565                         m_settings.export_child_hairs = true;
566                         m_shapes.push_back(new AbcHairWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings, psys));
567                 }
568                 else if (psys->part->type == PART_EMITTER) {
569                         m_shapes.push_back(new AbcPointsWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings, psys));
570                 }
571         }
572
573         switch (ob->type) {
574                 case OB_MESH:
575                 {
576                         Mesh *me = static_cast<Mesh *>(ob->data);
577
578                         if (!me || me->totvert == 0) {
579                                 return;
580                         }
581
582                         m_shapes.push_back(new AbcMeshWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings));
583                         break;
584                 }
585                 case OB_SURF:
586                 {
587                         Curve *cu = static_cast<Curve *>(ob->data);
588
589                         if (!cu) {
590                                 return;
591                         }
592
593                         m_shapes.push_back(new AbcNurbsWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings));
594                         break;
595                 }
596                 case OB_CURVE:
597                 {
598                         Curve *cu = static_cast<Curve *>(ob->data);
599
600                         if (!cu) {
601                                 return;
602                         }
603
604                         m_shapes.push_back(new AbcCurveWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings));
605                         break;
606                 }
607                 case OB_CAMERA:
608                 {
609                         Camera *cam = static_cast<Camera *>(ob->data);
610
611                         if (cam->type == CAM_PERSP) {
612                                 m_shapes.push_back(new AbcCameraWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings));
613                         }
614
615                         break;
616                 }
617         }
618 }
619
620 AbcTransformWriter *AbcExporter::getXForm(const std::string &name)
621 {
622         std::map<std::string, AbcTransformWriter *>::iterator it = m_xforms.find(name);
623
624         if (it == m_xforms.end()) {
625                 return NULL;
626         }
627
628         return it->second;
629 }
630
631 void AbcExporter::setCurrentFrame(Main *bmain, double t)
632 {
633         m_scene->r.cfra = static_cast<int>(t);
634         m_scene->r.subframe = static_cast<float>(t) - m_scene->r.cfra;
635         BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, m_scene, m_scene->lay);
636 }