collection attributes can now be resolved with by rna
authorCampbell Barton <ideasman42@gmail.com>
Mon, 23 Aug 2010 05:36:21 +0000 (05:36 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 23 Aug 2010 05:36:21 +0000 (05:36 +0000)
this now works...
  bpy.context.scene.path_resolve("objects.active.location")

Also added an option to coerce the property into a native pytype.

source/blender/editors/interface/interface.c
source/blender/editors/object/object_add.c
source/blender/makesrna/intern/rna_access.c
source/blender/python/intern/bpy_rna.c

index 4c86b94..d1c65fb 100644 (file)
@@ -2528,8 +2528,10 @@ uiBut *ui_def_but_rna(uiBlock *block, int type, int retval, char *str, short x1,
                        }
                }
        }
-       else
+       else {
+               printf("ui_def_but_rna: property not found: %s.%s\n", RNA_struct_identifier(ptr->type), propname);
                str= (char*)propname;
+       }
 
        /* now create button */
        but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
index d3debc8..a847296 100644 (file)
@@ -501,7 +501,6 @@ static EnumPropertyItem prop_metaball_types[]= {
 static int object_metaball_add_exec(bContext *C, wmOperator *op)
 {
        Object *obedit= CTX_data_edit_object(C);
-       MetaBall *mball;
        MetaElem *elem;
        int newob= 0;
        int enter_editmode;
index 73af75c..cdb032b 100644 (file)
@@ -2949,7 +2949,7 @@ int RNA_path_resolve_full(PointerRNA *ptr, const char *path, PointerRNA *r_ptr,
        PropertyRNA *prop;
        PointerRNA curptr, nextptr;
        char fixedbuf[256], *token;
-       int type, len, intkey;
+       int type, intkey;
 
        prop= NULL;
        curptr= *ptr;
@@ -3001,33 +3001,44 @@ int RNA_path_resolve_full(PointerRNA *ptr, const char *path, PointerRNA *r_ptr,
                        break;
                case PROP_COLLECTION:
                        if(*path) {
-                               /* resolve the lookup with [] brackets */
-                               token= rna_path_token(&path, fixedbuf, sizeof(fixedbuf), 1);
-
-                               if(!token)
-                                       return 0;
-
-                               len= strlen(token);
+                               if(*path == '[') {
+                                       /* resolve the lookup with [] brackets */
+                                       token= rna_path_token(&path, fixedbuf, sizeof(fixedbuf), 1);
+       
+                                       if(!token)
+                                               return 0;
+       
+                                       /* check for "" to see if it is a string */
+                                       if(rna_token_strip_quotes(token)) {
+                                               RNA_property_collection_lookup_string(&curptr, prop, token+1, &nextptr);
+                                       }
+                                       else {
+                                               /* otherwise do int lookup */
+                                               intkey= atoi(token);
+                                               RNA_property_collection_lookup_int(&curptr, prop, intkey, &nextptr);
+                                       }
 
-                               /* check for "" to see if it is a string */
-                               if(rna_token_strip_quotes(token)) {
-                                       RNA_property_collection_lookup_string(&curptr, prop, token+1, &nextptr);
+                                       if(token != fixedbuf) {
+                                               MEM_freeN(token);
+                                       }
                                }
                                else {
-                                       /* otherwise do int lookup */
-                                       intkey= atoi(token);
-                                       RNA_property_collection_lookup_int(&curptr, prop, intkey, &nextptr);
-                               }
+                                       PointerRNA c_ptr;
+                                       
+                                       /* ensure we quit on invalid values */
+                                       nextptr.data = NULL;
 
-                               if(token != fixedbuf)
-                                       MEM_freeN(token);
+                                       if(RNA_property_collection_type_get(&curptr, prop, &c_ptr)) {
+                                               nextptr= c_ptr;
+                                       }
+                               }
 
                                if(nextptr.data)
                                        curptr= nextptr;
                                else
                                        return 0;
                        }
-
+                       
                        break;
                default:
                        if (index==NULL)
index 07b237a..834254a 100644 (file)
@@ -2142,25 +2142,37 @@ static PyObject *pyrna_struct_is_property_hidden(BPy_StructRNA *self, PyObject *
 }
 
 static char pyrna_struct_path_resolve_doc[] =
-".. method:: path_resolve(path)\n"
+".. method:: path_resolve(path, coerce=True)\n"
 "\n"
-"   Returns the property from the path given or None if the property is not found.";
+"   Returns the property from the path, raise an exception when not found.\n"
+"\n"
+"   :arg path: path which this property resolves.\n"
+"   :type path: string\n"
+"   :arg coerce: optional argument, when True, the property will be converted into its python representation.\n"
+"   :type coerce: boolean\n";
 
-static PyObject *pyrna_struct_path_resolve(BPy_StructRNA *self, PyObject *value)
+static PyObject *pyrna_struct_path_resolve(BPy_StructRNA *self, PyObject *args)
 {
-       char *path= _PyUnicode_AsString(value);
+       char *path;
+       PyObject *coerce= Py_True;
        PointerRNA r_ptr;
        PropertyRNA *r_prop;
 
-       if(path==NULL) {
-               PyErr_SetString(PyExc_TypeError, "bpy_struct.path_resolve(): accepts only a single string argument");
+       if (!PyArg_ParseTuple(args, "s|O!:path_resolve", &path, &PyBool_Type, &coerce))
                return NULL;
-       }
-
-       if (RNA_path_resolve(&self->ptr, path, &r_ptr, &r_prop))
-               return pyrna_prop_CreatePyObject(&r_ptr, r_prop);
 
-       Py_RETURN_NONE;
+       if (RNA_path_resolve(&self->ptr, path, &r_ptr, &r_prop)) {
+               if(coerce == Py_False) {
+                       return pyrna_prop_CreatePyObject(&r_ptr, r_prop);
+               }
+               else {
+                       return pyrna_prop_to_py(&r_ptr, r_prop);
+               }
+       }
+       else {
+               PyErr_Format(PyExc_TypeError, "%.200s.path_resolve(\"%.200s\") could not be resolved", RNA_struct_identifier(self->ptr.type), path);
+               return NULL;
+       }
 }
 
 static char pyrna_struct_path_from_id_doc[] =
@@ -3112,7 +3124,7 @@ static struct PyMethodDef pyrna_struct_methods[] = {
        {"driver_remove", (PyCFunction)pyrna_struct_driver_remove, METH_VARARGS, pyrna_struct_driver_remove_doc},
        {"is_property_set", (PyCFunction)pyrna_struct_is_property_set, METH_VARARGS, pyrna_struct_is_property_set_doc},
        {"is_property_hidden", (PyCFunction)pyrna_struct_is_property_hidden, METH_VARARGS, pyrna_struct_is_property_hidden_doc},
-       {"path_resolve", (PyCFunction)pyrna_struct_path_resolve, METH_O, pyrna_struct_path_resolve_doc},
+       {"path_resolve", (PyCFunction)pyrna_struct_path_resolve, METH_VARARGS, pyrna_struct_path_resolve_doc},
        {"path_from_id", (PyCFunction)pyrna_struct_path_from_id, METH_VARARGS, pyrna_struct_path_from_id_doc},
        {"recast_type", (PyCFunction)pyrna_struct_recast_type, METH_NOARGS, pyrna_struct_recast_type_doc},
        {"__dir__", (PyCFunction)pyrna_struct_dir, METH_NOARGS, NULL},