[Bf-blender-cvs] [ff014cd] tmp-id-users: Address suggestions and comments from review.

Bastien Montagne noreply at git.blender.org
Wed Jan 6 16:18:24 CET 2016


Commit: ff014cdeed53303af3de0684afbbeb9eaac738f9
Author: Bastien Montagne
Date:   Wed Jan 6 16:13:46 2016 +0100
Branches: tmp-id-users
https://developer.blender.org/rBff014cdeed53303af3de0684afbbeb9eaac738f9

Address suggestions and comments from review.

===================================================================

M	release/scripts/modules/bpy_types.py
M	source/blender/python/intern/CMakeLists.txt
M	source/blender/python/intern/bpy.c
D	source/blender/python/intern/bpy_data.c
D	source/blender/python/intern/bpy_data.h
A	source/blender/python/intern/bpy_rna_id_collection.c
A	source/blender/python/intern/bpy_rna_id_collection.h

===================================================================

diff --git a/release/scripts/modules/bpy_types.py b/release/scripts/modules/bpy_types.py
index 71fb209..830db97 100644
--- a/release/scripts/modules/bpy_types.py
+++ b/release/scripts/modules/bpy_types.py
@@ -26,7 +26,7 @@ StructMetaPropGroup = bpy_types.bpy_struct_meta_idprop
 # StructRNA = bpy_types.Struct
 
 bpy_types.BlendDataLibraries.load = _bpy._library_load
-bpy_types.BlendData.user_map = _bpy._data_user_map
+bpy_types.BlendData.user_map = _bpy._rna_id_collection_user_map
 
 
 class Context(StructRNA):
