Merged changes in the trunk up to revision 30397.
[blender-staging.git] / source / blender / python / intern / bpy.c
index dfbad7aa610163d4485abe72588a3a40c4eb775c..4b54f5c6416a9bdd00cfbf1f979f0e50207c63a1 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * $Id:
+ * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
 /* This file defines the '_bpy' module which is used by python's 'bpy' package.
  * a script writer should never directly access this module */
  
-#include <Python.h>
+
+#include "bpy_util.h" 
 #include "bpy_rna.h"
 #include "bpy_app.h"
 #include "bpy_props.h"
 #include "bpy_operator.h"
+
 #include "BLI_path_util.h"
+#include "BLI_bpath.h"
  
  /* external util modules */
-#include "../generic/Mathutils.h"
-#include "../generic/Geometry.h"
+#include "../generic/geometry.h"
 #include "../generic/bgl.h"
-#include "../generic/blf.h"
+#include "../generic/blf_api.h"
 #include "../generic/IDProp.h"
 
-static char bpy_home_paths_doc[] =
-".. function:: home_paths(subfolder)\n"
+#include "BPy_Freestyle.h"
+
+static char bpy_script_paths_doc[] =
+".. function:: script_paths()\n"
 "\n"
-"   return 3 paths to blender home directories.\n"
+"   Return 2 paths to blender scripts directories.\n"
 "\n"
-"   :arg subfolder: The name of a subfolder to find within the blenders home directory.\n"
-"   :type subfolder: string\n"
-"   :return: (system, local, user) strings will be empty when not found.\n"
+"   :return: (system, user) strings will be empty when not found.\n"
 "   :rtype: tuple of strigs\n";
 
-PyObject *bpy_home_paths(PyObject *self, PyObject *args)
+PyObject *bpy_script_paths(PyObject *self)
 {
-    PyObject *ret= PyTuple_New(3);
-    char *path;
-    char *subfolder= "";
+       PyObject *ret= PyTuple_New(2);
+       char *path;
     
-       if (!PyArg_ParseTuple(args, "|s:blender_homes", &subfolder))
+       path= BLI_get_folder(BLENDER_USER_SCRIPTS, NULL);
+       PyTuple_SET_ITEM(ret, 0, PyUnicode_FromString(path?path:""));
+       path= BLI_get_folder(BLENDER_SYSTEM_SCRIPTS, NULL);
+       PyTuple_SET_ITEM(ret, 1, PyUnicode_FromString(path?path:""));
+    
+       return ret;
+}
+
+static char bpy_blend_paths_doc[] =
+".. function:: blend_paths(absolute=False)\n"
+"\n"
+"   Returns a list of paths associated with this blend file.\n"
+"\n"
+"   :arg absolute: When true the paths returned are made absolute.\n"
+"   :type absolute: boolean\n"
+"   :return: path list.\n"
+"   :rtype: list of strigs\n";
+static PyObject *bpy_blend_paths(PyObject * self, PyObject *args, PyObject *kw)
+{
+       struct BPathIterator bpi;
+       PyObject *list = PyList_New(0), *st; /* stupidly big string to be safe */
+       /* be sure there is low chance of the path being too short */
+       char filepath_expanded[1024];
+       char *lib;
+
+       int absolute = 0;
+       static char *kwlist[] = {"absolute", NULL};
+
+       if (!PyArg_ParseTupleAndKeywords(args, kw, "|i:blend_paths", kwlist, &absolute))
                return NULL;
 
-    path= BLI_gethome_folder(subfolder, BLI_GETHOME_SYSTEM);
-    PyTuple_SET_ITEM(ret, 0, PyUnicode_FromString(path?path:""));
+       for(BLI_bpathIterator_init(&bpi, NULL); !BLI_bpathIterator_isDone(&bpi); BLI_bpathIterator_step(&bpi)) {
+               /* build the list */
+               if (absolute) {
+                       BLI_bpathIterator_getPathExpanded(&bpi, filepath_expanded);
+               }
+               else {
+                       lib = BLI_bpathIterator_getLib(&bpi);
+                       if (lib && (strcmp(lib, bpi.base_path))) { /* relative path to the library is NOT the same as our blendfile path, return an absolute path */
+                               BLI_bpathIterator_getPathExpanded(&bpi, filepath_expanded);
+                       }
+                       else {
+                               BLI_bpathIterator_getPath(&bpi, filepath_expanded);
+                       }
+               }
+               st = PyUnicode_FromString(filepath_expanded);
+
+               PyList_Append(list, st);
+               Py_DECREF(st);
+       }
 
-    path= BLI_gethome_folder(subfolder, BLI_GETHOME_LOCAL);
-    PyTuple_SET_ITEM(ret, 1, PyUnicode_FromString(path?path:""));
+       BLI_bpathIterator_free(&bpi);
 
-    path= BLI_gethome_folder(subfolder, BLI_GETHOME_USER);
-    PyTuple_SET_ITEM(ret, 2, PyUnicode_FromString(path?path:""));
-    
-    return ret;
+       return list;
 }
 
