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