From 242d9c31d3316f99525925eb7da30522457064e8 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Wed, 24 Jun 2009 14:03:55 +0000 Subject: RNA * RNA_struct_name_get_alloc function to get the name from a pointer, instead of having to deal with name property. * CTX_data_pointer_get_type to get data from context with a check for the RNA type. --- source/blender/blenkernel/BKE_context.h | 1 + source/blender/blenkernel/intern/context.c | 19 +++++--- source/blender/editors/interface/interface.c | 28 +++++------- .../editors/space_buttons/buttons_context.c | 7 +-- source/blender/editors/space_outliner/outliner.c | 19 ++++---- source/blender/makesrna/RNA_access.h | 2 + source/blender/makesrna/intern/rna_access.c | 11 ++++- source/blender/python/intern/bpy_rna.c | 53 ++++++++++------------ 8 files changed, 71 insertions(+), 69 deletions(-) (limited to 'source') diff --git a/source/blender/blenkernel/BKE_context.h b/source/blender/blenkernel/BKE_context.h index 898b84ecdc3..f536e117b7b 100644 --- a/source/blender/blenkernel/BKE_context.h +++ b/source/blender/blenkernel/BKE_context.h @@ -131,6 +131,7 @@ void CTX_wm_menu_set(bContext *C, struct ARegion *menu); - the dir listbase consits of LinkData items */ PointerRNA CTX_data_pointer_get(const bContext *C, const char *member); +PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type); ListBase CTX_data_collection_get(const bContext *C, const char *member); ListBase CTX_data_dir_get(const bContext *C); void CTX_data_get(const bContext *C, const char *member, PointerRNA *r_ptr, ListBase *r_lb); diff --git a/source/blender/blenkernel/intern/context.c b/source/blender/blenkernel/intern/context.c index 12deec838a8..90880e354ec 100644 --- a/source/blender/blenkernel/intern/context.c +++ b/source/blender/blenkernel/intern/context.c @@ -365,15 +365,20 @@ PointerRNA CTX_data_pointer_get(const bContext *C, const char *member) { bContextDataResult result; - if(ctx_data_get((bContext*)C, member, &result)) { + if(ctx_data_get((bContext*)C, member, &result)) return result.ptr; - } - else { - PointerRNA ptr; - memset(&ptr, 0, sizeof(ptr)); - return ptr; - } + else + return PointerRNA_NULL; +} + +PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type) +{ + PointerRNA ptr = CTX_data_pointer_get(C, member); + if(ptr.data && ptr.type == type) + return ptr; + + return PointerRNA_NULL; } ListBase CTX_data_collection_get(const bContext *C, const char *member) diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index 1c17962075f..892a126feb6 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -1282,17 +1282,13 @@ void ui_get_but_string(uiBut *but, char *str, int maxlen) else if(type == PROP_POINTER) { /* RNA pointer */ PointerRNA ptr= RNA_property_pointer_get(&but->rnapoin, but->rnaprop); - PropertyRNA *nameprop; - - if(ptr.data && (nameprop = RNA_struct_name_property(ptr.type))) - buf= RNA_property_string_get_alloc(&ptr, nameprop, str, maxlen); - else - BLI_strncpy(str, "", maxlen); + buf= RNA_struct_name_get_alloc(&ptr, str, maxlen); } - else - BLI_strncpy(str, "", maxlen); - if(buf && buf != str) { + if(!buf) { + BLI_strncpy(str, "", maxlen); + } + else if(buf && buf != str) { /* string was too long, we have to truncate */ BLI_strncpy(str, buf, maxlen); MEM_freeN(buf); @@ -1375,7 +1371,7 @@ static void ui_rna_ID_autocomplete(bContext *C, char *str, void *arg_but) AutoComplete *autocpl; CollectionPropertyIterator iter; PointerRNA ptr; - PropertyRNA *prop, *nameprop; + PropertyRNA *prop; char *name; if(str[0]==0) return; @@ -1389,14 +1385,12 @@ static void ui_rna_ID_autocomplete(bContext *C, char *str, void *arg_but) /* loop over items in collection */ for(; iter.valid; RNA_property_collection_next(&iter)) { - if(iter.ptr.data && (nameprop = RNA_struct_name_property(iter.ptr.type))) { - name= RNA_property_string_get_alloc(&iter.ptr, nameprop, NULL, 0); + name= RNA_struct_name_get_alloc(&iter.ptr, NULL, 0); - if(name) { - /* test item name */ - autocomplete_do_name(autocpl, name); - MEM_freeN(name); - } + /* test item name */ + if(name) { + autocomplete_do_name(autocpl, name); + MEM_freeN(name); } } diff --git a/source/blender/editors/space_buttons/buttons_context.c b/source/blender/editors/space_buttons/buttons_context.c index d97b4acdb96..cba9a4cf8fc 100644 --- a/source/blender/editors/space_buttons/buttons_context.c +++ b/source/blender/editors/space_buttons/buttons_context.c @@ -668,7 +668,6 @@ void buttons_context_draw(const bContext *C, uiLayout *layout) uiBlock *block; uiBut *but; PointerRNA *ptr; - PropertyRNA *nameprop; char namebuf[128], *name; int a, icon; @@ -688,7 +687,7 @@ void buttons_context_draw(const bContext *C, uiLayout *layout) if(ptr->data) { icon= RNA_struct_ui_icon(ptr->type); - nameprop= RNA_struct_name_property(ptr->type); + name= RNA_struct_name_get_alloc(ptr, namebuf, sizeof(namebuf)); #if 0 if(sbuts->mainb != BCONTEXT_SCENE && ptr->type == &RNA_Scene) { @@ -696,9 +695,7 @@ void buttons_context_draw(const bContext *C, uiLayout *layout) } else #endif - if(nameprop) { - name= RNA_property_string_get_alloc(ptr, nameprop, namebuf, sizeof(namebuf)); - + if(name) { uiItemL(row, name, icon); if(name != namebuf) diff --git a/source/blender/editors/space_outliner/outliner.c b/source/blender/editors/space_outliner/outliner.c index 16748af39d5..8017b8437ff 100644 --- a/source/blender/editors/space_outliner/outliner.c +++ b/source/blender/editors/space_outliner/outliner.c @@ -1031,7 +1031,7 @@ static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *i } else if(ELEM3(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) { PointerRNA pptr, propptr, *ptr= (PointerRNA*)idv; - PropertyRNA *prop, *iterprop, *nameprop; + PropertyRNA *prop, *iterprop; PropertyType proptype; PropertySubType propsubtype; int a, tot; @@ -1043,12 +1043,10 @@ static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *i } else if(type == TSE_RNA_STRUCT) { /* struct */ - nameprop= RNA_struct_name_property(ptr->type); + te->name= RNA_struct_name_get_alloc(ptr, NULL, 0); - if(nameprop) { - te->name= RNA_property_string_get_alloc(ptr, nameprop, NULL, 0); + if(te->name) te->flag |= TE_FREE_NAME; - } else te->name= (char*)RNA_struct_ui_name(ptr->type); @@ -3075,7 +3073,7 @@ static void tree_element_to_path(SpaceOops *soops, TreeElement *te, TreeStoreEle TreeElement *tem, *temnext, *temsub; TreeStoreElem *tse, *tsenext; PointerRNA *ptr, *nextptr; - PropertyRNA *prop, *nameprop; + PropertyRNA *prop; char *newpath=NULL; /* optimise tricks: @@ -3119,17 +3117,16 @@ static void tree_element_to_path(SpaceOops *soops, TreeElement *te, TreeStoreEle newpath= RNA_path_append(*path, ptr, prop, 0, NULL); } else if(RNA_property_type(prop) == PROP_COLLECTION) { + char buf[128], *name; + temnext= (TreeElement*)(ld->next->data); tsenext= TREESTORE(temnext); nextptr= &temnext->rnaptr; - nameprop= RNA_struct_name_property(nextptr->type); + name= RNA_struct_name_get_alloc(nextptr, buf, sizeof(buf)); - if(nameprop) { + if(name) { /* if possible, use name as a key in the path */ - char buf[128], *name; - name= RNA_property_string_get_alloc(nextptr, nameprop, buf, sizeof(buf)); - newpath= RNA_path_append(*path, NULL, prop, 0, name); if(name != buf) diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index 4a074ab1546..bab6982abd9 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -514,6 +514,8 @@ const struct ListBase *RNA_struct_defined_properties(StructRNA *srna); FunctionRNA *RNA_struct_find_function(PointerRNA *ptr, const char *identifier); const struct ListBase *RNA_struct_defined_functions(StructRNA *srna); +char *RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen); + /* Properties * * Access to struct properties. All this works with RNA pointers rather than diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c index 60774c8432c..a2954d43e0e 100644 --- a/source/blender/makesrna/intern/rna_access.c +++ b/source/blender/makesrna/intern/rna_access.c @@ -518,6 +518,16 @@ void RNA_struct_blender_type_set(StructRNA *srna, void *blender_type) srna->blender_type= blender_type; } +char *RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen) +{ + PropertyRNA *nameprop; + + if(ptr->data && (nameprop = RNA_struct_name_property(ptr->type))) + return RNA_property_string_get_alloc(ptr, nameprop, fixedbuf, fixedlen); + + return NULL; +} + /* Property Information */ const char *RNA_property_identifier(PropertyRNA *prop) @@ -658,7 +668,6 @@ void RNA_property_enum_items(PointerRNA *ptr, PropertyRNA *prop, const EnumPrope int RNA_property_enum_value(PointerRNA *ptr, PropertyRNA *prop, const char *identifier, int *value) { const EnumPropertyItem *item; - int i; RNA_property_enum_items(ptr, prop, &item, NULL); diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index e91b96d6f26..1fd60a8ff27 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -161,14 +161,15 @@ static PyObject *pyrna_prop_richcmp(BPy_PropertyRNA * a, BPy_PropertyRNA * b, in /*----------------------repr--------------------------------------------*/ static PyObject *pyrna_struct_repr( BPy_StructRNA * self ) { - PropertyRNA *prop; - char str[512]; + PyObject *pyob; + char *name; /* print name if available */ - prop= RNA_struct_name_property(self->ptr.type); - if(prop) { - RNA_property_string_get(&self->ptr, prop, str); - return PyUnicode_FromFormat( "[BPy_StructRNA \"%s\" -> \"%s\"]", RNA_struct_identifier(self->ptr.type), str); + name= RNA_struct_name_get_alloc(&self->ptr, NULL, 0); + if(name) { + pyob= PyUnicode_FromFormat( "[BPy_StructRNA \"%s\" -> \"%s\"]", RNA_struct_identifier(self->ptr.type), name); + MEM_freeN(name); + return pyob; } return PyUnicode_FromFormat( "[BPy_StructRNA \"%s\"]", RNA_struct_identifier(self->ptr.type)); @@ -176,20 +177,19 @@ static PyObject *pyrna_struct_repr( BPy_StructRNA * self ) static PyObject *pyrna_prop_repr( BPy_PropertyRNA * self ) { - PropertyRNA *prop; + PyObject *pyob; PointerRNA ptr; - char str[512]; + char *name; /* if a pointer, try to print name of pointer target too */ if(RNA_property_type(self->prop) == PROP_POINTER) { ptr= RNA_property_pointer_get(&self->ptr, self->prop); + name= RNA_struct_name_get_alloc(&ptr, NULL, 0); - if(ptr.data) { - prop= RNA_struct_name_property(ptr.type); - if(prop) { - RNA_property_string_get(&ptr, prop, str); - return PyUnicode_FromFormat( "[BPy_PropertyRNA \"%s\" -> \"%s\" -> \"%s\" ]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop), str); - } + if(name) { + pyob= PyUnicode_FromFormat( "[BPy_PropertyRNA \"%s\" -> \"%s\" -> \"%s\" ]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop), name); + MEM_freeN(name); + return pyob; } } @@ -218,7 +218,6 @@ static void pyrna_struct_dealloc( BPy_StructRNA * self ) static char *pyrna_enum_as_string(PointerRNA *ptr, PropertyRNA *prop) { const EnumPropertyItem *item; - int totitem; RNA_property_enum_items(ptr, prop, &item, NULL); return (char*)BPy_enum_as_string((EnumPropertyItem*)item); @@ -971,21 +970,20 @@ static PyObject *pyrna_struct_dir(BPy_StructRNA * self) /* * Collect RNA attributes */ - PropertyRNA *nameprop; char name[256], *nameptr; iterprop= RNA_struct_iterator_property(self->ptr.type); RNA_property_collection_begin(&self->ptr, iterprop, &iter); for(; iter.valid; RNA_property_collection_next(&iter)) { - if(iter.ptr.data && (nameprop = RNA_struct_name_property(iter.ptr.type))) { - nameptr= RNA_property_string_get_alloc(&iter.ptr, nameprop, name, sizeof(name)); - + nameptr= RNA_struct_name_get_alloc(&iter.ptr, name, sizeof(name)); + + if(nameptr) { pystring = PyUnicode_FromString(nameptr); PyList_Append(ret, pystring); Py_DECREF(pystring); - if ((char *)&name != nameptr) + if(name != nameptr) MEM_freeN(nameptr); } } @@ -1132,7 +1130,9 @@ PyObject *pyrna_prop_keys(BPy_PropertyRNA *self) RNA_property_collection_begin(&self->ptr, self->prop, &iter); for(; iter.valid; RNA_property_collection_next(&iter)) { - if(iter.ptr.data && (nameprop = RNA_struct_name_property(iter.ptr.type))) { + nameptr= RNA_struct_name_get_alloc(&iter.ptr, name, sizeof(name)); + + if(nameptr) { nameptr= RNA_property_string_get_alloc(&iter.ptr, nameprop, name, sizeof(name)); /* add to python list */ @@ -1141,7 +1141,7 @@ PyObject *pyrna_prop_keys(BPy_PropertyRNA *self) Py_DECREF(item); /* done */ - if ((char *)&name != nameptr) + if(name != nameptr) MEM_freeN(nameptr); } } @@ -1160,7 +1160,6 @@ PyObject *pyrna_prop_items(BPy_PropertyRNA *self) } else { PyObject *item; CollectionPropertyIterator iter; - PropertyRNA *nameprop; char name[256], *nameptr; int i= 0; @@ -1171,10 +1170,10 @@ PyObject *pyrna_prop_items(BPy_PropertyRNA *self) if(iter.ptr.data) { /* add to python list */ item= PyTuple_New(2); - if(nameprop = RNA_struct_name_property(iter.ptr.type)) { - nameptr= RNA_property_string_get_alloc(&iter.ptr, nameprop, name, sizeof(name)); + nameptr= RNA_struct_name_get_alloc(&iter.ptr, name, sizeof(name)); + if(nameptr) { PyTuple_SET_ITEM(item, 0, PyUnicode_FromString( nameptr )); - if ((char *)&name != nameptr) + if(name != nameptr) MEM_freeN(nameptr); } else { @@ -1205,10 +1204,8 @@ PyObject *pyrna_prop_values(BPy_PropertyRNA *self) } else { PyObject *item; CollectionPropertyIterator iter; - PropertyRNA *iterprop; ret = PyList_New(0); - //iterprop= RNA_struct_iterator_property(self->ptr.type); RNA_property_collection_begin(&self->ptr, self->prop, &iter); for(; iter.valid; RNA_property_collection_next(&iter)) { item = pyrna_struct_CreatePyObject(&iter.ptr); -- cgit v1.2.3