This patch adds support in bpy.props for getter/setter callback functions. We already have update callbacks, but generic get/set functions can come in handy in some cases where the functionality is too complex to use a single value.

The current C callback functions are too simple allow a straightforward implementation, in particular they don't receive the PropertyRNA pointer itself as an argument, which means the callback cannot directly access the PropertyRNA's py_data pointers which store the python function objects. For this reason a second runtime variant of these callbacks has been added. It is only used for runtime callbacks and not in makesrna, but otherwise works the same way.
This commit is contained in:
Lukas Toenne 2013-01-05 14:56:37 +00:00
parent 5ee3cd6c86
commit e8b415bdb4
9 changed files with 2195 additions and 768 deletions

View File

@ -91,7 +91,6 @@ PropertyRNA *RNA_def_string_translate(StructOrFunctionRNA *cont, const char *ide
PropertyRNA *RNA_def_enum(StructOrFunctionRNA *cont, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description);
PropertyRNA *RNA_def_enum_flag(StructOrFunctionRNA *cont, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description);
void RNA_def_enum_funcs(PropertyRNA *prop, EnumPropertyItemFunc itemfunc);
void RNA_def_enum_py_data(PropertyRNA *prop, void *py_data);
PropertyRNA *RNA_def_float(StructOrFunctionRNA *cont, const char *identifier, float default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax);
PropertyRNA *RNA_def_float_vector(StructOrFunctionRNA *cont, const char *identifier, int len, const float *default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax);
@ -177,6 +176,17 @@ void RNA_def_property_collection_funcs(PropertyRNA *prop, const char *begin, con
void RNA_def_property_srna(PropertyRNA *prop, const char *type);
void RNA_def_py_data(PropertyRNA *prop, void *py_data);
void RNA_def_property_boolean_funcs_runtime(PropertyRNA *prop, BooleanPropertyGetFunc getfunc, BooleanPropertySetFunc setfunc);
void RNA_def_property_boolean_array_funcs_runtime(PropertyRNA *prop, BooleanArrayPropertyGetFunc getfunc, BooleanArrayPropertySetFunc setfunc);
void RNA_def_property_int_funcs_runtime(PropertyRNA *prop, IntPropertyGetFunc getfunc, IntPropertySetFunc setfunc, IntPropertyRangeFunc rangefunc);
void RNA_def_property_int_array_funcs_runtime(PropertyRNA *prop, IntArrayPropertyGetFunc getfunc, IntArrayPropertySetFunc setfunc, IntPropertyRangeFunc rangefunc);
void RNA_def_property_float_funcs_runtime(PropertyRNA *prop, FloatPropertyGetFunc getfunc, FloatPropertySetFunc setfunc, FloatPropertyRangeFunc rangefunc);
void RNA_def_property_float_array_funcs_runtime(PropertyRNA *prop, FloatArrayPropertyGetFunc getfunc, FloatArrayPropertySetFunc setfunc, FloatPropertyRangeFunc rangefunc);
void RNA_def_property_enum_funcs_runtime(PropertyRNA *prop, EnumPropertyGetFunc getfunc, EnumPropertySetFunc setfunc, EnumPropertyItemFunc itemfunc);
void RNA_def_property_string_funcs_runtime(PropertyRNA *prop, StringPropertyGetFunc getfunc, StringPropertyLengthFunc lengthfunc, StringPropertySetFunc setfunc);
void RNA_def_property_enum_py_data(PropertyRNA *prop, void *py_data);
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context);
/* Function */

View File

@ -269,7 +269,27 @@ typedef struct EnumPropertyItem {
const char *description;
} EnumPropertyItem;
/* this is a copy of 'PropEnumItemFunc' defined in rna_internal_types.h */
/* extended versions with PropertyRNA argument */
typedef int (*BooleanPropertyGetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop);
typedef void (*BooleanPropertySetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop, int value);
typedef void (*BooleanArrayPropertyGetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop, int *values);
typedef void (*BooleanArrayPropertySetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop, const int *values);
typedef int (*IntPropertyGetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop);
typedef void (*IntPropertySetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop, int value);
typedef void (*IntArrayPropertyGetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop, int *values);
typedef void (*IntArrayPropertySetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop, const int *values);
typedef void (*IntPropertyRangeFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop, int *min, int *max, int *softmin, int *softmax);
typedef float (*FloatPropertyGetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop);
typedef void (*FloatPropertySetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop, float value);
typedef void (*FloatArrayPropertyGetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop, float *values);
typedef void (*FloatArrayPropertySetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop, const float *values);
typedef void (*FloatPropertyRangeFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop, float *min, float *max, float *softmin, float *softmax);
typedef void (*StringPropertyGetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop, char *value);
typedef int (*StringPropertyLengthFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop);
typedef void (*StringPropertySetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop, const char *value);
typedef int (*EnumPropertyGetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop);
typedef void (*EnumPropertySetFunc)(struct PointerRNA *ptr, struct PropertyRNA *prop, int value);
/* same as PropEnumItemFunc */
typedef EnumPropertyItem *(*EnumPropertyItemFunc)(struct bContext *C, PointerRNA *ptr, struct PropertyRNA *prop, int *free);
typedef struct PropertyRNA PropertyRNA;

View File

@ -2947,11 +2947,15 @@ static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, Pr
case PROP_BOOLEAN:
{
BoolPropertyRNA *bprop = (BoolPropertyRNA *)prop;
fprintf(f, "\t%s, %s, %s, %s, %d, ",
fprintf(f, "\t%s, %s, %s, %s, %s, %s, %s, %s, %d, ",
rna_function_string(bprop->get),
rna_function_string(bprop->set),
rna_function_string(bprop->getarray),
rna_function_string(bprop->setarray),
rna_function_string(bprop->get_ex),
rna_function_string(bprop->set_ex),
rna_function_string(bprop->getarray_ex),
rna_function_string(bprop->setarray_ex),
bprop->defaultvalue);
if (prop->arraydimension && prop->totarraylength)
fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
@ -2961,12 +2965,17 @@ static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, Pr
case PROP_INT:
{
IntPropertyRNA *iprop = (IntPropertyRNA *)prop;
fprintf(f, "\t%s, %s, %s, %s, %s,\n\t",
fprintf(f, "\t%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,\n\t",
rna_function_string(iprop->get),
rna_function_string(iprop->set),
rna_function_string(iprop->getarray),
rna_function_string(iprop->setarray),
rna_function_string(iprop->range));
rna_function_string(iprop->range),
rna_function_string(iprop->get_ex),
rna_function_string(iprop->set_ex),
rna_function_string(iprop->getarray_ex),
rna_function_string(iprop->setarray_ex),
rna_function_string(iprop->range_ex));
rna_int_print(f, iprop->softmin); fprintf(f, ", ");
rna_int_print(f, iprop->softmax); fprintf(f, ", ");
rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
@ -2981,12 +2990,17 @@ static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, Pr
case PROP_FLOAT:
{
FloatPropertyRNA *fprop = (FloatPropertyRNA *)prop;
fprintf(f, "\t%s, %s, %s, %s, %s, ",
fprintf(f, "\t%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, ",
rna_function_string(fprop->get),
rna_function_string(fprop->set),
rna_function_string(fprop->getarray),
rna_function_string(fprop->setarray),
rna_function_string(fprop->range));
rna_function_string(fprop->range),
rna_function_string(fprop->get_ex),
rna_function_string(fprop->set_ex),
rna_function_string(fprop->getarray_ex),
rna_function_string(fprop->setarray_ex),
rna_function_string(fprop->range_ex));
rna_float_print(f, fprop->softmin); fprintf(f, ", ");
rna_float_print(f, fprop->softmax); fprintf(f, ", ");
rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
@ -3002,10 +3016,13 @@ static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, Pr
case PROP_STRING:
{
StringPropertyRNA *sprop = (StringPropertyRNA *)prop;
fprintf(f, "\t%s, %s, %s, %d, ",
fprintf(f, "\t%s, %s, %s, %s, %s, %s, %d, ",
rna_function_string(sprop->get),
rna_function_string(sprop->length),
rna_function_string(sprop->set),
rna_function_string(sprop->get_ex),
rna_function_string(sprop->length_ex),
rna_function_string(sprop->set_ex),
sprop->maxlength);
rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
break;
@ -3013,10 +3030,12 @@ static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, Pr
case PROP_ENUM:
{
EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop;
fprintf(f, "\t%s, %s, %s, NULL, ",
fprintf(f, "\t%s, %s, %s, %s, %s, NULL, ",
rna_function_string(eprop->get),
rna_function_string(eprop->set),
rna_function_string(eprop->itemf));
rna_function_string(eprop->itemf),
rna_function_string(eprop->get_ex),
rna_function_string(eprop->set_ex));
if (eprop->item)
fprintf(f, "rna_%s%s_%s_items, ", srna->identifier, strnest, prop->identifier);
else

View File

@ -40,6 +40,7 @@
#include "BLI_utildefines.h"
#include "BLI_dynstr.h"
#include "BLI_ghash.h"
#include "BLI_math.h"
#include "BLF_api.h"
#include "BLF_translation.h"
@ -937,6 +938,12 @@ void RNA_property_int_range(PointerRNA *ptr, PropertyRNA *prop, int *hardmin, in
iprop->range(ptr, hardmin, hardmax, &softmin, &softmax);
}
else if (iprop->range_ex) {
*hardmin = INT_MIN;
*hardmax = INT_MAX;
iprop->range_ex(ptr, prop, hardmin, hardmax, &softmin, &softmax);
}
else {
*hardmin = iprop->hardmin;
*hardmax = iprop->hardmax;
@ -977,8 +984,17 @@ void RNA_property_int_ui_range(PointerRNA *ptr, PropertyRNA *prop, int *softmin,
iprop->range(ptr, &hardmin, &hardmax, softmin, softmax);
*softmin = MAX2(*softmin, hardmin);
*softmax = MIN2(*softmax, hardmax);
*softmin = max_ii(*softmin, hardmin);
*softmax = min_ii(*softmax, hardmax);
}
else if (iprop->range_ex) {
hardmin = INT_MIN;
hardmax = INT_MAX;
iprop->range_ex(ptr, prop, &hardmin, &hardmax, softmin, softmax);
*softmin = max_ii(*softmin, hardmin);
*softmax = min_ii(*softmax, hardmax);
}
*step = iprop->step;
@ -1012,6 +1028,12 @@ void RNA_property_float_range(PointerRNA *ptr, PropertyRNA *prop, float *hardmin
fprop->range(ptr, hardmin, hardmax, &softmin, &softmax);
}
else if (fprop->range_ex) {
*hardmin = -FLT_MAX;
*hardmax = FLT_MAX;
fprop->range_ex(ptr, prop, hardmin, hardmax, &softmin, &softmax);
}
else {
*hardmin = fprop->hardmin;
*hardmax = fprop->hardmax;
@ -1056,8 +1078,17 @@ void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *soft
fprop->range(ptr, &hardmin, &hardmax, softmin, softmax);
*softmin = MAX2(*softmin, hardmin);
*softmax = MIN2(*softmax, hardmax);
*softmin = max_ff(*softmin, hardmin);
*softmax = min_ff(*softmax, hardmax);
}
else if (fprop->range_ex) {
hardmin = -FLT_MAX;
hardmax = FLT_MAX;
fprop->range_ex(ptr, prop, &hardmin, &hardmax, softmin, softmax);
*softmin = max_ff(*softmin, hardmin);
*softmax = min_ff(*softmax, hardmax);
}
*step = fprop->step;
@ -1645,6 +1676,8 @@ int RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
return IDP_Int(idprop);
else if (bprop->get)
return bprop->get(ptr);
else if (bprop->get_ex)
return bprop->get_ex(ptr, prop);
else
return bprop->defaultvalue;
}
@ -1667,6 +1700,9 @@ void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, int value)
else if (bprop->set) {
bprop->set(ptr, value);
}
else if (bprop->set_ex) {
bprop->set_ex(ptr, prop, value);
}
else if (prop->flag & PROP_EDITABLE) {
IDPropertyTemplate val = {0};
IDProperty *group;
@ -1697,6 +1733,8 @@ void RNA_property_boolean_get_array(PointerRNA *ptr, PropertyRNA *prop, int *val
values[0] = RNA_property_boolean_get(ptr, prop);
else if (bprop->getarray)
bprop->getarray(ptr, values);
else if (bprop->getarray_ex)
bprop->getarray_ex(ptr, prop, values);
else if (bprop->defaultarray)
memcpy(values, bprop->defaultarray, sizeof(int) * prop->totarraylength);
else
@ -1747,6 +1785,8 @@ void RNA_property_boolean_set_array(PointerRNA *ptr, PropertyRNA *prop, const in
RNA_property_boolean_set(ptr, prop, values[0]);
else if (bprop->setarray)
bprop->setarray(ptr, values);
else if (bprop->setarray_ex)
bprop->setarray_ex(ptr, prop, values);
else if (prop->flag & PROP_EDITABLE) {
IDPropertyTemplate val = {0};
IDProperty *group;
@ -1848,6 +1888,8 @@ int RNA_property_int_get(PointerRNA *ptr, PropertyRNA *prop)
return IDP_Int(idprop);
else if (iprop->get)
return iprop->get(ptr);
else if (iprop->get_ex)
return iprop->get_ex(ptr, prop);
else
return iprop->defaultvalue;
}
@ -1868,6 +1910,8 @@ void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
}
else if (iprop->set)
iprop->set(ptr, value);
else if (iprop->set_ex)
iprop->set_ex(ptr, prop, value);
else if (prop->flag & PROP_EDITABLE) {
IDPropertyTemplate val = {0};
IDProperty *group;
@ -1900,6 +1944,8 @@ void RNA_property_int_get_array(PointerRNA *ptr, PropertyRNA *prop, int *values)
values[0] = RNA_property_int_get(ptr, prop);
else if (iprop->getarray)
iprop->getarray(ptr, values);
else if (iprop->getarray_ex)
iprop->getarray_ex(ptr, prop, values);
else if (iprop->defaultarray)
memcpy(values, iprop->defaultarray, sizeof(int) * prop->totarraylength);
else
@ -1987,6 +2033,8 @@ void RNA_property_int_set_array(PointerRNA *ptr, PropertyRNA *prop, const int *v
RNA_property_int_set(ptr, prop, values[0]);
else if (iprop->setarray)
iprop->setarray(ptr, values);
else if (iprop->setarray_ex)
iprop->setarray_ex(ptr, prop, values);
else if (prop->flag & PROP_EDITABLE) {
IDPropertyTemplate val = {0};
IDProperty *group;
@ -2087,6 +2135,8 @@ float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
}
else if (fprop->get)
return fprop->get(ptr);
else if (fprop->get_ex)
return fprop->get_ex(ptr, prop);
else
return fprop->defaultvalue;
}
@ -2112,6 +2162,9 @@ void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
else if (fprop->set) {
fprop->set(ptr, value);
}
else if (fprop->set_ex) {
fprop->set_ex(ptr, prop, value);
}
else if (prop->flag & PROP_EDITABLE) {
IDPropertyTemplate val = {0};
IDProperty *group;
@ -2150,6 +2203,8 @@ void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *val
values[0] = RNA_property_float_get(ptr, prop);
else if (fprop->getarray)
fprop->getarray(ptr, values);
else if (fprop->getarray_ex)
fprop->getarray_ex(ptr, prop, values);
else if (fprop->defaultarray)
memcpy(values, fprop->defaultarray, sizeof(float) * prop->totarraylength);
else
@ -2249,6 +2304,9 @@ void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const floa
else if (fprop->setarray) {
fprop->setarray(ptr, values);
}
else if (fprop->setarray_ex) {
fprop->setarray_ex(ptr, prop, values);
}
else if (prop->flag & PROP_EDITABLE) {
IDPropertyTemplate val = {0};
IDProperty *group;
@ -2361,6 +2419,9 @@ void RNA_property_string_get(PointerRNA *ptr, PropertyRNA *prop, char *value)
else if (sprop->get) {
sprop->get(ptr, value);
}
else if (sprop->get_ex) {
sprop->get_ex(ptr, prop, value);
}
else {
strcpy(value, sprop->defaultvalue);
}
@ -2421,6 +2482,8 @@ int RNA_property_string_length(PointerRNA *ptr, PropertyRNA *prop)
}
else if (sprop->length)
return sprop->length(ptr);
else if (sprop->length_ex)
return sprop->length_ex(ptr, prop);
else
return strlen(sprop->defaultvalue);
}
@ -2439,6 +2502,8 @@ void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *val
}
else if (sprop->set)
sprop->set(ptr, value); /* set function needs to clamp its self */
else if (sprop->set_ex)
sprop->set_ex(ptr, prop, value); /* set function needs to clamp its self */
else if (prop->flag & PROP_EDITABLE) {
IDProperty *group;
@ -2497,6 +2562,8 @@ int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
return IDP_Int(idprop);
else if (eprop->get)
return eprop->get(ptr);
else if (eprop->get_ex)
return eprop->get_ex(ptr, prop);
else
return eprop->defaultvalue;
}
@ -2515,6 +2582,9 @@ void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
else if (eprop->set) {
eprop->set(ptr, value);
}
else if (eprop->set_ex) {
eprop->set_ex(ptr, prop, value);
}
else if (prop->flag & PROP_EDITABLE) {
IDPropertyTemplate val = {0};
IDProperty *group;

View File

@ -2017,6 +2017,38 @@ void RNA_def_property_boolean_funcs(PropertyRNA *prop, const char *get, const ch
}
}
void RNA_def_property_boolean_funcs_runtime(PropertyRNA *prop, BooleanPropertyGetFunc getfunc, BooleanPropertySetFunc setfunc)
{
BoolPropertyRNA *bprop = (BoolPropertyRNA *)prop;
if (getfunc) bprop->get_ex = getfunc;
if (setfunc) bprop->set_ex = setfunc;
if (getfunc || setfunc) {
/* don't save in id properties */
prop->flag &= ~PROP_IDPROPERTY;
if (!setfunc)
prop->flag &= ~PROP_EDITABLE;
}
}
void RNA_def_property_boolean_array_funcs_runtime(PropertyRNA *prop, BooleanArrayPropertyGetFunc getfunc, BooleanArrayPropertySetFunc setfunc)
{
BoolPropertyRNA *bprop = (BoolPropertyRNA *)prop;
if (getfunc) bprop->getarray_ex = getfunc;
if (setfunc) bprop->setarray_ex = setfunc;
if (getfunc || setfunc) {
/* don't save in id properties */
prop->flag &= ~PROP_IDPROPERTY;
if (!setfunc)
prop->flag &= ~PROP_EDITABLE;
}
}
void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
{
StructRNA *srna = DefRNA.laststruct;
@ -2049,6 +2081,38 @@ void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *
}
}
void RNA_def_property_int_funcs_runtime(PropertyRNA *prop, IntPropertyGetFunc getfunc, IntPropertySetFunc setfunc, IntPropertyRangeFunc rangefunc)
{
IntPropertyRNA *iprop = (IntPropertyRNA *)prop;
if (getfunc) iprop->get_ex = getfunc;
if (setfunc) iprop->set_ex = setfunc;
if (getfunc || setfunc) {
/* don't save in id properties */
prop->flag &= ~PROP_IDPROPERTY;
if (!setfunc)
prop->flag &= ~PROP_EDITABLE;
}
}
void RNA_def_property_int_array_funcs_runtime(PropertyRNA *prop, IntArrayPropertyGetFunc getfunc, IntArrayPropertySetFunc setfunc, IntPropertyRangeFunc rangefunc)
{
IntPropertyRNA *iprop = (IntPropertyRNA *)prop;
if (getfunc) iprop->getarray_ex = getfunc;
if (setfunc) iprop->setarray_ex = setfunc;
if (getfunc || setfunc) {
/* don't save in id properties */
prop->flag &= ~PROP_IDPROPERTY;
if (!setfunc)
prop->flag &= ~PROP_EDITABLE;
}
}
void RNA_def_property_float_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
{
StructRNA *srna = DefRNA.laststruct;
@ -2081,6 +2145,40 @@ void RNA_def_property_float_funcs(PropertyRNA *prop, const char *get, const char
}
}
void RNA_def_property_float_funcs_runtime(PropertyRNA *prop, FloatPropertyGetFunc getfunc, FloatPropertySetFunc setfunc, FloatPropertyRangeFunc rangefunc)
{
FloatPropertyRNA *fprop = (FloatPropertyRNA *)prop;
if (getfunc) fprop->get_ex = getfunc;
if (setfunc) fprop->set_ex = setfunc;
if (rangefunc) fprop->range_ex = rangefunc;
if (getfunc || setfunc) {
/* don't save in id properties */
prop->flag &= ~PROP_IDPROPERTY;
if (!setfunc)
prop->flag &= ~PROP_EDITABLE;
}
}
void RNA_def_property_float_array_funcs_runtime(PropertyRNA *prop, FloatArrayPropertyGetFunc getfunc, FloatArrayPropertySetFunc setfunc, FloatPropertyRangeFunc rangefunc)
{
FloatPropertyRNA *fprop = (FloatPropertyRNA *)prop;
if (getfunc) fprop->getarray_ex = getfunc;
if (setfunc) fprop->setarray_ex = setfunc;
if (rangefunc) fprop->range_ex = rangefunc;
if (getfunc || setfunc) {
/* don't save in id properties */
prop->flag &= ~PROP_IDPROPERTY;
if (!setfunc)
prop->flag &= ~PROP_EDITABLE;
}
}
void RNA_def_property_enum_funcs(PropertyRNA *prop, const char *get, const char *set, const char *item)
{
StructRNA *srna = DefRNA.laststruct;
@ -2107,6 +2205,29 @@ void RNA_def_property_enum_funcs(PropertyRNA *prop, const char *get, const char
}
}
void RNA_def_property_enum_funcs_runtime(PropertyRNA *prop, EnumPropertyGetFunc getfunc, EnumPropertySetFunc setfunc, EnumPropertyItemFunc itemfunc)
{
EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop;
if (getfunc) eprop->get_ex = getfunc;
if (setfunc) eprop->set_ex = setfunc;
if (itemfunc) eprop->itemf = itemfunc;
if (getfunc || setfunc) {
/* don't save in id properties */
prop->flag &= ~PROP_IDPROPERTY;
if (!setfunc)
prop->flag &= ~PROP_EDITABLE;
}
}
void RNA_def_property_enum_py_data(PropertyRNA *prop, void *py_data)
{
EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop;
eprop->py_data = py_data;
}
void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set)
{
StructRNA *srna = DefRNA.laststruct;
@ -2133,6 +2254,23 @@ void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const cha
}
}
void RNA_def_property_string_funcs_runtime(PropertyRNA *prop, StringPropertyGetFunc getfunc, StringPropertyLengthFunc lengthfunc, StringPropertySetFunc setfunc)
{
StringPropertyRNA *sprop = (StringPropertyRNA *)prop;
if (getfunc) sprop->get_ex = getfunc;
if (lengthfunc) sprop->length_ex = lengthfunc;
if (setfunc) sprop->set_ex = setfunc;
if (getfunc || setfunc) {
/* don't save in id properties */
prop->flag &= ~PROP_IDPROPERTY;
if (!setfunc)
prop->flag &= ~PROP_EDITABLE;
}
}
void RNA_def_property_pointer_funcs(PropertyRNA *prop, const char *get, const char *set,
const char *typef, const char *poll)
{
@ -2447,12 +2585,6 @@ void RNA_def_enum_funcs(PropertyRNA *prop, EnumPropertyItemFunc itemfunc)
eprop->itemf = itemfunc;
}
void RNA_def_enum_py_data(PropertyRNA *prop, void *py_data)
{
EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop;
eprop->py_data = py_data;
}
PropertyRNA *RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, float default_value,
float hardmin, float hardmax, const char *ui_name, const char *ui_description,
float softmin, float softmax)

View File

@ -30,6 +30,8 @@
#include "DNA_listBase.h"
#include "RNA_types.h"
struct BlenderRNA;
struct ContainerRNA;
struct StructRNA;
@ -103,6 +105,27 @@ typedef int (*PropCollectionLookupIntFunc)(struct PointerRNA *ptr, int key, stru
typedef int (*PropCollectionLookupStringFunc)(struct PointerRNA *ptr, const char *key, struct PointerRNA *r_ptr);
typedef int (*PropCollectionAssignIntFunc)(struct PointerRNA *ptr, int key, const struct PointerRNA *assign_ptr);
/* extended versions with PropertyRNA argument */
typedef int (*PropBooleanGetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop);
typedef void (*PropBooleanSetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop, int value);
typedef void (*PropBooleanArrayGetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop, int *values);
typedef void (*PropBooleanArraySetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop, const int *values);
typedef int (*PropIntGetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop);
typedef void (*PropIntSetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop, int value);
typedef void (*PropIntArrayGetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop, int *values);
typedef void (*PropIntArraySetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop, const int *values);
typedef void (*PropIntRangeFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop, int *min, int *max, int *softmin, int *softmax);
typedef float (*PropFloatGetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop);
typedef void (*PropFloatSetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop, float value);
typedef void (*PropFloatArrayGetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop, float *values);
typedef void (*PropFloatArraySetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop, const float *values);
typedef void (*PropFloatRangeFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop, float *min, float *max, float *softmin, float *softmax);
typedef void (*PropStringGetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop, char *value);
typedef int (*PropStringLengthFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop);
typedef void (*PropStringSetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop, const char *value);
typedef int (*PropEnumGetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop);
typedef void (*PropEnumSetFuncEx)(struct PointerRNA *ptr, struct PropertyRNA *prop, int value);
/* Container - generic abstracted container of RNA properties */
typedef struct ContainerRNA {
void *next, *prev;
@ -193,10 +216,14 @@ typedef struct BoolPropertyRNA {
PropBooleanGetFunc get;
PropBooleanSetFunc set;
PropBooleanArrayGetFunc getarray;
PropBooleanArraySetFunc setarray;
PropBooleanGetFuncEx get_ex;
PropBooleanSetFuncEx set_ex;
PropBooleanArrayGetFuncEx getarray_ex;
PropBooleanArraySetFuncEx setarray_ex;
int defaultvalue;
const int *defaultarray;
} BoolPropertyRNA;
@ -206,12 +233,16 @@ typedef struct IntPropertyRNA {
PropIntGetFunc get;
PropIntSetFunc set;
PropIntArrayGetFunc getarray;
PropIntArraySetFunc setarray;
PropIntRangeFunc range;
PropIntGetFuncEx get_ex;
PropIntSetFuncEx set_ex;
PropIntArrayGetFuncEx getarray_ex;
PropIntArraySetFuncEx setarray_ex;
PropIntRangeFuncEx range_ex;
int softmin, softmax;
int hardmin, hardmax;
int step;
@ -225,12 +256,16 @@ typedef struct FloatPropertyRNA {
PropFloatGetFunc get;
PropFloatSetFunc set;
PropFloatArrayGetFunc getarray;
PropFloatArraySetFunc setarray;
PropFloatRangeFunc range;
PropFloatGetFuncEx get_ex;
PropFloatSetFuncEx set_ex;
PropFloatArrayGetFuncEx getarray_ex;
PropFloatArraySetFuncEx setarray_ex;
PropFloatRangeFuncEx range_ex;
float softmin, softmax;
float hardmin, hardmax;
float step;
@ -247,6 +282,10 @@ typedef struct StringPropertyRNA {
PropStringLengthFunc length;
PropStringSetFunc set;
PropStringGetFuncEx get_ex;
PropStringLengthFuncEx length_ex;
PropStringSetFuncEx set_ex;
int maxlength; /* includes string terminator! */
const char *defaultvalue;
@ -258,6 +297,9 @@ typedef struct EnumPropertyRNA {
PropEnumGetFunc get;
PropEnumSetFunc set;
PropEnumItemFunc itemf;
PropEnumGetFuncEx get_ex;
PropEnumSetFuncEx set_ex;
void *py_data; /* store py callback here */
EnumPropertyItem *item;

View File

@ -112,6 +112,54 @@ int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length,
return 0;
}
/* array utility function */
PyObject *PyC_FromArray(const void *array, int length, const PyTypeObject *type,
const short is_double, const char *error_prefix)
{
PyObject *tuple;
int i;
tuple = PyTuple_New(length);
/* for each type */
if (type == &PyFloat_Type) {
if (is_double) {
const double *array_double = array;
for (i = 0; i < length; ++i) {
PyTuple_SET_ITEM(tuple, i, PyFloat_FromDouble(array_double[i]));
}
}
else {
const float *array_float = array;
for (i = 0; i < length; ++i) {
PyTuple_SET_ITEM(tuple, i, PyFloat_FromDouble(array_float[i]));
}
}
}
else if (type == &PyLong_Type) {
/* could use is_double for 'long int' but no use now */
const int *array_int = array;
for (i = 0; i < length; ++i) {
PyTuple_SET_ITEM(tuple, i, PyLong_FromLong(array_int[i]));
}
}
else if (type == &PyBool_Type) {
const int *array_bool = array;
for (i = 0; i < length; ++i) {
PyTuple_SET_ITEM(tuple, i, PyBool_FromLong(array_bool[i]));
}
}
else {
Py_DECREF(tuple);
PyErr_Format(PyExc_TypeError,
"%s: internal error %s is invalid",
error_prefix, type->tp_name);
return NULL;
}
return tuple;
}
/* for debugging */
void PyC_ObSpit(const char *name, PyObject *var)

View File

@ -39,6 +39,8 @@ void PyC_FileAndNum(const char **filename, int *lineno);
void PyC_FileAndNum_Safe(const char **filename, int *lineno); /* checks python is running */
int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length,
const PyTypeObject *type, const short is_double, const char *error_prefix);
PyObject * PyC_FromArray(const void *array, int length, const PyTypeObject *type,
const short is_double, const char *error_prefix);
/* follow http://www.python.org/dev/peps/pep-0383/ */
PyObject * PyC_UnicodeFromByte(const char *str);

File diff suppressed because it is too large Load Diff