[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24842] trunk/blender: - new pyrna api functions srna & prop path_to_id(), useful when setting driver target paths.

Campbell Barton ideasman42 at gmail.com
Tue Nov 24 00:17:23 CET 2009


Revision: 24842
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24842
Author:   campbellbarton
Date:     2009-11-24 00:17:23 +0100 (Tue, 24 Nov 2009)

Log Message:
-----------
- new pyrna api functions srna & prop path_to_id(), useful when setting driver target paths.
  This means you can have a pose bone for eg and get the path...
   pose.bones["Bone"]
 uses rna internal functions, so will work for sequence strips etc.

- StructRNA.get(), used for getting ID props without exceptions...
 val = C.object["someKey"]
 or..
 val = C.object.get("someKey", "defaultValue") # wont raise an error

- change rna property for testing if rna props are editable, test the flag rather then calling the function since the function depends on blenders state.

- fix a python exception with the ID-Property popup UI (when editing in more then 1 step)

Modified Paths:
--------------
    trunk/blender/release/scripts/modules/rna_prop_ui.py
    trunk/blender/source/blender/makesrna/intern/rna_fcurve.c
    trunk/blender/source/blender/makesrna/intern/rna_rna.c
    trunk/blender/source/blender/python/intern/bpy_rna.c

Modified: trunk/blender/release/scripts/modules/rna_prop_ui.py
===================================================================
--- trunk/blender/release/scripts/modules/rna_prop_ui.py	2009-11-23 23:03:04 UTC (rev 24841)
+++ trunk/blender/release/scripts/modules/rna_prop_ui.py	2009-11-23 23:17:23 UTC (rev 24842)
@@ -177,6 +177,7 @@
         exec_str = "item['%s'] = %s" % (prop, value_eval)
         # print(exec_str)
         exec(exec_str)
+        self._last_prop[:] = [prop]
 
         prop_type = type(item[prop])
 

Modified: trunk/blender/source/blender/makesrna/intern/rna_fcurve.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_fcurve.c	2009-11-23 23:03:04 UTC (rev 24841)
+++ trunk/blender/source/blender/makesrna/intern/rna_fcurve.c	2009-11-23 23:17:23 UTC (rev 24842)
@@ -649,7 +649,7 @@
 	RNA_def_property_flag(prop, PROP_EDITABLE);
 	RNA_def_property_editable_func(prop, "rna_DriverTarget_id_editable");
 	RNA_def_property_pointer_funcs(prop, NULL, NULL, "rna_DriverTarget_id_typef");
-	RNA_def_property_ui_text(prop, "ID", "ID-block that the specific property used can be found from");
+	RNA_def_property_ui_text(prop, "ID", "ID-block that the specific property used can be found from (id_type property must be set first)");
 	//RNA_def_property_update(prop, 0, "rna_ChannelDriver_update_data"); // XXX disabled for now, until we can turn off auto updates
 	
 	prop= RNA_def_property(srna, "id_type", PROP_ENUM, PROP_NONE);

Modified: trunk/blender/source/blender/makesrna/intern/rna_rna.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_rna.c	2009-11-23 23:03:04 UTC (rev 24841)
+++ trunk/blender/source/blender/makesrna/intern/rna_rna.c	2009-11-23 23:17:23 UTC (rev 24842)
@@ -428,7 +428,12 @@
 static int rna_Property_editable_get(PointerRNA *ptr)
 {
 	PropertyRNA *prop= (PropertyRNA*)ptr->data;
-	return RNA_property_editable(ptr, prop);
+
+	/* dont use this becaure it will call functions that check the internal
+	 * data for introspection we only need to know if it can be edited so the
+	 * flag is better for this */
+//	return RNA_property_editable(ptr, prop);
+	return prop->flag & PROP_EDITABLE ? 1:0;
 }
 
 static int rna_Property_use_return_get(PointerRNA *ptr)

Modified: trunk/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_rna.c	2009-11-23 23:03:04 UTC (rev 24841)
+++ trunk/blender/source/blender/python/intern/bpy_rna.c	2009-11-23 23:17:23 UTC (rev 24842)
@@ -1391,7 +1391,6 @@
 	return result;
 }
 
-
 static PyObject *pyrna_struct_is_property_set(BPy_StructRNA *self, PyObject *args)
 {
 	char *name;
@@ -1434,7 +1433,60 @@
 	Py_RETURN_NONE;
 }
 
