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