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