Alembic: don't use m_shapes_type
[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 static bool export_object(const ExportSettings * const settings, Object *ob)
128 {
129         if (settings->selected_only && !parent_selected(ob)) {
130                 return false;
131         }
132
133         if (settings->visible_layers_only && !(settings->scene->lay & ob->lay)) {
134                 return false;
135         }
136
137         if (settings->renderable_only && (ob->restrictflag & OB_RESTRICT_RENDER)) {
138                 return false;
139         }
140
141         return true;
142 }
143
144 /* ************************************************************************** */
145
146 AbcExporter::AbcExporter(Scene *scene, const char *filename, ExportSettings &settings)
147     : m_settings(settings)
148     , m_filename(filename)
149     , m_trans_sampling_index(0)
150     , m_shape_sampling_index(0)
151     , m_scene(scene)
152     , m_writer(NULL)
153 {}
154
155 AbcExporter::~AbcExporter()
156 {
157         /* Free xforms map */
158         m_xforms_type::iterator it_x, e_x;
159         for (it_x = m_xforms.begin(), e_x = m_xforms.end(); it_x != e_x; ++it_x) {
160                 delete it_x->second;
161         }
162
163         /* Free shapes vector */
164         for (int i = 0, e = m_shapes.size(); i != e; ++i) {
165                 delete m_shapes[i];
166         }
167
168         delete m_writer;
169 }
170
171 void AbcExporter::getShutterSamples(double step, bool time_relative,
172                                     std::vector<double> &samples)
173 {
174         samples.clear();
175
176         const double time_factor = time_relative ? m_scene->r.frs_sec : 1.0;
177         const double shutter_open = m_settings.shutter_open;
178         const double shutter_close = m_settings.shutter_close;
179
180         /* sample all frame */
181         if (shutter_open == 0.0 && shutter_close == 1.0) {
182                 for (double t = 0.0; t < 1.0; t += step) {
183                         samples.push_back((t + m_settings.frame_start) / time_factor);
184                 }
185         }
186         else {
187                 /* sample between shutter open & close */
188                 const int nsamples = static_cast<int>(std::max((1.0 / step) - 1.0, 1.0));
189                 const double time_inc = (shutter_close - shutter_open) / nsamples;
190
191                 for (double t = shutter_open; t <= shutter_close; t += time_inc) {
192                         samples.push_back((t + m_settings.frame_start) / time_factor);
193                 }
194         }
195 }
196
197 Alembic::Abc::TimeSamplingPtr AbcExporter::createTimeSampling(double step)
198 {
199         TimeSamplingPtr time_sampling;
200         std::vector<double> samples;
201
202         if (m_settings.frame_start == m_settings.frame_end) {
203                 time_sampling.reset(new Alembic::Abc::TimeSampling());
204                 return time_sampling;
205         }
206
207         getShutterSamples(step, true, samples);
208
209         Alembic::Abc::TimeSamplingType ts(static_cast<uint32_t>(samples.size()), 1.0 / m_scene->r.frs_sec);
210         time_sampling.reset(new Alembic::Abc::TimeSampling(ts, samples));
211
212         return time_sampling;
213 }
214
215 void AbcExporter::getFrameSet(double step, std::set<double> &frames)
216 {
217         frames.clear();
218
219         std::vector<double> shutter_samples;
220
221         getShutterSamples(step, false, shutter_samples);
222
223         for (double frame = m_settings.frame_start; frame <= m_settings.frame_end; frame += 1.0) {
224                 for (int j = 0, e = shutter_samples.size(); j < e; ++j) {
225                         frames.insert(frame + shutter_samples[j]);
226                 }
227         }
228 }
229
230 void AbcExporter::operator()(Main *bmain, float &progress, bool &was_canceled)
231 {
232         std::string scene_name;
233
234         if (bmain->name[0] != '\0') {
235                 char scene_file_name[FILE_MAX];
236                 BLI_strncpy(scene_file_name, bmain->name, FILE_MAX);
237                 scene_name = scene_file_name;
238         }
239         else {
240                 scene_name = "untitled";
241         }
242
243         Scene *scene = m_scene;
244         const double fps = FPS;
245         char buf[16];
246         snprintf(buf, 15, "%f", fps);
247         const std::string str_fps = buf;
248
249         Alembic::AbcCoreAbstract::MetaData md;
250         md.set("FramesPerTimeUnit", str_fps);
251
252         m_writer = new ArchiveWriter(m_filename, scene_name.c_str(), m_settings.export_ogawa, md);
253
254         /* Create time samplings for transforms and shapes. */
255
256         TimeSamplingPtr trans_time = createTimeSampling(m_settings.frame_step_xform);
257
258         m_trans_sampling_index = m_writer->archive().addTimeSampling(*trans_time);
259
260         TimeSamplingPtr shape_time;
261
262         if ((m_settings.frame_step_shape == m_settings.frame_step_xform) ||
263             (m_settings.frame_start == m_settings.frame_end))
264         {
265                 shape_time = trans_time;
266                 m_shape_sampling_index = m_trans_sampling_index;
267         }
268         else {
269                 shape_time = createTimeSampling(m_settings.frame_step_shape);
270                 m_shape_sampling_index = m_writer->archive().addTimeSampling(*shape_time);
271         }
272
273         OBox3dProperty archive_bounds_prop = Alembic::AbcGeom::CreateOArchiveBounds(m_writer->archive(), m_trans_sampling_index);
274
275         if (m_settings.flatten_hierarchy) {
276                 createTransformWritersFlat();
277         }
278         else {
279                 createTransformWritersHierarchy(bmain->eval_ctx);
280         }
281
282         createShapeWriters(bmain->eval_ctx);
283
284         /* Make a list of frames to export. */
285
286         std::set<double> xform_frames;
287         getFrameSet(m_settings.frame_step_xform, xform_frames);
288
289         std::set<double> shape_frames;
290         getFrameSet(m_settings.frame_step_shape, shape_frames);
291
292         /* Merge all frames needed. */
293
294         std::set<double> frames(xform_frames);
295         frames.insert(shape_frames.begin(), shape_frames.end());
296
297         /* Export all frames. */
298
299         std::set<double>::const_iterator begin = frames.begin();
300         std::set<double>::const_iterator end = frames.end();
301
302         const float size = static_cast<float>(frames.size());
303         size_t i = 0;
304
305         for (; begin != end; ++begin) {
306                 progress = (++i / size);
307
308                 if (G.is_break) {
309                         was_canceled = true;
310                         break;
311                 }
312
313                 const double frame = *begin;
314
315                 /* 'frame' is offset by start frame, so need to cancel the offset. */
316                 setCurrentFrame(bmain, frame - m_settings.frame_start);
317
318                 if (shape_frames.count(frame) != 0) {
319                         for (int i = 0, e = m_shapes.size(); i != e; ++i) {
320                                 m_shapes[i]->write();
321                         }
322                 }
323
324                 if (xform_frames.count(frame) == 0) {
325                         continue;
326                 }
327
328                 m_xforms_type::iterator xit, xe;
329                 for (xit = m_xforms.begin(), xe = m_xforms.end(); xit != xe; ++xit) {
330                         xit->second->write();
331                 }
332
333                 /* Save the archive 's bounding box. */
334                 Imath::Box3d bounds;
335
336                 for (xit = m_xforms.begin(), xe = m_xforms.end(); xit != xe; ++xit) {
337                         Imath::Box3d box = xit->second->bounds();
338                         bounds.extendBy(box);
339                 }
340
341                 archive_bounds_prop.set(bounds);
342         }
343 }
344
345 void AbcExporter::createTransformWritersHierarchy(EvaluationContext *eval_ctx)
346 {
347         Base *base = static_cast<Base *>(m_scene->base.first);
348
349         while (base) {
350                 Object *ob = base->object;
351
352                 if (export_object(&m_settings, ob)) {
353                         switch (ob->type) {
354                                 case OB_LAMP:
355                                 case OB_LATTICE:
356                                 case OB_MBALL:
357                                 case OB_SPEAKER:
358                                         /* We do not export transforms for objects of these classes. */
359                                         break;
360
361                                 default:
362                                         exploreTransform(eval_ctx, ob, ob->parent, NULL);
363                         }
364                 }
365
366                 base = base->next;
367         }
368 }
369
370 void AbcExporter::createTransformWritersFlat()
371 {
372         Base *base = static_cast<Base *>(m_scene->base.first);
373
374         while (base) {
375                 Object *ob = base->object;
376
377                 if (export_object(&m_settings, ob) && object_is_shape(ob)) {
378                         std::string name = get_id_name(ob);
379                         m_xforms[name] = new AbcTransformWriter(ob, m_writer->archive().getTop(), 0, m_trans_sampling_index, m_settings);
380                 }
381
382                 base = base->next;
383         }
384 }
385
386 void AbcExporter::exploreTransform(EvaluationContext *eval_ctx, Object *ob, Object *parent, Object *dupliObParent)
387 {
388
389         if (export_object(&m_settings, ob) && object_is_shape(ob)) {
390                 createTransformWriter(ob, parent, dupliObParent);
391         }
392
393         ListBase *lb = object_duplilist(eval_ctx, m_scene, ob);
394
395         if (lb) {
396                 DupliObject *link = static_cast<DupliObject *>(lb->first);
397                 Object *dupli_ob = NULL;
398                 Object *dupli_parent = NULL;
399                 
400                 while (link) {
401                         if (link->type == OB_DUPLIGROUP) {
402                                 dupli_ob = link->ob;
403                                 dupli_parent = (dupli_ob->parent) ? dupli_ob->parent : ob;
404
405                                 exploreTransform(eval_ctx, dupli_ob, dupli_parent, ob);
406                         }
407
408                         link = link->next;
409                 }
410         }
411
412         free_object_duplilist(lb);
413 }
414
415 AbcTransformWriter * AbcExporter::createTransformWriter(Object *ob, Object *parent, Object *dupliObParent)
416 {
417         const std::string name = get_object_dag_path_name(ob, dupliObParent);
418
419         /* An object should not be its own parent, or we'll get infinite loops. */
420         BLI_assert(ob != parent);
421         BLI_assert(ob != dupliObParent);
422
423         /* check if we have already created a transform writer for this object */
424         AbcTransformWriter *my_writer = getXForm(name);
425         if (my_writer != NULL){
426                 return my_writer;
427         }
428
429         AbcTransformWriter *parent_writer = NULL;
430         Alembic::Abc::OObject alembic_parent;
431
432         if (parent) {
433                 /* Since there are so many different ways to find parents (as evident
434                  * in the number of conditions below), we can't really look up the
435                  * parent by name. We'll just call createTransformWriter(), which will
436                  * return the parent's AbcTransformWriter pointer. */
437                 if (parent->parent) {
438                         parent_writer = createTransformWriter(parent, parent->parent, dupliObParent);
439                 }
440                 else if (parent == dupliObParent) {
441                         if (dupliObParent->parent == NULL) {
442                                 parent_writer = createTransformWriter(parent, NULL, NULL);
443                         }
444                         else {
445                                 parent_writer = createTransformWriter(parent, dupliObParent->parent, dupliObParent->parent);
446                         }
447                 }
448                 else {
449                         parent_writer = createTransformWriter(parent, dupliObParent, dupliObParent);
450                 }
451
452                 BLI_assert(parent_writer);
453                 alembic_parent = parent_writer->alembicXform();
454         }
455         else {
456                 /* Parentless objects still have the "top object" as parent
457                  * in Alembic. */
458                 alembic_parent = m_writer->archive().getTop();
459         }
460
461         my_writer = new AbcTransformWriter(ob, alembic_parent, parent_writer,
462                                            m_trans_sampling_index, m_settings);
463         m_xforms[name] = my_writer;
464         return my_writer;
465 }
466
467 void AbcExporter::createShapeWriters(EvaluationContext *eval_ctx)
468 {
469         Base *base = static_cast<Base *>(m_scene->base.first);
470
471         while (base) {
472                 Object *ob = base->object;
473                 exploreObject(eval_ctx, ob, NULL);
474
475                 base = base->next;
476         }
477 }
478
479 void AbcExporter::exploreObject(EvaluationContext *eval_ctx, Object *ob, Object *dupliObParent)
480 {
481         ListBase *lb = object_duplilist(eval_ctx, m_scene, ob);
482         
483         createShapeWriter(ob, dupliObParent);
484         
485         if (lb) {
486                 DupliObject *dupliob = static_cast<DupliObject *>(lb->first);
487
488                 while (dupliob) {
489                         if (dupliob->type == OB_DUPLIGROUP) {
490                                 exploreObject(eval_ctx, dupliob->ob, ob);
491                         }
492
493                         dupliob = dupliob->next;
494                 }
495         }
496
497         free_object_duplilist(lb);
498 }
499
500 void AbcExporter::createShapeWriter(Object *ob, Object *dupliObParent)
501 {
502         if (!object_is_shape(ob)) {
503                 return;
504         }
505
506         if (!export_object(&m_settings, ob)) {
507                 return;
508         }
509
510         std::string name;
511
512         if (m_settings.flatten_hierarchy) {
513                 name = get_id_name(ob);
514         }
515         else {
516                 name = get_object_dag_path_name(ob, dupliObParent);
517         }
518         
519         AbcTransformWriter *xform = getXForm(name);
520
521         if (!xform) {
522                 ABC_LOG(m_settings.logger) << __func__ << ": xform " << name << " is NULL\n";
523                 return;
524         }
525
526         ParticleSystem *psys = static_cast<ParticleSystem *>(ob->particlesystem.first);
527
528         for (; psys; psys = psys->next) {
529                 if (!psys_check_enabled(ob, psys, G.is_rendering) || !psys->part) {
530                         continue;
531                 }
532
533                 if (psys->part->type == PART_HAIR) {
534                         m_settings.export_child_hairs = true;
535                         m_shapes.push_back(new AbcHairWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings, psys));
536                 }
537                 else if (psys->part->type == PART_EMITTER) {
538                         m_shapes.push_back(new AbcPointsWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings, psys));
539                 }
540         }
541
542         switch (ob->type) {
543                 case OB_MESH:
544                 {
545                         Mesh *me = static_cast<Mesh *>(ob->data);
546
547                         if (!me || me->totvert == 0) {
548                                 return;
549                         }
550
551                         m_shapes.push_back(new AbcMeshWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings));
552                         break;
553                 }
554                 case OB_SURF:
555                 {
556                         Curve *cu = static_cast<Curve *>(ob->data);
557
558                         if (!cu) {
559                                 return;
560                         }
561
562                         m_shapes.push_back(new AbcNurbsWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings));
563                         break;
564                 }
565                 case OB_CURVE:
566                 {
567                         Curve *cu = static_cast<Curve *>(ob->data);
568
569                         if (!cu) {
570                                 return;
571                         }
572
573                         m_shapes.push_back(new AbcCurveWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings));
574                         break;
575                 }
576                 case OB_CAMERA:
577                 {
578                         Camera *cam = static_cast<Camera *>(ob->data);
579
580                         if (cam->type == CAM_PERSP) {
581                                 m_shapes.push_back(new AbcCameraWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings));
582                         }
583
584                         break;
585                 }
586         }
587 }
588
589 AbcTransformWriter *AbcExporter::getXForm(const std::string &name)
590 {
591         std::map<std::string, AbcTransformWriter *>::iterator it = m_xforms.find(name);
592
593         if (it == m_xforms.end()) {
594                 return NULL;
595         }
596
597         return it->second;
598 }
599
600 void AbcExporter::setCurrentFrame(Main *bmain, double t)
601 {
602         m_scene->r.cfra = static_cast<int>(t);
603         m_scene->r.subframe = static_cast<float>(t) - m_scene->r.cfra;
604         BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, m_scene, m_scene->lay);
605 }