patch [#20889] Support "unit"s for FloatProperty
authorCampbell Barton <ideasman42@gmail.com>
Tue, 2 Feb 2010 23:03:56 +0000 (23:03 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 2 Feb 2010 23:03:56 +0000 (23:03 +0000)
from Martin B├╝rbaum (pontiac)
(with own minor changes)

source/blender/editors/interface/interface.c
source/blender/makesrna/RNA_access.h
source/blender/makesrna/RNA_enum_types.h
source/blender/makesrna/intern/rna_access.c
source/blender/makesrna/intern/rna_rna.c
source/blender/python/intern/bpy_props.c
source/blender/python/intern/bpy_rna.c

index 7d9d78f5b2055a03fc82f345f9325ebe469e9d5a..40efabda0611e09706fa86c83ed4d52e76645154 100644 (file)
@@ -1429,6 +1429,12 @@ static double ui_get_but_scale_unit(uiBut *but, double value)
        if(subtype == PROP_UNIT_LENGTH) {
                return value * scene->unit.scale_length;
        }
+       else if(subtype == PROP_UNIT_AREA) {
+               return value * pow(scene->unit.scale_length, 2);
+       }
+       else if(subtype == PROP_UNIT_VOLUME) {
+               return value * pow(scene->unit.scale_length, 3);
+       }
        else if(subtype == PROP_UNIT_TIME) { /* WARNING - using evil_C :| */
                return FRA2TIME(value);
        }
index 8559f30bfebcfb8c34e7ac356832c62538f5a944..61197b2296ed9dae54234c00e3ad9addedee063f 100644 (file)
@@ -649,7 +649,6 @@ void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *soft
 int RNA_enum_identifier(EnumPropertyItem *item, const int value, const char **identifier);
 int RNA_enum_bitflag_identifiers(EnumPropertyItem *item, const int value, const char **identifier);
 int RNA_enum_name(EnumPropertyItem *item, const int value, const char **name);
-int RNA_enum_value(EnumPropertyItem *item, const char *name, int *r_value);
 
 void RNA_property_enum_items(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, EnumPropertyItem **item, int *totitem, int *free);
 int RNA_property_enum_value(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, const char *identifier, int *value);
index 14b2f0c561eab2eb18d01203c67160f1ba4db2d3..3e07896d760e40b28488746b4707f32827dc9b96 100644 (file)
@@ -77,6 +77,8 @@ extern EnumPropertyItem keymap_propvalue_items[];
 
 extern EnumPropertyItem wm_report_items[];
 
+extern EnumPropertyItem property_unit_items[];
+
 struct bContext;
 struct PointerRNA;
 EnumPropertyItem *rna_TransformOrientation_itemf(struct bContext *C, struct PointerRNA *ptr, int *free);
index 84baec784df4b271eeb22efa76c6f36a4b62cc29..9dd03ed74772e81fbc8b1650f5d3f23cc36cd591 100644 (file)
@@ -1034,17 +1034,6 @@ int RNA_enum_name(EnumPropertyItem *item, const int value, const char **name)
        return 0;
 }
 
-int RNA_enum_value(EnumPropertyItem *item, const char *name, int *r_value)
-{
-       for (; item->identifier; item++) {
-               if(item->identifier[0] && strcmp(item->identifier, name)==0) {
-                       *r_value= item->value;
-                       return 1;
-               }
-       }
-       return 0;
-}
-
 int RNA_property_enum_identifier(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const int value, const char **identifier)
 {      
        EnumPropertyItem *item= NULL;
index 7de418108ed0f423b4b58827495905ac71f8db5a..b185d8f8961f283024f56eb7c87f9e9bb49d606c 100644 (file)
 
 #include "rna_internal.h"
 
+EnumPropertyItem property_unit_items[] = {
+       {PROP_UNIT_NONE, "NONE", 0, "None", ""},
+       {PROP_UNIT_LENGTH, "LENGTH", 0, "Length", ""},
+       {PROP_UNIT_AREA, "AREA", 0, "Area", ""},
+       {PROP_UNIT_VOLUME, "VOLUME", 0, "Volume", ""},
+       {PROP_UNIT_ROTATION, "ROTATION", 0, "Rotation", ""},
+       {PROP_UNIT_TIME, "TIME", 0, "Time", ""},
+       {PROP_UNIT_VELOCITY, "VELOCITY", 0, "Velocity", ""},
+       {PROP_UNIT_ACCELERATION, "ACCELERATION", 0, "Acceleration", ""},
+       {0, NULL, 0, NULL, NULL}};
+
 #ifdef RNA_RUNTIME
 
 #include "BLI_ghash.h"
@@ -926,16 +937,6 @@ static void rna_def_property(BlenderRNA *brna)
                {PROP_LAYER, "LAYER", 0, "Layer", ""},
                {PROP_LAYER_MEMBER, "LAYER_MEMBERSHIP", 0, "Layer Membership", ""},
                {0, NULL, 0, NULL, NULL}};
-       static EnumPropertyItem unit_items[] = {
-               {PROP_UNIT_NONE, "NONE", 0, "None", ""},
-               {PROP_UNIT_LENGTH, "LENGTH", 0, "Length", ""},
-               {PROP_UNIT_AREA, "AREA", 0, "Area", ""},
-               {PROP_UNIT_VOLUME, "VOLUME", 0, "Volume", ""},
-               {PROP_UNIT_ROTATION, "ROTATION", 0, "Rotation", ""},
-               {PROP_UNIT_TIME, "TIME", 0, "Time", ""},
-               {PROP_UNIT_VELOCITY, "VELOCITY", 0, "Velocity", ""},
-               {PROP_UNIT_ACCELERATION, "ACCELERATION", 0, "Acceleration", ""},
-               {0, NULL, 0, NULL, NULL}};
 
        srna= RNA_def_struct(brna, "Property", NULL);
        RNA_def_struct_ui_text(srna, "Property Definition", "RNA property definition.");
@@ -978,7 +979,7 @@ static void rna_def_property(BlenderRNA *brna)
 
        prop= RNA_def_property(srna, "unit", PROP_ENUM, PROP_NONE);
        RNA_def_property_clear_flag(prop, PROP_EDITABLE);
-       RNA_def_property_enum_items(prop, unit_items);
+       RNA_def_property_enum_items(prop, property_unit_items);
        RNA_def_property_enum_funcs(prop, "rna_Property_unit_get", NULL, NULL);
        RNA_def_property_ui_text(prop, "Unit", "Type of units for this property.");
 
index 2268ca1ab6dc9f3983b040943153dbcab415c536..75b31e9f732960a74e67ad1a8c27e711915484a4 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "RNA_access.h"
 #include "RNA_define.h" /* for defining our own rna */
+#include "RNA_enum_types.h"
 
 #include "MEM_guardedalloc.h"
 
@@ -127,7 +128,7 @@ PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
                if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "BoolProperty(options={...}):"))
                        return NULL;
 
