Merge with 2.5 -r 21619:21756.
[blender.git] / source / blender / python / intern / bpy_util.c
index 3084cc1f8712d26aadedc9e6942423118592a8d4..dc3132814b436e6037a07eb6d097189ab13eadfe 100644 (file)
 #include "DNA_listBase.h"
 #include "RNA_access.h"
 #include "bpy_util.h"
-#include "BLI_dynstr.h"
+#include "bpy_rna.h"
+
 #include "MEM_guardedalloc.h"
-#include "BKE_report.h"
 
+#include "BLI_dynstr.h"
+#include "BLI_listbase.h"
 
+#include "BKE_report.h"
+#include "BKE_image.h"
 #include "BKE_context.h"
+
 bContext*      __py_context = NULL;
 bContext*      BPy_GetContext(void) { return __py_context; };
 void           BPy_SetContext(bContext *C) { __py_context= C; };
@@ -483,3 +488,126 @@ int BPy_errors_to_report(ReportList *reports)
        Py_DECREF(pystring);
        return 1;
 }
+
+
+/* bpy.util module */
+static PyObject *bpy_util_copy_images(PyObject *self, PyObject *args);
+
+struct PyMethodDef bpy_util_methods[] = {
+       {"copy_images", bpy_util_copy_images, METH_VARARGS, NULL},
+       {NULL, NULL, 0, NULL}
+};
+
+#if PY_VERSION_HEX >= 0x03000000
+static struct PyModuleDef bpy_util_module = {
+       PyModuleDef_HEAD_INIT,
+       "bpyutil",
+       NULL,
+       -1,
+       bpy_util_methods,
+       NULL, NULL, NULL, NULL
+};
+#endif
+
+PyObject *BPY_util_module( void )
+{
+       PyObject *submodule, *dict;
+
+#if PY_VERSION_HEX >= 0x03000000
+       submodule= PyModule_Create(&bpy_util_module);
+#else /* Py2.x */
+       submodule= Py_InitModule3("bpyutil", bpy_util_methods, NULL);
+#endif
+
+       dict = PyModule_GetDict(submodule);
+       
+       return submodule;
+}
+
+/*
+  copy_images(images, dest_dir)
+  return filenames
+*/
+static PyObject *bpy_util_copy_images(PyObject *self, PyObject *args)
+{
+       const char *dest_dir;
+       ListBase *images;
+       ListBase *paths;
+       LinkData *link;
+       PyObject *seq;
+       PyObject *ret;
+       PyObject *item;
+       int i;
+       int len;
+
+       /* check args/types */
+       if (!PyArg_ParseTuple(args, "Os", &seq, &dest_dir)) {
+               PyErr_SetString(PyExc_TypeError, "Invalid arguments.");
+               return NULL;
+       }
+
+       /* expecting a sequence of Image objects */
+       if (!PySequence_Check(seq)) {
+               PyErr_SetString(PyExc_TypeError, "Expected a sequence of images.");
+               return NULL;
+       }
+
+       /* create image list */
+       len= PySequence_Size(seq);
+
+       if (!len) {
+               PyErr_SetString(PyExc_TypeError, "At least one image should be specified.");
+               return NULL;
+       }
+
+       /* make sure all sequence items are Image */
+       for(i= 0; i < len; i++) {
+               item= PySequence_GetItem(seq, i);
+
+               if (!BPy_StructRNA_Check(item) || ((BPy_StructRNA*)item)->ptr.type != &RNA_Image) {
+                       PyErr_SetString(PyExc_TypeError, "Expected a sequence of Image objects.");
+                       return NULL;
+               }
+       }
+
+       images= MEM_callocN(sizeof(*images), "ListBase of images");
+
+       for(i= 0; i < len; i++) {
+               BPy_StructRNA* srna;
+
+               item= PySequence_GetItem(seq, i);
+               srna= (BPy_StructRNA*)item;
+
+               link= MEM_callocN(sizeof(LinkData), "LinkData image");
+               link->data= srna->ptr.data;
+               BLI_addtail(images, link);
+
+               Py_DECREF(item);
+       }
+
+       paths= MEM_callocN(sizeof(*paths), "ListBase of image paths");
+
+       /* call BKE_copy_images */
+       BKE_copy_images(images, dest_dir, paths);
+
+       /* convert filenames */
+       ret= PyList_New(0);
+
+       for(link= paths->first; link; link= link->next) {
+               if (link->data) {
+                       item= PyUnicode_FromString(link->data);
+                       PyList_Append(ret, item);
+                       Py_DECREF(item);
+               }
+               else {
+                       PyList_Append(ret, Py_None);
+               }
+       }
+
+       /* free memory */
+       BLI_freelistN(images);
+       BLI_freelistN(paths);
+
+       /* return filenames */
+       return ret;
+}