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