b10223207bf98f6a3e6cdee03d6c7a5c9169c75e
[blender.git] / source / blender / python / intern / bpy_props.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/intern/bpy_props.c
24  *  \ingroup pythonintern
25  */
26
27
28 #include <Python.h>
29
30 #include "RNA_types.h"
31
32 #include "bpy_props.h"
33 #include "bpy_rna.h"
34 #include "bpy_util.h"
35
36 #include "BLI_utildefines.h"
37
38 #include "BKE_idprop.h"
39
40 #include "RNA_access.h"
41 #include "RNA_define.h" /* for defining our own rna */
42 #include "RNA_enum_types.h"
43
44 #include "MEM_guardedalloc.h"
45
46 #include "../generic/py_capi_utils.h"
47
48 /* initial definition of callback slots we'll probably have more then 1 */
49 #define BPY_DATA_CB_SLOT_SIZE 1
50
51 #define BPY_DATA_CB_SLOT_UPDATE 0
52
53 extern BPy_StructRNA *bpy_context_module;
54
55 static EnumPropertyItem property_flag_items[]= {
56         {PROP_HIDDEN, "HIDDEN", 0, "Hidden", ""},
57         {PROP_SKIP_SAVE, "SKIP_SAVE", 0, "Skip Save", ""},
58         {PROP_ANIMATABLE, "ANIMATABLE", 0, "Animateable", ""},
59         {0, NULL, 0, NULL, NULL}};
60
61 static EnumPropertyItem property_flag_enum_items[]= {
62         {PROP_HIDDEN, "HIDDEN", 0, "Hidden", ""},
63         {PROP_SKIP_SAVE, "SKIP_SAVE", 0, "Skip Save", ""},
64         {PROP_ANIMATABLE, "ANIMATABLE", 0, "Animateable", ""},
65         {PROP_ENUM_FLAG, "ENUM_FLAG", 0, "Enum Flag", ""},
66         {0, NULL, 0, NULL, NULL}};
67
68 /* subtypes */
69 static EnumPropertyItem property_subtype_string_items[]= {
70         {PROP_FILEPATH, "FILE_PATH", 0, "File Path", ""},
71         {PROP_DIRPATH, "DIR_PATH", 0, "Directory Path", ""},
72         {PROP_FILENAME, "FILENAME", 0, "Filename", ""},
73         {PROP_TRANSLATE, "TRANSLATE", 0, "Translate", ""},
74
75         {PROP_NONE, "NONE", 0, "None", ""},
76         {0, NULL, 0, NULL, NULL}};
77
78 static EnumPropertyItem property_subtype_number_items[]= {
79         {PROP_UNSIGNED, "UNSIGNED", 0, "Unsigned", ""},
80         {PROP_PERCENTAGE, "PERCENTAGE", 0, "Percentage", ""},
81         {PROP_FACTOR, "FACTOR", 0, "Factor", ""},
82         {PROP_ANGLE, "ANGLE", 0, "Angle", ""},
83         {PROP_TIME, "TIME", 0, "Time", ""},
84         {PROP_DISTANCE, "DISTANCE", 0, "Distance", ""},
85
86         {PROP_NONE, "NONE", 0, "None", ""},
87         {0, NULL, 0, NULL, NULL}};
88
89 static EnumPropertyItem property_subtype_array_items[]= {
90         {PROP_COLOR, "COLOR", 0, "Color", ""},
91         {PROP_TRANSLATION, "TRANSLATION", 0, "Translation", ""},
92         {PROP_DIRECTION, "DIRECTION", 0, "Direction", ""},
93         {PROP_VELOCITY, "VELOCITY", 0, "Velocity", ""},
94         {PROP_ACCELERATION, "ACCELERATION", 0, "Acceleration", ""},
95         {PROP_MATRIX, "MATRIX", 0, "Matrix", ""},
96         {PROP_EULER, "EULER", 0, "Euler", ""},
97         {PROP_QUATERNION, "QUATERNION", 0, "Quaternion", ""},
98         {PROP_AXISANGLE, "AXISANGLE", 0, "Axis Angle", ""},
99         {PROP_XYZ, "XYZ", 0, "XYZ", ""},
100         {PROP_COLOR_GAMMA, "COLOR_GAMMA", 0, "Color Gamma", ""},
101         {PROP_LAYER, "LAYER", 0, "Layer", ""},
102
103         {PROP_NONE, "NONE", 0, "None", ""},
104         {0, NULL, 0, NULL, NULL}};
105
106 /* PyObject's */
107 static PyObject *pymeth_BoolProperty= NULL;
108 static PyObject *pymeth_BoolVectorProperty= NULL;
109 static PyObject *pymeth_IntProperty= NULL;
110 static PyObject *pymeth_IntVectorProperty= NULL;
111 static PyObject *pymeth_FloatProperty= NULL;
112 static PyObject *pymeth_FloatVectorProperty= NULL;
113 static PyObject *pymeth_StringProperty= NULL;
114 static PyObject *pymeth_EnumProperty= NULL;
115 static PyObject *pymeth_PointerProperty= NULL;
116 static PyObject *pymeth_CollectionProperty= NULL;
117 static PyObject *pymeth_RemoveProperty= NULL;
118
119 static PyObject *pyrna_struct_as_instance(PointerRNA *ptr)
120 {
121         PyObject *self= NULL;
122         /* first get self */
123         /* operators can store their own instance for later use */
124         if (ptr->data) {
125                 void **instance= RNA_struct_instance(ptr);
126
127                 if (instance) {
128                         if (*instance) {
129                                 self= *instance;
130                                 Py_INCREF(self);
131                         }
132                 }
133         }
134
135         /* in most cases this will run */
136         if (self == NULL) {
137                 self= pyrna_struct_CreatePyObject(ptr);
138         }
139
140         return self;
141 }
142
143 /* could be moved into bpy_utils */
144 static void printf_func_error(PyObject *py_func)
145 {
146         /* since we return to C code we can't leave the error */
147         PyCodeObject *f_code= (PyCodeObject *)PyFunction_GET_CODE(py_func);
148         PyErr_Print();
149         PyErr_Clear();
150
151         /* use py style error */
152         fprintf(stderr, "File \"%s\", line %d, in %s\n",
153                         _PyUnicode_AsString(f_code->co_filename),
154                         f_code->co_firstlineno,
155                         _PyUnicode_AsString(((PyFunctionObject *)py_func)->func_name)
156                         );
157 }
158
159 /* operators and classes use this so it can store the args given but defer
160  * running it until the operator runs where these values are used to setup
161  * the default args for that operator instance */
162 static PyObject *bpy_prop_deferred_return(PyObject *func, PyObject *kw)
163 {
164         PyObject *ret= PyTuple_New(2);
165         PyTuple_SET_ITEM(ret, 0, func);
166         Py_INCREF(func);
167
168         if (kw==NULL)
169                 kw= PyDict_New();
170         else
171                 Py_INCREF(kw);
172
173         PyTuple_SET_ITEM(ret, 1, kw);
174
175         return ret;
176 }
177
178 /* callbacks */
179 static void bpy_prop_update_cb(struct bContext *C, struct PointerRNA *ptr, struct PropertyRNA *prop)
180 {
181         PyGILState_STATE gilstate;
182         PyObject **py_data= (PyObject **)RNA_property_py_data_get(prop);
183         PyObject *py_func;
184         PyObject *args;
185         PyObject *self;
186         PyObject *ret;
187         const int is_write_ok= pyrna_write_check();
188
189         BLI_assert(py_data != NULL);
190
191         if (!is_write_ok) {
192                 pyrna_write_set(TRUE);
193         }
194
195         bpy_context_set(C, &gilstate);
196
197         py_func= py_data[BPY_DATA_CB_SLOT_UPDATE];
198
199         args= PyTuple_New(2);
200         self= pyrna_struct_as_instance(ptr);
201         PyTuple_SET_ITEM(args, 0, self);
202
203         PyTuple_SET_ITEM(args, 1, (PyObject *)bpy_context_module);
204         Py_INCREF(bpy_context_module);
205
206         ret= PyObject_CallObject(py_func, args);
207
208         Py_DECREF(args);
209
210         if (ret == NULL) {
211                 printf_func_error(py_func);
212         }
213         else {
214                 if (ret != Py_None) {
215                         PyErr_SetString(PyExc_ValueError, "the return value must be None");
216                         printf_func_error(py_func);
217                 }
218
219                 Py_DECREF(ret);
220         }
221
222         bpy_context_clear(C, &gilstate);
223
224         if (!is_write_ok) {
225                 pyrna_write_set(FALSE);
226         }
227 }
228
229 static int bpy_prop_callback_check(PyObject *py_func, int argcount)
230 {
231         if (py_func) {
232                 if (!PyFunction_Check(py_func)) {
233                         PyErr_Format(PyExc_TypeError,
234                                      "update keyword: expected a function type, not a %.200s",
235                                      Py_TYPE(py_func)->tp_name);
236                         return -1;
237                 }
238                 else {
239                         PyCodeObject *f_code= (PyCodeObject *)PyFunction_GET_CODE(py_func);
240                         if (f_code->co_argcount != argcount) {
241                                 PyErr_Format(PyExc_TypeError,
242                                              "update keyword: expected a function taking %d arguments, not %d",
243                                              argcount, f_code->co_argcount);
244                                 return -1;
245                         }
246                 }
247         }
248
249         return 0;
250 }
251
252
253 static int bpy_prop_callback_assign(struct PropertyRNA *prop, PyObject *update_cb)
254 {
255         /* assume this is already checked for type and arg length */
256         if (update_cb) {
257                 PyObject **py_data= MEM_callocN(sizeof(PyObject *) * BPY_DATA_CB_SLOT_SIZE, "bpy_prop_callback_assign");
258                 RNA_def_property_update_runtime(prop, (void *)bpy_prop_update_cb);
259                 py_data[BPY_DATA_CB_SLOT_UPDATE]= update_cb;
260                 RNA_def_py_data(prop, py_data);
261
262                 RNA_def_property_flag(prop, PROP_CONTEXT_PROPERTY_UPDATE);
263         }
264
265         return 0;
266 }
267
268 /* utility function we need for parsing int's in an if statement */
269 static int py_long_as_int(PyObject *py_long, int *r_int)
270 {
271         if (PyLong_CheckExact(py_long)) {
272                 *r_int= (int)PyLong_AS_LONG(py_long);
273                 return 0;
274         }
275         else {
276                 return -1;
277         }
278 }
279
280 /* this define runs at the start of each function and deals with 
281  * returning a deferred property (to be registered later) */
282 #define BPY_PROPDEF_HEAD(_func) \
283         if (PyTuple_GET_SIZE(args) == 1) { \
284                 PyObject *ret; \
285                 self= PyTuple_GET_ITEM(args, 0); \
286                 args= PyTuple_New(0); \
287                 ret= BPy_##_func(self, args, kw); \
288                 Py_DECREF(args); \
289                 return ret; \
290         } \
291         else if (PyTuple_GET_SIZE(args) > 1) { \
292                 PyErr_SetString(PyExc_ValueError, "all args must be keywords"); \
293                 return NULL; \
294         } \
295         srna= srna_from_self(self, #_func"(...):"); \
296         if (srna==NULL) { \
297                 if (PyErr_Occurred()) \
298                         return NULL; \
299                 return bpy_prop_deferred_return((void *)pymeth_##_func, kw); \
300         } \
301
302 /* terse macros for error checks shared between all funcs cant use function
303  * calls because of static strins passed to pyrna_set_to_enum_bitfield */
304 #define BPY_PROPDEF_CHECK(_func, _property_flag_items) \
305         if (id_len >= MAX_IDPROP_NAME) { \
306                 PyErr_Format(PyExc_TypeError, \
307                              #_func"(): '%.200s' too long, max length is %d", \
308                              id, MAX_IDPROP_NAME-1); \
309                 return NULL; \
310         } \
311         if (RNA_def_property_free_identifier(srna, id) == -1) { \
312                 PyErr_Format(PyExc_TypeError, \
313                              #_func"(): '%s' is defined as a non-dynamic type", \
314                              id); \
315                 return NULL; \
316         } \
317         if (pyopts && pyrna_set_to_enum_bitfield(_property_flag_items, pyopts, &opts, #_func"(options={...}):")) \
318                 return NULL; \
319
320 #define BPY_PROPDEF_SUBTYPE_CHECK(_func, _property_flag_items, _subtype) \
321         BPY_PROPDEF_CHECK(_func, _property_flag_items) \
322         if (pysubtype && RNA_enum_value_from_id(_subtype, pysubtype, &subtype)==0) { \
323                 PyErr_Format(PyExc_TypeError, \
324                              #_func"(subtype='%s'): invalid subtype", \
325                              pysubtype); \
326                 return NULL; \
327         } \
328
329
330 #define BPY_PROPDEF_NAME_DOC \
331 "   :arg name: Name used in the user interface.\n" \
332 "   :type name: string\n" \
333
334
335 #define BPY_PROPDEF_DESC_DOC \
336 "   :arg description: Text used for the tooltip and api documentation.\n" \
337 "   :type description: string\n" \
338
339
340 #define BPY_PROPDEF_UNIT_DOC \
341 "   :arg unit: Enumerator in ['NONE', 'LENGTH', 'AREA', 'VOLUME', 'ROTATION', 'TIME', 'VELOCITY', 'ACCELERATION'].\n" \
342 "   :type unit: string\n"       \
343
344
345 #define BPY_PROPDEF_UPDATE_DOC \
346 "   :arg update: function to be called when this value is modified,\n" \
347 "      This function must take 2 values (self, context) and return None.\n" \
348 "   :type update: function\n" \
349
350 #if 0
351 static int bpy_struct_id_used(StructRNA *srna, char *identifier)
352 {
353         PointerRNA ptr;
354         RNA_pointer_create(NULL, srna, NULL, &ptr);
355         return (RNA_struct_find_property(&ptr, identifier) != NULL);
356 }
357 #endif
358
359
360 /* Function that sets RNA, NOTE - self is NULL when called from python, but being abused from C so we can pass the srna along
361  * This isnt incorrect since its a python object - but be careful */
362 PyDoc_STRVAR(BPy_BoolProperty_doc,
363 ".. function:: BoolProperty(name=\"\", description=\"\", default=False, options={'ANIMATABLE'}, subtype='NONE', update=None)\n"
364 "\n"
365 "   Returns a new boolean property definition.\n"
366 "\n"
367 BPY_PROPDEF_NAME_DOC
368 BPY_PROPDEF_DESC_DOC
369 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
370 "   :type options: set\n"
371 "   :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
372 "   :type subtype: string\n"
373 BPY_PROPDEF_UPDATE_DOC
374 );
375 static PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
376 {
377         StructRNA *srna;
378
379         BPY_PROPDEF_HEAD(BoolProperty)
380
381         if (srna) {
382                 static const char *kwlist[]= {"attr", "name", "description", "default", "options", "subtype", "update", NULL};
383                 const char *id=NULL, *name="", *description="";
384                 int id_len;
385                 int def=0;
386                 PropertyRNA *prop;
387                 PyObject *pyopts= NULL;
388                 int opts=0;
389                 char *pysubtype= NULL;
390                 int subtype= PROP_NONE;
391                 PyObject *update_cb= NULL;
392
393                 if (!PyArg_ParseTupleAndKeywords(args, kw,
394                                                  "s#|ssiO!sO:BoolProperty",
395                                                  (char **)kwlist, &id, &id_len,
396                                                  &name, &description, &def,
397                                                  &PySet_Type, &pyopts, &pysubtype,
398                                                  &update_cb))
399                 {
400                         return NULL;
401                 }
402
403                 BPY_PROPDEF_SUBTYPE_CHECK(BoolProperty, property_flag_items, property_subtype_number_items)
404
405                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
406                         return NULL;
407                 }
408
409                 prop= RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
410                 RNA_def_property_boolean_default(prop, def);
411                 RNA_def_property_ui_text(prop, name, description);
412
413                 if (pyopts) {
414                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
415                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
416                 }
417                 bpy_prop_callback_assign(prop, update_cb);
418                 RNA_def_property_duplicate_pointers(srna, prop);
419         }
420
421         Py_RETURN_NONE;
422 }
423
424 PyDoc_STRVAR(BPy_BoolVectorProperty_doc,
425 ".. function:: BoolVectorProperty(name=\"\", description=\"\", default=(False, False, False), options={'ANIMATABLE'}, subtype='NONE', size=3, update=None)\n"
426 "\n"
427 "   Returns a new vector boolean property definition.\n"
428 "\n"
429 BPY_PROPDEF_NAME_DOC
430 BPY_PROPDEF_DESC_DOC
431 "   :arg default: sequence of booleans the length of *size*.\n"
432 "   :type default: sequence\n"
433 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
434 "   :type options: set\n"
435 "   :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'LAYER', 'NONE'].\n"
436 "   :type subtype: string\n"
437 "   :arg size: Vector dimensions in [1, and " STRINGIFY(PYRNA_STACK_ARRAY) "].\n"
438 "   :type size: int\n"
439 BPY_PROPDEF_UPDATE_DOC
440 );
441 static PyObject *BPy_BoolVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
442 {
443         StructRNA *srna;
444
445         BPY_PROPDEF_HEAD(BoolVectorProperty)
446
447         if (srna) {
448                 static const char *kwlist[]= {"attr", "name", "description", "default", "options", "subtype", "size", "update", NULL};
449                 const char *id=NULL, *name="", *description="";
450                 int id_len;
451                 int def[PYRNA_STACK_ARRAY]={0};
452                 int size=3;
453                 PropertyRNA *prop;
454                 PyObject *pydef= NULL;
455                 PyObject *pyopts= NULL;
456                 int opts=0;
457                 char *pysubtype= NULL;
458                 int subtype= PROP_NONE;
459                 PyObject *update_cb= NULL;
460
461                 if (!PyArg_ParseTupleAndKeywords(args, kw,
462                                                  "s#|ssOO!siO:BoolVectorProperty",
463                                                  (char **)kwlist, &id, &id_len,
464                                                  &name, &description, &pydef,
465                                                  &PySet_Type, &pyopts, &pysubtype, &size,
466                                                  &update_cb))
467                 {
468                         return NULL;
469                 }
470
471                 BPY_PROPDEF_SUBTYPE_CHECK(BoolVectorProperty, property_flag_items, property_subtype_array_items)
472
473                 if (size < 1 || size > PYRNA_STACK_ARRAY) {
474                         PyErr_Format(PyExc_TypeError, "BoolVectorProperty(size=%d): size must be between 0 and " STRINGIFY(PYRNA_STACK_ARRAY), size);
475                         return NULL;
476                 }
477
478                 if (pydef && PyC_AsArray(def, pydef, size, &PyBool_Type, FALSE, "BoolVectorProperty(default=sequence)") < 0)
479                         return NULL;
480
481                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
482                         return NULL;
483                 }
484
485                 // prop= RNA_def_boolean_array(srna, id, size, pydef ? def:NULL, name, description);
486                 prop= RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
487                 RNA_def_property_array(prop, size);
488                 if (pydef) RNA_def_property_boolean_array_default(prop, def);
489                 RNA_def_property_ui_text(prop, name, description);
490
491                 if (pyopts) {
492                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
493                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
494                 }
495                 bpy_prop_callback_assign(prop, update_cb);
496                 RNA_def_property_duplicate_pointers(srna, prop);
497         }
498         
499         Py_RETURN_NONE;
500 }
501
502 PyDoc_STRVAR(BPy_IntProperty_doc,
503 ".. function:: IntProperty(name=\"\", description=\"\", default=0, min=-sys.maxint, max=sys.maxint, soft_min=-sys.maxint, soft_max=sys.maxint, step=1, options={'ANIMATABLE'}, subtype='NONE', update=None)\n"
504 "\n"
505 "   Returns a new int property definition.\n"
506 "\n"
507 BPY_PROPDEF_NAME_DOC
508 BPY_PROPDEF_DESC_DOC
509 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
510 "   :type options: set\n"
511 "   :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
512 "   :type subtype: string\n"
513 BPY_PROPDEF_UPDATE_DOC
514 );
515 static PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
516 {
517         StructRNA *srna;
518
519         BPY_PROPDEF_HEAD(IntProperty)
520
521         if (srna) {
522                 static const char *kwlist[]= {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "options", "subtype", "update", NULL};
523                 const char *id=NULL, *name="", *description="";
524                 int id_len;
525                 int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def=0;
526                 PropertyRNA *prop;
527                 PyObject *pyopts= NULL;
528                 int opts=0;
529                 char *pysubtype= NULL;
530                 int subtype= PROP_NONE;
531                 PyObject *update_cb= NULL;
532
533                 if (!PyArg_ParseTupleAndKeywords(args, kw,
534                                                  "s#|ssiiiiiiO!sO:IntProperty",
535                                                  (char **)kwlist, &id, &id_len,
536                                                  &name, &description, &def,
537                                                  &min, &max, &soft_min, &soft_max,
538                                                  &step, &PySet_Type, &pyopts, &pysubtype,
539                                                  &update_cb))
540                 {
541                         return NULL;
542                 }
543
544                 BPY_PROPDEF_SUBTYPE_CHECK(IntProperty, property_flag_items, property_subtype_number_items)
545
546                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
547                         return NULL;
548                 }
549
550                 prop= RNA_def_property(srna, id, PROP_INT, subtype);
551                 RNA_def_property_int_default(prop, def);
552                 RNA_def_property_range(prop, min, max);
553                 RNA_def_property_ui_text(prop, name, description);
554                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, 3);
555
556                 if (pyopts) {
557                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
558                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
559                 }
560                 bpy_prop_callback_assign(prop, update_cb);
561                 RNA_def_property_duplicate_pointers(srna, prop);
562         }
563         Py_RETURN_NONE;
564 }
565
566 PyDoc_STRVAR(BPy_IntVectorProperty_doc,
567 ".. function:: IntVectorProperty(name=\"\", description=\"\", default=(0, 0, 0), min=-sys.maxint, max=sys.maxint, soft_min=-sys.maxint, soft_max=sys.maxint, options={'ANIMATABLE'}, subtype='NONE', size=3, update=None)\n"
568 "\n"
569 "   Returns a new vector int property definition.\n"
570 "\n"
571 BPY_PROPDEF_NAME_DOC
572 BPY_PROPDEF_DESC_DOC
573 "   :arg default: sequence of ints the length of *size*.\n"
574 "   :type default: sequence\n"
575 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
576 "   :type options: set\n"
577 "   :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'LAYER', 'NONE'].\n"
578 "   :type subtype: string\n"
579 "   :arg size: Vector dimensions in [1, and " STRINGIFY(PYRNA_STACK_ARRAY) "].\n"
580 "   :type size: int\n"
581 BPY_PROPDEF_UPDATE_DOC
582 );
583 static PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
584 {
585         StructRNA *srna;
586
587         BPY_PROPDEF_HEAD(IntVectorProperty)
588
589         if (srna) {
590                 static const char *kwlist[]= {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "options", "subtype", "size", "update", NULL};
591                 const char *id=NULL, *name="", *description="";
592                 int id_len;
593                 int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def[PYRNA_STACK_ARRAY]={0};
594                 int size=3;
595                 PropertyRNA *prop;
596                 PyObject *pydef= NULL;
597                 PyObject *pyopts= NULL;
598                 int opts=0;
599                 char *pysubtype= NULL;
600                 int subtype= PROP_NONE;
601                 PyObject *update_cb= NULL;
602
603                 if (!PyArg_ParseTupleAndKeywords(args, kw,
604                                                  "s#|ssOiiiiiO!siO:IntVectorProperty",
605                                                  (char **)kwlist, &id, &id_len,
606                                                  &name, &description, &pydef,
607                                                  &min, &max, &soft_min, &soft_max,
608                                                  &step, &PySet_Type, &pyopts,
609                                                  &pysubtype, &size,
610                                                  &update_cb))
611                 {
612                         return NULL;
613                 }
614
615                 BPY_PROPDEF_SUBTYPE_CHECK(IntVectorProperty, property_flag_items, property_subtype_array_items)
616
617                 if (size < 1 || size > PYRNA_STACK_ARRAY) {
618                         PyErr_Format(PyExc_TypeError, "IntVectorProperty(size=%d): size must be between 0 and " STRINGIFY(PYRNA_STACK_ARRAY), size);
619                         return NULL;
620                 }
621
622                 if (pydef && PyC_AsArray(def, pydef, size, &PyLong_Type, FALSE, "IntVectorProperty(default=sequence)") < 0)
623                         return NULL;
624
625                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
626                         return NULL;
627                 }
628
629                 prop= RNA_def_property(srna, id, PROP_INT, subtype);
630                 RNA_def_property_array(prop, size);
631                 if (pydef) RNA_def_property_int_array_default(prop, def);
632                 RNA_def_property_range(prop, min, max);
633                 RNA_def_property_ui_text(prop, name, description);
634                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, 3);
635
636                 if (pyopts) {
637                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
638                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
639                 }
640                 bpy_prop_callback_assign(prop, update_cb);
641                 RNA_def_property_duplicate_pointers(srna, prop);
642         }
643         Py_RETURN_NONE;
644 }
645
646
647 PyDoc_STRVAR(BPy_FloatProperty_doc,
648 ".. function:: FloatProperty(name=\"\", description=\"\", default=0.0, min=sys.float_info.min, max=sys.float_info.max, soft_min=sys.float_info.min, soft_max=sys.float_info.max, step=3, precision=2, options={'ANIMATABLE'}, subtype='NONE', unit='NONE', update=None)\n"
649 "\n"
650 "   Returns a new float property definition.\n"
651 "\n"
652 BPY_PROPDEF_NAME_DOC
653 BPY_PROPDEF_DESC_DOC
654 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
655 "   :type options: set\n"
656 "   :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
657 "   :type subtype: string\n"
658 BPY_PROPDEF_UNIT_DOC
659 BPY_PROPDEF_UPDATE_DOC
660 );
661 static PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
662 {
663         StructRNA *srna;
664
665         BPY_PROPDEF_HEAD(FloatProperty)
666
667         if (srna) {
668                 static const char *kwlist[]= {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", "unit", "update", NULL};
669                 const char *id=NULL, *name="", *description="";
670                 int id_len;
671                 float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def=0.0f;
672                 int precision= 2;
673                 PropertyRNA *prop;
674                 PyObject *pyopts= NULL;
675                 int opts=0;
676                 char *pysubtype= NULL;
677                 int subtype= PROP_NONE;
678                 char *pyunit= NULL;
679                 int unit= PROP_UNIT_NONE;
680                 PyObject *update_cb= NULL;
681
682                 if (!PyArg_ParseTupleAndKeywords(args, kw,
683                                                  "s#|ssffffffiO!ssO:FloatProperty",
684                                                  (char **)kwlist, &id, &id_len,
685                                                  &name, &description, &def,
686                                                  &min, &max, &soft_min, &soft_max,
687                                                  &step, &precision, &PySet_Type,
688                                                  &pyopts, &pysubtype, &pyunit,
689                                                  &update_cb))
690                 {
691                         return NULL;
692                 }
693
694                 BPY_PROPDEF_SUBTYPE_CHECK(FloatProperty, property_flag_items, property_subtype_number_items)
695
696                 if (pyunit && RNA_enum_value_from_id(property_unit_items, pyunit, &unit)==0) {
697                         PyErr_Format(PyExc_TypeError, "FloatProperty(unit='%s'): invalid unit", pyunit);
698                         return NULL;
699                 }
700
701                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
702                         return NULL;
703                 }
704
705                 prop= RNA_def_property(srna, id, PROP_FLOAT, subtype | unit);
706                 RNA_def_property_float_default(prop, def);
707                 RNA_def_property_range(prop, min, max);
708                 RNA_def_property_ui_text(prop, name, description);
709                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, precision);
710
711                 if (pyopts) {
712                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
713                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
714                 }
715                 bpy_prop_callback_assign(prop, update_cb);
716                 RNA_def_property_duplicate_pointers(srna, prop);
717         }
718         Py_RETURN_NONE;
719 }
720
721 PyDoc_STRVAR(BPy_FloatVectorProperty_doc,
722 ".. function:: FloatVectorProperty(name=\"\", description=\"\", default=(0.0, 0.0, 0.0), min=sys.float_info.min, max=sys.float_info.max, soft_min=sys.float_info.min, soft_max=sys.float_info.max, step=3, precision=2, options={'ANIMATABLE'}, subtype='NONE', size=3, update=None)\n"
723 "\n"
724 "   Returns a new vector float property definition.\n"
725 "\n"
726 BPY_PROPDEF_NAME_DOC
727 BPY_PROPDEF_DESC_DOC
728 "   :arg default: sequence of floats the length of *size*.\n"
729 "   :type default: sequence\n"
730 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
731 "   :type options: set\n"
732 "   :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'LAYER', 'NONE'].\n"
733 "   :type subtype: string\n"
734 BPY_PROPDEF_UNIT_DOC
735 "   :arg size: Vector dimensions in [1, and " STRINGIFY(PYRNA_STACK_ARRAY) "].\n"
736 "   :type size: int\n"
737 BPY_PROPDEF_UPDATE_DOC
738 );
739 static PyObject *BPy_FloatVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
740 {
741         StructRNA *srna;
742
743         BPY_PROPDEF_HEAD(FloatVectorProperty)
744
745         if (srna) {
746                 static const char *kwlist[]= {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", "unit", "size", "update", NULL};
747                 const char *id=NULL, *name="", *description="";
748                 int id_len;
749                 float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def[PYRNA_STACK_ARRAY]={0.0f};
750                 int precision= 2, size=3;
751                 PropertyRNA *prop;
752                 PyObject *pydef= NULL;
753                 PyObject *pyopts= NULL;
754                 int opts=0;
755                 char *pysubtype= NULL;
756                 int subtype= PROP_NONE;
757                 char *pyunit= NULL;
758                 int unit= PROP_UNIT_NONE;
759                 PyObject *update_cb= NULL;
760
761                 if (!PyArg_ParseTupleAndKeywords(args, kw,
762                                                  "s#|ssOfffffiO!ssiO:FloatVectorProperty",
763                                                  (char **)kwlist, &id, &id_len,
764                                                  &name, &description, &pydef,
765                                                  &min, &max, &soft_min, &soft_max,
766                                                  &step, &precision, &PySet_Type,
767                                                  &pyopts, &pysubtype, &pyunit, &size,
768                                                  &update_cb))
769                 {
770                         return NULL;
771                 }
772
773                 BPY_PROPDEF_SUBTYPE_CHECK(FloatVectorProperty, property_flag_items, property_subtype_array_items)
774
775                 if (pyunit && RNA_enum_value_from_id(property_unit_items, pyunit, &unit)==0) {
776                         PyErr_Format(PyExc_TypeError, "FloatVectorProperty(unit='%s'): invalid unit", pyunit);
777                         return NULL;
778                 }
779
780                 if (size < 1 || size > PYRNA_STACK_ARRAY) {
781                         PyErr_Format(PyExc_TypeError, "FloatVectorProperty(size=%d): size must be between 0 and " STRINGIFY(PYRNA_STACK_ARRAY), size);
782                         return NULL;
783                 }
784
785                 if (pydef && PyC_AsArray(def, pydef, size, &PyFloat_Type, FALSE, "FloatVectorProperty(default=sequence)") < 0)
786                         return NULL;
787
788                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
789                         return NULL;
790                 }
791
792                 prop= RNA_def_property(srna, id, PROP_FLOAT, subtype | unit);
793                 RNA_def_property_array(prop, size);
794                 if (pydef) RNA_def_property_float_array_default(prop, def);
795                 RNA_def_property_range(prop, min, max);
796                 RNA_def_property_ui_text(prop, name, description);
797                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, precision);
798
799                 if (pyopts) {
800                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
801                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
802                 }
803                 bpy_prop_callback_assign(prop, update_cb);
804                 RNA_def_property_duplicate_pointers(srna, prop);
805         }
806         Py_RETURN_NONE;
807 }
808
809 PyDoc_STRVAR(BPy_StringProperty_doc,
810 ".. function:: StringProperty(name=\"\", description=\"\", default=\"\", maxlen=0, options={'ANIMATABLE'}, subtype='NONE', update=None)\n"
811 "\n"
812 "   Returns a new string property definition.\n"
813 "\n"
814 BPY_PROPDEF_NAME_DOC
815 BPY_PROPDEF_DESC_DOC
816 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
817 "   :type options: set\n"
818 "   :arg subtype: Enumerator in ['FILE_PATH', 'DIR_PATH', 'FILENAME', 'NONE'].\n"
819 "   :type subtype: string\n"
820 BPY_PROPDEF_UPDATE_DOC
821 );
822 static PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw)
823 {
824         StructRNA *srna;
825
826         BPY_PROPDEF_HEAD(StringProperty)
827
828         if (srna) {
829                 static const char *kwlist[]= {"attr", "name", "description", "default", "maxlen", "options", "subtype", "update", NULL};
830                 const char *id=NULL, *name="", *description="", *def="";
831                 int id_len;
832                 int maxlen=0;
833                 PropertyRNA *prop;
834                 PyObject *pyopts= NULL;
835                 int opts=0;
836                 char *pysubtype= NULL;
837                 int subtype= PROP_NONE;
838                 PyObject *update_cb= NULL;
839
840                 if (!PyArg_ParseTupleAndKeywords(args, kw,
841                                                  "s#|sssiO!sO:StringProperty",
842                                                  (char **)kwlist, &id, &id_len,
843                                                  &name, &description, &def,
844                                                  &maxlen, &PySet_Type, &pyopts, &pysubtype,
845                                                  &update_cb))
846                 {
847                         return NULL;
848                 }
849
850                 BPY_PROPDEF_SUBTYPE_CHECK(StringProperty, property_flag_items, property_subtype_string_items)
851
852                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
853                         return NULL;
854                 }
855
856                 prop= RNA_def_property(srna, id, PROP_STRING, subtype);
857                 if (maxlen != 0) RNA_def_property_string_maxlength(prop, maxlen + 1); /* +1 since it includes null terminator */
858                 if (def) RNA_def_property_string_default(prop, def);
859                 RNA_def_property_ui_text(prop, name, description);
860
861                 if (pyopts) {
862                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
863                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
864                 }
865                 bpy_prop_callback_assign(prop, update_cb);
866                 RNA_def_property_duplicate_pointers(srna, prop);
867         }
868         Py_RETURN_NONE;
869 }
870
871 #if 0
872 /* copies orig to buf, then sets orig to buf, returns copy length */
873 static size_t strswapbufcpy(char *buf, const char **orig)
874 {
875         const char *src= *orig;
876         char *dst= buf;
877         size_t i= 0;
878         *orig= buf;
879         while ((*dst= *src)) { dst++; src++; i++; }
880         return i + 1; /* include '\0' */
881 }
882 #endif
883
884 static EnumPropertyItem *enum_items_from_py(PyObject *seq_fast, PyObject *def, int *defvalue, const short is_enum_flag)
885 {
886         EnumPropertyItem *items;
887         PyObject *item;
888         const Py_ssize_t seq_len= PySequence_Fast_GET_SIZE(seq_fast);
889         Py_ssize_t totbuf= 0;
890         int i;
891         short def_used= 0;
892         const char *def_cmp= NULL;
893
894         if (is_enum_flag) {
895                 if (seq_len > RNA_ENUM_BITFLAG_SIZE) {
896                         PyErr_SetString(PyExc_TypeError, "EnumProperty(...): maximum " STRINGIFY(RNA_ENUM_BITFLAG_SIZE) " members for a ENUM_FLAG type property");
897                         return NULL;
898                 }
899                 if (def && !PySet_Check(def)) {
900                         PyErr_Format(PyExc_TypeError,
901                                      "EnumProperty(...): default option must be a 'set' "
902                                      "type when ENUM_FLAG is enabled, not a '%.200s'",
903                                      Py_TYPE(def)->tp_name);
904                         return NULL;
905                 }
906         }
907         else {
908                 if (def) {
909                         def_cmp= _PyUnicode_AsString(def);
910                         if (def_cmp==NULL) {
911                                 PyErr_Format(PyExc_TypeError,
912                                              "EnumProperty(...): default option must be a 'str' "
913                                              "type when ENUM_FLAG is disabled, not a '%.200s'",
914                                              Py_TYPE(def)->tp_name);
915                                 return NULL;
916                         }
917                 }
918         }
919
920         /* blank value */
921         *defvalue= 0;
922
923         items= MEM_callocN(sizeof(EnumPropertyItem) * (seq_len + 1), "enum_items_from_py1");
924
925         for (i=0; i<seq_len; i++) {
926                 EnumPropertyItem tmp= {0, "", 0, "", ""};
927                 Py_ssize_t item_size;
928                 Py_ssize_t id_str_size;
929                 Py_ssize_t name_str_size;
930                 Py_ssize_t desc_str_size;
931
932                 item= PySequence_Fast_GET_ITEM(seq_fast, i);
933
934                 if (    (PyTuple_CheckExact(item)) &&
935                         (item_size= PyTuple_GET_SIZE(item)) &&
936                         (item_size == 3 || item_size == 4) &&
937                         (tmp.identifier=  _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 0), &id_str_size)) &&
938                         (tmp.name=        _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 1), &name_str_size)) &&
939                         (tmp.description= _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 2), &desc_str_size)) &&
940                         (item_size < 4 || py_long_as_int(PyTuple_GET_ITEM(item, 3), &tmp.value) != -1) /* TODO, number isnt ensured to be unique from the script author */
941                 ) {
942                         if (is_enum_flag) {
943                                 if (item_size < 4) {
944                                         tmp.value= 1<<i;
945                                 }
946
947                                 if (def && PySet_Contains(def, PyTuple_GET_ITEM(item, 0))) {
948                                         *defvalue |= tmp.value;
949                                         def_used++;
950                                 }
951                         }
952                         else {
953                                 if (item_size < 4) {
954                                         tmp.value= i;
955                                 }
956
957                                 if (def && def_used == 0 && strcmp(def_cmp, tmp.identifier)==0) {
958                                         *defvalue= tmp.value;
959                                         def_used++; /* only ever 1 */
960                                 }
961                         }
962
963                         items[i]= tmp;
964
965                         /* calculate combine string length */
966                         totbuf += id_str_size + name_str_size + desc_str_size + 3; /* 3 is for '\0's */
967                 }
968                 else {
969                         MEM_freeN(items);
970                         PyErr_SetString(PyExc_TypeError,
971                                         "EnumProperty(...): expected an tuple containing "
972                                         "(identifier, name description) and optionally a "
973                                         "unique number");
974                         return NULL;
975                 }
976
977         }
978
979         if (is_enum_flag) {
980                 /* strict check that all set members were used */
981                 if (def && def_used != PySet_GET_SIZE(def)) {
982                         MEM_freeN(items);
983
984                         PyErr_Format(PyExc_TypeError,
985                                      "EnumProperty(..., default={...}): set has %d unused member(s)",
986                                      PySet_GET_SIZE(def) - def_used);
987                         return NULL;
988                 }
989         }
990         else {
991                 if (def && def_used == 0) {
992                         MEM_freeN(items);
993
994                         PyErr_Format(PyExc_TypeError,
995                                      "EnumProperty(..., default=\'%s\'): not found in enum members",
996                                      def_cmp);
997                         return NULL;
998                 }
999         }
1000
1001         /* disabled duplicating strings because the array can still be freed and
1002          * the strings from it referenced, for now we can't support dynamically
1003          * created strings from python. */
1004 #if 0
1005         /* this would all work perfectly _but_ the python strings may be freed
1006          * immediately after use, so we need to duplicate them, ugh.
1007          * annoying because it works most of the time without this. */
1008         {
1009                 EnumPropertyItem *items_dup= MEM_mallocN((sizeof(EnumPropertyItem) * (seq_len + 1)) + (sizeof(char) * totbuf), "enum_items_from_py2");
1010                 EnumPropertyItem *items_ptr= items_dup;
1011                 char *buf= ((char *)items_dup) + (sizeof(EnumPropertyItem) * (seq_len + 1));
1012                 memcpy(items_dup, items, sizeof(EnumPropertyItem) * (seq_len + 1));
1013                 for (i=0; i<seq_len; i++, items_ptr++) {
1014                         buf += strswapbufcpy(buf, &items_ptr->identifier);
1015                         buf += strswapbufcpy(buf, &items_ptr->name);
1016                         buf += strswapbufcpy(buf, &items_ptr->description);
1017                 }
1018                 MEM_freeN(items);
1019                 items=items_dup;
1020         }
1021         /* end string duplication */
1022 #endif
1023
1024         return items;
1025 }
1026
1027 static EnumPropertyItem *bpy_props_enum_itemf(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, int *free)
1028 {
1029         PyGILState_STATE gilstate;
1030
1031         PyObject *py_func= RNA_property_enum_py_data_get(prop);
1032         PyObject *self= NULL;
1033         PyObject *args;
1034         PyObject *items; /* returned from the function call */
1035
1036         EnumPropertyItem *eitems= NULL;
1037         int err= 0;
1038
1039         bpy_context_set(C, &gilstate);
1040
1041         args= PyTuple_New(2);
1042         self= pyrna_struct_as_instance(ptr);
1043         PyTuple_SET_ITEM(args, 0, self);
1044
1045         /* now get the context */
1046         PyTuple_SET_ITEM(args, 1, (PyObject *)bpy_context_module);
1047         Py_INCREF(bpy_context_module);
1048
1049         items= PyObject_CallObject(py_func, args);
1050
1051         Py_DECREF(args);
1052
1053         if (items==NULL) {
1054                 err= -1;
1055         }
1056         else {
1057                 PyObject *items_fast;
1058                 int defvalue_dummy=0;
1059
1060                 if (!(items_fast= PySequence_Fast(items, "EnumProperty(...): return value from the callback was not a sequence"))) {
1061                         err= -1;
1062                 }
1063                 else {
1064                         eitems= enum_items_from_py(items_fast, NULL, &defvalue_dummy, (RNA_property_flag(prop) & PROP_ENUM_FLAG)!=0);
1065
1066                         Py_DECREF(items_fast);
1067
1068                         if (!eitems) {
1069                                 err= -1;
1070                         }
1071                 }
1072
1073                 Py_DECREF(items);
1074         }
1075
1076         if (err != -1) { /* worked */
1077                 *free= 1;
1078         }
1079         else {
1080                 printf_func_error(py_func);
1081
1082                 eitems= DummyRNA_NULL_items;
1083         }
1084
1085
1086         bpy_context_clear(C, &gilstate);
1087         return eitems;
1088 }
1089
1090 PyDoc_STRVAR(BPy_EnumProperty_doc,
1091 ".. function:: EnumProperty(items, name=\"\", description=\"\", default=\"\", options={'ANIMATABLE'}, update=None)\n"
1092 "\n"
1093 "   Returns a new enumerator property definition.\n"
1094 "\n"
1095 BPY_PROPDEF_NAME_DOC
1096 BPY_PROPDEF_DESC_DOC
1097 "   :arg default: The default value for this enum, A string when *ENUM_FLAG*\n"
1098 "      is disabled otherwise a set which may only contain string identifiers\n"
1099 "      used in *items*.\n"
1100 "   :type default: string or set\n"
1101 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE', 'ENUM_FLAG'].\n"
1102 "   :type options: set\n"
1103 "   :arg items: sequence of enum items formatted:\n"
1104 "      [(identifier, name, description, number), ...] where the identifier is used\n"
1105 "      for python access and other values are used for the interface.\n"
1106 "      Note the item is optional.\n"
1107 "      For dynamic values a callback can be passed which returns a list in\n"
1108 "      the same format as the static list.\n"
1109 "      This function must take 2 arguments (self, context)\n"
1110 "   :type items: sequence of string triplets or a function\n"
1111 BPY_PROPDEF_UPDATE_DOC
1112 );
1113 static PyObject *BPy_EnumProperty(PyObject *self, PyObject *args, PyObject *kw)
1114 {
1115         StructRNA *srna;
1116
1117         BPY_PROPDEF_HEAD(EnumProperty)
1118         
1119         if (srna) {
1120                 static const char *kwlist[]= {"attr", "items", "name", "description", "default", "options", "update", NULL};
1121                 const char *id=NULL, *name="", *description="";
1122                 PyObject *def= NULL;
1123                 int id_len;
1124                 int defvalue=0;
1125                 PyObject *items, *items_fast;
1126                 EnumPropertyItem *eitems;
1127                 PropertyRNA *prop;
1128                 PyObject *pyopts= NULL;
1129                 int opts=0;
1130                 short is_itemf= FALSE;
1131                 PyObject *update_cb= NULL;
1132
1133                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1134                                                  "s#O|ssOO!O:EnumProperty",
1135                                                  (char **)kwlist, &id, &id_len,
1136                                                  &items, &name, &description,
1137                                                  &def, &PySet_Type, &pyopts,
1138                                                  &update_cb))
1139                 {
1140                         return NULL;
1141                 }
1142
1143                 BPY_PROPDEF_CHECK(EnumProperty, property_flag_enum_items)
1144
1145                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
1146                         return NULL;
1147                 }
1148
1149                 /* items can be a list or a callable */
1150                 if (PyFunction_Check(items)) { /* dont use PyCallable_Check because we need the function code for errors */
1151                         PyCodeObject *f_code= (PyCodeObject *)PyFunction_GET_CODE(items);
1152                         if (f_code->co_argcount != 2) {
1153                                 PyErr_Format(PyExc_ValueError,
1154                                              "EnumProperty(...): expected 'items' function to take 2 arguments, not %d",
1155                                              f_code->co_argcount);
1156                                 return NULL;
1157                         }
1158
1159                         if (def) {
1160                                 /* note, using type error here is odd but python does this for invalid arguments */
1161                                 PyErr_SetString(PyExc_TypeError,
1162                                                 "EnumProperty(...): 'default' can't be set when 'items' is a function");
1163                                 return NULL;
1164                         }
1165
1166                         is_itemf= TRUE;
1167                         eitems= DummyRNA_NULL_items;
1168                 }
1169                 else {
1170                         if (!(items_fast= PySequence_Fast(items, "EnumProperty(...): expected a sequence of tuples for the enum items or a function"))) {
1171                                 return NULL;
1172                         }
1173
1174                         eitems= enum_items_from_py(items_fast, def, &defvalue, (opts & PROP_ENUM_FLAG)!=0);
1175
1176                         Py_DECREF(items_fast);
1177
1178                         if (!eitems) {
1179                                 return NULL;
1180                         }
1181                 }
1182
1183                 if (opts & PROP_ENUM_FLAG)  prop= RNA_def_enum_flag(srna, id, eitems, defvalue, name, description);
1184                 else                        prop= RNA_def_enum(srna, id, eitems, defvalue, name, description);
1185
1186                 if (is_itemf) {
1187                         RNA_def_enum_funcs(prop, bpy_props_enum_itemf);
1188                         RNA_def_enum_py_data(prop, (void *)items);
1189                         /* Py_INCREF(items); */ /* watch out!, if user is tricky they can probably crash blender if they manage to free the callback, take care! */
1190                 }
1191
1192                 if (pyopts) {
1193                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
1194                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
1195                 }
1196                 bpy_prop_callback_assign(prop, update_cb);
1197                 RNA_def_property_duplicate_pointers(srna, prop);
1198
1199                 if (is_itemf == FALSE) {
1200                         MEM_freeN(eitems);
1201                 }
1202         }
1203         Py_RETURN_NONE;
1204 }
1205
1206 static StructRNA *pointer_type_from_py(PyObject *value, const char *error_prefix)
1207 {
1208         StructRNA *srna;
1209
1210         srna= srna_from_self(value, "");
1211         if (!srna) {
1212                 if (PyErr_Occurred()) {
1213                         PyObject *msg= PyC_ExceptionBuffer();
1214                         const char *msg_char= _PyUnicode_AsString(msg);
1215                         PyErr_Format(PyExc_TypeError,
1216                                      "%.200s expected an RNA type derived from PropertyGroup, failed with: %s",
1217                                      error_prefix, msg_char);
1218                         Py_DECREF(msg);
1219                 }
1220                 else {
1221                         PyErr_Format(PyExc_TypeError,
1222                                      "%.200s expected an RNA type derived from PropertyGroup, failed with type '%s'",
1223                                      error_prefix, Py_TYPE(value)->tp_name);
1224                 }
1225                 return NULL;
1226         }
1227
1228         if (!RNA_struct_is_a(srna, &RNA_PropertyGroup)) {
1229                 PyErr_Format(PyExc_TypeError,
1230                              "%.200s expected an RNA type derived from PropertyGroup",
1231                              error_prefix);
1232                 return NULL;
1233         }
1234
1235         return srna;
1236 }
1237
1238 PyDoc_STRVAR(BPy_PointerProperty_doc,
1239 ".. function:: PointerProperty(type=\"\", description=\"\", options={'ANIMATABLE'}, update=None)\n"
1240 "\n"
1241 "   Returns a new pointer property definition.\n"
1242 "\n"
1243 "   :arg type: A subclass of :class:`bpy.types.PropertyGroup`.\n"
1244 "   :type type: class\n"
1245 BPY_PROPDEF_NAME_DOC
1246 BPY_PROPDEF_DESC_DOC
1247 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
1248 "   :type options: set\n"
1249 BPY_PROPDEF_UPDATE_DOC
1250 );
1251 static PyObject *BPy_PointerProperty(PyObject *self, PyObject *args, PyObject *kw)
1252 {
1253         StructRNA *srna;
1254
1255         BPY_PROPDEF_HEAD(PointerProperty)
1256
1257         if (srna) {
1258                 static const char *kwlist[]= {"attr", "type", "name", "description", "options", "update", NULL};
1259                 const char *id=NULL, *name="", *description="";
1260                 int id_len;
1261                 PropertyRNA *prop;
1262                 StructRNA *ptype;
1263                 PyObject *type= Py_None;
1264                 PyObject *pyopts= NULL;
1265                 int opts=0;
1266                 PyObject *update_cb= NULL;
1267
1268                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1269                                                  "s#O|ssO!O:PointerProperty",
1270                                                  (char **)kwlist, &id, &id_len,
1271                                                  &type, &name, &description,
1272                                                  &PySet_Type, &pyopts,
1273                                                  &update_cb))
1274                 {
1275                         return NULL;
1276                 }
1277
1278                 BPY_PROPDEF_CHECK(PointerProperty, property_flag_items)
1279
1280                 ptype= pointer_type_from_py(type, "PointerProperty(...):");
1281                 if (!ptype)
1282                         return NULL;
1283
1284                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
1285                         return NULL;
1286                 }
1287
1288                 prop= RNA_def_pointer_runtime(srna, id, ptype, name, description);
1289                 if (pyopts) {
1290                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
1291                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
1292                 }
1293                 bpy_prop_callback_assign(prop, update_cb);
1294                 RNA_def_property_duplicate_pointers(srna, prop);
1295         }
1296         Py_RETURN_NONE;
1297 }
1298
1299 PyDoc_STRVAR(BPy_CollectionProperty_doc,
1300 ".. function:: CollectionProperty(items, type=\"\", description=\"\", default=\"\", options={'ANIMATABLE'})\n"
1301 "\n"
1302 "   Returns a new collection property definition.\n"
1303 "\n"
1304 "   :arg type: A subclass of :class:`bpy.types.PropertyGroup`.\n"
1305 "   :type type: class\n"
1306 BPY_PROPDEF_NAME_DOC
1307 BPY_PROPDEF_DESC_DOC
1308 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
1309 "   :type options: set\n"
1310 );
1311 static PyObject *BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw)
1312 {
1313         StructRNA *srna;
1314
1315         BPY_PROPDEF_HEAD(CollectionProperty)
1316
1317         if (srna) {
1318                 static const char *kwlist[]= {"attr", "type", "name", "description", "options", NULL};
1319                 const char *id=NULL, *name="", *description="";
1320                 int id_len;
1321                 PropertyRNA *prop;
1322                 StructRNA *ptype;
1323                 PyObject *type= Py_None;
1324                 PyObject *pyopts= NULL;
1325                 int opts=0;
1326
1327                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1328                                                  "s#O|ssO!:CollectionProperty",
1329                                                  (char **)kwlist, &id, &id_len,
1330                                                  &type, &name, &description,
1331                                                  &PySet_Type, &pyopts))
1332                 {
1333                         return NULL;
1334                 }
1335
1336                 BPY_PROPDEF_CHECK(CollectionProperty, property_flag_items)
1337
1338                 ptype= pointer_type_from_py(type, "CollectionProperty(...):");
1339                 if (!ptype)
1340                         return NULL;
1341
1342                 prop= RNA_def_collection_runtime(srna, id, ptype, name, description);
1343                 if (pyopts) {
1344                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
1345                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
1346                 }
1347                 RNA_def_property_duplicate_pointers(srna, prop);
1348         }
1349         Py_RETURN_NONE;
1350 }
1351
1352 PyDoc_STRVAR(BPy_RemoveProperty_doc,
1353 ".. function:: RemoveProperty(attr)\n"
1354 "\n"
1355 "   Removes a dynamically defined property.\n"
1356 "\n"
1357 "   :arg attr: Property name.\n"
1358 "   :type attr: string\n"
1359 );
1360 static PyObject *BPy_RemoveProperty(PyObject *self, PyObject *args, PyObject *kw)
1361 {
1362         StructRNA *srna;
1363
1364         if (PyTuple_GET_SIZE(args) == 1) {
1365                 PyObject *ret;
1366                 self= PyTuple_GET_ITEM(args, 0);
1367                 args= PyTuple_New(0);
1368                 ret= BPy_RemoveProperty(self, args, kw);
1369                 Py_DECREF(args);
1370                 return ret;     
1371         }
1372         else if (PyTuple_GET_SIZE(args) > 1) {
1373                 PyErr_SetString(PyExc_ValueError, "all args must be keywords"); \
1374                 return NULL;
1375         }
1376
1377         srna= srna_from_self(self, "RemoveProperty(...):");
1378         if (srna==NULL && PyErr_Occurred()) {
1379                 return NULL; /* self's type was compatible but error getting the srna */
1380         }
1381         else if (srna==NULL) {
1382                 PyErr_SetString(PyExc_TypeError, "RemoveProperty(): struct rna not available for this type");
1383                 return NULL;
1384         }
1385         else {
1386                 static const char *kwlist[]= {"attr", NULL};
1387                 
1388                 char *id=NULL;
1389
1390                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1391                                                  "s:RemoveProperty",
1392                                                  (char **)kwlist, &id))
1393                 {
1394                         return NULL;
1395                 }
1396
1397                 if (RNA_def_property_free_identifier(srna, id) != 1) {
1398                         PyErr_Format(PyExc_TypeError, "RemoveProperty(): '%s' not a defined dynamic property", id);
1399                         return NULL;
1400                 }
1401         }
1402         Py_RETURN_NONE;
1403 }
1404
1405 static struct PyMethodDef props_methods[]= {
1406         {"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS|METH_KEYWORDS, BPy_BoolProperty_doc},
1407         {"BoolVectorProperty", (PyCFunction)BPy_BoolVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_BoolVectorProperty_doc},
1408         {"IntProperty", (PyCFunction)BPy_IntProperty, METH_VARARGS|METH_KEYWORDS, BPy_IntProperty_doc},
1409         {"IntVectorProperty", (PyCFunction)BPy_IntVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_IntVectorProperty_doc},
1410         {"FloatProperty", (PyCFunction)BPy_FloatProperty, METH_VARARGS|METH_KEYWORDS, BPy_FloatProperty_doc},
1411         {"FloatVectorProperty", (PyCFunction)BPy_FloatVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_FloatVectorProperty_doc},
1412         {"StringProperty", (PyCFunction)BPy_StringProperty, METH_VARARGS|METH_KEYWORDS, BPy_StringProperty_doc},
1413         {"EnumProperty", (PyCFunction)BPy_EnumProperty, METH_VARARGS|METH_KEYWORDS, BPy_EnumProperty_doc},
1414         {"PointerProperty", (PyCFunction)BPy_PointerProperty, METH_VARARGS|METH_KEYWORDS, BPy_PointerProperty_doc},
1415         {"CollectionProperty", (PyCFunction)BPy_CollectionProperty, METH_VARARGS|METH_KEYWORDS, BPy_CollectionProperty_doc},
1416
1417         {"RemoveProperty", (PyCFunction)BPy_RemoveProperty, METH_VARARGS|METH_KEYWORDS, BPy_RemoveProperty_doc},
1418         {NULL, NULL, 0, NULL}
1419 };
1420
1421 static struct PyModuleDef props_module= {
1422         PyModuleDef_HEAD_INIT,
1423         "bpy.props",
1424         "This module defines properties to extend blenders internal data, the result of these functions"
1425         " is used to assign properties to classes registered with blender and can't be used directly.",
1426         -1,/* multiple "initialization" just copies the module dict. */
1427         props_methods,
1428         NULL, NULL, NULL, NULL
1429 };
1430
1431 PyObject *BPY_rna_props(void)
1432 {
1433         PyObject *submodule;
1434         PyObject *submodule_dict;
1435         
1436         submodule= PyModule_Create(&props_module);
1437         PyDict_SetItemString(PyImport_GetModuleDict(), props_module.m_name, submodule);
1438
1439         /* INCREF since its its assumed that all these functions return the
1440          * module with a new ref like PyDict_New, since they are passed to
1441          * PyModule_AddObject which steals a ref */
1442         Py_INCREF(submodule);
1443         
1444         /* api needs the PyObjects internally */
1445         submodule_dict= PyModule_GetDict(submodule);
1446
1447 #define ASSIGN_STATIC(_name) pymeth_##_name= PyDict_GetItemString(submodule_dict, #_name)
1448
1449         ASSIGN_STATIC(BoolProperty);
1450         ASSIGN_STATIC(BoolVectorProperty);
1451         ASSIGN_STATIC(IntProperty);
1452         ASSIGN_STATIC(IntVectorProperty);
1453         ASSIGN_STATIC(FloatProperty);
1454         ASSIGN_STATIC(FloatVectorProperty);
1455         ASSIGN_STATIC(StringProperty);
1456         ASSIGN_STATIC(EnumProperty);
1457         ASSIGN_STATIC(PointerProperty);
1458         ASSIGN_STATIC(CollectionProperty);
1459         ASSIGN_STATIC(RemoveProperty);
1460         
1461         return submodule;
1462 }