-               if(pysubtype && RNA_enum_value(property_subtype_number_items, pysubtype, &subtype)==0) {
+               if(pysubtype && RNA_enum_value_from_id(property_subtype_number_items, pysubtype, &subtype)==0) {
                        PyErr_Format(PyExc_TypeError, "BoolProperty(subtype='%s'): invalid subtype.");
                        return NULL;
                }
@@ -189,7 +190,7 @@ PyObject *BPy_BoolVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
                if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "BoolVectorProperty(options={...}):"))
                        return NULL;
 
-               if(pysubtype && RNA_enum_value(property_subtype_array_items, pysubtype, &subtype)==0) {
+               if(pysubtype && RNA_enum_value_from_id(property_subtype_array_items, pysubtype, &subtype)==0) {
                        PyErr_Format(PyExc_TypeError, "BoolVectorProperty(subtype='%s'): invalid subtype.");
                        return NULL;
                }
@@ -258,7 +259,7 @@ PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
                if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "IntProperty(options={...}):"))
                        return NULL;
 
-               if(pysubtype && RNA_enum_value(property_subtype_number_items, pysubtype, &subtype)==0) {
+               if(pysubtype && RNA_enum_value_from_id(property_subtype_number_items, pysubtype, &subtype)==0) {
                        PyErr_Format(PyExc_TypeError, "IntProperty(subtype='%s'): invalid subtype.");
                        return NULL;
                }
@@ -321,7 +322,7 @@ PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
                if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "IntVectorProperty(options={...}):"))
                        return NULL;
 
