style edits for function declarations
[blender.git] / source / blender / editors / physics / physics_fluid.c
1 /*
2  * fluidsim.c
3  * 
4  *
5  * ***** BEGIN GPL LICENSE BLOCK *****
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The Original Code is Copyright (C) Blender Foundation
22  * All rights reserved.
23  *
24  * The Original Code is: all of this file.
25  *
26  * Contributor(s): none yet.
27  *
28  * ***** END GPL LICENSE BLOCK *****
29  */
30
31 /** \file blender/editors/physics/physics_fluid.c
32  *  \ingroup edphys
33  */
34
35
36
37
38 #include <math.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/stat.h>
42
43 #include "MEM_guardedalloc.h"
44
45 /* types */
46 #include "DNA_anim_types.h"
47 #include "DNA_action_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_object_fluidsim.h"        
50
51 #include "BLI_blenlib.h"
52 #include "BLI_threads.h"
53 #include "BLI_math.h"
54 #include "BLI_utildefines.h"
55
56 #include "BKE_animsys.h"
57 #include "BKE_armature.h"
58 #include "BKE_blender.h"
59 #include "BKE_context.h"
60 #include "BKE_customdata.h"
61 #include "BKE_DerivedMesh.h"
62 #include "BKE_displist.h"
63 #include "BKE_effect.h"
64 #include "BKE_fluidsim.h"
65 #include "BKE_global.h"
66 #include "BKE_ipo.h"
67 #include "BKE_key.h"
68 #include "BKE_main.h"
69 #include "BKE_modifier.h"
70 #include "BKE_object.h"
71 #include "BKE_report.h"
72 #include "BKE_scene.h"
73 #include "BKE_softbody.h"
74 #include "BKE_unit.h"
75
76
77 #include "LBM_fluidsim.h"
78
79 #include "ED_screen.h"
80 #include "ED_fluidsim.h"
81
82 #include "WM_types.h"
83 #include "WM_api.h"
84
85 #include "physics_intern.h" // own include
86
87 /* enable/disable overall compilation */
88 #ifdef WITH_MOD_FLUID
89
90 #include "WM_api.h"
91
92 #include "DNA_scene_types.h"
93 #include "DNA_ipo_types.h"
94 #include "DNA_mesh_types.h"
95
96 #include "PIL_time.h"
97
98
99 static float get_fluid_viscosity(FluidsimSettings *settings)
100 {
101         switch (settings->viscosityMode) {
102                 case 0:         /* unused */
103                         return -1.0;
104                 case 2:         /* water */
105                         return 1.0e-6;
106                 case 3:         /* some (thick) oil */
107                         return 5.0e-5;
108                 case 4:         /* ca. honey */
109                         return 2.0e-3;
110                 case 1:         /* manual */
111                 default:
112                         return (1.0/pow(10.0, settings->viscosityExponent)) * settings->viscosityValue;
113         }
114 }
115
116 static void get_fluid_gravity(float *gravity, Scene *scene, FluidsimSettings *fss)
117 {
118         if (scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY) {
119                 copy_v3_v3(gravity, scene->physics_settings.gravity);
120         } else {
121                 copy_v3_v3(gravity, fss->grav);
122         }
123 }
124
125 static float get_fluid_size_m(Scene *scene, Object *domainob, FluidsimSettings *fss)
126 {
127         if (!scene->unit.system) {
128                 return fss->realsize;
129         } else {
130                 float dim[3];
131                 float longest_axis;
132                 
133                 object_get_dimensions(domainob, dim);
134                 longest_axis = MAX3(dim[0], dim[1], dim[2]);
135                 
136                 return longest_axis * scene->unit.scale_length;
137         }
138 }
139
140 static int fluid_is_animated_mesh(FluidsimSettings *fss)
141 {
142         return ((fss->type == OB_FLUIDSIM_CONTROL) || fss->domainNovecgen);
143 }
144
145 /* ********************** fluid sim settings struct functions ********************** */
146
147 #if 0
148 /* helper function */
149 void fluidsimGetGeometryObjFilename(Object *ob, char *dst) { //, char *srcname)
150 {
151         //BLI_snprintf(dst,FILE_MAXFILE, "%s_cfgdata_%s.bobj.gz", srcname, ob->id.name);
152         BLI_snprintf(dst,FILE_MAXFILE, "fluidcfgdata_%s.bobj.gz", ob->id.name);
153 }
154 #endif
155
156
157 /* ********************** fluid sim channel helper functions ********************** */
158
159 typedef struct FluidAnimChannels {
160         int length;
161         
162         double aniFrameTime;
163         
164         float *timeAtFrame;
165         float *DomainTime;
166         float *DomainGravity;
167         float *DomainViscosity;
168 } FluidAnimChannels;
169
170 typedef struct FluidObject {
171         struct FluidObject *next, *prev;
172         
173         struct Object *object;
174         
175         float *Translation;
176         float *Rotation;
177         float *Scale;
178         float *Active;
179         
180         float *InitialVelocity;
181         
182         float *AttractforceStrength;
183         float *AttractforceRadius;
184         float *VelocityforceStrength;
185         float *VelocityforceRadius;
186         
187         float *VertexCache;
188         int numVerts, numTris;
189 } FluidObject;
190
191 // no. of entries for the two channel sizes
192 #define CHANNEL_FLOAT 1
193 #define CHANNEL_VEC   3
194
195 // simplify channels before printing
196 // for API this is done anyway upon init
197 #if 0
198 static void fluidsimPrintChannel(FILE *file, float *channel, int paramsize, char *str, int entries) 
199
200         int i,j; 
201         int channelSize = paramsize; 
202
203         if(entries==3) {
204                 elbeemSimplifyChannelVec3( channel, &channelSize); 
205         } else if(entries==1) {
206                 elbeemSimplifyChannelFloat( channel, &channelSize); 
207         } else {
208                 // invalid, cant happen?
209         }
210
211         fprintf(file, "      CHANNEL %s = \n", str); 
212         for(i=0; i<channelSize;i++) { 
213                 fprintf(file,"        ");  
214                 for(j=0;j<=entries;j++) {  // also print time value
215                         fprintf(file," %f ", channel[i*(entries+1)+j] ); 
216                         if(j==entries-1){ fprintf(file,"  "); }
217                 } 
218                 fprintf(file," \n");  
219         } 
220
221         fprintf(file,  "      ; \n" ); 
222 }
223 #endif
224
225
226 /* Note: fluid anim channel data layout
227  * ------------------------------------
228  * CHANNEL_FLOAT:
229  * frame 1     |frame 2
230  * [dataF][time][dataF][time]
231  *
232  * CHANNEL_VEC:
233  * frame 1                   |frame 2
234  * [dataX][dataY][dataZ][time][dataX][dataY][dataZ][time]
235  *
236  */
237
238 static void init_time(FluidsimSettings *domainSettings, FluidAnimChannels *channels)
239 {
240         int i;
241         
242         channels->timeAtFrame = MEM_callocN( (channels->length+1)*sizeof(float), "timeAtFrame channel");
243         
244         channels->timeAtFrame[0] = channels->timeAtFrame[1] = domainSettings->animStart; // start at index 1
245         
246         for(i=2; i<=channels->length; i++) {
247                 channels->timeAtFrame[i] = channels->timeAtFrame[i-1] + channels->aniFrameTime;
248         }
249 }
250
251 /* if this is slow, can replace with faster, less readable code */
252 static void set_channel(float *channel, float time, float *value, int i, int size)
253 {
254         if (size == CHANNEL_FLOAT) {
255                 channel[(i * 2) + 0] = value[0];
256                 channel[(i * 2) + 1] = time;
257         }
258         else if (size == CHANNEL_VEC) {
259                 channel[(i * 4) + 0] = value[0];
260                 channel[(i * 4) + 1] = value[1];
261                 channel[(i * 4) + 2] = value[2];
262                 channel[(i * 4) + 3] = time;
263         }
264 }
265
266 static void set_vertex_channel(float *channel, float time, struct Scene *scene, struct FluidObject *fobj, int i)
267 {
268         Object *ob = fobj->object;
269         FluidsimModifierData *fluidmd = (FluidsimModifierData *)modifiers_findByType(ob, eModifierType_Fluidsim);
270         float *verts;
271         int *tris=NULL, numVerts=0, numTris=0;
272         int modifierIndex = modifiers_indexInObject(ob, (ModifierData *)fluidmd);
273         int framesize = (3*fobj->numVerts) + 1;
274         int j;
275         
276         if (channel == NULL)
277                 return;
278         
279         initElbeemMesh(scene, ob, &numVerts, &verts, &numTris, &tris, 1, modifierIndex);
280         
281         /* don't allow mesh to change number of verts in anim sequence */
282         if (numVerts != fobj->numVerts) {
283                 MEM_freeN(channel);
284                 channel = NULL;
285                 return;
286         }
287         
288         /* fill frame of channel with vertex locations */
289         for(j=0; j < (3*numVerts); j++) {
290                 channel[i*framesize + j] = verts[j];
291         }
292         channel[i*framesize + framesize-1] = time;
293         
294         MEM_freeN(verts);
295         MEM_freeN(tris);
296 }
297
298 static void free_domain_channels(FluidAnimChannels *channels)
299 {
300         if (!channels->timeAtFrame)
301                 return;
302         MEM_freeN(channels->timeAtFrame);
303         channels->timeAtFrame = NULL;
304         MEM_freeN(channels->DomainGravity);
305         channels->DomainGravity = NULL;
306         MEM_freeN(channels->DomainViscosity);
307         channels->DomainViscosity = NULL;
308 }
309
310 static void free_all_fluidobject_channels(ListBase *fobjects)
311 {
312         FluidObject *fobj;
313         
314         for (fobj=fobjects->first; fobj; fobj=fobj->next) {
315                 if (fobj->Translation) {
316                         MEM_freeN(fobj->Translation);
317                         fobj->Translation = NULL;
318                         MEM_freeN(fobj->Rotation);
319                         fobj->Rotation = NULL;
320                         MEM_freeN(fobj->Scale);
321                         fobj->Scale = NULL;
322                         MEM_freeN(fobj->Active);
323                         fobj->Active = NULL;
324                         MEM_freeN(fobj->InitialVelocity);
325                         fobj->InitialVelocity = NULL;
326                 }
327                 
328                 if (fobj->AttractforceStrength) {
329                         MEM_freeN(fobj->AttractforceStrength);
330                         fobj->AttractforceStrength = NULL;
331                         MEM_freeN(fobj->AttractforceRadius);
332                         fobj->AttractforceRadius = NULL;
333                         MEM_freeN(fobj->VelocityforceStrength);
334                         fobj->VelocityforceStrength = NULL;
335                         MEM_freeN(fobj->VelocityforceRadius);
336                         fobj->VelocityforceRadius = NULL;
337                 }
338                 
339                 if (fobj->VertexCache) {
340                         MEM_freeN(fobj->VertexCache);
341                         fobj->VertexCache = NULL;
342                 }
343         }
344 }
345
346 static void fluid_init_all_channels(bContext *C, Object *UNUSED(fsDomain), FluidsimSettings *domainSettings, FluidAnimChannels *channels, ListBase *fobjects)
347 {
348         Scene *scene = CTX_data_scene(C);
349         Base *base;
350         int i;
351         int length = channels->length;
352         float eval_time;
353         
354         /* XXX: first init time channel - temporary for now */
355         /* init time values (should be done after evaluating animated time curve) */
356         init_time(domainSettings, channels);
357         
358         /* allocate domain animation channels */
359         channels->DomainGravity = MEM_callocN( length * (CHANNEL_VEC+1) * sizeof(float), "channel DomainGravity");
360         channels->DomainViscosity = MEM_callocN( length * (CHANNEL_FLOAT+1) * sizeof(float), "channel DomainViscosity");
361         //channels->DomainTime = MEM_callocN( length * (CHANNEL_FLOAT+1) * sizeof(float), "channel DomainTime");
362         
363         /* allocate fluid objects */
364         for (base=scene->base.first; base; base= base->next) {
365                 Object *ob = base->object;
366                 FluidsimModifierData *fluidmd = (FluidsimModifierData *)modifiers_findByType(ob, eModifierType_Fluidsim);
367                 
368                 if (fluidmd) {
369                         FluidObject *fobj = MEM_callocN(sizeof(FluidObject), "Fluid Object");
370                         fobj->object = ob;
371                         
372                         if (ELEM(fluidmd->fss->type, OB_FLUIDSIM_DOMAIN, OB_FLUIDSIM_PARTICLE)) {
373                                 BLI_addtail(fobjects, fobj);
374                                 continue;
375                         }
376                         
377                         fobj->Translation = MEM_callocN( length * (CHANNEL_VEC+1) * sizeof(float), "fluidobject Translation");
378                         fobj->Rotation = MEM_callocN( length * (CHANNEL_VEC+1) * sizeof(float), "fluidobject Rotation");
379                         fobj->Scale = MEM_callocN( length * (CHANNEL_VEC+1) * sizeof(float), "fluidobject Scale");
380                         fobj->Active = MEM_callocN( length * (CHANNEL_FLOAT+1) * sizeof(float), "fluidobject Active");
381                         fobj->InitialVelocity = MEM_callocN( length * (CHANNEL_VEC+1) * sizeof(float), "fluidobject InitialVelocity");
382                         
383                         if (fluidmd->fss->type == OB_FLUIDSIM_CONTROL) {
384                                 fobj->AttractforceStrength = MEM_callocN( length * (CHANNEL_FLOAT+1) * sizeof(float), "fluidobject AttractforceStrength");
385                                 fobj->AttractforceRadius = MEM_callocN( length * (CHANNEL_FLOAT+1) * sizeof(float), "fluidobject AttractforceRadius");
386                                 fobj->VelocityforceStrength = MEM_callocN( length * (CHANNEL_FLOAT+1) * sizeof(float), "fluidobject VelocityforceStrength");
387                                 fobj->VelocityforceRadius = MEM_callocN( length * (CHANNEL_FLOAT+1) * sizeof(float), "fluidobject VelocityforceRadius");
388                         }
389                         
390                         if (fluid_is_animated_mesh(fluidmd->fss)) {
391                                 float *verts=NULL;
392                                 int *tris=NULL, modifierIndex = modifiers_indexInObject(ob, (ModifierData *)fluidmd);
393
394                                 initElbeemMesh(scene, ob, &fobj->numVerts, &verts, &fobj->numTris, &tris, 0, modifierIndex);
395                                 fobj->VertexCache = MEM_callocN( length *((fobj->numVerts*CHANNEL_VEC)+1) * sizeof(float), "fluidobject VertexCache");
396                                 
397                                 MEM_freeN(verts);
398                                 MEM_freeN(tris);
399                         }
400                         
401                         BLI_addtail(fobjects, fobj);
402                 }
403         }
404         
405         /* now we loop over the frames and fill the allocated channels with data */
406         for (i=0; i<channels->length; i++) {
407                 FluidObject *fobj;
408                 float viscosity, gravity[3];
409                 float timeAtFrame;
410                 
411                 eval_time = domainSettings->bakeStart + i;
412                 timeAtFrame = channels->timeAtFrame[i+1];
413                 
414                 /* XXX: This can't be used due to an anim sys optimisation that ignores recalc object animation,
415                  * leaving it for the depgraph (this ignores object animation such as modifier properties though... :/ )
416                  * --> BKE_animsys_evaluate_all_animation(G.main, eval_time);
417                  * This doesn't work with drivers:
418                  * --> BKE_animsys_evaluate_animdata(&fsDomain->id, fsDomain->adt, eval_time, ADT_RECALC_ALL);
419                  */
420                 
421                 /* Modifying the global scene isn't nice, but we can do it in 
422                  * this part of the process before a threaded job is created */
423                 scene->r.cfra = (int)eval_time;
424                 ED_update_for_newframe(CTX_data_main(C), scene, CTX_wm_screen(C), 1);
425                 
426                 /* now scene data should be current according to animation system, so we fill the channels */
427                 
428                 /* Domain properties - gravity/viscosity/time */
429                 get_fluid_gravity(gravity, scene, domainSettings);
430                 set_channel(channels->DomainGravity, timeAtFrame, gravity, i, CHANNEL_VEC);
431                 viscosity = get_fluid_viscosity(domainSettings);
432                 set_channel(channels->DomainViscosity, timeAtFrame, &viscosity, i, CHANNEL_FLOAT);
433                 // XXX : set_channel(channels->DomainTime, timeAtFrame, &time, i, CHANNEL_VEC);
434                 
435                 /* object movement */
436                 for (fobj=fobjects->first; fobj; fobj=fobj->next) {
437                         Object *ob = fobj->object;
438                         FluidsimModifierData *fluidmd = (FluidsimModifierData *)modifiers_findByType(ob, eModifierType_Fluidsim);
439                         float active= (float)(fluidmd->fss->flag & OB_FLUIDSIM_ACTIVE);
440                         float rot_d[3] = {0.f, 0.f, 0.f}, old_rot[3] = {0.f, 0.f, 0.f};
441                         
442                         if (ELEM(fluidmd->fss->type, OB_FLUIDSIM_DOMAIN, OB_FLUIDSIM_PARTICLE))
443                                 continue;
444                         
445                         /* init euler rotation values and convert to elbeem format */
446                         /* get the rotation from ob->obmat rather than ob->rot to account for parent animations */
447                         if(i) {
448                                 copy_v3_v3(old_rot, fobj->Rotation + 4*(i-1));
449                                 mul_v3_fl(old_rot, -M_PI/180.f);
450                         }
451
452                         mat4_to_compatible_eulO(rot_d, old_rot, 0, ob->obmat);
453                         mul_v3_fl(rot_d, -180.f/M_PI);
454                         
455                         set_channel(fobj->Translation, timeAtFrame, ob->loc, i, CHANNEL_VEC);
456                         set_channel(fobj->Rotation, timeAtFrame, rot_d, i, CHANNEL_VEC);
457                         set_channel(fobj->Scale, timeAtFrame, ob->size, i, CHANNEL_VEC);
458                         set_channel(fobj->Active, timeAtFrame, &active, i, CHANNEL_FLOAT);
459                         set_channel(fobj->InitialVelocity, timeAtFrame, &fluidmd->fss->iniVelx, i, CHANNEL_VEC);
460                         
461                         if (fluidmd->fss->type == OB_FLUIDSIM_CONTROL) {
462                                 set_channel(fobj->AttractforceStrength, timeAtFrame, &fluidmd->fss->attractforceStrength, i, CHANNEL_FLOAT);
463                                 set_channel(fobj->AttractforceRadius, timeAtFrame, &fluidmd->fss->attractforceRadius, i, CHANNEL_FLOAT);
464                                 set_channel(fobj->VelocityforceStrength, timeAtFrame, &fluidmd->fss->velocityforceStrength, i, CHANNEL_FLOAT);
465                                 set_channel(fobj->VelocityforceRadius, timeAtFrame, &fluidmd->fss->velocityforceRadius, i, CHANNEL_FLOAT);
466                         }
467                         
468                         if (fluid_is_animated_mesh(fluidmd->fss)) {
469                                 set_vertex_channel(fobj->VertexCache, timeAtFrame, scene, fobj, i);
470                         }
471                 }
472         }
473 }
474
475 static void export_fluid_objects(ListBase *fobjects, Scene *scene, int length)
476 {
477         FluidObject *fobj;
478         
479         for (fobj=fobjects->first; fobj; fobj=fobj->next) {
480                 Object *ob = fobj->object;
481                 FluidsimModifierData *fluidmd = (FluidsimModifierData *)modifiers_findByType(ob, eModifierType_Fluidsim);
482                 int modifierIndex = modifiers_indexInObject(ob, (ModifierData *)fluidmd);
483                 
484                 float *verts=NULL;
485                 int *tris=NULL;
486                 int numVerts=0, numTris=0;
487                 int deform = fluid_is_animated_mesh(fluidmd->fss);
488                 
489                 elbeemMesh fsmesh;
490                 
491                 if (ELEM(fluidmd->fss->type, OB_FLUIDSIM_DOMAIN, OB_FLUIDSIM_PARTICLE))
492                         continue;
493                 
494                 elbeemResetMesh( &fsmesh );
495                 
496                 fsmesh.type = fluidmd->fss->type;
497                 fsmesh.name = ob->id.name;
498                 
499                 initElbeemMesh(scene, ob, &numVerts, &verts, &numTris, &tris, 0, modifierIndex);
500                 
501                 fsmesh.numVertices   = numVerts;
502                 fsmesh.numTriangles  = numTris;
503                 fsmesh.vertices      = verts;
504                 fsmesh.triangles     = tris;
505                 
506                 fsmesh.channelSizeTranslation  = 
507                 fsmesh.channelSizeRotation     = 
508                 fsmesh.channelSizeScale        = 
509                 fsmesh.channelSizeInitialVel   = 
510                 fsmesh.channelSizeActive       = length;
511                 
512                 fsmesh.channelTranslation      = fobj->Translation;
513                 fsmesh.channelRotation         = fobj->Rotation;
514                 fsmesh.channelScale            = fobj->Scale;
515                 fsmesh.channelActive           = fobj->Active;
516                 
517                 if( ELEM(fsmesh.type, OB_FLUIDSIM_FLUID, OB_FLUIDSIM_INFLOW)) {
518                         fsmesh.channelInitialVel = fobj->InitialVelocity;
519                         fsmesh.localInivelCoords = ((fluidmd->fss->typeFlags & OB_FSINFLOW_LOCALCOORD)?1:0);
520                 } 
521                 
522                 if(fluidmd->fss->typeFlags & OB_FSBND_NOSLIP)
523                         fsmesh.obstacleType = FLUIDSIM_OBSTACLE_NOSLIP;
524                 else if(fluidmd->fss->typeFlags & OB_FSBND_PARTSLIP)
525                         fsmesh.obstacleType = FLUIDSIM_OBSTACLE_PARTSLIP;
526                 else if(fluidmd->fss->typeFlags & OB_FSBND_FREESLIP)
527                         fsmesh.obstacleType = FLUIDSIM_OBSTACLE_FREESLIP;
528                 
529                 fsmesh.obstaclePartslip = fluidmd->fss->partSlipValue;
530                 fsmesh.volumeInitType = fluidmd->fss->volumeInitType;
531                 fsmesh.obstacleImpactFactor = fluidmd->fss->surfaceSmoothing; // misused value
532                 
533                 if (fsmesh.type == OB_FLUIDSIM_CONTROL) {
534                         fsmesh.cpsTimeStart = fluidmd->fss->cpsTimeStart;
535                         fsmesh.cpsTimeEnd = fluidmd->fss->cpsTimeEnd;
536                         fsmesh.cpsQuality = fluidmd->fss->cpsQuality;
537                         fsmesh.obstacleType = (fluidmd->fss->flag & OB_FLUIDSIM_REVERSE);
538                         
539                         fsmesh.channelSizeAttractforceRadius = 
540                         fsmesh.channelSizeVelocityforceStrength = 
541                         fsmesh.channelSizeVelocityforceRadius = 
542                         fsmesh.channelSizeAttractforceStrength = length;
543                         
544                         fsmesh.channelAttractforceStrength = fobj->AttractforceStrength;
545                         fsmesh.channelAttractforceRadius = fobj->AttractforceRadius;
546                         fsmesh.channelVelocityforceStrength = fobj->VelocityforceStrength;
547                         fsmesh.channelVelocityforceRadius = fobj->VelocityforceRadius;
548                 }
549                 else {
550                         fsmesh.channelAttractforceStrength =
551                         fsmesh.channelAttractforceRadius = 
552                         fsmesh.channelVelocityforceStrength = 
553                         fsmesh.channelVelocityforceRadius = NULL; 
554                 }
555                 
556                 /* animated meshes */
557                 if(deform) {
558                         fsmesh.channelSizeVertices = length;
559                         fsmesh.channelVertices = fobj->VertexCache;
560                                 
561                         // remove channels
562                         fsmesh.channelTranslation      = 
563                         fsmesh.channelRotation         = 
564                         fsmesh.channelScale            = NULL; 
565                 }
566                 
567                 elbeemAddMesh(&fsmesh);
568                 
569                 if(verts) MEM_freeN(verts);
570                 if(tris) MEM_freeN(tris);
571         }
572 }
573
574 static int fluid_validate_scene(ReportList *reports, Scene *scene, Object *fsDomain)
575 {
576         Base *base;
577         Object *newdomain = NULL;
578         int channelObjCount = 0;
579         int fluidInputCount = 0;
580
581         for(base=scene->base.first; base; base= base->next)
582         {
583                 Object *ob = base->object;
584                 FluidsimModifierData *fluidmdtmp = (FluidsimModifierData *)modifiers_findByType(ob, eModifierType_Fluidsim);                    
585
586                 /* only find objects with fluid modifiers */
587                 if (!fluidmdtmp || ob->type != OB_MESH) continue;
588                         
589                 if(fluidmdtmp->fss->type == OB_FLUIDSIM_DOMAIN) {
590                         /* if no initial domain object given, find another potential domain */
591                         if (!fsDomain) {
592                                 newdomain = ob;
593                         }
594                         /* if there's more than one domain, cancel */
595                         else if (fsDomain && ob != fsDomain) {
596                                 BKE_report(reports, RPT_ERROR, "There should be only one domain object");
597                                 return 0;
598                         }
599                 }
600                 
601                 /* count number of objects needed for animation channels */
602                 if ( !ELEM(fluidmdtmp->fss->type, OB_FLUIDSIM_DOMAIN, OB_FLUIDSIM_PARTICLE) )
603                         channelObjCount++;
604                 
605                 /* count number of fluid input objects */
606                 if (ELEM(fluidmdtmp->fss->type, OB_FLUIDSIM_FLUID, OB_FLUIDSIM_INFLOW))
607                         fluidInputCount++;
608         }
609
610         if (newdomain)
611                 fsDomain = newdomain;
612         
613         if (!fsDomain) {
614                 BKE_report(reports, RPT_ERROR, "No domain object found");
615                 return 0;
616         }
617         
618         if (channelObjCount>=255) {
619                 BKE_report(reports, RPT_ERROR, "Cannot bake with more then 256 objects");
620                 return 0;
621         }
622         
623         if (fluidInputCount == 0) {
624                 BKE_report(reports, RPT_ERROR, "No fluid input objects in the scene");
625                 return 0;
626         }
627         
628         return 1;
629 }
630
631
632 #define FLUID_SUFFIX_CONFIG             "fluidsim.cfg"
633 #define FLUID_SUFFIX_SURFACE    "fluidsurface"
634
635 static int fluid_init_filepaths(Object *fsDomain, char *targetDir, char *targetFile, char *debugStrBuffer)
636 {
637         FluidsimModifierData *fluidmd = (FluidsimModifierData *)modifiers_findByType(fsDomain, eModifierType_Fluidsim);
638         FluidsimSettings *domainSettings= fluidmd->fss; 
639         FILE *fileCfg;
640         int dirExist = 0;
641         char newSurfdataPath[FILE_MAX]; // modified output settings
642         const char *suffixConfig = FLUID_SUFFIX_CONFIG;
643         int outStringsChanged = 0;
644
645         // prepare names...
646         const char *relbase= modifier_path_relbase(fsDomain);
647
648         BLI_strncpy(targetDir, domainSettings->surfdataPath, FILE_MAXDIR);
649         BLI_strncpy(newSurfdataPath, domainSettings->surfdataPath, FILE_MAXDIR); /* if 0'd out below, this value is never used! */
650         BLI_path_abs(targetDir, relbase); // fixed #frame-no
651
652         BLI_join_dirfile(targetFile, FILE_MAX, targetDir, suffixConfig);
653         /* .tmp: dont overwrite/delete original file */
654         strncat(targetFile, ".tmp", FILE_MAX);
655
656         // make sure all directories exist
657         // as the bobjs use the same dir, this only needs to be checked
658         // for the cfg output
659         BLI_make_existing_file(targetFile);
660         
661         // check selected directory
662         // simply try to open cfg file for writing to test validity of settings
663         fileCfg = fopen(targetFile, "w");
664         if(fileCfg) { 
665                 dirExist = 1; fclose(fileCfg); 
666                 // remove cfg dummy from  directory test
667                 BLI_delete(targetFile, 0,0);
668         }
669         
670         if(targetDir[0] == '\0' || (!dirExist)) {
671                 char blendDir[FILE_MAX];
672                 char blendFile[FILE_MAX];
673                 
674                 // invalid dir, reset to current/previous
675                 BLI_strncpy(blendDir, G.main->name, FILE_MAX);
676                 BLI_splitdirstring(blendDir, blendFile);
677                 BLI_replace_extension(blendFile, FILE_MAX, ""); /* strip .blend */
678
679                 BLI_snprintf(newSurfdataPath, FILE_MAX ,"//fluidsimdata/%s_%s_", blendFile, fsDomain->id.name);
680                 
681                 BLI_snprintf(debugStrBuffer, 256, "fluidsimBake::error - warning resetting output dir to '%s'\n", newSurfdataPath);
682                 elbeemDebugOut(debugStrBuffer);
683                 outStringsChanged=1;
684         }
685         
686         // check if modified output dir is ok
687 #if 0
688         if(outStringsChanged) {
689                 char dispmsg[FILE_MAX+256];
690                 int  selection=0;
691                 BLI_strncpy(dispmsg,"Output settings set to: '", sizeof(dispmsg));
692                 strcat(dispmsg, newSurfdataPath);
693                 strcat(dispmsg, "'%t|Continue with changed settings%x1|Discard and abort%x0");
694                 
695                 // ask user if thats what he/she wants...
696                 selection = pupmenu(dispmsg);
697                 if(selection<1) return 0; // 0 from menu, or -1 aborted
698                 BLI_strncpy(targetDir, newSurfdataPath, sizeof(targetDir));
699                 strncpy(domainSettings->surfdataPath, newSurfdataPath, FILE_MAXDIR);
700                 BLI_path_abs(targetDir, G.main->name); // fixed #frame-no 
701         }
702 #endif  
703         return outStringsChanged;
704 }
705
706 /* ******************************************************************************** */
707 /* ********************** write fluidsim config to file ************************* */
708 /* ******************************************************************************** */
709
710 typedef struct FluidBakeJob {
711         /* from wmJob */
712         void *owner;
713         short *stop, *do_update;
714         float *progress;
715         int current_frame;
716         elbeemSimulationSettings *settings;
717 } FluidBakeJob;
718
719 static void fluidbake_free(void *customdata)
720 {
721         FluidBakeJob *fb= (FluidBakeJob *)customdata;
722         MEM_freeN(fb);
723 }
724
725 /* called by fluidbake, only to check job 'stop' value */
726 static int fluidbake_breakjob(void *customdata)
727 {
728         FluidBakeJob *fb= (FluidBakeJob *)customdata;
729
730         if(fb->stop && *(fb->stop))
731                 return 1;
732         
733         /* this is not nice yet, need to make the jobs list template better 
734          * for identifying/acting upon various different jobs */
735         /* but for now we'll reuse the render break... */
736         return (G.afbreek);
737 }
738
739 /* called by fluidbake, wmJob sends notifier */
740 static void fluidbake_updatejob(void *customdata, float progress)
741 {
742         FluidBakeJob *fb= (FluidBakeJob *)customdata;
743         
744         *(fb->do_update)= 1;
745         *(fb->progress)= progress;
746 }
747
748 static void fluidbake_startjob(void *customdata, short *stop, short *do_update, float *progress)
749 {
750         FluidBakeJob *fb= (FluidBakeJob *)customdata;
751         
752         fb->stop= stop;
753         fb->do_update = do_update;
754         fb->progress = progress;
755         
756         G.afbreek= 0;   /* XXX shared with render - replace with job 'stop' switch */
757         
758         elbeemSimulate();
759         *do_update= 1;
760         *stop = 0;
761 }
762
763 static void fluidbake_endjob(void *customdata)
764 {
765         FluidBakeJob *fb= (FluidBakeJob *)customdata;
766         
767         if (fb->settings) {
768                 MEM_freeN(fb->settings);
769                 fb->settings = NULL;
770         }
771 }
772
773 int runSimulationCallback(void *data, int status, int frame)
774 {
775         FluidBakeJob *fb = (FluidBakeJob *)data;
776         elbeemSimulationSettings *settings = fb->settings;
777         
778         if (status == FLUIDSIM_CBSTATUS_NEWFRAME) {
779                 fluidbake_updatejob(fb, frame / (float)settings->noOfFrames);
780                 //printf("elbeem blender cb s%d, f%d, domainid:%d noOfFrames: %d \n", status,frame, settings->domainId, settings->noOfFrames ); // DEBUG
781         }
782         
783         if (fluidbake_breakjob(fb))  {
784                 return FLUIDSIM_CBRET_ABORT;
785         }
786         
787         return FLUIDSIM_CBRET_CONTINUE;
788 }
789
790 static void fluidbake_free_data(FluidAnimChannels *channels, ListBase *fobjects, elbeemSimulationSettings *fsset, FluidBakeJob *fb)
791 {
792         free_domain_channels(channels);
793         MEM_freeN(channels);
794         channels = NULL;
795
796         free_all_fluidobject_channels(fobjects);
797         BLI_freelistN(fobjects);
798         MEM_freeN(fobjects);
799         fobjects = NULL;
800         
801         if (fsset) {
802                 MEM_freeN(fsset);
803                 fsset = NULL;
804         }
805         
806         if (fb) {
807                 MEM_freeN(fb);
808                 fb = NULL;
809         }
810 }
811
812 /* copied from rna_fluidsim.c: fluidsim_find_lastframe() */
813 static void fluidsim_delete_until_lastframe(FluidsimSettings *fss, const char *relbase)
814 {
815         char targetDir[FILE_MAX], targetFile[FILE_MAX];
816         char targetDirVel[FILE_MAX], targetFileVel[FILE_MAX];
817         char previewDir[FILE_MAX], previewFile[FILE_MAX];
818         int curFrame = 1, exists = 0;
819
820         BLI_join_dirfile(targetDir,    sizeof(targetDir),    fss->surfdataPath, OB_FLUIDSIM_SURF_FINAL_OBJ_FNAME);
821         BLI_join_dirfile(targetDirVel, sizeof(targetDirVel), fss->surfdataPath, OB_FLUIDSIM_SURF_FINAL_VEL_FNAME);
822         BLI_join_dirfile(previewDir,   sizeof(previewDir),   fss->surfdataPath, OB_FLUIDSIM_SURF_PREVIEW_OBJ_FNAME);
823
824         BLI_path_abs(targetDir,    relbase);
825         BLI_path_abs(targetDirVel, relbase);
826         BLI_path_abs(previewDir,   relbase);
827
828         do {
829                 BLI_strncpy(targetFile, targetDir, sizeof(targetFile));
830                 BLI_strncpy(targetFileVel, targetDirVel, sizeof(targetFileVel));
831                 BLI_strncpy(previewFile, previewDir, sizeof(previewFile));
832
833                 BLI_path_frame(targetFile, curFrame, 0);
834                 BLI_path_frame(targetFileVel, curFrame, 0);
835                 BLI_path_frame(previewFile, curFrame, 0);
836
837                 curFrame++;
838
839                 if((exists = BLI_exists(targetFile)))
840                 {
841                         BLI_delete(targetFile, 0, 0);
842                         BLI_delete(targetFileVel, 0, 0);
843                         BLI_delete(previewFile, 0, 0);
844                 }
845         } while(exists);
846
847         return;
848 }
849
850 static int fluidsimBake(bContext *C, ReportList *reports, Object *fsDomain, short do_job)
851 {
852         Scene *scene= CTX_data_scene(C);
853         int i;
854         FluidsimSettings *domainSettings;
855
856         char debugStrBuffer[256];
857         
858         int gridlevels = 0;
859         const char *relbase= modifier_path_relbase(fsDomain);
860         const char *strEnvName = "BLENDER_ELBEEMDEBUG"; // from blendercall.cpp
861         const char *suffixConfig = FLUID_SUFFIX_CONFIG;
862         const char *suffixSurface = FLUID_SUFFIX_SURFACE;
863
864         char targetDir[FILE_MAX];  // store & modify output settings
865         char targetFile[FILE_MAX]; // temp. store filename from targetDir for access
866         int  outStringsChanged = 0;             // modified? copy back before baking
867
868         float domainMat[4][4];
869         float invDomMat[4][4];
870
871         int noFrames;
872         int origFrame = scene->r.cfra;
873         
874         FluidAnimChannels *channels = MEM_callocN(sizeof(FluidAnimChannels), "fluid domain animation channels");
875         ListBase *fobjects = MEM_callocN(sizeof(ListBase), "fluid objects");
876         FluidsimModifierData *fluidmd = NULL;
877         Mesh *mesh = NULL;
878
879         FluidBakeJob *fb;
880         elbeemSimulationSettings *fsset= MEM_callocN(sizeof(elbeemSimulationSettings), "Fluid sim settings");
881
882         fb= MEM_callocN(sizeof(FluidBakeJob), "fluid bake job");
883         
884         if(getenv(strEnvName)) {
885                 int dlevel = atoi(getenv(strEnvName));
886                 elbeemSetDebugLevel(dlevel);
887                 BLI_snprintf(debugStrBuffer,256,"fluidsimBake::msg: Debug messages activated due to envvar '%s'\n",strEnvName); 
888                 elbeemDebugOut(debugStrBuffer);
889         }
890         
891         /* make sure it corresponds to startFrame setting (old: noFrames = scene->r.efra - scene->r.sfra +1) */;
892         noFrames = scene->r.efra - 0;
893         if(noFrames<=0) {
894                 BKE_report(reports, RPT_ERROR, "No frames to export - check your animation range settings");
895                 fluidbake_free_data(channels, fobjects, fsset, fb);
896                 return 0;
897         }
898         
899         /* check scene for sane object/modifier settings */
900         if (!fluid_validate_scene(reports, scene, fsDomain)) {
901                 fluidbake_free_data(channels, fobjects, fsset, fb);
902                 return 0;
903         }
904         
905         /* these both have to be valid, otherwise we wouldnt be here */
906         fluidmd = (FluidsimModifierData *)modifiers_findByType(fsDomain, eModifierType_Fluidsim);
907         domainSettings = fluidmd->fss;
908         mesh = fsDomain->data;
909         
910         domainSettings->bakeStart = 1;
911         domainSettings->bakeEnd = scene->r.efra;
912         
913         // calculate bounding box
914         fluid_get_bb(mesh->mvert, mesh->totvert, fsDomain->obmat, domainSettings->bbStart, domainSettings->bbSize);
915         
916         // reset last valid frame
917         domainSettings->lastgoodframe = -1;
918
919         /* delete old baked files */
920         fluidsim_delete_until_lastframe(domainSettings, relbase);
921         
922         /* rough check of settings... */
923         if(domainSettings->previewresxyz > domainSettings->resolutionxyz) {
924                 BLI_snprintf(debugStrBuffer,256,"fluidsimBake::warning - Preview (%d) >= Resolution (%d)... setting equal.\n", domainSettings->previewresxyz ,  domainSettings->resolutionxyz); 
925                 elbeemDebugOut(debugStrBuffer);
926                 domainSettings->previewresxyz = domainSettings->resolutionxyz;
927         }
928         // set adaptive coarsening according to resolutionxyz
929         // this should do as an approximation, with in/outflow
930         // doing this more accurate would be overkill
931         // perhaps add manual setting?
932         if(domainSettings->maxRefine <0) {
933                 if(domainSettings->resolutionxyz>128) {
934                         gridlevels = 2;
935                 } else
936                 if(domainSettings->resolutionxyz>64) {
937                         gridlevels = 1;
938                 } else {
939                         gridlevels = 0;
940                 }
941         } else {
942                 gridlevels = domainSettings->maxRefine;
943         }
944         BLI_snprintf(debugStrBuffer,256,"fluidsimBake::msg: Baking %s, refine: %d\n", fsDomain->id.name , gridlevels ); 
945         elbeemDebugOut(debugStrBuffer);
946         
947         
948         
949         /* ******** prepare output file paths ******** */
950         outStringsChanged = fluid_init_filepaths(fsDomain, targetDir, targetFile, debugStrBuffer);
951         channels->length = scene->r.efra;
952         channels->aniFrameTime = (domainSettings->animEnd - domainSettings->animStart)/(double)noFrames;
953         
954         /* ******** initialise and allocate animation channels ******** */
955         fluid_init_all_channels(C, fsDomain, domainSettings, channels, fobjects);
956
957         /* reset to original current frame */
958         scene->r.cfra = origFrame;
959         ED_update_for_newframe(CTX_data_main(C), scene, CTX_wm_screen(C), 1);
960         
961         
962         /* ---- XXX: No Time animation curve for now, leaving this code here for reference 
963          
964         { int timeIcu[1] = { FLUIDSIM_TIME };
965                 float timeDef[1] = { 1. };
966
967                 // time channel is a bit special, init by hand...
968                 timeAtIndex = MEM_callocN( (allchannelSize+1)*1*sizeof(float), "fluidsiminit_timeatindex");
969                 for(i=0; i<=scene->r.efra; i++) {
970                         timeAtIndex[i] = (float)(i-startFrame);
971                 }
972                 fluidsimInitChannel(scene, &channelDomainTime, allchannelSize, timeAtIndex, timeIcu,timeDef, domainSettings->ipo, CHANNEL_FLOAT ); // NDEB
973                 // time channel is a multiplicator for 
974                 if(channelDomainTime) {
975                         for(i=0; i<allchannelSize; i++) { 
976                                 channelDomainTime[i*2+0] = aniFrameTime * channelDomainTime[i*2+0]; 
977                                 if(channelDomainTime[i*2+0]<0.) channelDomainTime[i*2+0] = 0.;
978                         }
979                 }
980                 timeAtFrame = MEM_callocN( (allchannelSize+1)*1*sizeof(float), "fluidsiminit_timeatframe");
981                 timeAtFrame[0] = timeAtFrame[1] = domainSettings->animStart; // start at index 1
982                 if(channelDomainTime) {
983                         for(i=2; i<=allchannelSize; i++) {
984                                 timeAtFrame[i] = timeAtFrame[i-1]+channelDomainTime[(i-1)*2+0];
985                         }
986                 fsset->} else {
987                         for(i=2; i<=allchannelSize; i++) { timeAtFrame[i] = timeAtFrame[i-1]+aniFrameTime; }
988                 }
989
990         } // domain channel init
991         */
992                 
993         /* ******** init domain object's matrix ******** */
994         copy_m4_m4(domainMat, fsDomain->obmat);
995         if(!invert_m4_m4(invDomMat, domainMat)) {
996                 BLI_snprintf(debugStrBuffer,256,"fluidsimBake::error - Invalid obj matrix?\n"); 
997                 elbeemDebugOut(debugStrBuffer);
998                 BKE_report(reports, RPT_ERROR, "Invalid object matrix"); 
999
1000                 fluidbake_free_data(channels, fobjects, fsset, fb);
1001                 return 0;
1002         }
1003
1004         /* ********  start writing / exporting ******** */
1005         // use .tmp, dont overwrite/delete original file
1006         BLI_join_dirfile(targetFile, sizeof(targetFile), targetDir, suffixConfig);
1007         strncat(targetFile, ".tmp", sizeof(targetFile));
1008         
1009         // make sure these directories exist as well
1010         if(outStringsChanged) {
1011                 BLI_make_existing_file(targetFile);
1012         }
1013
1014         /* ******** export domain to elbeem ******** */
1015         elbeemResetSettings(fsset);
1016         fsset->version = 1;
1017
1018         // setup global settings
1019         copy_v3_v3(fsset->geoStart, domainSettings->bbStart);
1020         copy_v3_v3(fsset->geoSize, domainSettings->bbSize);
1021         
1022         // simulate with 50^3
1023         fsset->resolutionxyz = (int)domainSettings->resolutionxyz;
1024         fsset->previewresxyz = (int)domainSettings->previewresxyz;
1025
1026         fsset->realsize = get_fluid_size_m(scene, fsDomain, domainSettings);
1027         fsset->viscosity = get_fluid_viscosity(domainSettings);
1028         get_fluid_gravity(fsset->gravity, scene, domainSettings);
1029
1030         // simulate 5 frames, each 0.03 seconds, output to ./apitest_XXX.bobj.gz
1031         fsset->animStart = domainSettings->animStart;
1032         fsset->aniFrameTime = channels->aniFrameTime;
1033         fsset->noOfFrames = noFrames; // is otherwise subtracted in parser
1034
1035         BLI_join_dirfile(targetFile, sizeof(targetFile), targetDir, suffixSurface);
1036
1037         // defaults for compressibility and adaptive grids
1038         fsset->gstar = domainSettings->gstar;
1039         fsset->maxRefine = domainSettings->maxRefine; // check <-> gridlevels
1040         fsset->generateParticles = domainSettings->generateParticles; 
1041         fsset->numTracerParticles = domainSettings->generateTracers; 
1042         fsset->surfaceSmoothing = domainSettings->surfaceSmoothing; 
1043         fsset->surfaceSubdivs = domainSettings->surfaceSubdivs; 
1044         fsset->farFieldSize = domainSettings->farFieldSize; 
1045         BLI_strncpy(fsset->outputPath, targetFile, 240);
1046
1047         // domain channels
1048         fsset->channelSizeFrameTime = 
1049         fsset->channelSizeViscosity = 
1050         fsset->channelSizeGravity = channels->length;
1051         fsset->channelFrameTime = channels->DomainTime;
1052         fsset->channelViscosity = channels->DomainViscosity;
1053         fsset->channelGravity = channels->DomainGravity;
1054         
1055         fsset->runsimCallback = &runSimulationCallback;
1056         fsset->runsimUserData = fb;
1057
1058         if (domainSettings->typeFlags & OB_FSBND_NOSLIP)                fsset->domainobsType = FLUIDSIM_OBSTACLE_NOSLIP;
1059         else if (domainSettings->typeFlags&OB_FSBND_PARTSLIP)   fsset->domainobsType = FLUIDSIM_OBSTACLE_PARTSLIP;
1060         else if (domainSettings->typeFlags&OB_FSBND_FREESLIP)   fsset->domainobsType = FLUIDSIM_OBSTACLE_FREESLIP;
1061         fsset->domainobsPartslip = domainSettings->partSlipValue;
1062
1063         /* use domainobsType also for surface generation flag (bit: >=64) */
1064         if(domainSettings->typeFlags & OB_FSSG_NOOBS)
1065                 fsset->mFsSurfGenSetting = FLUIDSIM_FSSG_NOOBS;
1066         else
1067                 fsset->mFsSurfGenSetting = 0; // "normal" mode
1068
1069         fsset->generateVertexVectors = (domainSettings->domainNovecgen==0);
1070
1071         // init blender domain transform matrix
1072         { int j; 
1073         for(i=0; i<4; i++) {
1074                 for(j=0; j<4; j++) {
1075                         fsset->surfaceTrafo[i*4+j] = invDomMat[j][i];
1076                 }
1077         } }
1078
1079         /* ******** init solver with settings ******** */
1080         elbeemInit();
1081         elbeemAddDomain(fsset);
1082         
1083         /* ******** export all fluid objects to elbeem ******** */
1084         export_fluid_objects(fobjects, scene, channels->length);
1085         
1086         /* custom data for fluid bake job */
1087         fb->settings = fsset;
1088         
1089         if(do_job) {
1090                 wmJob *steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, "Fluid Simulation", WM_JOB_PROGRESS);
1091
1092                 /* setup job */
1093                 WM_jobs_customdata(steve, fb, fluidbake_free);
1094                 WM_jobs_timer(steve, 0.1, NC_SCENE|ND_FRAME, NC_SCENE|ND_FRAME);
1095                 WM_jobs_callbacks(steve, fluidbake_startjob, NULL, NULL, fluidbake_endjob);
1096
1097                 WM_jobs_start(CTX_wm_manager(C), steve);
1098         }
1099         else {
1100                 short dummy_stop, dummy_do_update;
1101                 float dummy_progress;
1102
1103                 /* blocking, use with exec() */
1104                 fluidbake_startjob((void *)fb, &dummy_stop, &dummy_do_update, &dummy_progress);
1105                 fluidbake_endjob((void *)fb);
1106                 fluidbake_free((void *)fb);
1107         }
1108
1109         /* ******** free stored animation data ******** */
1110         fluidbake_free_data(channels, fobjects, NULL, NULL);
1111
1112         // elbeemFree();
1113         return 1;
1114 }
1115
1116 void fluidsimFreeBake(Object *UNUSED(ob))
1117 {
1118         /* not implemented yet */
1119 }
1120
1121 #else /* WITH_MOD_FLUID */
1122
1123 /* compile dummy functions for disabled fluid sim */
1124
1125 FluidsimSettings *fluidsimSettingsNew(Object *UNUSED(srcob))
1126 {
1127         return NULL;
1128 }
1129
1130 void fluidsimSettingsFree(FluidsimSettings *UNUSED(fss))
1131 {
1132 }
1133
1134 FluidsimSettings* fluidsimSettingsCopy(FluidsimSettings *UNUSED(fss))
1135 {
1136         return NULL;
1137 }
1138
1139 /* only compile dummy functions */
1140 static int fluidsimBake(bContext *UNUSED(C), ReportList *UNUSED(reports), Object *UNUSED(ob), short UNUSED(do_job))
1141 {
1142         return 0;
1143 }
1144
1145 #endif /* WITH_MOD_FLUID */
1146
1147 /***************************** Operators ******************************/
1148
1149 static int fluid_bake_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1150 {
1151         /* only one bake job at a time */
1152         if(WM_jobs_test(CTX_wm_manager(C), CTX_data_scene(C)))
1153                 return OPERATOR_CANCELLED;
1154
1155         if(!fluidsimBake(C, op->reports, CTX_data_active_object(C), TRUE))
1156                 return OPERATOR_CANCELLED;
1157
1158         return OPERATOR_FINISHED;
1159 }
1160
1161 static int fluid_bake_exec(bContext *C, wmOperator *op)
1162 {
1163         if(!fluidsimBake(C, op->reports, CTX_data_active_object(C), FALSE))
1164                 return OPERATOR_CANCELLED;
1165
1166         return OPERATOR_FINISHED;
1167 }
1168
1169 void FLUID_OT_bake(wmOperatorType *ot)
1170 {
1171         /* identifiers */
1172         ot->name= "Fluid Simulation Bake";
1173         ot->description= "Bake fluid simulation";
1174         ot->idname= "FLUID_OT_bake";
1175         
1176         /* api callbacks */
1177         ot->invoke= fluid_bake_invoke;
1178         ot->exec= fluid_bake_exec;
1179         ot->poll= ED_operator_object_active_editable;
1180 }
1181