diff --git a/source/blender/python/intern/CMakeLists.txt b/source/blender/python/intern/CMakeLists.txt
index 1f66af8..cbfbe0a 100644
--- a/source/blender/python/intern/CMakeLists.txt
+++ b/source/blender/python/intern/CMakeLists.txt
@@ -56,7 +56,6 @@ set(SRC
 	bpy_app_oiio.c
 	bpy_app_sdl.c
 	bpy_app_translations.c
-	bpy_data.c
 	bpy_driver.c
 	bpy_interface.c
 	bpy_interface_atexit.c
@@ -70,6 +69,7 @@ set(SRC
 	bpy_rna_anim.c
 	bpy_rna_array.c
 	bpy_rna_callback.c
+	bpy_rna_id_collection.c
 	bpy_traceback.c
 	bpy_util.c
 	bpy_utils_previews.c
@@ -86,7 +86,6 @@ set(SRC
 	bpy_app_oiio.h
 	bpy_app_sdl.h
 	bpy_app_translations.h
-	bpy_data.h
 	bpy_driver.h
 	bpy_intern_string.h
 	bpy_library.h
@@ -97,6 +96,7 @@ set(SRC
 	bpy_rna.h
 	bpy_rna_anim.h
 	bpy_rna_callback.h
+	bpy_rna_id_collection.h
 	bpy_traceback.h
 	bpy_util.h
 	bpy_utils_previews.h
diff --git a/source/blender/python/intern/bpy.c b/source/blender/python/intern/bpy.c
index b37e108..930950a 100644
--- a/source/blender/python/intern/bpy.c
+++ b/source/blender/python/intern/bpy.c
@@ -45,7 +45,7 @@
 #include "bpy_util.h"
 #include "bpy_rna.h"
 #include "bpy_app.h"
-#include "bpy_data.h"
+#include "bpy_rna_id_collection.h"
 #include "bpy_props.h"
 #include "bpy_library.h"
 #include "bpy_operator.h"
@@ -323,7 +323,7 @@ void BPy_init_modules(void)
 
 	/* needs to be first so bpy_types can run */
 	BPY_library_module(mod);
-	BPY_data_module(mod);
+	BPY_rna_id_collection_module(mod);
 
 	bpy_import_test("bpy_types");
 	PyModule_AddObject(mod, "data", BPY_rna_module()); /* imports bpy_types by running this */
diff --git a/source/blender/python/intern/bpy_data.c b/source/blender/python/intern/bpy_rna_id_collection.c
similarity index 69%
rename from source/blender/python/intern/bpy_data.c
rename to source/blender/python/intern/bpy_rna_id_collection.c
index 46215ae..6846e0a 100644
--- a/source/blender/python/intern/bpy_data.c
+++ b/source/blender/python/intern/bpy_rna_id_collection.c
@@ -20,15 +20,10 @@
  * ***** END GPL LICENSE BLOCK *****
  */
 
-/** \file blender/python/intern/bpy_data.c
+/** \file blender/python/intern/bpy_rna_id_collection.c
  *  \ingroup pythonintern
  *
- * This file exposed blend file library appending/linking to python, typically
- * this would be done via RNA api but in this case a hand written python api
- * allows us to use pythons context manager (__enter__ and __exit__).
- *
- * Everything here is exposed via bpy.data.libraries.load(...) which returns
- * a context manager.
+ * This file adds some helpers related to ID/Main handling, that cannot fit well in RNA itself.
  */
 
 #include <Python.h>
@@ -44,7 +39,7 @@
 #include "DNA_ID.h"
 
 #include "bpy_util.h"
-#include "bpy_data.h"
+#include "bpy_rna_id_collection.h"
 
 #include "../generic/py_capi_utils.h"
 #include "../generic/python_utildefines.h"
@@ -55,6 +50,7 @@
 
 typedef struct IDUserMapData {
 	PyObject *py_id_key;
+	ID *orig_id_key;
 
 	ID *id_curr;
 	PyObject *py_id_curr;
@@ -69,12 +65,14 @@ static bool foreach_libblock_id_user_map_callback(void *user_data, ID **id_p, in
 
 	if (*id_p) {
 		PyObject *set;
+		PyObject *key = data->py_id_key;
 
-		/* Only stuff used by pyrna_struct_hash(). */
-		((BPy_StructRNA *)data->py_id_key)->ptr.data = *id_p;
+		/* pyrna_struct_hash() uses ptr.data only, but pyrna_struct_richcmp() uses also ptr.type, so we need to create
+		 * a valid PointerRNA here... */
+		RNA_id_pointer_create(*id_p, &((BPy_StructRNA *)key)->ptr);
 
 		if (data->is_restricted) {
-			if ((set = PyDict_GetItem(data->user_map, data->py_id_key))) {
+			if ((set = PyDict_GetItem(data->user_map, key))) {
 				if (data->py_id_curr == NULL) {
 					data->py_id_curr = pyrna_id_CreatePyObject(data->id_curr);
 				}
@@ -82,9 +80,9 @@ static bool foreach_libblock_id_user_map_callback(void *user_data, ID **id_p, in
 			}
 		}
 		else {
-			if ((set = PyDict_GetItem(data->user_map, data->py_id_key)) == NULL) {
+			if ((set = PyDict_GetItem(data->user_map, key)) == NULL) {
 				/* Cannot use our placeholder key here! */
-				PyObject *key = pyrna_id_CreatePyObject(data->id_curr);
+				key = pyrna_id_CreatePyObject(*id_p);
 				set = PySet_New(NULL);
 				PyDict_SetItem(data->user_map, key, set);
 				Py_DECREF(set);
@@ -101,7 +99,7 @@ static bool foreach_libblock_id_user_map_callback(void *user_data, ID **id_p, in
 }
 
 PyDoc_STRVAR(bpy_user_map_doc,
-".. method:: user_map(*args)\n"
+".. method:: user_map([subset=(id1, id2, ...)])\n"
 "\n"
 "   Returns a dict mapping all ID datablocks in current bpy.data (or the subset matching\n"
 "   given IDs as optional parameters) to a set of all datablocks using them.\n"
@@ -110,10 +108,10 @@ PyDoc_STRVAR(bpy_user_map_doc,
 "            This means the dict will likely become invalid after undo/redo and other operations\n"
 "            affecting Blender's internal database."
 "\n"
-"   :arg *args: One or more datablocks (or iterables of datablocks, e.g. bpy.data.objects...) to search for.\n"
-"   :type *args: ID\n"
+"   :arg subset: An iterable of datablocks to search for.\n"
+"   :type subset: iterable\n"
 );
-static PyObject *bpy_user_map(PyObject *UNUSED(self), PyObject *args)
+static PyObject *bpy_user_map(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
 {
 #if 0  /* If someone knows how to get a proper 'self' in that case... */
 	BPy_StructRNA *pyrna = (BPy_StructRNA *)self;
@@ -122,41 +120,34 @@ static PyObject *bpy_user_map(PyObject *UNUSED(self), PyObject *args)
 	Main *bmain = G.main;  /* XXX Ugly, but should work! */
 #endif
 
+	static const char *kwlist[] = {"subset", NULL};
+	PyObject *iter_id = NULL;
+
 	PyObject *user_map = PyDict_New();
-	PyObject *ref_id;
 
 	IDUserMapData data_cb = {NULL};
 
 	ListBase *lb_array[MAX_LIBARRAY];
 	int lb_idx;
 
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:user_map", (char **)kwlist, &iter_id)) {
+		return NULL;
+	}
+
 	data_cb.user_map = user_map;
 
-	if (PyTuple_Size(args)) {
-		PyObject *iter = PyObject_GetIter(args);
+	if (iter_id) {
+		PyObject *fast_it = PySequence_Fast(iter_id, "user_map");
+		PyObject **ref_id = PySequence_Fast_ITEMS(fast_it);
+		Py_ssize_t iter_id_len = PySequence_Fast_GET_SIZE(fast_it);
 
 		data_cb.is_restricted = true;
-		while ((ref_id = PyIter_Next(iter))) {
-			PyObject *sub_iter = PyObject_GetIter(ref_id);
-			if (sub_iter && PyIter_Check(sub_iter)) {
-				PyObject *sub_ref_id;
-
-				while ((sub_ref_id = PyIter_Next(sub_iter))) {
-					PyObject *set = PySet_New(NULL);
-					PyDict_SetItem(user_map, sub_ref_id, set);
-					Py_DECREF(set);
-					Py_DECREF(sub_ref_id);
-				}
-				Py_DECREF(sub_iter);
-			}
-			else {
-				PyObject *set = PySet_New(NULL);
-				PyDict_SetItem(user_map, ref_id, set);
-				Py_DECREF(set);
-			}
-			Py_DECREF(ref_id);
+		for (; iter_id_len; ref_id++, iter_id_len--) {
+			PyObject *set = PySet_New(NULL);
+			PyDict_SetItem(user_map, *ref_id, set);
+			Py_DECREF(set);
 		}
-		Py_DECREF(iter);
+		Py_DECREF(fast_it);
 	}
 
 	lb_idx = set_listbasepointers(bmain, lb_array);
@@ -184,11 +175,11 @@ static PyObject *bpy_user_map(PyObject *UNUSED(self), PyObject *args)
 	return user_map;
 }
 
-int BPY_data_module(PyObject *mod_par)
+int BPY_rna_id_collection_module(PyObject *mod_par)
 {
-	static PyMethodDef user_map = {"user_map", (PyCFunction)bpy_user_map, METH_VARARGS, bpy_user_map_doc};
+	static PyMethodDef user_map = {"user_map", (PyCFunction)bpy_user_map, METH_VARARGS | METH_KEYWORDS, bpy_user_map_doc};
 
-	PyModule_AddObject(mod_par, "_data_user_map", PyCFunction_New(&user_map, NULL));
+	PyModule_AddObject(mod_par, "_rna_id_collection_user_map", PyCFunction_New(&user_map, NULL));
 
 	return 0;
 }
diff --git a/source/blender/python/intern/bpy_data.h b/source/blender/python/intern/bpy_rna_id_collection.h
similarity index 80%
rename from source/blender/python/intern/bpy_data.h
rename to source/blender/python/intern/bpy_rna_id_collection.h
index a11f830..c293289 100644
--- a/source/blender/python/intern/bpy_data.h
+++ b/source/blender/python/intern/bpy_rna_id_collection.h
@@ -20,13 +20,13 @@
  * ***** END GPL LICENSE BLOCK *****
  */
 
-/** \file blender/python/intern/bpy_data.h
+/** \file blender/python/intern/bpy_rna_id_collection.h
  *  \ingroup pythonintern
  */
 
-#ifndef __BPY_DATA_H__
-#define __BPY_DATA_H__
+#ifndef __BPY_RNA_ID_COLLECTION_H__
+#define __BPY_RNA_ID_COLLECTION_H__
 
-int BPY_data_module(PyObject *);
+int BPY_rna_id_collection_module(PyObject *);
 
-#endif  /* __BPY_DATA_H__ */
+#endif  /* __BPY_RNA_ID_COLLECTION_H__ */




More information about the Bf-blender-cvs mailing list