f71d78bd60e5459464f27d2b812a7572cb72d2e4
[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 #ifdef WITH_ALEMBIC_HDF5
28 #  include <Alembic/AbcCoreHDF5/All.h>
29 #endif
30
31 #include <Alembic/AbcCoreOgawa/All.h>
32
33 #include "abc_camera.h"
34 #include "abc_curves.h"
35 #include "abc_hair.h"
36 #include "abc_mesh.h"
37 #include "abc_nurbs.h"
38 #include "abc_points.h"
39 #include "abc_transform.h"
40 #include "abc_util.h"
41
42 extern "C" {
43 #include "DNA_camera_types.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_mesh_types.h"
46 #include "DNA_modifier_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_space_types.h"  /* for FILE_MAX */
49
50 #include "BLI_string.h"
51
52 #ifdef WIN32
53 /* needed for MSCV because of snprintf from BLI_string */
54 #       include "BLI_winstuff.h"
55 #endif
56
57 #include "BKE_anim.h"
58 #include "BKE_global.h"
59 #include "BKE_idprop.h"
60 #include "BKE_main.h"
61 #include "BKE_modifier.h"
62 #include "BKE_particle.h"
63 #include "BKE_scene.h"
64 }
65
66 using Alembic::Abc::TimeSamplingPtr;
67 using Alembic::Abc::OBox3dProperty;
68
69 /* ************************************************************************** */
70
71 ExportSettings::ExportSettings()
72     : scene(NULL)
73     , selected_only(false)
74     , visible_layers_only(false)
75     , renderable_only(false)
76     , frame_start(1)
77     , frame_end(1)
78     , frame_step_xform(1)
79     , frame_step_shape(1)
80     , shutter_open(0.0)
81     , shutter_close(1.0)
82     , global_scale(1.0f)
83     , flatten_hierarchy(false)
84     , export_normals(false)
85     , export_uvs(false)
86     , export_vcols(false)
87     , export_face_sets(false)
88     , export_vweigths(false)
89     , apply_subdiv(false)
90     , use_subdiv_schema(false)
91     , export_child_hairs(true)
92     , export_ogawa(true)
93     , pack_uv(false)
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 {}
153
154 AbcExporter::~AbcExporter()
155 {
156         std::map<std::string, AbcTransformWriter*>::iterator it, e;
157         for (it = m_xforms.begin(), e = m_xforms.end(); it != e; ++it) {
158                 delete it->second;
159         }
160
161         for (int i = 0, e = m_shapes.size(); i != e; ++i) {
162                 delete m_shapes[i];
163         }
164 }
165
166 void AbcExporter::getShutterSamples(double step, bool time_relative,
167                                     std::vector<double> &samples)
168 {
169         samples.clear();
170
171         const double time_factor = time_relative ? m_scene->r.frs_sec : 1.0;
172         const double shutter_open = m_settings.shutter_open;
173         const double shutter_close = m_settings.shutter_close;
174
175         /* sample all frame */
176         if (shutter_open == 0.0 && shutter_close == 1.0) {
177                 for (double t = 0; t < 1.0; t += step) {
178                         samples.push_back((t + m_settings.frame_start) / time_factor);
179                 }
180         }
181         else {
182                 /* sample between shutter open & close */
183                 const int nsamples = std::max((1.0 / step) - 1.0, 1.0);
184                 const double time_inc = (shutter_close - shutter_open) / nsamples;
185
186                 for (double t = shutter_open; t <= shutter_close; t += time_inc) {
187                         samples.push_back((t + m_settings.frame_start) / time_factor);
188                 }
189         }
190 }
191
192 Alembic::Abc::TimeSamplingPtr AbcExporter::createTimeSampling(double step)
193 {
194         TimeSamplingPtr time_sampling;
195         std::vector<double> samples;
196
197         if (m_settings.frame_start == m_settings.frame_end) {
198                 time_sampling.reset(new Alembic::Abc::TimeSampling());
199                 return time_sampling;
200         }
201
202         getShutterSamples(step, true, samples);
203
204         Alembic::Abc::TimeSamplingType ts(static_cast<uint32_t>(samples.size()), 1.0 / m_scene->r.frs_sec);
205         time_sampling.reset(new Alembic::Abc::TimeSampling(ts, samples));
206
207         return time_sampling;
208 }
209
210 void AbcExporter::getFrameSet(double step, std::set<double> &frames)
211 {
212         frames.clear();
213
214         std::vector<double> shutter_samples;
215
216         getShutterSamples(step, false, shutter_samples);
217
218         for (int frame = m_settings.frame_start; frame <= m_settings.frame_end; ++frame) {
219                 for (int j = 0, e = shutter_samples.size(); j < e; ++j) {
220                         frames.insert(frame + shutter_samples[j]);
221                 }
222         }
223 }
224
225 void AbcExporter::operator()(Main *bmain, float &progress, bool &was_canceled)
226 {
227         std::string scene_name;
228
229         if (bmain->name[0] != '\0') {
230                 char scene_file_name[FILE_MAX];
231                 BLI_strncpy(scene_file_name, bmain->name, FILE_MAX);
232                 scene_name = scene_file_name;
233         }
234         else {
235                 scene_name = "untitled";
236         }
237
238         Scene *scene = m_scene;
239         const int fps = FPS;
240         char buf[16];
241         snprintf(buf, 15, "%d", fps);
242         const std::string str_fps = buf;
243
244         Alembic::AbcCoreAbstract::MetaData md;
245         md.set("FramesPerTimeUnit", str_fps);
246
247         Alembic::Abc::Argument arg(md);
248
249 #ifdef WITH_ALEMBIC_HDF5
250         if (!m_settings.export_ogawa) {
251                 m_archive = Alembic::Abc::CreateArchiveWithInfo(Alembic::AbcCoreHDF5::WriteArchive(),
252                                                                 m_filename,
253                                                                 "Blender",
254                                                                 scene_name,
255                                                                 Alembic::Abc::ErrorHandler::kThrowPolicy,
256                                                                 arg);
257         }
258         else
259 #endif
260         {
261                 m_archive = Alembic::Abc::CreateArchiveWithInfo(Alembic::AbcCoreOgawa::WriteArchive(),
262                                                                 m_filename,
263                                                                 "Blender",
264                                                                 scene_name,
265                                                                 Alembic::Abc::ErrorHandler::kThrowPolicy,
266                                                                 arg);
267         }
268
269         /* Create time samplings for transforms and shapes. */
270
271         TimeSamplingPtr trans_time = createTimeSampling(m_settings.frame_step_xform);
272
273         m_trans_sampling_index = m_archive.addTimeSampling(*trans_time);
274
275         TimeSamplingPtr shape_time;
276
277         if ((m_settings.frame_step_shape == m_settings.frame_step_xform) ||
278             (m_settings.frame_start == m_settings.frame_end))
279         {
280                 shape_time = trans_time;
281                 m_shape_sampling_index = m_trans_sampling_index;
282         }
283         else {
284                 shape_time = createTimeSampling(m_settings.frame_step_shape);
285                 m_shape_sampling_index = m_archive.addTimeSampling(*shape_time);
286         }
287
288         OBox3dProperty archive_bounds_prop = Alembic::AbcGeom::CreateOArchiveBounds(m_archive, m_trans_sampling_index);
289
290         if (m_settings.flatten_hierarchy) {
291                 createTransformWritersFlat();
292         }
293         else {
294                 createTransformWritersHierarchy(bmain->eval_ctx);
295         }
296
297         createShapeWriters(bmain->eval_ctx);
298
299         /* Make a list of frames to export. */
300
301         std::set<double> xform_frames;
302         getFrameSet(m_settings.frame_step_xform, xform_frames);
303
304         std::set<double> shape_frames;
305         getFrameSet(m_settings.frame_step_shape, shape_frames);
306
307         /* Merge all frames needed. */
308
309         std::set<double> frames(xform_frames);
310         frames.insert(shape_frames.begin(), shape_frames.end());
311
312         /* Export all frames. */
313
314         std::set<double>::const_iterator begin = frames.begin();
315         std::set<double>::const_iterator end = frames.end();
316
317         const float size = static_cast<float>(frames.size());
318         size_t i = 0;
319
320         for (; begin != end; ++begin) {
321                 progress = (++i / size);
322
323                 if (G.is_break) {
324                         was_canceled = true;
325                         break;
326                 }
327
328                 const double frame = *begin;
329
330                 /* 'frame' is offset by start frame, so need to cancel the offset. */
331                 setCurrentFrame(bmain, frame - m_settings.frame_start);
332
333                 if (shape_frames.count(frame) != 0) {
334                         for (int i = 0, e = m_shapes.size(); i != e; ++i) {
335                                 m_shapes[i]->write();
336                         }
337                 }
338
339                 if (xform_frames.count(frame) == 0) {
340                         continue;
341                 }
342
343                 std::map<std::string, AbcTransformWriter *>::iterator xit, xe;
344                 for (xit = m_xforms.begin(), xe = m_xforms.end(); xit != xe; ++xit) {
345                         xit->second->write();
346                 }
347
348                 /* Save the archive 's bounding box. */
349                 Imath::Box3d bounds;
350
351                 for (xit = m_xforms.begin(), xe = m_xforms.end(); xit != xe; ++xit) {
352                         Imath::Box3d box = xit->second->bounds();
353                         bounds.extendBy(box);
354                 }
355
356                 archive_bounds_prop.set(bounds);
357         }
358 }
359
360 void AbcExporter::createTransformWritersHierarchy(EvaluationContext *eval_ctx)
361 {
362         Base *base = static_cast<Base *>(m_scene->base.first);
363
364         while (base) {
365                 Object *ob = base->object;
366
367                 if (export_object(&m_settings, ob)) {
368                         switch(ob->type) {
369                                 case OB_LAMP:
370                                 case OB_LATTICE:
371                                 case OB_MBALL:
372                                 case OB_SPEAKER:
373                                         /* We do not export transforms for objects of these classes. */
374                                         break;
375
376                                 default:
377                                         exploreTransform(eval_ctx, ob, ob->parent, NULL);
378                         }
379                 }
380
381                 base = base->next;
382         }
383 }
384
385 void AbcExporter::createTransformWritersFlat()
386 {
387         Base *base = static_cast<Base *>(m_scene->base.first);
388
389         while (base) {
390                 Object *ob = base->object;
391
392                 if (export_object(&m_settings, ob) && object_is_shape(ob)) {
393                         std::string name = get_id_name(ob);
394                         m_xforms[name] = new AbcTransformWriter(ob, m_archive.getTop(), 0, m_trans_sampling_index, m_settings);
395                 }
396
397                 base = base->next;
398         }
399 }
400
401 void AbcExporter::exploreTransform(EvaluationContext *eval_ctx, Object *ob, Object *parent, Object *dupliObParent)
402 {
403         createTransformWriter(ob, parent, dupliObParent);
404
405         ListBase *lb = object_duplilist(eval_ctx, m_scene, ob);
406
407         if (lb) {
408                 DupliObject *link = static_cast<DupliObject *>(lb->first);
409                 Object *dupli_ob = NULL;
410                 Object *dupli_parent = NULL;
411                 
412                 while (link) {
413                         if (link->type == OB_DUPLIGROUP) {
414                                 dupli_ob = link->ob;
415                                 dupli_parent = (dupli_ob->parent) ? dupli_ob->parent : ob;
416
417                                 exploreTransform(eval_ctx, dupli_ob, dupli_parent, ob);
418                         }
419
420                         link = link->next;
421                 }
422         }
423
424         free_object_duplilist(lb);
425 }
426
427 void AbcExporter::createTransformWriter(Object *ob, Object *parent, Object *dupliObParent)
428 {
429         const std::string name = get_object_dag_path_name(ob, dupliObParent);
430
431         /* check if we have already created a transform writer for this object */
432         if (m_xforms.find(name) != m_xforms.end()){
433                 std::cerr << "xform " << name << " already exists\n";
434                 return;
435         }
436
437         AbcTransformWriter *parent_xform = NULL;
438
439         if (parent) {
440                 const std::string parentname = get_object_dag_path_name(parent, dupliObParent);
441                 parent_xform = getXForm(parentname);
442
443                 if (!parent_xform) {
444                         if (parent->parent) {
445                                 createTransformWriter(parent, parent->parent, dupliObParent);
446                         }
447                         else {
448                                 createTransformWriter(parent, dupliObParent, dupliObParent);
449                         }
450
451                         parent_xform = getXForm(parentname);
452                 }
453         }
454
455         if (parent_xform) {
456                 m_xforms[name] = new AbcTransformWriter(ob, parent_xform->alembicXform(), parent_xform, m_trans_sampling_index, m_settings);
457                 m_xforms[name]->setParent(parent);
458         }
459         else {
460                 m_xforms[name] = new AbcTransformWriter(ob, m_archive.getTop(), NULL, m_trans_sampling_index, m_settings);
461         }
462 }
463
464 void AbcExporter::createShapeWriters(EvaluationContext *eval_ctx)
465 {
466         Base *base = static_cast<Base *>(m_scene->base.first);
467
468         while (base) {
469                 Object *ob = base->object;
470                 exploreObject(eval_ctx, ob, NULL);
471
472                 base = base->next;
473         }
474 }
475
476 void AbcExporter::exploreObject(EvaluationContext *eval_ctx, Object *ob, Object *dupliObParent)
477 {
478         ListBase *lb = object_duplilist(eval_ctx, m_scene, ob);
479         
480         createShapeWriter(ob, dupliObParent);
481         
482         if (lb) {
483                 DupliObject *dupliob = static_cast<DupliObject *>(lb->first);
484
485                 while (dupliob) {
486                         if (dupliob->type == OB_DUPLIGROUP) {
487                                 exploreObject(eval_ctx, dupliob->ob, ob);
488                         }
489
490                         dupliob = dupliob->next;
491                 }
492         }
493
494         free_object_duplilist(lb);
495 }
496
497 void AbcExporter::createShapeWriter(Object *ob, Object *dupliObParent)
498 {
499         if (!object_is_shape(ob)) {
500                 return;
501         }
502
503         if (!export_object(&m_settings, ob)) {
504                 return;
505         }
506
507         std::string name;
508
509         if (m_settings.flatten_hierarchy) {
510                 name = get_id_name(ob);
511         }
512         else {
513                 name = get_object_dag_path_name(ob, dupliObParent);
514         }
515         
516         AbcTransformWriter *xform = getXForm(name);
517
518         if (!xform) {
519                 std::cerr << __func__ << ": xform " << name << " is NULL\n";
520                 return;
521         }
522
523         ParticleSystem *psys = static_cast<ParticleSystem *>(ob->particlesystem.first);
524
525         for (; psys; psys = psys->next) {
526                 if (!psys_check_enabled(ob, psys, G.is_rendering) || !psys->part) {
527                         continue;
528                 }
529
530                 if (psys->part->type == PART_HAIR) {
531                         m_settings.export_child_hairs = true;
532                         m_shapes.push_back(new AbcHairWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings, psys));
533                 }
534                 else if (psys->part->type == PART_EMITTER) {
535                         m_shapes.push_back(new AbcPointsWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings, psys));
536                 }
537         }
538
539         switch(ob->type) {
540                 case OB_MESH:
541                 {
542                         Mesh *me = static_cast<Mesh *>(ob->data);
543
544                         if (!me || me->totvert == 0) {
545                                 return;
546                         }
547
548                         m_shapes.push_back(new AbcMeshWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings));
549                         break;
550                 }
551                 case OB_SURF:
552                 {
553                         Curve *cu = static_cast<Curve *>(ob->data);
554
555                         if (!cu) {
556                                 return;
557                         }
558
559                         m_shapes.push_back(new AbcNurbsWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings));
560                         break;
561                 }
562                 case OB_CURVE:
563                 {
564                         Curve *cu = static_cast<Curve *>(ob->data);
565
566                         if (!cu) {
567                                 return;
568                         }
569
570                         m_shapes.push_back(new AbcCurveWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings));
571                         break;
572                 }
573                 case OB_CAMERA:
574                 {
575                         Camera *cam = static_cast<Camera *>(ob->data);
576
577                         if (cam->type == CAM_PERSP) {
578                                 m_shapes.push_back(new AbcCameraWriter(m_scene, ob, xform, m_shape_sampling_index, m_settings));
579                         }
580
581                         break;
582                 }
583         }
584 }
585
586 AbcTransformWriter *AbcExporter::getXForm(const std::string &name)
587 {
588         std::map<std::string, AbcTransformWriter *>::iterator it = m_xforms.find(name);
589
590         if (it == m_xforms.end()) {
591                 return NULL;
592         }
593
594         return it->second;
595 }
596
597 void AbcExporter::setCurrentFrame(Main *bmain, double t)
598 {
599         m_scene->r.cfra = std::floor(t);
600         m_scene->r.subframe = t - m_scene->r.cfra;
601         BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, m_scene, m_scene->lay);
602 }