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