~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/blender/python/intern/bpy.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * $Id: bpy.c 30521 2010-07-20 03:14:21Z campbellbarton $
3
 
 *
 
1
/*
4
2
 * ***** BEGIN GPL LICENSE BLOCK *****
5
3
 *
6
4
 * This program is free software; you can redistribute it and/or
21
19
 *
22
20
 * ***** END GPL LICENSE BLOCK *****
23
21
 */
24
 
 
25
 
/* This file defines the '_bpy' module which is used by python's 'bpy' package.
26
 
 * a script writer should never directly access this module */
27
 
 
28
 
 
 
22
 
 
23
/** \file blender/python/intern/bpy.c
 
24
 *  \ingroup pythonintern
 
25
 *
 
26
 * This file defines the '_bpy' module which is used by python's 'bpy' package
 
27
 * to access C defined builtin functions.
 
28
 * A script writer should never directly access this module.
 
29
 */
 
30
 
 
31
#define WITH_PYTHON /* for AUD_PyInit.h, possibly others */
 
32
 
 
33
#include <Python.h>
 
34
 
 
35
#include "bpy.h" 
29
36
#include "bpy_util.h" 
30
37
#include "bpy_rna.h"
31
38
#include "bpy_app.h"
33
40
#include "bpy_operator.h"
34
41
 
35
42
#include "BLI_path_util.h"
 
43
#include "BLI_string.h"
36
44
#include "BLI_bpath.h"
37
 
 
38
 
 /* external util modules */
39
 
#include "../generic/geometry.h"
 
45
#include "BLI_utildefines.h"
 
46
 
 
47
#include "BKE_main.h"
 
48
#include "BKE_global.h" /* XXX, G.main only */
 
49
#include "BKE_blender.h"
 
50
 
 
51
#include "RNA_access.h"
 
52
 
 
53
#include "MEM_guardedalloc.h"
 
54
 
 
55
/* external util modules */
 
56
#include "../generic/idprop_py_api.h"
40
57
#include "../generic/bgl.h"
41
 
#include "../generic/blf_api.h"
42
 
#include "../generic/IDProp.h"
43
 
 
44
 
static char bpy_script_paths_doc[] =
 
58
#include "../generic/blf_py_api.h"
 
59
#include "../mathutils/mathutils.h"
 
60
 
 
61
PyObject *bpy_package_py = NULL;
 
62
 
 
63
PyDoc_STRVAR(bpy_script_paths_doc,
45
64
".. function:: script_paths()\n"
46
65
"\n"
47
66
"   Return 2 paths to blender scripts directories.\n"
48
67
"\n"
49
68
"   :return: (system, user) strings will be empty when not found.\n"
50
 
"   :rtype: tuple of strigs\n";
51
 
 
52
 
PyObject *bpy_script_paths(PyObject *self)
 
69
"   :rtype: tuple of strings\n"
 
70
);
 
71
static PyObject *bpy_script_paths(PyObject *UNUSED(self))
53
72
{
54
 
        PyObject *ret= PyTuple_New(2);
 
73
        PyObject *ret = PyTuple_New(2);
 
74
        PyObject *item;
55
75
        char *path;
56
 
    
57
 
        path= BLI_get_folder(BLENDER_USER_SCRIPTS, NULL);
58
 
        PyTuple_SET_ITEM(ret, 0, PyUnicode_FromString(path?path:""));
59
 
        path= BLI_get_folder(BLENDER_SYSTEM_SCRIPTS, NULL);
60
 
        PyTuple_SET_ITEM(ret, 1, PyUnicode_FromString(path?path:""));
61
 
    
 
76
 
 
77
        path = BLI_get_folder(BLENDER_SYSTEM_SCRIPTS, NULL);
 
78
        item = PyUnicode_DecodeFSDefault(path ? path : "");
 
79
        BLI_assert(item != NULL);
 
80
        PyTuple_SET_ITEM(ret, 0, item);
 
81
        path = BLI_get_folder(BLENDER_USER_SCRIPTS, NULL);
 
82
        item = PyUnicode_DecodeFSDefault(path ? path : "");
 
83
        BLI_assert(item != NULL);
 
84
        PyTuple_SET_ITEM(ret, 1, item);
 
85
 
62
86
        return ret;
63
87
}
64
88
 