-static PyMethodDef meth_bpy_home_paths[] = {{ "home_paths", (PyCFunction)bpy_home_paths, METH_VARARGS, bpy_home_paths_doc}};
+static PyMethodDef meth_bpy_script_paths[] = {{ "script_paths", (PyCFunction)bpy_script_paths, METH_NOARGS, bpy_script_paths_doc}};
+static PyMethodDef meth_bpy_blend_paths[] = {{ "blend_paths", (PyCFunction)bpy_blend_paths, METH_VARARGS|METH_KEYWORDS, bpy_blend_paths_doc}};
 
 static void bpy_import_test(char *modname)
 {
@@ -91,24 +132,29 @@ static void bpy_import_test(char *modname)
 *****************************************************************************/
 void BPy_init_modules( void )
 {
-    extern BPy_StructRNA *bpy_context_module;
+       extern BPy_StructRNA *bpy_context_module;
+       PointerRNA ctx_ptr;
        PyObject *mod;
 
        /* Needs to be first since this dir is needed for future modules */
-       char *modpath= BLI_gethome_folder("scripts/modules", BLI_GETHOME_ALL);
+       char *modpath= BLI_get_folder(BLENDER_SCRIPTS, "modules");
        if(modpath) {
+               // printf("bpy: found module path '%s'.\n", modpath);
                PyObject *sys_path= PySys_GetObject("path"); /* borrow */
                PyObject *py_modpath= PyUnicode_FromString(modpath);
                PyList_Insert(sys_path, 0, py_modpath); /* add first */
                Py_DECREF(py_modpath);
        }
-       
+       else {
+               printf("bpy: couldnt find 'scripts/modules', blender probably wont start.\n");
+       }
        /* stand alone utility modules not related to blender directly */
        Geometry_Init();
        Mathutils_Init();
        BGL_Init();
        BLF_Init();
        IDProp_Init_Types();
+       Freestyle_Init();
 
 
        mod = PyModule_New("_bpy");
@@ -129,13 +175,17 @@ void BPy_init_modules( void )
        PyModule_AddObject( mod, "app", BPY_app_struct() );
 
        /* bpy context */
-    bpy_context_module= ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type );
-    RNA_pointer_create(NULL, &RNA_Context, NULL, &bpy_context_module->ptr);
-    bpy_context_module->freeptr= 0;
-    PyModule_AddObject(mod, "context", (PyObject *)bpy_context_module);
-    
+       RNA_pointer_create(NULL, &RNA_Context, BPy_GetContext(), &ctx_ptr);
+       bpy_context_module= (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ctx_ptr);
+       /* odd that this is needed, 1 ref on creation and another for the module
+        * but without we get a crash on exit */
+       Py_INCREF(bpy_context_module);
+
+       PyModule_AddObject(mod, "context", (PyObject *)bpy_context_module);
+
        /* utility func's that have nowhere else to go */
-       PyModule_AddObject(mod, meth_bpy_home_paths->ml_name, (PyObject *)PyCFunction_New(meth_bpy_home_paths, NULL));
+       PyModule_AddObject(mod, meth_bpy_script_paths->ml_name, (PyObject *)PyCFunction_New(meth_bpy_script_paths, NULL));
+       PyModule_AddObject(mod, meth_bpy_blend_paths->ml_name, (PyObject *)PyCFunction_New(meth_bpy_blend_paths, NULL));
 
        /* add our own modules dir, this is a python package */
        bpy_import_test("bpy");