[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [36869] trunk/blender: loading data with bpy.data.libraries.load(), now swaps out the strings in the list to load with the actual datablocks, this is convenient because it saves the script author having to find them after .

Campbell Barton ideasman42 at gmail.com
Tue May 24 17:21:14 CEST 2011


Revision: 36869
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=36869
Author:   campbellbarton
Date:     2011-05-24 15:21:14 +0000 (Tue, 24 May 2011)
Log Message:
-----------
loading data with bpy.data.libraries.load(), now swaps out the strings in the list to load with the actual datablocks, this is convenient because it saves the script author having to find them after.

also raise warnings rather then errors if the datablock can't be found.

Modified Paths:
--------------
    trunk/blender/doc/python_api/examples/bpy.types.BlendDataLibraries.load.py
    trunk/blender/source/blender/python/intern/bpy_library.c

Modified: trunk/blender/doc/python_api/examples/bpy.types.BlendDataLibraries.load.py
===================================================================
--- trunk/blender/doc/python_api/examples/bpy.types.BlendDataLibraries.load.py	2011-05-24 15:02:46 UTC (rev 36868)
+++ trunk/blender/doc/python_api/examples/bpy.types.BlendDataLibraries.load.py	2011-05-24 15:21:14 UTC (rev 36869)
@@ -21,3 +21,19 @@
 with bpy.data.libraries.load(filepath) as (data_from, data_to):
     for attr in dir(data_to):
         setattr(data_to, attr, getattr(data_from, attr))
+
+
+# the 'data_to' variables lists are 
+with bpy.data.libraries.load(filepath) as (data_from, data_to):
+    data_to.scenes = ["Scene"]
+
+
+# the loaded objects can be accessed from 'data_to' outside of the context
+# since loading the data replaces the strings for the datablocks or None
+# if the datablock could not be loaded.
+with bpy.data.libraries.load(filepath) as (data_from, data_to):
+    data_to.meshes = data_from.meshes
+# now operate directly on the loaded data
+for mesh in mdata_to.meshes:
+    if mesh is not None:
+        print(mesh.name)

Modified: trunk/blender/source/blender/python/intern/bpy_library.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_library.c	2011-05-24 15:02:46 UTC (rev 36868)
+++ trunk/blender/source/blender/python/intern/bpy_library.c	2011-05-24 15:21:14 UTC (rev 36869)
@@ -26,6 +26,9 @@
  *  \ingroup pythonintern
  */
 
+/* nifty feature. swap out strings for RNA data */
+#define USE_RNA_DATABLOCKS
+
 #include <Python.h>
 #include <stddef.h>
 
@@ -47,6 +50,11 @@
 
 #include "bpy_util.h"
 
+#ifdef USE_RNA_DATABLOCKS
+#  include "bpy_rna.h"
+#  include "RNA_access.h"
+#endif
+
 typedef struct {
 	PyObject_HEAD /* required python macro   */
 	/* collection iterator spesific parts */
@@ -271,6 +279,36 @@
 	return ret;
 }
 
+static void bpy_lib_exit_warn_idname(BPy_Library *self, const char *name_plural, const char *idname)
+{
+	PyObject *exc, *val, *tb;
+	PyErr_Fetch(&exc, &val, &tb);
+	if (PyErr_WarnFormat(PyExc_UserWarning, 1,
+						 "load: '%s' does not contain %s[\"%s\"]",
+	                     self->abspath, name_plural, idname)) {
+		/* Spurious errors can appear at shutdown */
+		if (PyErr_ExceptionMatches(PyExc_Warning)) {
+			PyErr_WriteUnraisable((PyObject *)self);
+		}
+	}
+	PyErr_Restore(exc, val, tb);
+}
+
+static void bpy_lib_exit_warn_type(BPy_Library *self, PyObject *item)
+{
+	PyObject *exc, *val, *tb;
+	PyErr_Fetch(&exc, &val, &tb);
+	if (PyErr_WarnFormat(PyExc_UserWarning, 1,
+						 "load: '%s' expected a string type, not a %.200s",
+	                     self->abspath, Py_TYPE(item)->tp_name)) {
+		/* Spurious errors can appear at shutdown */
+		if (PyErr_ExceptionMatches(PyExc_Warning)) {
+			PyErr_WriteUnraisable((PyObject *)self);
+		}
+	}
+	PyErr_Restore(exc, val, tb);
+}
+
 static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args))
 {
 	Main *mainl= NULL;
@@ -302,18 +340,41 @@
 						// printf("  %s\n", item_str);
 
 						if(item_str) {
-							if(!BLO_library_append_named_part(NULL, mainl, &(self->blo_handle), item_str, code, self->flag)) {
-								PyErr_Format(PyExc_KeyError,
-								             "load: %s does not contain %s[\"%s\"]",
-								             self->abspath, name_plural, item_str);
-								err= -1;
-								break;
+							ID *id= BLO_library_append_named_part(NULL, mainl, &(self->blo_handle), item_str, code, self->flag);
+							if(id) {
+#ifdef USE_RNA_DATABLOCKS
+								PointerRNA id_ptr;
+								RNA_id_pointer_create(id, &id_ptr);
+								Py_DECREF(item);
+								item= pyrna_struct_CreatePyObject(&id_ptr);
+#endif
 							}
+							else {
+								bpy_lib_exit_warn_idname(self, name_plural, item_str);
+								/* just warn for now */
+								/* err = -1; */
+#ifdef USE_RNA_DATABLOCKS
+								item= Py_None;
+								Py_INCREF(item);
+#endif
+							}
+
+							/* ID or None */
 						}
 						else {
 							/* XXX, could complain about this */
+							bpy_lib_exit_warn_type(self, item);
 							PyErr_Clear();
+
+#ifdef USE_RNA_DATABLOCKS
+							item= Py_None;
+							Py_INCREF(item);
+#endif
 						}
+
+#ifdef USE_RNA_DATABLOCKS
+						PyList_SET_ITEM(ls, i, item);
+#endif
 					}
 				}
 			}




More information about the Bf-blender-cvs mailing list