65
 
static char bpy_blend_paths_doc[] =
66
 
".. function:: blend_paths(absolute=False)\n"
 
89
static int bpy_blend_paths_visit_cb(void *userdata, char *UNUSED(path_dst), const char *path_src)
 
90
{
 
91
        PyObject *list = (PyObject *)userdata;
 
92
        PyObject *item = PyUnicode_DecodeFSDefault(path_src);
 
93
        PyList_Append(list, item);
 
94
        Py_DECREF(item);
 
95
        return FALSE; /* never edits the path */
 
96
}
 
97
 
 
98
PyDoc_STRVAR(bpy_blend_paths_doc,
 
99
".. function:: blend_paths(absolute=False, packed=False, local=False)\n"
67
100
"\n"
68
 
"   Returns a list of paths associated with this blend file.\n"
 
101
"   Returns a list of paths to external files referenced by the loaded .blend file.\n"
69
102
"\n"
70
103
"   :arg absolute: When true the paths returned are made absolute.\n"
71
104
"   :type absolute: boolean\n"
 
105
"   :arg packed: When true skip file paths for packed data.\n"
 
106
"   :type packed: boolean\n"
 
107
"   :arg local: When true skip linked library paths.\n"
 
108
"   :type local: boolean\n"
72
109
"   :return: path list.\n"
73
 
"   :rtype: list of strigs\n";
74
 
static PyObject *bpy_blend_paths(PyObject * self, PyObject *args, PyObject *kw)
 
110
"   :rtype: list of strings\n"
 
111
);
 