-               if(pysubtype && RNA_enum_value(property_subtype_array_items, pysubtype, &subtype)==0) {
+               if(pysubtype && RNA_enum_value_from_id(property_subtype_array_items, pysubtype, &subtype)==0) {
                        PyErr_Format(PyExc_TypeError, "IntVectorProperty(subtype='%s'): invalid subtype.");
                        return NULL;
                }
@@ -355,14 +356,16 @@ PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
 
 
 static char BPy_FloatProperty_doc[] =
-".. function:: FloatProperty(name=\"\", description=\"\", default=0.0, min=sys.float_info.min, max=sys.float_info.max, soft_min=sys.float_info.min, soft_max=sys.float_info.max, step=3, precision=2, options={'ANIMATABLE'}, subtype='NONE')\n"
+".. function:: FloatProperty(name=\"\", description=\"\", default=0.0, min=sys.float_info.min, max=sys.float_info.max, soft_min=sys.float_info.min, soft_max=sys.float_info.max, step=3, precision=2, options={'ANIMATABLE'}, subtype='NONE', unit='NONE')\n"
 "\n"
 "   Returns a new float property definition.\n"
 "\n"
 "   :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
 "   :type options: set\n"
 "   :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
-"   :type subtype: string";
+"   :type subtype: string\n"
+"   :arg unit: Enumerator in ['NONE', 'LENGTH', 'AREA', 'VOLUME', 'ROTATION', 'TIME', 'VELOCITY', 'ACCELERATION'].\n"
+"   :type unit: string\n";
 PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
 {
        StructRNA *srna;
@@ -377,7 +380,7 @@ PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
                return NULL; /* self's type was compatible but error getting the srna */
        }
        else if(srna) {
-               static char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", NULL};
+               static char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", "unit", NULL};
                char *id=NULL, *name="", *description="";
                float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def=0.0f;
                int precision= 2;
@@ -386,19 +389,26 @@ PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
                int opts=0;
                char *pysubtype= NULL;
                int subtype= PROP_NONE;
+               char *pyunit= NULL;
+               int unit= PROP_UNIT_NONE;
 
-               if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssffffffiO!s:FloatProperty", kwlist, &id, &name, &description, &def, &min, &max, &soft_min, &soft_max, &step, &precision, &PySet_Type, &pyopts, &pysubtype))
+               if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssffffffiO!ss:FloatProperty", kwlist, &id, &name, &description, &def, &min, &max, &soft_min, &soft_max, &step, &precision, &PySet_Type, &pyopts, &pysubtype, &pyunit))
                        return NULL;
 
                if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "FloatProperty(options={...}):"))
                        return NULL;
 
-               if(pysubtype && RNA_enum_value(property_subtype_number_items, pysubtype, &subtype)==0) {
+               if(pysubtype && RNA_enum_value_from_id(property_subtype_number_items, pysubtype, &subtype)==0) {
                        PyErr_Format(PyExc_TypeError, "FloatProperty(subtype='%s'): invalid subtype.");
                        return NULL;
                }
 
-               prop= RNA_def_property(srna, id, PROP_FLOAT, subtype);
+               if(pyunit && RNA_enum_value_from_id(property_unit_items, pyunit, &unit)==0) {
+                       PyErr_Format(PyExc_TypeError, "FloatProperty(unit='%s'): invalid unit.");
+                       return NULL;
+               }
+
+               prop= RNA_def_property(srna, id, PROP_FLOAT, subtype | unit);
                RNA_def_property_float_default(prop, def);
                RNA_def_property_range(prop, min, max);
                RNA_def_property_ui_text(prop, name, description);
@@ -456,7 +466,7 @@ PyObject *BPy_FloatVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
                if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "FloatVectorProperty(options={...}):"))
                        return NULL;
 
-               if(pysubtype && RNA_enum_value(property_subtype_array_items, pysubtype, &subtype)==0) {
+               if(pysubtype && RNA_enum_value_from_id(property_subtype_array_items, pysubtype, &subtype)==0) {
                        PyErr_Format(PyExc_TypeError, "FloatVectorProperty(subtype='%s'): invalid subtype.");
                        return NULL;
                }
@@ -526,7 +536,7 @@ PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw)
                if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "StringProperty(options={...}):"))
                        return NULL;
 
-               if(pysubtype && RNA_enum_value(property_subtype_string_items, pysubtype, &subtype)==0) {
+               if(pysubtype && RNA_enum_value_from_id(property_subtype_string_items, pysubtype, &subtype)==0) {
                        PyErr_Format(PyExc_TypeError, "StringProperty(subtype='%s'): invalid subtype.");
                        return NULL;
                }
index ea445ecc59529eac4237d2c7e146f444fc215bd0..fde805f94eedba6baa49f9a5728f161d3845b7e0 100644 (file)
@@ -442,7 +442,7 @@ int pyrna_set_to_enum_bitfield(EnumPropertyItem *items, PyObject *value, int *r_
                        return -1;
                }
 
-               if(RNA_enum_value(items, param, &ret) == 0) {
+               if(RNA_enum_value_from_id(items, param, &ret) == 0) {
                        char *enum_str= BPy_enum_as_string(items);
                        PyErr_Format(PyExc_TypeError, "%s \"%.200s\" not found in (%.200s)", error_prefix, param, enum_str);
                        MEM_freeN(enum_str);