Spelling Cleanup
[blender.git] / source / blender / editors / physics / dynamicpaint_ops.c
index a868cb55ca59d0d83f813ec1aadb08eefac63666..7f5688338668d15ee4f2b56bdd92c6a4c5c840ef 100644 (file)
@@ -23,6 +23,8 @@
 #include <string.h>
 
 #include "BLI_blenlib.h"
+#include "BLI_string.h"
+#include "BLI_utildefines.h"
 
 #include "DNA_dynamicpaint_types.h"
 #include "DNA_modifier_types.h"
 
 #include "ED_mesh.h"
 #include "ED_screen.h"
+#include "ED_object.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
 #include "RNA_enum_types.h"
 
-/* Platform independend time   */
 #include "PIL_time.h"
 
 #include "WM_types.h"
 #include "WM_api.h"
 
-static int surface_slot_add_exec(bContext *C, wmOperator *op)
+#include "physics_intern.h" /* own include */
+
+static int surface_slot_add_exec(bContext *C, wmOperator *UNUSED(op))
 {
-       DynamicPaintModifierData *pmd = 0;
-       Object *cObject = CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+       DynamicPaintModifierData *pmd = NULL;
+       Object *cObject = ED_object_context(C);
+       DynamicPaintCanvasSettings *canvas;
        DynamicPaintSurface *surface;
 
        /* Make sure we're dealing with a canvas */
        pmd = (DynamicPaintModifierData *)modifiers_findByType(cObject, eModifierType_DynamicPaint);
-       if (!pmd) return OPERATOR_CANCELLED;
-       if (!pmd->canvas) return OPERATOR_CANCELLED;
+       if (!pmd || !pmd->canvas) return OPERATOR_CANCELLED;
 
-       surface = dynamicPaint_createNewSurface(pmd->canvas, CTX_data_scene(C));
+       canvas = pmd->canvas;
+       surface = dynamicPaint_createNewSurface(canvas, CTX_data_scene(C));
 
        if (!surface) return OPERATOR_CANCELLED;
 
        /* set preview for this surface only and set active */
-       pmd->canvas->active_sur = 0;
+       canvas->active_sur = 0;
        for(surface=surface->prev; surface; surface=surface->prev) {
                                surface->flags &= ~MOD_DPAINT_PREVIEW;
-                               pmd->canvas->active_sur++;
+                               canvas->active_sur++;
        }
 
        return OPERATOR_FINISHED;
@@ -92,31 +97,32 @@ void DPAINT_OT_surface_slot_add(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-static int surface_slot_remove_exec(bContext *C, wmOperator *op)
+static int surface_slot_remove_exec(bContext *C, wmOperator *UNUSED(op))
 {
-       DynamicPaintModifierData *pmd = 0;
-       Object *cObject = CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+       DynamicPaintModifierData *pmd = NULL;
+       Object *cObject = ED_object_context(C);
+       DynamicPaintCanvasSettings *canvas;
        DynamicPaintSurface *surface;
        int id=0;
 
        /* Make sure we're dealing with a canvas */
        pmd = (DynamicPaintModifierData *)modifiers_findByType(cObject, eModifierType_DynamicPaint);
-       if (!pmd) return OPERATOR_CANCELLED;
-       if (!pmd->canvas) return OPERATOR_CANCELLED;
+       if (!pmd || !pmd->canvas) return OPERATOR_CANCELLED;
 
-       surface = pmd->canvas->surfaces.first;
+       canvas = pmd->canvas;
+       surface = canvas->surfaces.first;
 
        /* find active surface and remove it */
        for(; surface; surface=surface->next) {
-               if(id == pmd->canvas->active_sur) {
-                               pmd->canvas->active_sur -= 1;
+               if(id == canvas->active_sur) {
+                               canvas->active_sur -= 1;
                                dynamicPaint_freeSurface(surface);
                                break;
                        }
                id++;
        }
 
-       dynamicPaint_resetPreview(pmd->canvas);
+       dynamicPaint_resetPreview(canvas);
        DAG_id_tag_update(&cObject->id, OB_RECALC_DATA);
        WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, cObject);
 
@@ -142,7 +148,7 @@ void DPAINT_OT_surface_slot_remove(wmOperatorType *ot)
 static int type_toggle_exec(bContext *C, wmOperator *op)
 {
 
-       Object *cObject = CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+       Object *cObject = ED_object_context(C);
        Scene *scene = CTX_data_scene(C);
        DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)modifiers_findByType(cObject, eModifierType_DynamicPaint);
        int type= RNA_enum_get(op->ptr, "type");
@@ -162,7 +168,7 @@ static int type_toggle_exec(bContext *C, wmOperator *op)
                        return OPERATOR_CANCELLED;
        }
        
-       /* update dependancy */
+       /* update dependency */
        DAG_id_tag_update(&cObject->id, OB_RECALC_DATA);
        WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, cObject);
        DAG_scene_sort(CTX_data_main(C), scene);
@@ -177,7 +183,7 @@ void DPAINT_OT_type_toggle(wmOperatorType *ot)
        /* identifiers */
        ot->name= "Toggle Type Active";
        ot->idname= "DPAINT_OT_type_toggle";
-       ot->description = "Toggles whether given type is active or not";
+       ot->description = "Toggle whether given type is active or not";
        
        /* api callbacks */
        ot->exec= type_toggle_exec;
@@ -193,45 +199,42 @@ void DPAINT_OT_type_toggle(wmOperatorType *ot)
 
 static int output_toggle_exec(bContext *C, wmOperator *op)
 {
-
-       Object *ob = CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+       Object *ob = ED_object_context(C);
        Scene *scene = CTX_data_scene(C);
+       DynamicPaintSurface *surface;
        DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)modifiers_findByType(ob, eModifierType_DynamicPaint);
-       int index= RNA_int_get(op->ptr, "index");
-
-       if (!pmd) return OPERATOR_CANCELLED;
+       int output= RNA_enum_get(op->ptr, "output"); /* currently only 1/0 */
 
+       if (!pmd || !pmd->canvas) return OPERATOR_CANCELLED;
+       surface = get_activeSurface(pmd->canvas);
 
        /* if type is already enabled, toggle it off */
-       if (pmd->canvas) {
-                       DynamicPaintSurface *surface = get_activeSurface(pmd->canvas);
-
-                       if (surface->format == MOD_DPAINT_SURFACE_F_VERTEX) {
-                               int exists = dynamicPaint_outputLayerExists(surface, ob, index);
-                               char *name;
-                               
-                               if (index == 0)
-                                       name = surface->output_name;
-                               else if (index == 1)
-                                       name = surface->output_name2;
-
-                               /* Vertex Color Layer */
-                               if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
-                                       if (!exists)
-                                               ED_mesh_color_add(C, scene, ob, ob->data, name, 1);
-                                       else 
-                                               ED_mesh_color_remove_named(C, ob, ob->data, name);
-                               }
-                               /* Vertex Weight Layer */
-                               else if (surface->type == MOD_DPAINT_SURFACE_T_WEIGHT) {
-                                       if (!exists)
-                                               ED_vgroup_add_name(ob, name);
-                                       else {
-                                               bDeformGroup *defgroup = defgroup_find_name(ob, name);
-                                               if (defgroup) ED_vgroup_delete(ob, defgroup);
-                                       }
-                               }
+       if (surface->format == MOD_DPAINT_SURFACE_F_VERTEX) {
+               int exists = dynamicPaint_outputLayerExists(surface, ob, output);
+               const char *name;
+               
+               if (output == 0)
+                       name = surface->output_name;
+               else
+                       name = surface->output_name2;
+
+               /* Vertex Color Layer */
+               if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
+                       if (!exists)
+                               ED_mesh_color_add(C, scene, ob, ob->data, name, 1);
+                       else 
+                               ED_mesh_color_remove_named(C, ob, ob->data, name);
+               }
+               /* Vertex Weight Layer */
+               else if (surface->type == MOD_DPAINT_SURFACE_T_WEIGHT) {
+                       if (!exists) {
+                               ED_vgroup_add_name(ob, name);
                        }
+                       else {
+                               bDeformGroup *defgroup = defgroup_find_name(ob, name);
+                               if (defgroup) ED_vgroup_delete(ob, defgroup);
+                       }
+               }
        }
 
        return OPERATOR_FINISHED;
@@ -239,12 +242,16 @@ static int output_toggle_exec(bContext *C, wmOperator *op)
 
 void DPAINT_OT_output_toggle(wmOperatorType *ot)
 {
-       PropertyRNA *prop;
+       static EnumPropertyItem prop_output_toggle_types[] = {
+               {0, "A", 0, "Output A", ""},
+               {1, "B", 0, "Output B", ""},
+               {0, NULL, 0, NULL, NULL}
+       };
 
        /* identifiers */
        ot->name= "Toggle Output Layer";
        ot->idname= "DPAINT_OT_output_toggle";
-       ot->description = "Adds or removes Dynamic Paint output data layer";
+       ot->description = "Add or remove Dynamic Paint output data layer";
        
        /* api callbacks */
        ot->exec= output_toggle_exec;
@@ -254,8 +261,7 @@ void DPAINT_OT_output_toggle(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
        /* properties */
-       prop= RNA_def_int(ot->srna, "index", 0, 0, 1, "Index", "", 0, 1);
-       ot->prop= prop;
+       ot->prop= RNA_def_enum(ot->srna, "output", prop_output_toggle_types, 0, "Output Toggle", "");
 }
 
 
@@ -274,11 +280,9 @@ static int dynamicPaint_bakeImageSequence(bContext *C, DynamicPaintSurface *surf
        int frames;
 
        frames = surface->end_frame - surface->start_frame + 1;
-       if (frames <= 0) {sprintf(canvas->error, "No frames to bake.");printf("DynamicPaint bake failed: %s", canvas->error);return 0;}
+       if (frames <= 0) {BLI_strncpy(canvas->error, "No frames to bake.", sizeof(canvas->error)); return 0;}
 
-       /*
-       *       Set frame to start point (also inits modifier data)
-       */
+       /* Set frame to start point (also inits modifier data) */
        frame = surface->start_frame;
        scene->r.cfra = (int)frame;
        ED_update_for_newframe(CTX_data_main(C), scene, win->screen, 1);
@@ -286,9 +290,7 @@ static int dynamicPaint_bakeImageSequence(bContext *C, DynamicPaintSurface *surf
        /* Init surface */
        if (!dynamicPaint_createUVSurface(surface)) return 0;
 
-       /*
-       *       Loop through selected frames
-       */
+       /* Loop through selected frames */
        for (frame=surface->start_frame; frame<=surface->end_frame; frame++)
        {
                float progress = (frame - surface->start_frame) / (float)frames * 100;
@@ -299,7 +301,6 @@ static int dynamicPaint_bakeImageSequence(bContext *C, DynamicPaintSurface *surf
 
                /* Update progress bar cursor */
                WM_timecursor(win, (int)progress);
-               printf("DynamicPaint: Baking frame %i\n", frame);
 
                /* calculate a frame */
                scene->r.cfra = (int)frame;
@@ -310,48 +311,25 @@ static int dynamicPaint_bakeImageSequence(bContext *C, DynamicPaintSurface *surf
                *       Save output images
                */
                {
-                       char filename[250];
-                       char pad[4];
-                       char dir_slash[2];
-                       /* OpenEXR or PNG       */
-                       short format = (surface->image_fileformat & MOD_DPAINT_IMGFORMAT_OPENEXR) ? DPOUTPUT_OPENEXR : DPOUTPUT_PNG;
-
-                       /* Add frame number padding     */
-                       if (frame<10) sprintf(pad,"000");
-                       else if (frame<100) sprintf(pad,"00");
-                       else if (frame<1000) sprintf(pad,"0");
-                       else pad[0] = '\0';
-
-                       /* make sure directory path is valid to append filename */
-                       if (surface->image_output_path[strlen(surface->image_output_path)-1] != 47 &&
-                               surface->image_output_path[strlen(surface->image_output_path)-1] != 92)
-                               strcpy(dir_slash,"/");
-                       else
-                               dir_slash[0] = '\0';
-
-
-                       /* color map    */
-                       if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
-                               if (surface->flags & MOD_DPAINT_OUT1) {
-                                       sprintf(filename, "%s%s%s%s%i", surface->image_output_path, dir_slash, surface->output_name, pad, (int)frame);
-                                       dynamicPaint_outputImage(surface, filename, format, DPOUTPUT_PAINT);
-                               }
-                               if (surface->flags & MOD_DPAINT_OUT2) {
-                                       sprintf(filename, "%s%s%s%s%i", surface->image_output_path, dir_slash, surface->output_name2, pad, (int)frame);
-                                       dynamicPaint_outputImage(surface, filename, format, DPOUTPUT_WET);
-                               }
-                       }
+                       char filename[FILE_MAX];
 
-                       /* displacement map     */
-                       else if (surface->type == MOD_DPAINT_SURFACE_T_DISPLACE) {
-                               sprintf(filename, "%s%s%s%s%i", surface->image_output_path, dir_slash, surface->output_name, pad, (int)frame);
-                               dynamicPaint_outputImage(surface, filename, format, DPOUTPUT_DISPLACE);
-                       }
+                       /* primary output layer */
+                       if (surface->flags & MOD_DPAINT_OUT1) {
+                               /* set filepath */
+                               BLI_join_dirfile(filename, sizeof(filename), surface->image_output_path, surface->output_name);
+                               BLI_path_frame(filename, frame, 4);
 
-                       /* waves        */
-                       else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE) {
-                               sprintf(filename, "%s%s%s%s%i", surface->image_output_path, dir_slash, surface->output_name, pad, (int)frame);
-                               dynamicPaint_outputImage(surface, filename, format, DPOUTPUT_WAVES);
+                               /* save image */
+                               dynamicPaint_outputSurfaceImage(surface, filename, 0);
+                       }
+                       /* secondary output */
+                       if (surface->flags & MOD_DPAINT_OUT2 && surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
+                               /* set filepath */
+                               BLI_join_dirfile(filename, sizeof(filename), surface->image_output_path, surface->output_name2);
+                               BLI_path_frame(filename, frame, 4);
+
+                               /* save image */
+                               dynamicPaint_outputSurfaceImage(surface, filename, 1);
                        }
                }
        }
@@ -360,14 +338,16 @@ static int dynamicPaint_bakeImageSequence(bContext *C, DynamicPaintSurface *surf
 
 
 /*
-*      Bake Dynamic Paint image sequence surface
-*/
-int dynamicPaint_initBake(struct bContext *C, struct wmOperator *op)
+ * Bake Dynamic Paint image sequence surface
+ */
+static int dynamicPaint_initBake(struct bContext *C, struct wmOperator *op)
 {
        DynamicPaintModifierData *pmd = NULL;
-       Object *ob = CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+       DynamicPaintCanvasSettings *canvas;
+       Object *ob = ED_object_context(C);
        int status = 0;
        double timer = PIL_check_seconds_timer();
+       char result_str[80];
        DynamicPaintSurface *surface;
 
        /*
@@ -380,66 +360,46 @@ int dynamicPaint_initBake(struct bContext *C, struct wmOperator *op)
        }
 
        /* Make sure we're dealing with a canvas */
-       if (!pmd->canvas) {
+       canvas = pmd->canvas;
+       if (!canvas) {
                BKE_report(op->reports, RPT_ERROR, "Bake Failed: Invalid Canvas.");
                return 0;
        }
-       surface = get_activeSurface(pmd->canvas);
+       surface = get_activeSurface(canvas);
 
        /* Set state to baking and init surface */
-       pmd->canvas->error[0] = '\0';
-       pmd->canvas->flags |= MOD_DPAINT_BAKING;
+       canvas->error[0] = '\0';
+       canvas->flags |= MOD_DPAINT_BAKING;
        G.afbreek= 0;   /* reset blender_test_break*/
 
        /*  Bake Dynamic Paint  */
        status = dynamicPaint_bakeImageSequence(C, surface, ob);
        /* Clear bake */
-       pmd->canvas->flags &= ~MOD_DPAINT_BAKING;
+       canvas->flags &= ~MOD_DPAINT_BAKING;
        WM_cursor_restore(CTX_wm_window(C));
        dynamicPaint_freeSurfaceData(surface);
 
        /* Bake was successful:
        *  Report for ended bake and how long it took */
        if (status) {
-
                /* Format time string   */
-               char timestr[30];
+               char time_str[30];
                double time = PIL_check_seconds_timer() - timer;
-               int tmp_val;
-               timestr[0] = '\0';
-
-               /* days (just in case someone actually has a very slow pc)      */
-               tmp_val = (int)floor(time / 86400.0f);
-               if (tmp_val > 0) sprintf(timestr, "%i Day(s) - ", tmp_val);
-               /* hours        */
-               time -= 86400.0f * tmp_val;
-               tmp_val = (int)floor(time / 3600.0f);
-               if (tmp_val > 0) sprintf(timestr, "%s%i h ", timestr, tmp_val);
-               /* minutes      */
-               time -= 3600.0f * tmp_val;
-               tmp_val = (int)floor(time / 60.0f);
-               if (tmp_val > 0) sprintf(timestr, "%s%i min ", timestr, tmp_val);
-               /* seconds      */
-               time -= 60.0f * tmp_val;
-               tmp_val = (int)ceil(time);
-               sprintf(timestr, "%s%i s", timestr, tmp_val);
+               BLI_timestr(time, time_str);
 
                /* Show bake info */
-               sprintf(pmd->canvas->ui_info, "Bake Complete! (Time: %s)", timestr);
-               printf("%s\n", pmd->canvas->ui_info);
+               BLI_snprintf(result_str, sizeof(result_str), "Bake Complete! (%s)", time_str);
+               BKE_report(op->reports, RPT_INFO, result_str);
        }
        else {
-               if (strlen(pmd->canvas->error)) { /* If an error occured */
-                       sprintf(pmd->canvas->ui_info, "Bake Failed: %s", pmd->canvas->error);
-                       BKE_report(op->reports, RPT_ERROR, pmd->canvas->ui_info);
+               if (strlen(canvas->error)) { /* If an error occured */
+                       BLI_snprintf(result_str, sizeof(result_str), "Bake Failed: %s", canvas->error);
+                       BKE_report(op->reports, RPT_ERROR, result_str);
                }
                else {  /* User cancelled the bake */
-                       sprintf(pmd->canvas->ui_info, "Baking Cancelled!");
-                       BKE_report(op->reports, RPT_WARNING, pmd->canvas->ui_info);
+                       BLI_strncpy(result_str, "Baking Cancelled!", sizeof(result_str));
+                       BKE_report(op->reports, RPT_WARNING, result_str);
                }
-
-               /* Print failed bake to console */
-               printf("Baking Cancelled!\n");
        }
 
        return status;
@@ -464,4 +424,4 @@ void DPAINT_OT_bake(wmOperatorType *ot)
        /* api callbacks */
        ot->exec= dynamicpaint_bake_exec;
        ot->poll= ED_operator_object_active_editable;
-}
\ No newline at end of file
+}