112
static PyObject *bpy_blend_paths(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
75
113
{
76
 
        struct BPathIterator bpi;
77
 
        PyObject *list = PyList_New(0), *st; /* stupidly big string to be safe */
78
 
        /* be sure there is low chance of the path being too short */
79
 
        char filepath_expanded[1024];
80
 
        char *lib;
81
 
 
82
 
        int absolute = 0;
83
 
        static char *kwlist[] = {"absolute", NULL};
84
 
 
85
 
        if (!PyArg_ParseTupleAndKeywords(args, kw, "|i:blend_paths", kwlist, &absolute))
 
114
        int flag = 0;
 
115
        PyObject *list;
 
116
 
 
117
        int absolute = FALSE;
 
118
        int packed =   FALSE;
 
119
        int local =    FALSE;
 
120
        static const char *kwlist[] = {"absolute", "packed", "local", NULL};
 
121
 
 
122
        if (!PyArg_ParseTupleAndKeywords(args, kw, "|ii:blend_paths",
 
123
                                         (char **)kwlist, &absolute, &packed))
 
124
        {
86
125
                return NULL;
87
 
 
88
 
        for(BLI_bpathIterator_init(&bpi, NULL); !BLI_bpathIterator_isDone(&bpi); BLI_bpathIterator_step(&bpi)) {
89
 
                /* build the list */
90
 
                if (absolute) {
91
 
                        BLI_bpathIterator_getPathExpanded(&bpi, filepath_expanded);
92
 
                }
93
 
                else {
94
 
                        lib = BLI_bpathIterator_getLib(&bpi);
95
 
                        if (lib && (strcmp(lib, bpi.base_path))) { /* relative path to the library is NOT the same as our blendfile path, return an absolute path */
96
 
                                BLI_bpathIterator_getPathExpanded(&bpi, filepath_expanded);
97
 
                        }
98
 
                        else {
99
 
                                BLI_bpathIterator_getPath(&bpi, filepath_expanded);
100
 
                        }
101
 
                }
102
 
                st = PyUnicode_FromString(filepath_expanded);
103
 
 
104
 
                PyList_Append(list, st);
105
 
                Py_DECREF(st);
106
126
        }
107
127
 
108
 
        BLI_bpathIterator_free(&bpi);
 
128
        if (absolute) flag |= BPATH_TRAVERSE_ABS;
 
129
        if (!packed)  flag |= BPATH_TRAVERSE_SKIP_PACKED;
 
130
        if (local)    flag |= BPATH_TRAVERSE_SKIP_LIBRARY;
 
131
 
 
132
        list = PyList_New(0);
 
133
 
 
134
        bpath_traverse_main(G.main, bpy_blend_paths_visit_cb, flag, (void *)list);
109
135
 
110
136
        return list;
111
137
}
112
138
 
113
 
static PyMethodDef meth_bpy_script_paths[] = {{ "script_paths", (PyCFunction)bpy_script_paths, METH_NOARGS, bpy_script_paths_doc}};
114
 
static PyMethodDef meth_bpy_blend_paths[] = {{ "blend_paths", (PyCFunction)bpy_blend_paths, METH_VARARGS|METH_KEYWORDS, bpy_blend_paths_doc}};
115
 
 
116
 
static void bpy_import_test(char *modname)
117
 
{
118
 
        PyObject *mod= PyImport_ImportModuleLevel(modname, NULL, NULL, NULL, 0);
119
 
        if(mod) {
 
139
 
 
140
// PyDoc_STRVAR(bpy_user_resource_doc[] = // now in bpy/utils.py
 
141
static PyObject *bpy_user_resource(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
 
142
{
 
143
        char *type;
 
144
        char *subdir = NULL;
 
145
        int folder_id;
 
146
        static const char *kwlist[] = {"type", "subdir", NULL};
 
147
 
 
148
        char *path;
 
149
 
 
150
        if (!PyArg_ParseTupleAndKeywords(args, kw, "s|s:user_resource", (char **)kwlist, &type, &subdir))
 
151
                return NULL;
 
152
        
 
153
        /* stupid string compare */
 
154
        if      (!strcmp(type, "DATAFILES")) folder_id = BLENDER_USER_DATAFILES;
 
155
        else if (!strcmp(type, "CONFIG"))    folder_id = BLENDER_USER_CONFIG;
 
156
        else if (!strcmp(type, "SCRIPTS"))   folder_id = BLENDER_USER_SCRIPTS;
 
157
        else if (!strcmp(type, "AUTOSAVE"))  folder_id = BLENDER_USER_AUTOSAVE;
 
158
        else {
 
159
                PyErr_SetString(PyExc_ValueError, "invalid resource argument");
 
160
                return NULL;
 
161
        }
 
162
        
 
163
        /* same logic as BLI_get_folder_create(), but best leave it up to the script author to create */
 
164
        path = BLI_get_folder(folder_id, subdir);
 
165
 
 
166
        if (!path)
 
167
                path = BLI_get_user_folder_notest(folder_id, subdir);
 
168
 
 
169
        return PyUnicode_DecodeFSDefault(path ? path : "");
 
170
}
 
171
 
 
172
PyDoc_STRVAR(bpy_resource_path_doc,
 
173
".. function:: resource_path(type, major=2, minor=57)\n"
 
174
"\n"
 
175
"   Return the base path for storing system files.\n"
 
176
"\n"
 
177
"   :arg type: string in ['USER', 'LOCAL', 'SYSTEM'].\n"
 
178
"   :type type: string\n"
 
179
"   :arg major: major version, defaults to current.\n"
 
180
"   :type major: int\n"
 
181
"   :arg minor: minor version, defaults to current.\n"
 
182
"   :type minor: string\n"
 
183
"   :return: the resource path (not necessarily existing).\n"
 
184
"   :rtype: string\n"
 
185
);
 
186
static PyObject *bpy_resource_path(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
 
187
{
 
188
        char *type;
 
189
        int major = BLENDER_VERSION / 100, minor = BLENDER_VERSION % 100;
 
190
        static const char *kwlist[] = {"type", "major", "minor", NULL};
 
191
        int folder_id;
 
192
        char *path;
 
193
 
 
194
        if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ii:resource_path", (char **)kwlist, &type, &major, &minor))
 
195
                return NULL;
 
196
 
 
197
        /* stupid string compare */
 
198
        if      (!strcmp(type, "USER"))    folder_id = BLENDER_RESOURCE_PATH_USER;
 
199
        else if (!strcmp(type, "LOCAL"))   folder_id = BLENDER_RESOURCE_PATH_LOCAL;
 
200
        else if (!strcmp(type, "SYSTEM"))  folder_id = BLENDER_RESOURCE_PATH_SYSTEM;
 
201
        else {
 
202
                PyErr_SetString(PyExc_ValueError, "invalid resource argument");
 
203
                return NULL;
 
204
        }
 
205
 
 
206
        path = BLI_get_folder_version(folder_id, (major * 100) + minor, FALSE);
 
207
 
 
208
        return PyUnicode_DecodeFSDefault(path ? path : "");
 
209
}
 
210
 
 
211
static PyMethodDef meth_bpy_script_paths =
 
212
        {"script_paths", (PyCFunction)bpy_script_paths, METH_NOARGS, bpy_script_paths_doc};
 
213
static PyMethodDef meth_bpy_blend_paths =
 
214
        {"blend_paths", (PyCFunction)bpy_blend_paths, METH_VARARGS | METH_KEYWORDS, bpy_blend_paths_doc};
 
215
static PyMethodDef meth_bpy_user_resource =
 
216
        {"user_resource", (PyCFunction)bpy_user_resource, METH_VARARGS | METH_KEYWORDS, NULL};
 
217
static PyMethodDef meth_bpy_resource_path =
 
218
        {"resource_path", (PyCFunction)bpy_resource_path, METH_VARARGS | METH_KEYWORDS, bpy_resource_path_doc};
 
219
 
 
220
 
 
221
static PyObject *bpy_import_test(const char *modname)
 
222
{
 
223
        PyObject *mod = PyImport_ImportModuleLevel((char *)modname, NULL, NULL, NULL, 0);
 
224
        if (mod) {
120
225
                Py_DECREF(mod);
121
226
        }
122
227
        else {
123
228
                PyErr_Print();
124
229
                PyErr_Clear();
125
 
        }       
 
230
        }
 
231
 
 
232
        return mod;
126
233
}
127
234
 
128
 
/*****************************************************************************
129
 
* Description: Creates the bpy module and adds it to sys.modules for importing
130
 
*****************************************************************************/
131
 
void BPy_init_modules( void )
 
235
/******************************************************************************
 
236
 * Description: Creates the bpy module and adds it to sys.modules for importing
 
237
 ******************************************************************************/
 
238
void BPy_init_modules(void)
132
239
{
133
240
        extern BPy_StructRNA *bpy_context_module;
 
241
        extern int bpy_lib_init(PyObject *);
134
242
        PointerRNA ctx_ptr;
135
243
        PyObject *mod;
136
244
 
137
245
        /* Needs to be first since this dir is needed for future modules */
138
 
        char *modpath= BLI_get_folder(BLENDER_SCRIPTS, "modules");
139
 
        if(modpath) {
 
246
        char *modpath = BLI_get_folder(BLENDER_SYSTEM_SCRIPTS, "modules");
 
247
        if (modpath) {
140
248
                // printf("bpy: found module path '%s'.\n", modpath);
141
 
                PyObject *sys_path= PySys_GetObject("path"); /* borrow */
142
 
                PyObject *py_modpath= PyUnicode_FromString(modpath);
 
249
                PyObject *sys_path = PySys_GetObject("path"); /* borrow */
 
250
                PyObject *py_modpath = PyUnicode_FromString(modpath);
143
251
                PyList_Insert(sys_path, 0, py_modpath); /* add first */
144
252
                Py_DECREF(py_modpath);
145
253
        }
147
255
                printf("bpy: couldnt find 'scripts/modules', blender probably wont start.\n");
148
256
        }
149
257
        /* stand alone utility modules not related to blender directly */
150
 
        Geometry_Init();
151
 
        Mathutils_Init();
152
 
        Noise_Init();
153
 
        BGL_Init();
154
 
        BLF_Init();
155
 
        IDProp_Init_Types();
156
 
 
 
258
        IDProp_Init_Types(); /* not actually a submodule, just types */
157
259
 
158
260
        mod = PyModule_New("_bpy");
159
261
 
160
262
        /* add the module so we can import it */
161
 
        PyDict_SetItemString(PySys_GetObject("modules"), "_bpy", mod);
 
263
        PyDict_SetItemString(PyImport_GetModuleDict(), "_bpy", mod);
162
264
        Py_DECREF(mod);
163
265
 
164
266
        /* run first, initializes rna types */
165
267
        BPY_rna_init();
166
268
 
167
 
        PyModule_AddObject( mod, "types", BPY_rna_types() ); /* needs to be first so bpy_types can run */
168
 
        bpy_import_test("bpy_types");
169
 
        PyModule_AddObject( mod, "data", BPY_rna_module() ); /* imports bpy_types by running this */
170
 
        bpy_import_test("bpy_types");
171
 
        PyModule_AddObject( mod, "props", BPY_rna_props() );
172
 
        PyModule_AddObject( mod, "ops", BPY_operator_module() ); /* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */
173
 
        PyModule_AddObject( mod, "app", BPY_app_struct() );
 
269
        /* needs to be first so bpy_types can run */
 
270
        PyModule_AddObject(mod, "types", BPY_rna_types());
 
271
 
 
272
        /* metaclass for idprop types, bpy_types.py needs access */
 
273
        PyModule_AddObject(mod, "StructMetaPropGroup", (PyObject *)&pyrna_struct_meta_idprop_Type);
 
274
 
 
275
        /* needs to be first so bpy_types can run */
 
276
        bpy_lib_init(mod);
 
277
 
 
278
        bpy_import_test("bpy_types");
 
279
        PyModule_AddObject(mod, "data", BPY_rna_module()); /* imports bpy_types by running this */
 
280
        bpy_import_test("bpy_types");
 
281
        PyModule_AddObject(mod, "props", BPY_rna_props());      
 
282
        /* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */
 
283
        PyModule_AddObject(mod, "ops", BPY_operator_module());
 
284
        PyModule_AddObject(mod, "app", BPY_app_struct());
174
285
 
175
286
        /* bpy context */
176
 
        RNA_pointer_create(NULL, &RNA_Context, BPy_GetContext(), &ctx_ptr);
177
 
        bpy_context_module= (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ctx_ptr);
 
287
        RNA_pointer_create(NULL, &RNA_Context, (void *)BPy_GetContext(), &ctx_ptr);
 
288
        bpy_context_module = (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ctx_ptr);
178
289
        /* odd that this is needed, 1 ref on creation and another for the module
179
290
         * but without we get a crash on exit */
180
291
        Py_INCREF(bpy_context_module);
182
293
        PyModule_AddObject(mod, "context", (PyObject *)bpy_context_module);
183
294
 
184
295
        /* utility func's that have nowhere else to go */
185
 
        PyModule_AddObject(mod, meth_bpy_script_paths->ml_name, (PyObject *)PyCFunction_New(meth_bpy_script_paths, NULL));
186
 
        PyModule_AddObject(mod, meth_bpy_blend_paths->ml_name, (PyObject *)PyCFunction_New(meth_bpy_blend_paths, NULL));
 
296
        PyModule_AddObject(mod, meth_bpy_script_paths.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_script_paths, NULL));
 
297
        PyModule_AddObject(mod, meth_bpy_blend_paths.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_blend_paths, NULL));
 
298
        PyModule_AddObject(mod, meth_bpy_user_resource.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_user_resource, NULL));
 
299
        PyModule_AddObject(mod, meth_bpy_resource_path.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_resource_path, NULL));
 
300
 
 
301
        /* register funcs (bpy_rna.c) */
 
302
        PyModule_AddObject(mod, meth_bpy_register_class.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_register_class, NULL));
 
303
        PyModule_AddObject(mod, meth_bpy_unregister_class.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_unregister_class, NULL));
187
304
 
188
305
        /* add our own modules dir, this is a python package */
189
 
        bpy_import_test("bpy");
 
306
        bpy_package_py = bpy_import_test("bpy");
190
307
}