RNA
authorBrecht Van Lommel <brechtvanlommel@pandora.be>
Wed, 24 Jun 2009 14:03:55 +0000 (14:03 +0000)
committerBrecht Van Lommel <brechtvanlommel@pandora.be>
Wed, 24 Jun 2009 14:03:55 +0000 (14:03 +0000)
* 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
source/blender/blenkernel/intern/context.c
source/blender/editors/interface/interface.c
source/blender/editors/space_buttons/buttons_context.c
source/blender/editors/space_outliner/outliner.c
source/blender/makesrna/RNA_access.h
source/blender/makesrna/intern/rna_access.c
source/blender/python/intern/bpy_rna.c

index 898b84ecdc3cab9560e8631f8eeabd1e9000a503..f536e117b7b1ad3dbd926f216f2bc153c61eb67d 100644 (file)
@@ -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);
index 12deec838a8bd9ce6bf757a7e2c0d164592df2a5..90880e354ec0cffd49139bded76e5bc899f4f71b 100644 (file)
@@ -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)
index 1c17962075fef21e3ca41a612368a54a35c12631..892a126feb66615f554e13fbdb6b87606a08296d 100644 (file)
@@ -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);
                }
        }
 
index d97b4acdb96540797e0a1beb6e74e3c790de92f0..cba9a4cf8fc72606f297dbe89c1d8d94b6ddfd92 100644 (file)
@@ -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)
index 16748af39d5967ca043a4f9798226bff5bc19fff..8017b8437ff729af75520624cc143da9790105cc 100644 (file)
@@ -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)
index 4a074ab1546ff5dad7e4d5004eb9fb9cb04d0ac3..bab6982abd96687fcacf4c5ad89a7f990146e061 100644 (file)
@@ -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
index 60774c8432c1f0384b420bcdd7023c090ebbf75f..a2954d43e0effd5d569c4c0680d47de94d78991e 100644 (file)
@@ -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);
        
index e91b96d6f26c92075aed55a86b6c7b0e1014f724..1fd60a8ff2796cc60b5942e3421e4989861cbb2d 100644 (file)
@@ -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);