From 35c2267aee84d086c00574496b52e9c0641e320d Mon Sep 17 00:00:00 2001 From: Lukas Toenne Date: Thu, 20 Dec 2012 09:33:12 +0000 Subject: Support for actual class methods in the RNA/bpy. Previously all functions with FUNC_NO_SELF were treated as static methods, which is not sufficient for getting actual type information if the function can not be generated in advance in makesrna. Now the FUNC_USE_SELF_TYPE flag can be set in addition to FUNC_NO_SELF (if FUNC_NO_SELF is not set, FUNC_USE_SELF_TYPE has no effect). Such functions will be interpreted as class methods and must take a StructRNA pointer argument. This pointer is the same as the type member in PointerRNA, but can be passed without an actual data/id instance. --- source/blender/makesrna/RNA_types.h | 7 ++++--- source/blender/makesrna/intern/makesrna.c | 26 +++++++++++++++++++++++++- source/blender/makesrna/intern/rna_rna.c | 14 +++++++++++++- 3 files changed, 42 insertions(+), 5 deletions(-) (limited to 'source/blender/makesrna') diff --git a/source/blender/makesrna/RNA_types.h b/source/blender/makesrna/RNA_types.h index 87504dc6eb7..dacb2fee212 100644 --- a/source/blender/makesrna/RNA_types.h +++ b/source/blender/makesrna/RNA_types.h @@ -309,9 +309,10 @@ typedef struct ParameterDynAlloc { typedef enum FunctionFlag { FUNC_NO_SELF = 1, /* for static functions */ - FUNC_USE_MAIN = 2, - FUNC_USE_CONTEXT = 4, - FUNC_USE_REPORTS = 8, + FUNC_USE_SELF_TYPE = 2, /* for class methods, only used when FUNC_NO_SELF is set */ + FUNC_USE_MAIN = 4, + FUNC_USE_CONTEXT = 8, + FUNC_USE_REPORTS = 16, FUNC_USE_SELF_ID = 2048, FUNC_ALLOW_WRITE = 4096, diff --git a/source/blender/makesrna/intern/makesrna.c b/source/blender/makesrna/intern/makesrna.c index 9b7872f0b4b..2785a95ddb0 100644 --- a/source/blender/makesrna/intern/makesrna.c +++ b/source/blender/makesrna/intern/makesrna.c @@ -1989,6 +1989,10 @@ static void rna_def_struct_function_call_impl_cpp(FILE *f, StructRNA *srna, Func if (dsrna->dnaname) fprintf(f, "(::%s *) this->ptr.data", dsrna->dnaname); else fprintf(f, "(::%s *) this->ptr.data", srna->identifier); } + else if (func->flag & FUNC_USE_SELF_TYPE) { + WRITE_COMMA; + fprintf(f, "this->ptr.type"); + } if (func->flag & FUNC_USE_MAIN) WRITE_PARAM("(::Main *) main"); @@ -2112,8 +2116,12 @@ static void rna_def_function_wrapper_funcs(FILE *f, StructDefRNA *dsrna, Functio if (func->flag & FUNC_USE_SELF_ID) WRITE_PARAM("_selfid"); - if ((func->flag & FUNC_NO_SELF) == 0) + if ((func->flag & FUNC_NO_SELF) == 0) { WRITE_PARAM("_self"); + } + else if (func->flag & FUNC_USE_SELF_TYPE) { + WRITE_PARAM("_type"); + } if (func->flag & FUNC_USE_MAIN) WRITE_PARAM("bmain"); @@ -2174,6 +2182,9 @@ static void rna_def_function_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA if (dsrna->dnaname) fprintf(f, "\tstruct %s *_self;\n", dsrna->dnaname); else fprintf(f, "\tstruct %s *_self;\n", srna->identifier); } + else if (func->flag & FUNC_USE_SELF_TYPE) { + fprintf(f, "\tstruct StructRNA *_type;\n"); + } dparm = dfunc->cont.properties.first; for (; dparm; dparm = dparm->next) { @@ -2223,6 +2234,9 @@ static void rna_def_function_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA if (dsrna->dnaname) fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", dsrna->dnaname); else fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", srna->identifier); } + else if (func->flag & FUNC_USE_SELF_TYPE) { + fprintf(f, "\t_type= _ptr->type;\n"); + } if (has_data) { fprintf(f, "\t_data= (char *)_parms->data;\n"); @@ -2300,6 +2314,11 @@ static void rna_def_function_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA fprintf(f, "_self"); first = 0; } + else if (func->flag & FUNC_USE_SELF_TYPE) { + if (!first) fprintf(f, ", "); + fprintf(f, "_type"); + first = 0; + } if (func->flag & FUNC_USE_MAIN) { if (!first) fprintf(f, ", "); @@ -2613,6 +2632,11 @@ static void rna_generate_static_parameter_prototypes(FILE *f, StructRNA *srna, F else fprintf(f, "struct %s *_self", srna->identifier); first = 0; } + else if (func->flag & FUNC_USE_SELF_TYPE) { + if (!first) fprintf(f, ", "); + fprintf(f, "struct StructRNA *_type"); + first = 0; + } if (func->flag & FUNC_USE_MAIN) { if (!first) fprintf(f, ", "); diff --git a/source/blender/makesrna/intern/rna_rna.c b/source/blender/makesrna/intern/rna_rna.c index 7355e464fc3..3f03445635f 100644 --- a/source/blender/makesrna/intern/rna_rna.c +++ b/source/blender/makesrna/intern/rna_rna.c @@ -939,6 +939,12 @@ static int rna_Function_no_self_get(PointerRNA *ptr) return !(func->flag & FUNC_NO_SELF); } +static int rna_Function_use_self_type_get(PointerRNA *ptr) +{ + FunctionRNA *func = (FunctionRNA *)ptr->data; + return (func->flag & FUNC_USE_SELF_TYPE); +} + /* Blender RNA */ static void rna_BlenderRNA_structs_begin(CollectionPropertyIterator *iter, PointerRNA *ptr) @@ -1230,7 +1236,13 @@ static void rna_def_function(BlenderRNA *brna) RNA_def_property_clear_flag(prop, PROP_EDITABLE); RNA_def_property_boolean_funcs(prop, "rna_Function_no_self_get", NULL); RNA_def_property_ui_text(prop, "No Self", - "Function does not pass its self as an argument (becomes a class method in python)"); + "Function does not pass its self as an argument (becomes a static method in python)"); + + prop = RNA_def_property(srna, "use_self_type", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_clear_flag(prop, PROP_EDITABLE); + RNA_def_property_boolean_funcs(prop, "rna_Function_use_self_type_get", NULL); + RNA_def_property_ui_text(prop, "Use Self Type", + "Function passes its self type as an argument (becomes a class method in python if use_self is false)"); } static void rna_def_number_property(StructRNA *srna, PropertyType type) -- cgit v1.2.3