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