Revision: 30883
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=30883
Author:   campbellbarton
Date:     2010-07-30 02:20:05 +0200 (Fri, 30 Jul 2010)

Log Message:
-----------
patch [#23060] ColorRamp python access functions
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)

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_texture.h
    trunk/blender/source/blender/blenkernel/intern/texture.c
    trunk/blender/source/blender/blenloader/intern/readfile.c
    trunk/blender/source/blender/editors/interface/interface_handlers.c
    trunk/blender/source/blender/editors/interface/interface_templates.c
    trunk/blender/source/blender/makesrna/intern/rna_color.c

Modified: trunk/blender/source/blender/blenkernel/BKE_texture.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_texture.h       2010-07-30 
00:06:59 UTC (rev 30882)
+++ trunk/blender/source/blender/blenkernel/BKE_texture.h       2010-07-30 
00:20:05 UTC (rev 30883)
@@ -61,6 +61,9 @@
 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);

Modified: trunk/blender/source/blender/blenkernel/intern/texture.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/texture.c    2010-07-30 
00:06:59 UTC (rev 30882)
+++ trunk/blender/source/blender/blenkernel/intern/texture.c    2010-07-30 
00:20:05 UTC (rev 30883)
@@ -403,6 +403,73 @@
                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)

Modified: trunk/blender/source/blender/blenloader/intern/readfile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/readfile.c   2010-07-30 
00:06:59 UTC (rev 30882)
+++ trunk/blender/source/blender/blenloader/intern/readfile.c   2010-07-30 
00:20:05 UTC (rev 30883)
@@ -11078,10 +11078,6 @@
 
                                                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;
 

Modified: trunk/blender/source/blender/editors/interface/interface_handlers.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface_handlers.c 
2010-07-30 00:06:59 UTC (rev 30882)
+++ trunk/blender/source/blender/editors/interface/interface_handlers.c 
2010-07-30 00:20:05 UTC (rev 30883)
@@ -3251,24 +3251,8 @@
 
                        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 {

Modified: trunk/blender/source/blender/editors/interface/interface_templates.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface_templates.c        
2010-07-30 00:06:59 UTC (rev 30882)
+++ trunk/blender/source/blender/editors/interface/interface_templates.c        
2010-07-30 00:20:05 UTC (rev 30883)
@@ -1302,71 +1302,20 @@
 
 #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 @@
        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)

Modified: trunk/blender/source/blender/makesrna/intern/rna_color.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_color.c    2010-07-30 
00:06:59 UTC (rev 30882)
+++ trunk/blender/source/blender/makesrna/intern/rna_color.c    2010-07-30 
00:20:05 UTC (rev 30883)
@@ -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_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 @@
 {
        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 @@
        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 @@
        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");

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to