+static PyObject *pyrna_struct_path_to_id(BPy_StructRNA *self, PyObject *args)
+{
+	char *name= NULL;
+	char *path;
+	PropertyRNA *prop;
+	PyObject *ret;
 
+	if (!PyArg_ParseTuple(args, "|s:path_to_id", &name))
+		return NULL;
+
+	if(name) {
+		prop= RNA_struct_find_property(&self->ptr, name);
+		if(prop==NULL) {
+			PyErr_Format(PyExc_TypeError, "path_to_id(\"%.200s\") not found", name);
+			return NULL;
+		}
+
+		path= RNA_path_from_ID_to_property(&self->ptr, prop);
+	}
+	else {
+		path= RNA_path_from_ID_to_struct(&self->ptr);
+	}
+
+	if(path==NULL) {
+		if(name)	PyErr_Format(PyExc_TypeError, "%.200s.path_to_id(\"%s\") found but does not support path creation", RNA_struct_identifier(self->ptr.type), name);
+		else		PyErr_Format(PyExc_TypeError, "%.200s.path_to_id() does not support path creation for this type", name);
+		return NULL;
+	}
+
+	ret= PyUnicode_FromString(path);
+	MEM_freeN(path);
+
+	return ret;
+}
+
+static PyObject *pyrna_prop_path_to_id(BPy_PropertyRNA *self)
+{
+	char *path;
+	PropertyRNA *prop;
+	PyObject *ret;
+
+	path= RNA_path_from_ID_to_property(&self->ptr, self->prop);
+
+	if(path==NULL) {
+		PyErr_Format(PyExc_TypeError, "%.200s.%.200s.path_to_id() does not support path creation for this type", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(prop));
+		return NULL;
+	}
+
+	ret= PyUnicode_FromString(path);
+	MEM_freeN(path);
+
+	return ret;
+}
+
 static void pyrna_dir_members_py(PyObject *list, PyObject *self)
 {
 	PyObject *dict;
@@ -1890,6 +1942,34 @@
 	return ret;
 }
 
+static PyObject *pyrna_struct_get(BPy_StructRNA *self, PyObject *args)
+{
+	IDProperty *group, *idprop;
+
+	char *key;
+	PyObject* def = Py_None;
+
+	if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
+		return NULL;
+
+	/* mostly copied from BPy_IDGroup_Map_GetItem */
+	if(RNA_struct_idproperties_check(&self->ptr)==0) {
+		PyErr_SetString( PyExc_TypeError, "this type doesn't support IDProperties");
+		return NULL;
+	}
+
+	group= RNA_struct_idproperties(&self->ptr, 0);
+	if(group) {
+		idprop= IDP_GetPropertyFromGroup(group, key);
+
+		if(idprop)
+			return BPy_IDGroup_WrapData(self->ptr.id.data, idprop);
+	}
+
+	Py_INCREF(def);
+	return def;
+}
+
 static PyObject *pyrna_prop_get(BPy_PropertyRNA *self, PyObject *args)
 {
 	PointerRNA newptr;
@@ -2182,12 +2262,15 @@
 	{"values", (PyCFunction)pyrna_struct_values, METH_NOARGS, NULL},
 	{"items", (PyCFunction)pyrna_struct_items, METH_NOARGS, NULL},
 
+	{"get", (PyCFunction)pyrna_struct_get, METH_VARARGS, NULL},
+
 	/* maybe this become and ID function */
 	{"keyframe_insert", (PyCFunction)pyrna_struct_keyframe_insert, METH_VARARGS, NULL},
 	{"driver_add", (PyCFunction)pyrna_struct_driver_add, METH_VARARGS, NULL},
 	{"is_property_set", (PyCFunction)pyrna_struct_is_property_set, METH_VARARGS, NULL},
 	{"is_property_hidden", (PyCFunction)pyrna_struct_is_property_hidden, METH_VARARGS, NULL},
 	{"path_resolve", (PyCFunction)pyrna_struct_path_resolve, METH_O, NULL},
+	{"path_to_id", (PyCFunction)pyrna_struct_path_to_id, METH_VARARGS, NULL},
 	{"__dir__", (PyCFunction)pyrna_struct_dir, METH_NOARGS, NULL},
 	{NULL, NULL, 0, NULL}
 };
@@ -2203,6 +2286,9 @@
 	{"add", (PyCFunction)pyrna_prop_add, METH_NOARGS, NULL},
 	{"remove", (PyCFunction)pyrna_prop_remove, METH_O, NULL},
 
+	/* almost the same as the srna function */
+	{"path_to_id", (PyCFunction)pyrna_prop_path_to_id, METH_NOARGS, NULL},
+
 	/* array accessor function */
 	{"foreach_get", (PyCFunction)pyrna_prop_foreach_get, METH_VARARGS, NULL},
 	{"foreach_set", (PyCFunction)pyrna_prop_foreach_set, METH_VARARGS, NULL},





More information about the Bf-blender-cvs mailing list