patch [#23060] ColorRamp python access functions
authorCampbell Barton <ideasman42@gmail.com>
Fri, 30 Jul 2010 00:20:05 +0000 (00:20 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 30 Jul 2010 00:20:05 +0000 (00:20 +0000)
from Dan Eicher (dna)
 elem = color_ramp.elements.new(position=0.3)
 color_ramp.elements.remove(elem)

- Modified the patch to make generic functions for adding and removing (inline code was in 3 different places)

source/blender/blenkernel/BKE_texture.h
source/blender/blenkernel/intern/texture.c
source/blender/blenloader/intern/readfile.c
source/blender/editors/interface/interface_handlers.c
source/blender/editors/interface/interface_templates.c
source/blender/makesrna/intern/rna_color.c

index 39c12a2bfd875ea9be70c1d83c4b3beef15c03ae..380672dae04adf6e6401ebbc594db1e043e7ff50 100644 (file)
@@ -61,6 +61,9 @@ void init_colorband(struct ColorBand *coba, int rangetype);
 struct ColorBand *add_colorband(int rangetype);
 int do_colorband(struct ColorBand *coba, float in, float out[4]);
 void colorband_table_RGBA(struct ColorBand *coba, float **array, int *size);
+int vergcband(const void *a1, const void *a2);
+struct CBData *colorband_element_add(struct ColorBand *coba, float position);
+int colorband_element_remove(struct ColorBand *coba, int index);
 
 void default_tex(struct Tex *tex);
 struct Tex *add_texture(const char *name);
index 57816b7e47068ba7629416824c9f29ce36715787..d05d8ccbc0880efd106acc5261f6e5534910db87 100644 (file)
@@ -403,6 +403,73 @@ void colorband_table_RGBA(ColorBand *coba, float **array, int *size)
                do_colorband(coba, (float)a/(float)CM_TABLE, &(*array)[a*4]);
 }
 
+int vergcband(const void *a1, const void *a2)
+{
+       const CBData *x1=a1, *x2=a2;
+
+       if( x1->pos > x2->pos ) return 1;
+       else if( x1->pos < x2->pos) return -1;
+       return 0;
+}
+
+CBData *colorband_element_add(struct ColorBand *coba, float position)
+{
+       int a;
+
+       if(coba->tot==MAXCOLORBAND) {
+               return NULL;
+       }
+       else if(coba->tot > 0) {
+               CBData *xnew;
+               float col[4];
+
+               do_colorband(coba, position, col);
+
+               xnew = &coba->data[coba->tot];
+               xnew->pos = position;
+
+               xnew->r = col[0];
+               xnew->g = col[1];
+               xnew->b = col[2];
+               xnew->a = col[3];
+       }
+
+       coba->tot++;
+       coba->cur = coba->tot-1;
+
+       for(a = 0; a < coba->tot; a++)
+               coba->data[a].cur = a;
+
+       qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
+
+       for(a = 0; a < coba->tot; a++) {
+               if(coba->data[a].cur == coba->cur) {
+                       coba->cur = a;
+                       break;
+               }
+       }
+
+       return coba->data + coba->cur;
+}
+
+int colorband_element_remove(struct ColorBand *coba, int index)
+{
+       int a;
+
+       if(coba->tot < 2)
+               return 0;
+
+       if(index < 0 || index >= coba->tot)
+               return 0;
+
+       for(a = index; a < coba->tot; a++) {
+               coba->data[a] = coba->data[a + 1];
+       }
+       if(coba->cur) coba->cur--;
+       coba->tot--;
+       return 1;
+}
+
 /* ******************* TEX ************************ */
 
 void free_texture(Tex *tex)
index 3227d4b25bb14284b8b4e7e361ea1d150c5f344e..c050f25256adf3a3a8d01f3831e3252428ff388d 100644 (file)
@@ -11078,10 +11078,6 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
 
                                                if((smd->type & MOD_SMOKE_TYPE_DOMAIN) && smd->domain)
                                                {
-                                                       GroupObject *go = NULL;
-                                                       Base *base = NULL;
-                                                       Scene *scene = NULL;
-
                                                        smd->domain->vorticity = 2.0f;
                                                        smd->domain->time_scale = 1.0f;
 
index 0f8f84b94dc35bc7367e52ca017526990edec906..f617cc4ae8d829d5b5b535fb48a48c6ae2b22eca 100644 (file)
@@ -3251,24 +3251,8 @@ static int ui_do_but_COLORBAND(bContext *C, uiBlock *block, uiBut *but, uiHandle
 
                        if(event->ctrl) {
                                /* insert new key on mouse location */
-                               if(coba->tot < MAXCOLORBAND-1) {
-                                       float pos= ((float)(mx - but->x1))/(but->x2-but->x1);
-                                       float col[4];
-                                       
-                                       do_colorband(coba, pos, col);   /* executes it */
-                                       
-                                       coba->tot++;
-                                       coba->cur= coba->tot-1;
-                                       
-                                       coba->data[coba->cur].r= col[0];
-                                       coba->data[coba->cur].g= col[1];
-                                       coba->data[coba->cur].b= col[2];
-                                       coba->data[coba->cur].a= col[3];
-                                       coba->data[coba->cur].pos= pos;
-
-                                       ui_colorband_update(coba);
-                               }
-
+                               float pos= ((float)(mx - but->x1))/(but->x2-but->x1);
+                               colorband_element_add(coba, pos);
                                button_activate_state(C, but, BUTTON_STATE_EXIT);
                        }
                        else {
index 28d0e131ef24c33242635ed30929e98190bd8c29..01d2ad29b0e6b8b22253e301de28da355c9e0d4f 100644 (file)
@@ -1302,71 +1302,20 @@ static void rna_update_cb(bContext *C, void *arg_cb, void *arg_unused)
 
 #define B_BANDCOL 1
 
-static int vergcband(const void *a1, const void *a2)
-{
-       const CBData *x1=a1, *x2=a2;
-
-       if( x1->pos > x2->pos ) return 1;
-       else if( x1->pos < x2->pos) return -1;
-       return 0;
-}
-
-static void colorband_pos_cb(bContext *C, void *cb_v, void *coba_v)
-{
-       ColorBand *coba= coba_v;
-       int a;
-
-       if(coba->tot<2) return;
-
-       for(a=0; a<coba->tot; a++) coba->data[a].cur= a;
-       qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
-       for(a=0; a<coba->tot; a++) {
-               if(coba->data[a].cur==coba->cur) {
-                       coba->cur= a;
-                       break;
-               }
-       }
-
-       rna_update_cb(C, cb_v, NULL);
-}
-
 static void colorband_add_cb(bContext *C, void *cb_v, void *coba_v)
 {
        ColorBand *coba= coba_v;
+       float pos= 0.5f;
 
-       if(coba->tot > 0) {
-               CBData *xnew, *x1, *x2;
-               float col[4];
-
-               xnew= &coba->data[coba->tot];
-
-               if(coba->tot > 1) {
-                       if(coba->cur > 0) {
-                               x1= &coba->data[coba->cur-1];
-                               x2= &coba->data[coba->cur];
-                       }
-                       else {
-                               x1= &coba->data[coba->cur];
-                               x2= &coba->data[coba->cur+1];
-                       }
-
-                       xnew->pos = x1->pos + ((x2->pos - x1->pos) / 2);
-               }
-
-               do_colorband(coba, xnew->pos, col);
-
-               xnew->r= col[0];
-               xnew->g= col[1];
-               xnew->b= col[2];
-               xnew->a= col[3];
+       if(coba->tot > 1) {
+               if(coba->cur > 0)       pos= (coba->data[coba->cur-1].pos + coba->data[coba->cur].pos) * 0.5f;
+               else                            pos= (coba->data[coba->cur+1].pos + coba->data[coba->cur].pos) * 0.5f;
        }
 
-       if(coba->tot < MAXCOLORBAND-1) coba->tot++;
-       coba->cur= coba->tot-1;
-
-       colorband_pos_cb(C, cb_v, coba_v);
-
-       ED_undo_push(C, "Add colorband");
+       if(colorband_element_add(coba, pos)) {
+               rna_update_cb(C, cb_v, NULL);
+               ED_undo_push(C, "Add colorband");       
+       }
 }
 
 static void colorband_del_cb(bContext *C, void *cb_v, void *coba_v)
@@ -1374,17 +1323,10 @@ static void colorband_del_cb(bContext *C, void *cb_v, void *coba_v)
        ColorBand *coba= coba_v;
        int a;
 
-       if(coba->tot<2) return;
-
-       for(a=coba->cur; a<coba->tot; a++) {
-               coba->data[a]= coba->data[a+1];
+       if(colorband_element_remove(coba, coba->cur)) {
+               ED_undo_push(C, "Delete colorband");
+               rna_update_cb(C, cb_v, NULL);
        }
-       if(coba->cur) coba->cur--;
-       coba->tot--;
-
-       ED_undo_push(C, "Delete colorband");
-
-       rna_update_cb(C, cb_v, NULL);
 }
 
 static void colorband_flip_cb(bContext *C, void *cb_v, void *coba_v)
index 9e004cd5946a82f0906ccae2855b52bc4d9fc8f7..f33ca10f934ae40ea8135d7fc833d4f01de2598a 100644 (file)
@@ -42,6 +42,7 @@
 #include "BKE_colortools.h"
 #include "BKE_depsgraph.h"
 #include "BKE_node.h"
+#include "BKE_texture.h"
 
 #include "WM_api.h"
 #include "WM_types.h"
@@ -270,6 +271,29 @@ static void rna_ColorRamp_update(Main *bmain, Scene *scene, PointerRNA *ptr)
        }
 }
 
+static void rna_ColorRamp_eval(struct ColorBand *coba, float position, float color[4])
+{
+       do_colorband(coba, position, color);
+}
+
+static CBData *rna_ColorRampElement_new(struct ColorBand *coba, ReportList *reports, float position)
+{
+       CBData *element= colorband_element_add(coba, position);
+
+       if(element==NULL)
+               BKE_reportf(reports, RPT_ERROR, "Unable to add element to colorband (limit %d)", MAXCOLORBAND);
+
+       return element;
+}
+
+static void rna_ColorRampElement_remove(struct ColorBand *coba, ReportList *reports, CBData *element)
+{
+       int index = (int)(element - coba->data);
+       if(colorband_element_remove(coba, index) == 0)
+               BKE_report(reports, RPT_ERROR, "Element not found in element collection or last element");
+
+}
+
 static void rna_Scopes_update(Main *bmain, Scene *scene, PointerRNA *ptr)
 {
        Scopes *s= (Scopes*)ptr->data;
@@ -396,7 +420,7 @@ static void rna_def_color_ramp_element(BlenderRNA *brna)
 {
        StructRNA *srna;
        PropertyRNA *prop;
-       
+
        srna= RNA_def_struct(brna, "ColorRampElement", NULL);
        RNA_def_struct_sdna(srna, "CBData");
        RNA_def_struct_path_func(srna, "rna_ColorRampElement_path");
@@ -415,11 +439,42 @@ static void rna_def_color_ramp_element(BlenderRNA *brna)
        RNA_def_property_update(prop, 0, "rna_ColorRamp_update");
 }
 
+static void rna_def_color_ramp_element_api(BlenderRNA *brna, PropertyRNA *cprop)
+{
+       StructRNA *srna;
+       PropertyRNA *parm;
+       FunctionRNA *func;
+
+       RNA_def_property_srna(cprop, "ColorRampElements");
+       srna= RNA_def_struct(brna, "ColorRampElements", NULL);
+       RNA_def_struct_sdna(srna, "ColorBand");
+       RNA_def_struct_path_func(srna, "rna_ColorRampElement_path");
+       RNA_def_struct_ui_text(srna, "Color Ramp Elements", "Collection of Color Ramp Elements");
+
+       /* TODO, make these functions generic in texture.c */
+       func = RNA_def_function(srna, "new", "rna_ColorRampElement_new");
+       RNA_def_function_ui_description(func, "Add element to ColorRamp");
+       RNA_def_function_flag(func, FUNC_USE_REPORTS);
+       parm= RNA_def_float(func, "position", 0.0f, 0.0f, 1.0f, "Position", "Position to add element", 0.0f, 1.0f);
+       RNA_def_property_flag(parm, PROP_REQUIRED);
+       /* return type */
+       parm= RNA_def_pointer(func, "element", "ColorRampElement", "", "New element.");
+       RNA_def_function_return(func, parm);
+
+       func = RNA_def_function(srna, "remove", "rna_ColorRampElement_remove");
+       RNA_def_function_ui_description(func, "Delete element from ColorRamp");
+       RNA_def_function_flag(func, FUNC_USE_REPORTS);
+       //parm= RNA_def_int(func, "index", 0, 0, 31, "Index", "Element to delete.", 0, 31);
+       parm= RNA_def_pointer(func, "element", "ColorRampElement", "", "Element to remove.");
+       RNA_def_property_flag(parm, PROP_REQUIRED);
+}
+
 static void rna_def_color_ramp(BlenderRNA *brna)
 {
        StructRNA *srna;
        PropertyRNA *prop;
-       
+       FunctionRNA *func;
+
        static EnumPropertyItem prop_interpolation_items[] = {
                {1, "EASE", 0, "Ease", ""},
                {3, "CARDINAL", 0, "Cardinal", ""},
@@ -438,12 +493,30 @@ static void rna_def_color_ramp(BlenderRNA *brna)
        RNA_def_property_struct_type(prop, "ColorRampElement");
        RNA_def_property_ui_text(prop, "Elements", "");
        RNA_def_property_update(prop, 0, "rna_ColorRamp_update");
-       
+       rna_def_color_ramp_element_api(brna, prop);
+
        prop= RNA_def_property(srna, "interpolation", PROP_ENUM, PROP_NONE);
        RNA_def_property_enum_sdna(prop, NULL, "ipotype");
        RNA_def_property_enum_items(prop, prop_interpolation_items);
        RNA_def_property_ui_text(prop, "Interpolation", "");
        RNA_def_property_update(prop, 0, "rna_ColorRamp_update");
+
+       prop= RNA_def_property(srna, "total", PROP_INT, PROP_NONE);
+       RNA_def_property_int_sdna(prop, NULL, "tot");
+       /* needs a function to do the right thing when adding elements like colorband_add_cb() */
+       RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+       RNA_def_property_range(prop, 0, 31); /* MAXCOLORBAND = 32 */
+       RNA_def_property_ui_text(prop, "Total", "Total number of elements");
+       RNA_def_property_update(prop, 0, "rna_ColorRamp_update");
+
+       func = RNA_def_function(srna, "evaluate", "rna_ColorRamp_eval");
+       RNA_def_function_ui_description(func, "Evaluate ColorRamp");
+       prop= RNA_def_float(func, "position", 1.0f, 0.0f, 1.0f, "Position", "Evaluate ColorRamp at position", 0.0f, 1.0f);
+       RNA_def_property_flag(prop, PROP_REQUIRED);
+       /* return */
+       prop = RNA_def_float_color(func, "color", 4, NULL, -FLT_MAX, FLT_MAX, "Color", "Color at given position", -FLT_MAX, FLT_MAX);
+       RNA_def_property_flag(prop, PROP_THICK_WRAP);
+       RNA_def_function_output(func, prop);
 }
 
 static void rna_def_histogram(BlenderRNA *brna)