cfd2e5556a1a5ca827176011e5d2258e3e645fb2
[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,            \
318                                                  pyopts,                          \
319                                                  &opts,                           \
320                                                  #_func"(options={...}):"))       \
321         {                                                                         \
322                 return NULL;                                                          \
323         }                                                                         \
324
325 #define BPY_PROPDEF_SUBTYPE_CHECK(_func, _property_flag_items, _subtype)      \
326         BPY_PROPDEF_CHECK(_func, _property_flag_items)                            \
327         if (pysubtype && RNA_enum_value_from_id(_subtype,                         \
328                                                 pysubtype,                        \
329                                                 &subtype)==0)                     \
330         {                                                                         \
331                 PyErr_Format(PyExc_TypeError,                                         \
332                              #_func"(subtype='%s'): invalid subtype",                 \
333                              pysubtype);                                              \
334                 return NULL;                                                          \
335         }                                                                         \
336
337
338 #define BPY_PROPDEF_NAME_DOC \
339 "   :arg name: Name used in the user interface.\n" \
340 "   :type name: string\n" \
341
342
343 #define BPY_PROPDEF_DESC_DOC \
344 "   :arg description: Text used for the tooltip and api documentation.\n" \
345 "   :type description: string\n" \
346
347
348 #define BPY_PROPDEF_UNIT_DOC \
349 "   :arg unit: Enumerator in ['NONE', 'LENGTH', 'AREA', 'VOLUME', 'ROTATION', 'TIME', 'VELOCITY', 'ACCELERATION'].\n" \
350 "   :type unit: string\n"       \
351
352
353 #define BPY_PROPDEF_UPDATE_DOC \
354 "   :arg update: function to be called when this value is modified,\n" \
355 "      This function must take 2 values (self, context) and return None.\n" \
356 "   :type update: function\n" \
357
358 #if 0
359 static int bpy_struct_id_used(StructRNA *srna, char *identifier)
360 {
361         PointerRNA ptr;
362         RNA_pointer_create(NULL, srna, NULL, &ptr);
363         return (RNA_struct_find_property(&ptr, identifier) != NULL);
364 }
365 #endif
366
367
368 /* Function that sets RNA, NOTE - self is NULL when called from python, but being abused from C so we can pass the srna along
369  * This isnt incorrect since its a python object - but be careful */
370 PyDoc_STRVAR(BPy_BoolProperty_doc,
371 ".. function:: BoolProperty(name=\"\", description=\"\", default=False, options={'ANIMATABLE'}, subtype='NONE', update=None)\n"
372 "\n"
373 "   Returns a new boolean property definition.\n"
374 "\n"
375 BPY_PROPDEF_NAME_DOC
376 BPY_PROPDEF_DESC_DOC
377 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
378 "   :type options: set\n"
379 "   :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
380 "   :type subtype: string\n"
381 BPY_PROPDEF_UPDATE_DOC
382 );
383 static PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
384 {
385         StructRNA *srna;
386
387         BPY_PROPDEF_HEAD(BoolProperty)
388
389         if (srna) {
390                 static const char *kwlist[]= {"attr", "name", "description", "default", "options", "subtype", "update", NULL};
391                 const char *id=NULL, *name="", *description="";
392                 int id_len;
393                 int def=0;
394                 PropertyRNA *prop;
395                 PyObject *pyopts= NULL;
396                 int opts=0;
397                 char *pysubtype= NULL;
398                 int subtype= PROP_NONE;
399                 PyObject *update_cb= NULL;
400
401                 if (!PyArg_ParseTupleAndKeywords(args, kw,
402                                                  "s#|ssiO!sO:BoolProperty",
403                                                  (char **)kwlist, &id, &id_len,
404                                                  &name, &description, &def,
405                                                  &PySet_Type, &pyopts, &pysubtype,
406                                                  &update_cb))
407                 {
408                         return NULL;
409                 }
410
411                 BPY_PROPDEF_SUBTYPE_CHECK(BoolProperty, property_flag_items, property_subtype_number_items)
412
413                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
414                         return NULL;
415                 }
416
417                 prop= RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
418                 RNA_def_property_boolean_default(prop, def);
419                 RNA_def_property_ui_text(prop, name, description);
420
421                 if (pyopts) {
422                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
423                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
424                 }
425                 bpy_prop_callback_assign(prop, update_cb);
426                 RNA_def_property_duplicate_pointers(srna, prop);
427         }
428
429         Py_RETURN_NONE;
430 }
431
432 PyDoc_STRVAR(BPy_BoolVectorProperty_doc,
433 ".. function:: BoolVectorProperty(name=\"\", description=\"\", default=(False, False, False), options={'ANIMATABLE'}, subtype='NONE', size=3, update=None)\n"
434 "\n"
435 "   Returns a new vector boolean property definition.\n"
436 "\n"
437 BPY_PROPDEF_NAME_DOC
438 BPY_PROPDEF_DESC_DOC
439 "   :arg default: sequence of booleans the length of *size*.\n"
440 "   :type default: sequence\n"
441 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
442 "   :type options: set\n"
443 "   :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'LAYER', 'NONE'].\n"
444 "   :type subtype: string\n"
445 "   :arg size: Vector dimensions in [1, and " STRINGIFY(PYRNA_STACK_ARRAY) "].\n"
446 "   :type size: int\n"
447 BPY_PROPDEF_UPDATE_DOC
448 );
449 static PyObject *BPy_BoolVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
450 {
451         StructRNA *srna;
452
453         BPY_PROPDEF_HEAD(BoolVectorProperty)
454
455         if (srna) {
456                 static const char *kwlist[]= {"attr", "name", "description", "default", "options", "subtype", "size", "update", NULL};
457                 const char *id=NULL, *name="", *description="";
458                 int id_len;
459                 int def[PYRNA_STACK_ARRAY]={0};
460                 int size=3;
461                 PropertyRNA *prop;
462                 PyObject *pydef= NULL;
463                 PyObject *pyopts= NULL;
464                 int opts=0;
465                 char *pysubtype= NULL;
466                 int subtype= PROP_NONE;
467                 PyObject *update_cb= NULL;
468
469                 if (!PyArg_ParseTupleAndKeywords(args, kw,
470                                                  "s#|ssOO!siO:BoolVectorProperty",
471                                                  (char **)kwlist, &id, &id_len,
472                                                  &name, &description, &pydef,
473                                                  &PySet_Type, &pyopts, &pysubtype, &size,
474                                                  &update_cb))
475                 {
476                         return NULL;
477                 }
478
479                 BPY_PROPDEF_SUBTYPE_CHECK(BoolVectorProperty, property_flag_items, property_subtype_array_items)
480
481                 if (size < 1 || size > PYRNA_STACK_ARRAY) {
482                         PyErr_Format(PyExc_TypeError, "BoolVectorProperty(size=%d): size must be between 0 and " STRINGIFY(PYRNA_STACK_ARRAY), size);
483                         return NULL;
484                 }
485
486                 if (pydef && PyC_AsArray(def, pydef, size, &PyBool_Type, FALSE, "BoolVectorProperty(default=sequence)") < 0)
487                         return NULL;
488
489                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
490                         return NULL;
491                 }
492
493                 // prop= RNA_def_boolean_array(srna, id, size, pydef ? def:NULL, name, description);
494                 prop= RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
495                 RNA_def_property_array(prop, size);
496                 if (pydef) RNA_def_property_boolean_array_default(prop, def);
497                 RNA_def_property_ui_text(prop, name, description);
498
499                 if (pyopts) {
500                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
501                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
502                 }
503                 bpy_prop_callback_assign(prop, update_cb);
504                 RNA_def_property_duplicate_pointers(srna, prop);
505         }
506         
507         Py_RETURN_NONE;
508 }
509
510 PyDoc_STRVAR(BPy_IntProperty_doc,
511 ".. 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"
512 "\n"
513 "   Returns a new int property definition.\n"
514 "\n"
515 BPY_PROPDEF_NAME_DOC
516 BPY_PROPDEF_DESC_DOC
517 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
518 "   :type options: set\n"
519 "   :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
520 "   :type subtype: string\n"
521 BPY_PROPDEF_UPDATE_DOC
522 );
523 static PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
524 {
525         StructRNA *srna;
526
527         BPY_PROPDEF_HEAD(IntProperty)
528
529         if (srna) {
530                 static const char *kwlist[]= {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "options", "subtype", "update", NULL};
531                 const char *id=NULL, *name="", *description="";
532                 int id_len;
533                 int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def=0;
534                 PropertyRNA *prop;
535                 PyObject *pyopts= NULL;
536                 int opts=0;
537                 char *pysubtype= NULL;
538                 int subtype= PROP_NONE;
539                 PyObject *update_cb= NULL;
540
541                 if (!PyArg_ParseTupleAndKeywords(args, kw,
542                                                  "s#|ssiiiiiiO!sO:IntProperty",
543                                                  (char **)kwlist, &id, &id_len,
544                                                  &name, &description, &def,
545                                                  &min, &max, &soft_min, &soft_max,
546                                                  &step, &PySet_Type, &pyopts, &pysubtype,
547                                                  &update_cb))
548                 {
549                         return NULL;
550                 }
551
552                 BPY_PROPDEF_SUBTYPE_CHECK(IntProperty, property_flag_items, property_subtype_number_items)
553
554                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
555                         return NULL;
556                 }
557
558                 prop= RNA_def_property(srna, id, PROP_INT, subtype);
559                 RNA_def_property_int_default(prop, def);
560                 RNA_def_property_range(prop, min, max);
561                 RNA_def_property_ui_text(prop, name, description);
562                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, 3);
563
564                 if (pyopts) {
565                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
566                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
567                 }
568                 bpy_prop_callback_assign(prop, update_cb);
569                 RNA_def_property_duplicate_pointers(srna, prop);
570         }
571         Py_RETURN_NONE;
572 }
573
574 PyDoc_STRVAR(BPy_IntVectorProperty_doc,
575 ".. 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"
576 "\n"
577 "   Returns a new vector int property definition.\n"
578 "\n"
579 BPY_PROPDEF_NAME_DOC
580 BPY_PROPDEF_DESC_DOC
581 "   :arg default: sequence of ints the length of *size*.\n"
582 "   :type default: sequence\n"
583 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
584 "   :type options: set\n"
585 "   :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'LAYER', 'NONE'].\n"
586 "   :type subtype: string\n"
587 "   :arg size: Vector dimensions in [1, and " STRINGIFY(PYRNA_STACK_ARRAY) "].\n"
588 "   :type size: int\n"
589 BPY_PROPDEF_UPDATE_DOC
590 );
591 static PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
592 {
593         StructRNA *srna;
594
595         BPY_PROPDEF_HEAD(IntVectorProperty)
596
597         if (srna) {
598                 static const char *kwlist[]= {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "options", "subtype", "size", "update", NULL};
599                 const char *id=NULL, *name="", *description="";
600                 int id_len;
601                 int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def[PYRNA_STACK_ARRAY]={0};
602                 int size=3;
603                 PropertyRNA *prop;
604                 PyObject *pydef= NULL;
605                 PyObject *pyopts= NULL;
606                 int opts=0;
607                 char *pysubtype= NULL;
608                 int subtype= PROP_NONE;
609                 PyObject *update_cb= NULL;
610
611                 if (!PyArg_ParseTupleAndKeywords(args, kw,
612                                                  "s#|ssOiiiiiO!siO:IntVectorProperty",
613                                                  (char **)kwlist, &id, &id_len,
614                                                  &name, &description, &pydef,
615                                                  &min, &max, &soft_min, &soft_max,
616                                                  &step, &PySet_Type, &pyopts,
617                                                  &pysubtype, &size,
618                                                  &update_cb))
619                 {
620                         return NULL;
621                 }
622
623                 BPY_PROPDEF_SUBTYPE_CHECK(IntVectorProperty, property_flag_items, property_subtype_array_items)
624
625                 if (size < 1 || size > PYRNA_STACK_ARRAY) {
626                         PyErr_Format(PyExc_TypeError, "IntVectorProperty(size=%d): size must be between 0 and " STRINGIFY(PYRNA_STACK_ARRAY), size);
627                         return NULL;
628                 }
629
630                 if (pydef && PyC_AsArray(def, pydef, size, &PyLong_Type, FALSE, "IntVectorProperty(default=sequence)") < 0)
631                         return NULL;
632
633                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
634                         return NULL;
635                 }
636
637                 prop= RNA_def_property(srna, id, PROP_INT, subtype);
638                 RNA_def_property_array(prop, size);
639                 if (pydef) RNA_def_property_int_array_default(prop, def);
640                 RNA_def_property_range(prop, min, max);
641                 RNA_def_property_ui_text(prop, name, description);
642                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, 3);
643
644                 if (pyopts) {
645                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
646                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
647                 }
648                 bpy_prop_callback_assign(prop, update_cb);
649                 RNA_def_property_duplicate_pointers(srna, prop);
650         }
651         Py_RETURN_NONE;
652 }
653
654
655 PyDoc_STRVAR(BPy_FloatProperty_doc,
656 ".. 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"
657 "\n"
658 "   Returns a new float property definition.\n"
659 "\n"
660 BPY_PROPDEF_NAME_DOC
661 BPY_PROPDEF_DESC_DOC
662 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
663 "   :type options: set\n"
664 "   :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
665 "   :type subtype: string\n"
666 BPY_PROPDEF_UNIT_DOC
667 BPY_PROPDEF_UPDATE_DOC
668 );
669 static PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
670 {
671         StructRNA *srna;
672
673         BPY_PROPDEF_HEAD(FloatProperty)
674
675         if (srna) {
676                 static const char *kwlist[]= {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", "unit", "update", NULL};
677                 const char *id=NULL, *name="", *description="";
678                 int id_len;
679                 float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def=0.0f;
680                 int precision= 2;
681                 PropertyRNA *prop;
682                 PyObject *pyopts= NULL;
683                 int opts=0;
684                 char *pysubtype= NULL;
685                 int subtype= PROP_NONE;
686                 char *pyunit= NULL;
687                 int unit= PROP_UNIT_NONE;
688                 PyObject *update_cb= NULL;
689
690                 if (!PyArg_ParseTupleAndKeywords(args, kw,
691                                                  "s#|ssffffffiO!ssO:FloatProperty",
692                                                  (char **)kwlist, &id, &id_len,
693                                                  &name, &description, &def,
694                                                  &min, &max, &soft_min, &soft_max,
695                                                  &step, &precision, &PySet_Type,
696                                                  &pyopts, &pysubtype, &pyunit,
697                                                  &update_cb))
698                 {
699                         return NULL;
700                 }
701
702                 BPY_PROPDEF_SUBTYPE_CHECK(FloatProperty, property_flag_items, property_subtype_number_items)
703
704                 if (pyunit && RNA_enum_value_from_id(property_unit_items, pyunit, &unit)==0) {
705                         PyErr_Format(PyExc_TypeError, "FloatProperty(unit='%s'): invalid unit", pyunit);
706                         return NULL;
707                 }
708
709                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
710                         return NULL;
711                 }
712
713                 prop= RNA_def_property(srna, id, PROP_FLOAT, subtype | unit);
714                 RNA_def_property_float_default(prop, def);
715                 RNA_def_property_range(prop, min, max);
716                 RNA_def_property_ui_text(prop, name, description);
717                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, precision);
718
719                 if (pyopts) {
720                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
721                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
722                 }
723                 bpy_prop_callback_assign(prop, update_cb);
724                 RNA_def_property_duplicate_pointers(srna, prop);
725         }
726         Py_RETURN_NONE;
727 }
728
729 PyDoc_STRVAR(BPy_FloatVectorProperty_doc,
730 ".. 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"
731 "\n"
732 "   Returns a new vector float property definition.\n"
733 "\n"
734 BPY_PROPDEF_NAME_DOC
735 BPY_PROPDEF_DESC_DOC
736 "   :arg default: sequence of floats the length of *size*.\n"
737 "   :type default: sequence\n"
738 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
739 "   :type options: set\n"
740 "   :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'LAYER', 'NONE'].\n"
741 "   :type subtype: string\n"
742 BPY_PROPDEF_UNIT_DOC
743 "   :arg size: Vector dimensions in [1, and " STRINGIFY(PYRNA_STACK_ARRAY) "].\n"
744 "   :type size: int\n"
745 BPY_PROPDEF_UPDATE_DOC
746 );
747 static PyObject *BPy_FloatVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
748 {
749         StructRNA *srna;
750
751         BPY_PROPDEF_HEAD(FloatVectorProperty)
752
753         if (srna) {
754                 static const char *kwlist[]= {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", "unit", "size", "update", NULL};
755                 const char *id=NULL, *name="", *description="";
756                 int id_len;
757                 float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def[PYRNA_STACK_ARRAY]={0.0f};
758                 int precision= 2, size=3;
759                 PropertyRNA *prop;
760                 PyObject *pydef= NULL;
761                 PyObject *pyopts= NULL;
762                 int opts=0;
763                 char *pysubtype= NULL;
764                 int subtype= PROP_NONE;
765                 char *pyunit= NULL;
766                 int unit= PROP_UNIT_NONE;
767                 PyObject *update_cb= NULL;
768
769                 if (!PyArg_ParseTupleAndKeywords(args, kw,
770                                                  "s#|ssOfffffiO!ssiO:FloatVectorProperty",
771                                                  (char **)kwlist, &id, &id_len,
772                                                  &name, &description, &pydef,
773                                                  &min, &max, &soft_min, &soft_max,
774                                                  &step, &precision, &PySet_Type,
775                                                  &pyopts, &pysubtype, &pyunit, &size,
776                                                  &update_cb))
777                 {
778                         return NULL;
779                 }
780
781                 BPY_PROPDEF_SUBTYPE_CHECK(FloatVectorProperty, property_flag_items, property_subtype_array_items)
782
783                 if (pyunit && RNA_enum_value_from_id(property_unit_items, pyunit, &unit)==0) {
784                         PyErr_Format(PyExc_TypeError, "FloatVectorProperty(unit='%s'): invalid unit", pyunit);
785                         return NULL;
786                 }
787
788                 if (size < 1 || size > PYRNA_STACK_ARRAY) {
789                         PyErr_Format(PyExc_TypeError, "FloatVectorProperty(size=%d): size must be between 0 and " STRINGIFY(PYRNA_STACK_ARRAY), size);
790                         return NULL;
791                 }
792
793                 if (pydef && PyC_AsArray(def, pydef, size, &PyFloat_Type, FALSE, "FloatVectorProperty(default=sequence)") < 0)
794                         return NULL;
795
796                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
797                         return NULL;
798                 }
799
800                 prop= RNA_def_property(srna, id, PROP_FLOAT, subtype | unit);
801                 RNA_def_property_array(prop, size);
802                 if (pydef) RNA_def_property_float_array_default(prop, def);
803                 RNA_def_property_range(prop, min, max);
804                 RNA_def_property_ui_text(prop, name, description);
805                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, precision);
806
807                 if (pyopts) {
808                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
809                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
810                 }
811                 bpy_prop_callback_assign(prop, update_cb);
812                 RNA_def_property_duplicate_pointers(srna, prop);
813         }
814         Py_RETURN_NONE;
815 }
816
817 PyDoc_STRVAR(BPy_StringProperty_doc,
818 ".. function:: StringProperty(name=\"\", description=\"\", default=\"\", maxlen=0, options={'ANIMATABLE'}, subtype='NONE', update=None)\n"
819 "\n"
820 "   Returns a new string property definition.\n"
821 "\n"
822 BPY_PROPDEF_NAME_DOC
823 BPY_PROPDEF_DESC_DOC
824 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
825 "   :type options: set\n"
826 "   :arg subtype: Enumerator in ['FILE_PATH', 'DIR_PATH', 'FILENAME', 'NONE'].\n"
827 "   :type subtype: string\n"
828 BPY_PROPDEF_UPDATE_DOC
829 );
830 static PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw)
831 {
832         StructRNA *srna;
833
834         BPY_PROPDEF_HEAD(StringProperty)
835
836         if (srna) {
837                 static const char *kwlist[]= {"attr", "name", "description", "default", "maxlen", "options", "subtype", "update", NULL};
838                 const char *id=NULL, *name="", *description="", *def="";
839                 int id_len;
840                 int maxlen=0;
841                 PropertyRNA *prop;
842                 PyObject *pyopts= NULL;
843                 int opts=0;
844                 char *pysubtype= NULL;
845                 int subtype= PROP_NONE;
846                 PyObject *update_cb= NULL;
847
848                 if (!PyArg_ParseTupleAndKeywords(args, kw,
849                                                  "s#|sssiO!sO:StringProperty",
850                                                  (char **)kwlist, &id, &id_len,
851                                                  &name, &description, &def,
852                                                  &maxlen, &PySet_Type, &pyopts, &pysubtype,
853                                                  &update_cb))
854                 {
855                         return NULL;
856                 }
857
858                 BPY_PROPDEF_SUBTYPE_CHECK(StringProperty, property_flag_items, property_subtype_string_items)
859
860                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
861                         return NULL;
862                 }
863
864                 prop= RNA_def_property(srna, id, PROP_STRING, subtype);
865                 if (maxlen != 0) RNA_def_property_string_maxlength(prop, maxlen + 1); /* +1 since it includes null terminator */
866                 if (def) RNA_def_property_string_default(prop, def);
867                 RNA_def_property_ui_text(prop, name, description);
868
869                 if (pyopts) {
870                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
871                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
872                 }
873                 bpy_prop_callback_assign(prop, update_cb);
874                 RNA_def_property_duplicate_pointers(srna, prop);
875         }
876         Py_RETURN_NONE;
877 }
878
879 #if 0
880 /* copies orig to buf, then sets orig to buf, returns copy length */
881 static size_t strswapbufcpy(char *buf, const char **orig)
882 {
883         const char *src= *orig;
884         char *dst= buf;
885         size_t i= 0;
886         *orig= buf;
887         while ((*dst= *src)) { dst++; src++; i++; }
888         return i + 1; /* include '\0' */
889 }
890 #endif
891
892 static EnumPropertyItem *enum_items_from_py(PyObject *seq_fast, PyObject *def, int *defvalue, const short is_enum_flag)
893 {
894         EnumPropertyItem *items;
895         PyObject *item;
896         const Py_ssize_t seq_len= PySequence_Fast_GET_SIZE(seq_fast);
897         Py_ssize_t totbuf= 0;
898         int i;
899         short def_used= 0;
900         const char *def_cmp= NULL;
901
902         if (is_enum_flag) {
903                 if (seq_len > RNA_ENUM_BITFLAG_SIZE) {
904                         PyErr_SetString(PyExc_TypeError, "EnumProperty(...): maximum " STRINGIFY(RNA_ENUM_BITFLAG_SIZE) " members for a ENUM_FLAG type property");
905                         return NULL;
906                 }
907                 if (def && !PySet_Check(def)) {
908                         PyErr_Format(PyExc_TypeError,
909                                      "EnumProperty(...): default option must be a 'set' "
910                                      "type when ENUM_FLAG is enabled, not a '%.200s'",
911                                      Py_TYPE(def)->tp_name);
912                         return NULL;
913                 }
914         }
915         else {
916                 if (def) {
917                         def_cmp= _PyUnicode_AsString(def);
918                         if (def_cmp==NULL) {
919                                 PyErr_Format(PyExc_TypeError,
920                                              "EnumProperty(...): default option must be a 'str' "
921                                              "type when ENUM_FLAG is disabled, not a '%.200s'",
922                                              Py_TYPE(def)->tp_name);
923                                 return NULL;
924                         }
925                 }
926         }
927
928         /* blank value */
929         *defvalue= 0;
930
931         items= MEM_callocN(sizeof(EnumPropertyItem) * (seq_len + 1), "enum_items_from_py1");
932
933         for (i=0; i<seq_len; i++) {
934                 EnumPropertyItem tmp= {0, "", 0, "", ""};
935                 Py_ssize_t item_size;
936                 Py_ssize_t id_str_size;
937                 Py_ssize_t name_str_size;
938                 Py_ssize_t desc_str_size;
939
940                 item= PySequence_Fast_GET_ITEM(seq_fast, i);
941
942                 if (    (PyTuple_CheckExact(item)) &&
943                         (item_size= PyTuple_GET_SIZE(item)) &&
944                         (item_size == 3 || item_size == 4) &&
945                         (tmp.identifier=  _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 0), &id_str_size)) &&
946                         (tmp.name=        _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 1), &name_str_size)) &&
947                         (tmp.description= _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 2), &desc_str_size)) &&
948                         (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 */
949                 ) {
950                         if (is_enum_flag) {
951                                 if (item_size < 4) {
952                                         tmp.value= 1<<i;
953                                 }
954
955                                 if (def && PySet_Contains(def, PyTuple_GET_ITEM(item, 0))) {
956                                         *defvalue |= tmp.value;
957                                         def_used++;
958                                 }
959                         }
960                         else {
961                                 if (item_size < 4) {
962                                         tmp.value= i;
963                                 }
964
965                                 if (def && def_used == 0 && strcmp(def_cmp, tmp.identifier)==0) {
966                                         *defvalue= tmp.value;
967                                         def_used++; /* only ever 1 */
968                                 }
969                         }
970
971                         items[i]= tmp;
972
973                         /* calculate combine string length */
974                         totbuf += id_str_size + name_str_size + desc_str_size + 3; /* 3 is for '\0's */
975                 }
976                 else {
977                         MEM_freeN(items);
978                         PyErr_SetString(PyExc_TypeError,
979                                         "EnumProperty(...): expected an tuple containing "
980                                         "(identifier, name description) and optionally a "
981                                         "unique number");
982                         return NULL;
983                 }
984
985         }
986
987         if (is_enum_flag) {
988                 /* strict check that all set members were used */
989                 if (def && def_used != PySet_GET_SIZE(def)) {
990                         MEM_freeN(items);
991
992                         PyErr_Format(PyExc_TypeError,
993                                      "EnumProperty(..., default={...}): set has %d unused member(s)",
994                                      PySet_GET_SIZE(def) - def_used);
995                         return NULL;
996                 }
997         }
998         else {
999                 if (def && def_used == 0) {
1000                         MEM_freeN(items);
1001
1002                         PyErr_Format(PyExc_TypeError,
1003                                      "EnumProperty(..., default=\'%s\'): not found in enum members",
1004                                      def_cmp);
1005                         return NULL;
1006                 }
1007         }
1008
1009         /* disabled duplicating strings because the array can still be freed and
1010          * the strings from it referenced, for now we can't support dynamically
1011          * created strings from python. */
1012 #if 0
1013         /* this would all work perfectly _but_ the python strings may be freed
1014          * immediately after use, so we need to duplicate them, ugh.
1015          * annoying because it works most of the time without this. */
1016         {
1017                 EnumPropertyItem *items_dup= MEM_mallocN((sizeof(EnumPropertyItem) * (seq_len + 1)) + (sizeof(char) * totbuf), "enum_items_from_py2");
1018                 EnumPropertyItem *items_ptr= items_dup;
1019                 char *buf= ((char *)items_dup) + (sizeof(EnumPropertyItem) * (seq_len + 1));
1020                 memcpy(items_dup, items, sizeof(EnumPropertyItem) * (seq_len + 1));
1021                 for (i=0; i<seq_len; i++, items_ptr++) {
1022                         buf += strswapbufcpy(buf, &items_ptr->identifier);
1023                         buf += strswapbufcpy(buf, &items_ptr->name);
1024                         buf += strswapbufcpy(buf, &items_ptr->description);
1025                 }
1026                 MEM_freeN(items);
1027                 items=items_dup;
1028         }
1029         /* end string duplication */
1030 #endif
1031
1032         return items;
1033 }
1034
1035 static EnumPropertyItem *bpy_props_enum_itemf(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, int *free)
1036 {
1037         PyGILState_STATE gilstate;
1038
1039         PyObject *py_func= RNA_property_enum_py_data_get(prop);
1040         PyObject *self= NULL;
1041         PyObject *args;
1042         PyObject *items; /* returned from the function call */
1043
1044         EnumPropertyItem *eitems= NULL;
1045         int err= 0;
1046
1047         bpy_context_set(C, &gilstate);
1048
1049         args= PyTuple_New(2);
1050         self= pyrna_struct_as_instance(ptr);
1051         PyTuple_SET_ITEM(args, 0, self);
1052
1053         /* now get the context */
1054         PyTuple_SET_ITEM(args, 1, (PyObject *)bpy_context_module);
1055         Py_INCREF(bpy_context_module);
1056
1057         items= PyObject_CallObject(py_func, args);
1058
1059         Py_DECREF(args);
1060
1061         if (items==NULL) {
1062                 err= -1;
1063         }
1064         else {
1065                 PyObject *items_fast;
1066                 int defvalue_dummy=0;
1067
1068                 if (!(items_fast= PySequence_Fast(items, "EnumProperty(...): return value from the callback was not a sequence"))) {
1069                         err= -1;
1070                 }
1071                 else {
1072                         eitems= enum_items_from_py(items_fast, NULL, &defvalue_dummy, (RNA_property_flag(prop) & PROP_ENUM_FLAG)!=0);
1073
1074                         Py_DECREF(items_fast);
1075
1076                         if (!eitems) {
1077                                 err= -1;
1078                         }
1079                 }
1080
1081                 Py_DECREF(items);
1082         }
1083
1084         if (err != -1) { /* worked */
1085                 *free= 1;
1086         }
1087         else {
1088                 printf_func_error(py_func);
1089
1090                 eitems= DummyRNA_NULL_items;
1091         }
1092
1093
1094         bpy_context_clear(C, &gilstate);
1095         return eitems;
1096 }
1097
1098 PyDoc_STRVAR(BPy_EnumProperty_doc,
1099 ".. function:: EnumProperty(items, name=\"\", description=\"\", default=\"\", options={'ANIMATABLE'}, update=None)\n"
1100 "\n"
1101 "   Returns a new enumerator property definition.\n"
1102 "\n"
1103 BPY_PROPDEF_NAME_DOC
1104 BPY_PROPDEF_DESC_DOC
1105 "   :arg default: The default value for this enum, A string when *ENUM_FLAG*\n"
1106 "      is disabled otherwise a set which may only contain string identifiers\n"
1107 "      used in *items*.\n"
1108 "   :type default: string or set\n"
1109 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE', 'ENUM_FLAG'].\n"
1110 "   :type options: set\n"
1111 "   :arg items: sequence of enum items formatted:\n"
1112 "      [(identifier, name, description, number), ...] where the identifier is used\n"
1113 "      for python access and other values are used for the interface.\n"
1114 "      Note the item is optional.\n"
1115 "      For dynamic values a callback can be passed which returns a list in\n"
1116 "      the same format as the static list.\n"
1117 "      This function must take 2 arguments (self, context)\n"
1118 "   :type items: sequence of string triplets or a function\n"
1119 BPY_PROPDEF_UPDATE_DOC
1120 );
1121 static PyObject *BPy_EnumProperty(PyObject *self, PyObject *args, PyObject *kw)
1122 {
1123         StructRNA *srna;
1124
1125         BPY_PROPDEF_HEAD(EnumProperty)
1126         
1127         if (srna) {
1128                 static const char *kwlist[]= {"attr", "items", "name", "description", "default", "options", "update", NULL};
1129                 const char *id=NULL, *name="", *description="";
1130                 PyObject *def= NULL;
1131                 int id_len;
1132                 int defvalue=0;
1133                 PyObject *items, *items_fast;
1134                 EnumPropertyItem *eitems;
1135                 PropertyRNA *prop;
1136                 PyObject *pyopts= NULL;
1137                 int opts=0;
1138                 short is_itemf= FALSE;
1139                 PyObject *update_cb= NULL;
1140
1141                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1142                                                  "s#O|ssOO!O:EnumProperty",
1143                                                  (char **)kwlist, &id, &id_len,
1144                                                  &items, &name, &description,
1145                                                  &def, &PySet_Type, &pyopts,
1146                                                  &update_cb))
1147                 {
1148                         return NULL;
1149                 }
1150
1151                 BPY_PROPDEF_CHECK(EnumProperty, property_flag_enum_items)
1152
1153                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
1154                         return NULL;
1155                 }
1156
1157                 /* items can be a list or a callable */
1158                 if (PyFunction_Check(items)) { /* dont use PyCallable_Check because we need the function code for errors */
1159                         PyCodeObject *f_code= (PyCodeObject *)PyFunction_GET_CODE(items);
1160                         if (f_code->co_argcount != 2) {
1161                                 PyErr_Format(PyExc_ValueError,
1162                                              "EnumProperty(...): expected 'items' function to take 2 arguments, not %d",
1163                                              f_code->co_argcount);
1164                                 return NULL;
1165                         }
1166
1167                         if (def) {
1168                                 /* note, using type error here is odd but python does this for invalid arguments */
1169                                 PyErr_SetString(PyExc_TypeError,
1170                                                 "EnumProperty(...): 'default' can't be set when 'items' is a function");
1171                                 return NULL;
1172                         }
1173
1174                         is_itemf= TRUE;
1175                         eitems= DummyRNA_NULL_items;
1176                 }
1177                 else {
1178                         if (!(items_fast= PySequence_Fast(items, "EnumProperty(...): expected a sequence of tuples for the enum items or a function"))) {
1179                                 return NULL;
1180                         }
1181
1182                         eitems= enum_items_from_py(items_fast, def, &defvalue, (opts & PROP_ENUM_FLAG)!=0);
1183
1184                         Py_DECREF(items_fast);
1185
1186                         if (!eitems) {
1187                                 return NULL;
1188                         }
1189                 }
1190
1191                 if (opts & PROP_ENUM_FLAG)  prop= RNA_def_enum_flag(srna, id, eitems, defvalue, name, description);
1192                 else                        prop= RNA_def_enum(srna, id, eitems, defvalue, name, description);
1193
1194                 if (is_itemf) {
1195                         RNA_def_enum_funcs(prop, bpy_props_enum_itemf);
1196                         RNA_def_enum_py_data(prop, (void *)items);
1197                         /* Py_INCREF(items); */ /* watch out!, if user is tricky they can probably crash blender if they manage to free the callback, take care! */
1198                 }
1199
1200                 if (pyopts) {
1201                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
1202                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
1203                 }
1204                 bpy_prop_callback_assign(prop, update_cb);
1205                 RNA_def_property_duplicate_pointers(srna, prop);
1206
1207                 if (is_itemf == FALSE) {
1208                         MEM_freeN(eitems);
1209                 }
1210         }
1211         Py_RETURN_NONE;
1212 }
1213
1214 static StructRNA *pointer_type_from_py(PyObject *value, const char *error_prefix)
1215 {
1216         StructRNA *srna;
1217
1218         srna= srna_from_self(value, "");
1219         if (!srna) {
1220                 if (PyErr_Occurred()) {
1221                         PyObject *msg= PyC_ExceptionBuffer();
1222                         const char *msg_char= _PyUnicode_AsString(msg);
1223                         PyErr_Format(PyExc_TypeError,
1224                                      "%.200s expected an RNA type derived from PropertyGroup, failed with: %s",
1225                                      error_prefix, msg_char);
1226                         Py_DECREF(msg);
1227                 }
1228                 else {
1229                         PyErr_Format(PyExc_TypeError,
1230                                      "%.200s expected an RNA type derived from PropertyGroup, failed with type '%s'",
1231                                      error_prefix, Py_TYPE(value)->tp_name);
1232                 }
1233                 return NULL;
1234         }
1235
1236         if (!RNA_struct_is_a(srna, &RNA_PropertyGroup)) {
1237                 PyErr_Format(PyExc_TypeError,
1238                              "%.200s expected an RNA type derived from PropertyGroup",
1239                              error_prefix);
1240                 return NULL;
1241         }
1242
1243         return srna;
1244 }
1245
1246 PyDoc_STRVAR(BPy_PointerProperty_doc,
1247 ".. function:: PointerProperty(type=\"\", description=\"\", options={'ANIMATABLE'}, update=None)\n"
1248 "\n"
1249 "   Returns a new pointer property definition.\n"
1250 "\n"
1251 "   :arg type: A subclass of :class:`bpy.types.PropertyGroup`.\n"
1252 "   :type type: class\n"
1253 BPY_PROPDEF_NAME_DOC
1254 BPY_PROPDEF_DESC_DOC
1255 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
1256 "   :type options: set\n"
1257 BPY_PROPDEF_UPDATE_DOC
1258 );
1259 static PyObject *BPy_PointerProperty(PyObject *self, PyObject *args, PyObject *kw)
1260 {
1261         StructRNA *srna;
1262
1263         BPY_PROPDEF_HEAD(PointerProperty)
1264
1265         if (srna) {
1266                 static const char *kwlist[]= {"attr", "type", "name", "description", "options", "update", NULL};
1267                 const char *id=NULL, *name="", *description="";
1268                 int id_len;
1269                 PropertyRNA *prop;
1270                 StructRNA *ptype;
1271                 PyObject *type= Py_None;
1272                 PyObject *pyopts= NULL;
1273                 int opts=0;
1274                 PyObject *update_cb= NULL;
1275
1276                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1277                                                  "s#O|ssO!O:PointerProperty",
1278                                                  (char **)kwlist, &id, &id_len,
1279                                                  &type, &name, &description,
1280                                                  &PySet_Type, &pyopts,
1281                                                  &update_cb))
1282                 {
1283                         return NULL;
1284                 }
1285
1286                 BPY_PROPDEF_CHECK(PointerProperty, property_flag_items)
1287
1288                 ptype= pointer_type_from_py(type, "PointerProperty(...):");
1289                 if (!ptype)
1290                         return NULL;
1291
1292                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
1293                         return NULL;
1294                 }
1295
1296                 prop= RNA_def_pointer_runtime(srna, id, ptype, name, description);
1297                 if (pyopts) {
1298                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
1299                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
1300                 }
1301                 bpy_prop_callback_assign(prop, update_cb);
1302                 RNA_def_property_duplicate_pointers(srna, prop);
1303         }
1304         Py_RETURN_NONE;
1305 }
1306
1307 PyDoc_STRVAR(BPy_CollectionProperty_doc,
1308 ".. function:: CollectionProperty(items, type=\"\", description=\"\", default=\"\", options={'ANIMATABLE'})\n"
1309 "\n"
1310 "   Returns a new collection property definition.\n"
1311 "\n"
1312 "   :arg type: A subclass of :class:`bpy.types.PropertyGroup`.\n"
1313 "   :type type: class\n"
1314 BPY_PROPDEF_NAME_DOC
1315 BPY_PROPDEF_DESC_DOC
1316 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
1317 "   :type options: set\n"
1318 );
1319 static PyObject *BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw)
1320 {
1321         StructRNA *srna;
1322
1323         BPY_PROPDEF_HEAD(CollectionProperty)
1324
1325         if (srna) {
1326                 static const char *kwlist[]= {"attr", "type", "name", "description", "options", NULL};
1327                 const char *id=NULL, *name="", *description="";
1328                 int id_len;
1329                 PropertyRNA *prop;
1330                 StructRNA *ptype;
1331                 PyObject *type= Py_None;
1332                 PyObject *pyopts= NULL;
1333                 int opts=0;
1334
1335                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1336                                                  "s#O|ssO!:CollectionProperty",
1337                                                  (char **)kwlist, &id, &id_len,
1338                                                  &type, &name, &description,
1339                                                  &PySet_Type, &pyopts))
1340                 {
1341                         return NULL;
1342                 }
1343
1344                 BPY_PROPDEF_CHECK(CollectionProperty, property_flag_items)
1345
1346                 ptype= pointer_type_from_py(type, "CollectionProperty(...):");
1347                 if (!ptype)
1348                         return NULL;
1349
1350                 prop= RNA_def_collection_runtime(srna, id, ptype, name, description);
1351                 if (pyopts) {
1352                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
1353                         if ((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
1354                 }
1355                 RNA_def_property_duplicate_pointers(srna, prop);
1356         }
1357         Py_RETURN_NONE;
1358 }
1359
1360 PyDoc_STRVAR(BPy_RemoveProperty_doc,
1361 ".. function:: RemoveProperty(attr)\n"
1362 "\n"
1363 "   Removes a dynamically defined property.\n"
1364 "\n"
1365 "   :arg attr: Property name.\n"
1366 "   :type attr: string\n"
1367 );
1368 static PyObject *BPy_RemoveProperty(PyObject *self, PyObject *args, PyObject *kw)
1369 {
1370         StructRNA *srna;
1371
1372         if (PyTuple_GET_SIZE(args) == 1) {
1373                 PyObject *ret;
1374                 self= PyTuple_GET_ITEM(args, 0);
1375                 args= PyTuple_New(0);
1376                 ret= BPy_RemoveProperty(self, args, kw);
1377                 Py_DECREF(args);
1378                 return ret;     
1379         }
1380         else if (PyTuple_GET_SIZE(args) > 1) {
1381                 PyErr_SetString(PyExc_ValueError, "all args must be keywords"); \
1382                 return NULL;
1383         }
1384
1385         srna= srna_from_self(self, "RemoveProperty(...):");
1386         if (srna==NULL && PyErr_Occurred()) {
1387                 return NULL; /* self's type was compatible but error getting the srna */
1388         }
1389         else if (srna==NULL) {
1390                 PyErr_SetString(PyExc_TypeError, "RemoveProperty(): struct rna not available for this type");
1391                 return NULL;
1392         }
1393         else {
1394                 static const char *kwlist[]= {"attr", NULL};
1395                 
1396                 char *id=NULL;
1397
1398                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1399                                                  "s:RemoveProperty",
1400                                                  (char **)kwlist, &id))
1401                 {
1402                         return NULL;
1403                 }
1404
1405                 if (RNA_def_property_free_identifier(srna, id) != 1) {
1406                         PyErr_Format(PyExc_TypeError, "RemoveProperty(): '%s' not a defined dynamic property", id);
1407                         return NULL;
1408                 }
1409         }
1410         Py_RETURN_NONE;
1411 }
1412
1413 static struct PyMethodDef props_methods[]= {
1414         {"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS|METH_KEYWORDS, BPy_BoolProperty_doc},
1415         {"BoolVectorProperty", (PyCFunction)BPy_BoolVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_BoolVectorProperty_doc},
1416         {"IntProperty", (PyCFunction)BPy_IntProperty, METH_VARARGS|METH_KEYWORDS, BPy_IntProperty_doc},
1417         {"IntVectorProperty", (PyCFunction)BPy_IntVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_IntVectorProperty_doc},
1418         {"FloatProperty", (PyCFunction)BPy_FloatProperty, METH_VARARGS|METH_KEYWORDS, BPy_FloatProperty_doc},
1419         {"FloatVectorProperty", (PyCFunction)BPy_FloatVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_FloatVectorProperty_doc},
1420         {"StringProperty", (PyCFunction)BPy_StringProperty, METH_VARARGS|METH_KEYWORDS, BPy_StringProperty_doc},
1421         {"EnumProperty", (PyCFunction)BPy_EnumProperty, METH_VARARGS|METH_KEYWORDS, BPy_EnumProperty_doc},
1422         {"PointerProperty", (PyCFunction)BPy_PointerProperty, METH_VARARGS|METH_KEYWORDS, BPy_PointerProperty_doc},
1423         {"CollectionProperty", (PyCFunction)BPy_CollectionProperty, METH_VARARGS|METH_KEYWORDS, BPy_CollectionProperty_doc},
1424
1425         {"RemoveProperty", (PyCFunction)BPy_RemoveProperty, METH_VARARGS|METH_KEYWORDS, BPy_RemoveProperty_doc},
1426         {NULL, NULL, 0, NULL}
1427 };
1428
1429 static struct PyModuleDef props_module= {
1430         PyModuleDef_HEAD_INIT,
1431         "bpy.props",
1432         "This module defines properties to extend blenders internal data, the result of these functions"
1433         " is used to assign properties to classes registered with blender and can't be used directly.",
1434         -1,/* multiple "initialization" just copies the module dict. */
1435         props_methods,
1436         NULL, NULL, NULL, NULL
1437 };
1438
1439 PyObject *BPY_rna_props(void)
1440 {
1441         PyObject *submodule;
1442         PyObject *submodule_dict;
1443         
1444         submodule= PyModule_Create(&props_module);
1445         PyDict_SetItemString(PyImport_GetModuleDict(), props_module.m_name, submodule);
1446
1447         /* INCREF since its its assumed that all these functions return the
1448          * module with a new ref like PyDict_New, since they are passed to
1449          * PyModule_AddObject which steals a ref */
1450         Py_INCREF(submodule);
1451         
1452         /* api needs the PyObjects internally */
1453         submodule_dict= PyModule_GetDict(submodule);
1454
1455 #define ASSIGN_STATIC(_name) pymeth_##_name= PyDict_GetItemString(submodule_dict, #_name)
1456
1457         ASSIGN_STATIC(BoolProperty);
1458         ASSIGN_STATIC(BoolVectorProperty);
1459         ASSIGN_STATIC(IntProperty);
1460         ASSIGN_STATIC(IntVectorProperty);
1461         ASSIGN_STATIC(FloatProperty);
1462         ASSIGN_STATIC(FloatVectorProperty);
1463         ASSIGN_STATIC(StringProperty);
1464         ASSIGN_STATIC(EnumProperty);
1465         ASSIGN_STATIC(PointerProperty);
1466         ASSIGN_STATIC(CollectionProperty);
1467         ASSIGN_STATIC(RemoveProperty);
1468         
1469         return submodule;
1470 }