remove unused includes
[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 #include "bpy_props.h"
26 #include "bpy_rna.h"
27 #include "bpy_util.h"
28
29 #include "RNA_define.h" /* for defining our own rna */
30 #include "RNA_enum_types.h"
31
32 #include "MEM_guardedalloc.h"
33
34 EnumPropertyItem property_flag_items[] = {
35         {PROP_HIDDEN, "HIDDEN", 0, "Hidden", ""},
36         {PROP_ANIMATABLE, "ANIMATABLE", 0, "Animateable", ""},
37         {0, NULL, 0, NULL, NULL}};
38
39 /* subtypes */
40 EnumPropertyItem property_subtype_string_items[] = {
41         {PROP_FILEPATH, "FILE_PATH", 0, "File Path", ""},
42         {PROP_DIRPATH, "DIR_PATH", 0, "Directory Path", ""},
43         {PROP_FILENAME, "FILENAME", 0, "Filename", ""},
44
45         {PROP_NONE, "NONE", 0, "None", ""},
46         {0, NULL, 0, NULL, NULL}};
47
48 EnumPropertyItem property_subtype_number_items[] = {
49         {PROP_UNSIGNED, "UNSIGNED", 0, "Unsigned", ""},
50         {PROP_PERCENTAGE, "PERCENTAGE", 0, "Percentage", ""},
51         {PROP_FACTOR, "FACTOR", 0, "Factor", ""},
52         {PROP_ANGLE, "ANGLE", 0, "Angle", ""},
53         {PROP_TIME, "TIME", 0, "Time", ""},
54         {PROP_DISTANCE, "DISTANCE", 0, "Distance", ""},
55
56         {PROP_NONE, "NONE", 0, "None", ""},
57         {0, NULL, 0, NULL, NULL}};
58
59 EnumPropertyItem property_subtype_array_items[] = {
60         {PROP_COLOR, "COLOR", 0, "Color", ""},
61         {PROP_TRANSLATION, "TRANSLATION", 0, "Translation", ""},
62         {PROP_DIRECTION, "DIRECTION", 0, "Direction", ""},
63         {PROP_VELOCITY, "VELOCITY", 0, "Velocity", ""},
64         {PROP_ACCELERATION, "ACCELERATION", 0, "Acceleration", ""},
65         {PROP_MATRIX, "MATRIX", 0, "Matrix", ""},
66         {PROP_EULER, "EULER", 0, "Euler", ""},
67         {PROP_QUATERNION, "QUATERNION", 0, "Quaternion", ""},
68         {PROP_AXISANGLE, "AXISANGLE", 0, "Axis Angle", ""},
69         {PROP_XYZ, "XYZ", 0, "XYZ", ""},
70         {PROP_COLOR_GAMMA, "COLOR_GAMMA", 0, "Color Gamma", ""},
71         {PROP_LAYER, "LAYER", 0, "Layer", ""},
72
73         {PROP_NONE, "NONE", 0, "None", ""},
74         {0, NULL, 0, NULL, NULL}};
75
76 /* operators use this so it can store the args given but defer running
77  * it until the operator runs where these values are used to setup the
78  * default args for that operator instance */
79 static PyObject *bpy_prop_deferred_return(void *func, PyObject *kw)
80 {
81         PyObject *ret = PyTuple_New(2);
82         PyTuple_SET_ITEM(ret, 0, PyCapsule_New(func, NULL, NULL));
83         if(kw==NULL)    kw= PyDict_New();
84         else                    Py_INCREF(kw);
85         PyTuple_SET_ITEM(ret, 1, kw);
86         return ret;
87 }
88
89 static int bpy_struct_id_used(StructRNA *srna, char *identifier)
90 {
91         PointerRNA ptr;
92         RNA_pointer_create(NULL, srna, NULL, &ptr);
93         return (RNA_struct_find_property(&ptr, identifier) != NULL);
94 }
95
96
97 /* Function that sets RNA, NOTE - self is NULL when called from python, but being abused from C so we can pass the srna allong
98  * This isnt incorrect since its a python object - but be careful */
99 static char BPy_BoolProperty_doc[] =
100 ".. function:: BoolProperty(name=\"\", description=\"\", default=False, options={'ANIMATABLE'}, subtype='NONE')\n"
101 "\n"
102 "   Returns a new boolean property definition.\n"
103 "\n"
104 "   :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
105 "   :type options: set\n"
106 "   :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
107 "   :type subtype: string";
108
109 PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
110 {
111         StructRNA *srna;
112
113         if (PyTuple_GET_SIZE(args) > 0) {
114                 PyErr_SetString(PyExc_ValueError, "all args must be keywords");
115                 return NULL;
116         }
117
118         srna= srna_from_self(self);
119         if(srna==NULL && PyErr_Occurred()) {
120                 return NULL; /* self's type was compatible but error getting the srna */
121         }
122         else if(srna) {
123                 static char *kwlist[] = {"attr", "name", "description", "default", "options", "subtype", NULL};
124                 char *id=NULL, *name="", *description="";
125                 int def=0;
126                 PropertyRNA *prop;
127                 PyObject *pyopts= NULL;
128                 int opts=0;
129                 char *pysubtype= NULL;
130                 int subtype= PROP_NONE;
131
132                 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssiO!s:BoolProperty", (char **)kwlist, &id, &name, &description, &def, &PySet_Type, &pyopts, &pysubtype))
133                         return NULL;
134
135                 if(bpy_struct_id_used(srna, id)) {
136                         // PyErr_Format(PyExc_TypeError, "BoolProperty(): '%s' already defined.", id);
137                         // return NULL;
138                         Py_RETURN_NONE;
139                 }
140
141                 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "BoolProperty(options={...}):"))
142                         return NULL;
143
144                 if(pysubtype && RNA_enum_value_from_id(property_subtype_number_items, pysubtype, &subtype)==0) {
145                         PyErr_Format(PyExc_TypeError, "BoolProperty(subtype='%s'): invalid subtype.", pysubtype);
146                         return NULL;
147                 }
148
149                 // prop= RNA_def_boolean(srna, id, def, name, description);
150                 prop= RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
151                 RNA_def_property_boolean_default(prop, def);
152                 RNA_def_property_ui_text(prop, name, description);
153
154                 if(pyopts) {
155                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
156                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
157                 }
158                 RNA_def_property_duplicate_pointers(prop);
159                 Py_RETURN_NONE;
160         }
161         else { /* operators defer running this function */
162                 return bpy_prop_deferred_return((void *)BPy_BoolProperty, kw);
163         }
164 }
165
166 static char BPy_BoolVectorProperty_doc[] =
167 ".. function:: BoolVectorProperty(name=\"\", description=\"\", default=(False, False, False), options={'ANIMATABLE'}, subtype='NONE', size=3)\n"
168 "\n"
169 "   Returns a new vector boolean property definition.\n"
170 "\n"
171 "   :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
172 "   :type options: set\n"
173 "   :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'LAYER', 'NONE'].\n"
174 "   :type subtype: string";
175 PyObject *BPy_BoolVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
176 {
177         StructRNA *srna;
178
179         if (PyTuple_GET_SIZE(args) > 0) {
180                 PyErr_SetString(PyExc_ValueError, "all args must be keywords");
181                 return NULL;
182         }
183
184         srna= srna_from_self(self);
185         if(srna==NULL && PyErr_Occurred()) {
186                 return NULL; /* self's type was compatible but error getting the srna */
187         }
188         else if(srna) {
189                 static const char *kwlist[] = {"attr", "name", "description", "default", "options", "subtype", "size", NULL};
190                 char *id=NULL, *name="", *description="";
191                 int def[PYRNA_STACK_ARRAY]={0};
192                 int size=3;
193                 PropertyRNA *prop;
194                 PyObject *pydef= NULL;
195                 PyObject *pyopts= NULL;
196                 int opts=0;
197                 char *pysubtype= NULL;
198                 int subtype= PROP_NONE;
199
200                 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssOO!si:BoolVectorProperty", (char **)kwlist, &id, &name, &description, &pydef, &PySet_Type, &pyopts, &pysubtype, &size))
201                         return NULL;
202
203                 if(bpy_struct_id_used(srna, id)) {
204                         // PyErr_Format(PyExc_TypeError, "BoolVectorProperty(): '%s' already defined.", id);
205                         // return NULL;
206                         Py_RETURN_NONE;
207                 }
208
209                 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "BoolVectorProperty(options={...}):"))
210                         return NULL;
211
212                 if(pysubtype && RNA_enum_value_from_id(property_subtype_array_items, pysubtype, &subtype)==0) {
213                         PyErr_Format(PyExc_TypeError, "BoolVectorProperty(subtype='%s'): invalid subtype.", pysubtype);
214                         return NULL;
215                 }
216
217                 if(size < 1 || size > PYRNA_STACK_ARRAY) {
218                         PyErr_Format(PyExc_TypeError, "BoolVectorProperty(size=%d): size must be between 0 and %d.", size, PYRNA_STACK_ARRAY);
219                         return NULL;
220                 }
221
222                 if(pydef && BPyAsPrimitiveArray(def, pydef, size, &PyBool_Type, "BoolVectorProperty(default=sequence)") < 0)
223                         return NULL;
224
225                 // prop= RNA_def_boolean_array(srna, id, size, pydef ? def:NULL, name, description);
226                 prop= RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
227                 RNA_def_property_array(prop, size);
228                 if(pydef) RNA_def_property_boolean_array_default(prop, def);
229                 RNA_def_property_ui_text(prop, name, description);
230
231                 if(pyopts) {
232                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
233                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
234                 }
235                 RNA_def_property_duplicate_pointers(prop);
236                 Py_RETURN_NONE;
237         }
238         else { /* operators defer running this function */
239                 return bpy_prop_deferred_return((void *)BPy_BoolVectorProperty, kw);
240         }
241 }
242
243 static char BPy_IntProperty_doc[] =
244 ".. 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')\n"
245 "\n"
246 "   Returns a new int property definition.\n"
247 "\n"
248 "   :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
249 "   :type options: set\n"
250 "   :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
251 "   :type subtype: string";
252 PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
253 {
254         StructRNA *srna;
255
256         if (PyTuple_GET_SIZE(args) > 0) {
257                 PyErr_SetString(PyExc_ValueError, "all args must be keywords");
258                 return NULL;
259         }
260
261         srna= srna_from_self(self);
262         if(srna==NULL && PyErr_Occurred()) {
263                 return NULL; /* self's type was compatible but error getting the srna */
264         }
265         else if(srna) {
266                 static const char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "options", "subtype", NULL};
267                 char *id=NULL, *name="", *description="";
268                 int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def=0;
269                 PropertyRNA *prop;
270                 PyObject *pyopts= NULL;
271                 int opts=0;
272                 char *pysubtype= NULL;
273                 int subtype= PROP_NONE;
274
275                 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssiiiiiiO!s:IntProperty", (char **)kwlist, &id, &name, &description, &def, &min, &max, &soft_min, &soft_max, &step, &PySet_Type, &pyopts, &pysubtype))
276                         return NULL;
277
278                 if(bpy_struct_id_used(srna, id)) {
279                         // PyErr_Format(PyExc_TypeError, "IntProperty(): '%s' already defined.", id);
280                         // return NULL;
281                         Py_RETURN_NONE;
282                 }
283
284                 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "IntProperty(options={...}):"))
285                         return NULL;
286
287                 if(pysubtype && RNA_enum_value_from_id(property_subtype_number_items, pysubtype, &subtype)==0) {
288                         PyErr_Format(PyExc_TypeError, "IntProperty(subtype='%s'): invalid subtype.", pysubtype);
289                         return NULL;
290                 }
291
292                 prop= RNA_def_property(srna, id, PROP_INT, subtype);
293                 RNA_def_property_int_default(prop, def);
294                 RNA_def_property_range(prop, min, max);
295                 RNA_def_property_ui_text(prop, name, description);
296                 RNA_def_property_ui_range(prop, soft_min, soft_max, step, 3);
297
298                 if(pyopts) {
299                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
300                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
301                 }
302                 RNA_def_property_duplicate_pointers(prop);
303                 Py_RETURN_NONE;
304         }
305         else { /* operators defer running this function */
306                 return bpy_prop_deferred_return((void *)BPy_IntProperty, kw);
307         }
308 }
309
310 static char BPy_IntVectorProperty_doc[] =
311 ".. 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)\n"
312 "\n"
313 "   Returns a new vector int property definition.\n"
314 "\n"
315 "   :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
316 "   :type options: set\n"
317 "   :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'LAYER', 'NONE'].\n"
318 "   :type subtype: string";
319 PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
320 {
321         StructRNA *srna;
322
323         if (PyTuple_GET_SIZE(args) > 0) {
324                 PyErr_SetString(PyExc_ValueError, "all args must be keywords");
325                 return NULL;
326         }
327
328         srna= srna_from_self(self);
329         if(srna==NULL && PyErr_Occurred()) {
330                 return NULL; /* self's type was compatible but error getting the srna */
331         }
332         else if(srna) {
333                 static const char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "options", "subtype", "size", NULL};
334                 char *id=NULL, *name="", *description="";
335                 int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def[PYRNA_STACK_ARRAY]={0};
336                 int size=3;
337                 PropertyRNA *prop;
338                 PyObject *pydef= NULL;
339                 PyObject *pyopts= NULL;
340                 int opts=0;
341                 char *pysubtype= NULL;
342                 int subtype= PROP_NONE;
343
344                 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssOiiiiO!si:IntVectorProperty", (char **)kwlist, &id, &name, &description, &pydef, &min, &max, &soft_min, &soft_max, &PySet_Type, &pyopts, &pysubtype, &size))
345                         return NULL;
346
347                 if(bpy_struct_id_used(srna, id)) {
348                         // PyErr_Format(PyExc_TypeError, "IntVectorProperty(): '%s' already defined.", id);
349                         // return NULL;
350                         Py_RETURN_NONE;
351                 }
352
353                 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "IntVectorProperty(options={...}):"))
354                         return NULL;
355
356                 if(pysubtype && RNA_enum_value_from_id(property_subtype_array_items, pysubtype, &subtype)==0) {
357                         PyErr_Format(PyExc_TypeError, "IntVectorProperty(subtype='%s'): invalid subtype.", pysubtype);
358                         return NULL;
359                 }
360
361                 if(size < 1 || size > PYRNA_STACK_ARRAY) {
362                         PyErr_Format(PyExc_TypeError, "IntVectorProperty(size=%d): size must be between 0 and %d.", size, PYRNA_STACK_ARRAY);
363                         return NULL;
364                 }
365
366                 if(pydef && BPyAsPrimitiveArray(def, pydef, size, &PyLong_Type, "IntVectorProperty(default=sequence)") < 0)
367                         return NULL;
368
369                 prop= RNA_def_property(srna, id, PROP_INT, subtype);
370                 RNA_def_property_array(prop, size);
371                 if(pydef) RNA_def_property_int_array_default(prop, def);
372                 RNA_def_property_range(prop, min, max);
373                 RNA_def_property_ui_text(prop, name, description);
374                 RNA_def_property_ui_range(prop, soft_min, soft_max, step, 3);
375
376                 if(pyopts) {
377                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
378                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
379                 }
380                 RNA_def_property_duplicate_pointers(prop);
381                 Py_RETURN_NONE;
382         }
383         else { /* operators defer running this function */
384                 return bpy_prop_deferred_return((void *)BPy_IntVectorProperty, kw);
385         }
386 }
387
388
389 static char BPy_FloatProperty_doc[] =
390 ".. 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')\n"
391 "\n"
392 "   Returns a new float property definition.\n"
393 "\n"
394 "   :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
395 "   :type options: set\n"
396 "   :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
397 "   :type subtype: string\n"
398 "   :arg unit: Enumerator in ['NONE', 'LENGTH', 'AREA', 'VOLUME', 'ROTATION', 'TIME', 'VELOCITY', 'ACCELERATION'].\n"
399 "   :type unit: string\n";
400 PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
401 {
402         StructRNA *srna;
403
404         if (PyTuple_GET_SIZE(args) > 0) {
405                 PyErr_SetString(PyExc_ValueError, "all args must be keywords");
406                 return NULL;
407         }
408
409         srna= srna_from_self(self);
410         if(srna==NULL && PyErr_Occurred()) {
411                 return NULL; /* self's type was compatible but error getting the srna */
412         }
413         else if(srna) {
414                 static const char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", "unit", NULL};
415                 char *id=NULL, *name="", *description="";
416                 float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def=0.0f;
417                 int precision= 2;
418                 PropertyRNA *prop;
419                 PyObject *pyopts= NULL;
420                 int opts=0;
421                 char *pysubtype= NULL;
422                 int subtype= PROP_NONE;
423                 char *pyunit= NULL;
424                 int unit= PROP_UNIT_NONE;
425
426                 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssffffffiO!ss:FloatProperty", (char **)kwlist, &id, &name, &description, &def, &min, &max, &soft_min, &soft_max, &step, &precision, &PySet_Type, &pyopts, &pysubtype, &pyunit))
427                         return NULL;
428
429                 if(bpy_struct_id_used(srna, id)) {
430                         // PyErr_Format(PyExc_TypeError, "FloatProperty(): '%s' already defined.", id);
431                         // return NULL;
432                         Py_RETURN_NONE;
433                 }
434
435                 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "FloatProperty(options={...}):"))
436                         return NULL;
437
438                 if(pysubtype && RNA_enum_value_from_id(property_subtype_number_items, pysubtype, &subtype)==0) {
439                         PyErr_Format(PyExc_TypeError, "FloatProperty(subtype='%s'): invalid subtype.", pysubtype);
440                         return NULL;
441                 }
442
443                 if(pyunit && RNA_enum_value_from_id(property_unit_items, pyunit, &unit)==0) {
444                         PyErr_Format(PyExc_TypeError, "FloatProperty(unit='%s'): invalid unit.");
445                         return NULL;
446                 }
447
448                 prop= RNA_def_property(srna, id, PROP_FLOAT, subtype | unit);
449                 RNA_def_property_float_default(prop, def);
450                 RNA_def_property_range(prop, min, max);
451                 RNA_def_property_ui_text(prop, name, description);
452                 RNA_def_property_ui_range(prop, soft_min, soft_max, step, precision);
453
454                 if(pyopts) {
455                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
456                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
457                 }
458                 RNA_def_property_duplicate_pointers(prop);
459                 Py_RETURN_NONE;
460         }
461         else { /* operators defer running this function */
462                 return bpy_prop_deferred_return((void *)BPy_FloatProperty, kw);
463         }
464 }
465
466 static char BPy_FloatVectorProperty_doc[] =
467 ".. 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)\n"
468 "\n"
469 "   Returns a new vector float property definition.\n"
470 "\n"
471 "   :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
472 "   :type options: set\n"
473 "   :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'LAYER', 'NONE'].\n"
474 "   :type subtype: string";
475 PyObject *BPy_FloatVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
476 {
477         StructRNA *srna;
478
479         if (PyTuple_GET_SIZE(args) > 0) {
480                 PyErr_SetString(PyExc_ValueError, "all args must be keywords");
481                 return NULL;
482         }
483
484         srna= srna_from_self(self);
485         if(srna==NULL && PyErr_Occurred()) {
486                 return NULL; /* self's type was compatible but error getting the srna */
487         }
488         else if(srna) {
489                 static const char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", "size", NULL};
490                 char *id=NULL, *name="", *description="";
491                 float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def[PYRNA_STACK_ARRAY]={0.0f};
492                 int precision= 2, size=3;
493                 PropertyRNA *prop;
494                 PyObject *pydef= NULL;
495                 PyObject *pyopts= NULL;
496                 int opts=0;
497                 char *pysubtype= NULL;
498                 int subtype= PROP_NONE;
499
500                 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssOfffffiO!si:FloatVectorProperty", (char **)kwlist, &id, &name, &description, &pydef, &min, &max, &soft_min, &soft_max, &step, &precision, &PySet_Type, &pyopts, &pysubtype, &size))
501                         return NULL;
502
503                 if(bpy_struct_id_used(srna, id)) {
504                         // PyErr_Format(PyExc_TypeError, "FloatVectorProperty(): '%s' already defined.", id);
505                         // return NULL;
506                         Py_RETURN_NONE;
507                 }
508
509                 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "FloatVectorProperty(options={...}):"))
510                         return NULL;
511
512                 if(pysubtype && RNA_enum_value_from_id(property_subtype_array_items, pysubtype, &subtype)==0) {
513                         PyErr_Format(PyExc_TypeError, "FloatVectorProperty(subtype='%s'): invalid subtype.", pysubtype);
514                         return NULL;
515                 }
516
517                 if(size < 1 || size > PYRNA_STACK_ARRAY) {
518                         PyErr_Format(PyExc_TypeError, "FloatVectorProperty(size=%d): size must be between 0 and %d.", size, PYRNA_STACK_ARRAY);
519                         return NULL;
520                 }
521
522                 if(pydef && BPyAsPrimitiveArray(def, pydef, size, &PyFloat_Type, "FloatVectorProperty(default=sequence)") < 0)
523                         return NULL;
524
525                 prop= RNA_def_property(srna, id, PROP_FLOAT, subtype);
526                 RNA_def_property_array(prop, size);
527                 if(pydef) RNA_def_property_float_array_default(prop, def);
528                 RNA_def_property_range(prop, min, max);
529                 RNA_def_property_ui_text(prop, name, description);
530                 RNA_def_property_ui_range(prop, soft_min, soft_max, step, precision);
531
532                 if(pyopts) {
533                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
534                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
535                 }
536                 RNA_def_property_duplicate_pointers(prop);
537                 Py_RETURN_NONE;
538         }
539         else { /* operators defer running this function */
540                 return bpy_prop_deferred_return((void *)BPy_FloatVectorProperty, kw);
541         }
542 }
543
544 static char BPy_StringProperty_doc[] =
545 ".. function:: StringProperty(name=\"\", description=\"\", default=\"\", maxlen=0, options={'ANIMATABLE'}, subtype='NONE')\n"
546 "\n"
547 "   Returns a new string property definition.\n"
548 "\n"
549 "   :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
550 "   :type options: set\n"
551 "   :arg subtype: Enumerator in ['FILE_PATH', 'DIR_PATH', 'FILENAME', 'NONE'].\n"
552 "   :type subtype: string";
553 PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw)
554 {
555         StructRNA *srna;
556
557         if (PyTuple_GET_SIZE(args) > 0) {
558                 PyErr_SetString(PyExc_ValueError, "all args must be keywords");
559                 return NULL;
560         }
561
562         srna= srna_from_self(self);
563         if(srna==NULL && PyErr_Occurred()) {
564                 return NULL; /* self's type was compatible but error getting the srna */
565         }
566         else if(srna) {
567                 static const char *kwlist[] = {"attr", "name", "description", "default", "maxlen", "options", "subtype", NULL};
568                 char *id=NULL, *name="", *description="", *def="";
569                 int maxlen=0;
570                 PropertyRNA *prop;
571                 PyObject *pyopts= NULL;
572                 int opts=0;
573                 char *pysubtype= NULL;
574                 int subtype= PROP_NONE;
575
576                 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|sssiO!s:StringProperty", (char **)kwlist, &id, &name, &description, &def, &maxlen, &PySet_Type, &pyopts, &pysubtype))
577                         return NULL;
578
579                 if(bpy_struct_id_used(srna, id)) {
580                         // PyErr_Format(PyExc_TypeError, "StringProperty(): '%s' already defined.", id);
581                         // return NULL;
582                         Py_RETURN_NONE;
583                 }
584
585                 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "StringProperty(options={...}):"))
586                         return NULL;
587
588                 if(pysubtype && RNA_enum_value_from_id(property_subtype_string_items, pysubtype, &subtype)==0) {
589                         PyErr_Format(PyExc_TypeError, "StringProperty(subtype='%s'): invalid subtype.", pysubtype);
590                         return NULL;
591                 }
592
593                 prop= RNA_def_property(srna, id, PROP_STRING, subtype);
594                 if(maxlen != 0) RNA_def_property_string_maxlength(prop, maxlen);
595                 if(def) RNA_def_property_string_default(prop, def);
596                 RNA_def_property_ui_text(prop, name, description);
597
598                 if(pyopts) {
599                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
600                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
601                 }
602                 RNA_def_property_duplicate_pointers(prop);
603                 Py_RETURN_NONE;
604         }
605         else { /* operators defer running this function */
606                 return bpy_prop_deferred_return((void *)BPy_StringProperty, kw);
607         }
608 }
609
610 static EnumPropertyItem *enum_items_from_py(PyObject *value, const char *def, int *defvalue)
611 {
612         EnumPropertyItem *items= NULL;
613         PyObject *item;
614         int seq_len, i, totitem= 0;
615
616         if(!PySequence_Check(value)) {
617                 PyErr_SetString(PyExc_TypeError, "expected a sequence of tuples for the enum items");
618                 return NULL;
619         }
620
621         seq_len = PySequence_Length(value);
622         for(i=0; i<seq_len; i++) {
623                 EnumPropertyItem tmp= {0, "", 0, "", ""};
624
625                 item= PySequence_GetItem(value, i);
626                 if(item==NULL || PyTuple_Check(item)==0) {
627                         PyErr_SetString(PyExc_TypeError, "expected a sequence of tuples for the enum items");
628                         if(items) MEM_freeN(items);
629                         Py_XDECREF(item);
630                         return NULL;
631                 }
632
633                 if(!PyArg_ParseTuple(item, "sss", &tmp.identifier, &tmp.name, &tmp.description)) {
634                         PyErr_SetString(PyExc_TypeError, "expected an identifier, name and description in the tuple");
635                         Py_DECREF(item);
636                         return NULL;
637                 }
638
639                 tmp.value= i;
640                 RNA_enum_item_add(&items, &totitem, &tmp);
641
642                 if(def[0] && strcmp(def, tmp.identifier) == 0)
643                         *defvalue= tmp.value;
644
645                 Py_DECREF(item);
646         }
647
648         if(!def[0])
649                 *defvalue= 0;
650
651         RNA_enum_item_end(&items, &totitem);
652
653         return items;
654 }
655
656 static char BPy_EnumProperty_doc[] =
657 ".. function:: EnumProperty(items, name=\"\", description=\"\", default=\"\", options={'ANIMATABLE'})\n"
658 "\n"
659 "   Returns a new enumerator property definition.\n"
660 "\n"
661 "   :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
662 "   :type options: set\n"
663 "   :arg items: The items that make up this enumerator.\n"
664 "   :type items: sequence of string triplets";
665 PyObject *BPy_EnumProperty(PyObject *self, PyObject *args, PyObject *kw)
666 {
667         StructRNA *srna;
668
669         if (PyTuple_GET_SIZE(args) > 0) {
670                 PyErr_SetString(PyExc_ValueError, "all args must be keywords");
671                 return NULL;
672         }
673
674         srna= srna_from_self(self);
675         if(srna==NULL && PyErr_Occurred()) {
676                 return NULL; /* self's type was compatible but error getting the srna */
677         }
678         else if(srna) {
679                 static const char *kwlist[] = {"attr", "items", "name", "description", "default", "options", NULL};
680                 char *id=NULL, *name="", *description="", *def="";
681                 int defvalue=0;
682                 PyObject *items= Py_None;
683                 EnumPropertyItem *eitems;
684                 PropertyRNA *prop;
685                 PyObject *pyopts= NULL;
686                 int opts=0;
687
688                 if (!PyArg_ParseTupleAndKeywords(args, kw, "sO|sssO!:EnumProperty", (char **)kwlist, &id, &items, &name, &description, &def, &PySet_Type, &pyopts))
689                         return NULL;
690
691                 if(bpy_struct_id_used(srna, id)) {
692                         // PyErr_Format(PyExc_TypeError, "EnumProperty(): '%s' already defined.", id);
693                         // return NULL;
694                         Py_RETURN_NONE;
695                 }
696
697                 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "EnumProperty(options={...}):"))
698                         return NULL;
699
700                 eitems= enum_items_from_py(items, def, &defvalue);
701                 if(!eitems)
702                         return NULL;
703
704                 prop= RNA_def_enum(srna, id, eitems, defvalue, name, description);
705                 if(pyopts) {
706                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
707                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
708                 }
709                 RNA_def_property_duplicate_pointers(prop);
710                 MEM_freeN(eitems);
711
712                 Py_RETURN_NONE;
713         }
714         else { /* operators defer running this function */
715                 return bpy_prop_deferred_return((void *)BPy_EnumProperty, kw);
716         }
717 }
718
719 static StructRNA *pointer_type_from_py(PyObject *value)
720 {
721         StructRNA *srna;
722
723         srna= srna_from_self(value);
724         if(!srna) {
725                 PyErr_SetString(PyExc_SystemError, "expected an RNA type derived from IDPropertyGroup");
726                 return NULL;
727         }
728
729         if(!RNA_struct_is_a(srna, &RNA_IDPropertyGroup)) {
730                 PyErr_SetString(PyExc_SystemError, "expected an RNA type derived from IDPropertyGroup");
731                 return NULL;
732         }
733
734         return srna;
735 }
736
737 static char BPy_PointerProperty_doc[] =
738 ".. function:: PointerProperty(items, type=\"\", description=\"\", default=\"\", options={'ANIMATABLE'})\n"
739 "\n"
740 "   Returns a new pointer property definition.\n"
741 "\n"
742 "   :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
743 "   :type options: set\n"
744 "   :arg type: Dynamic type from :mod:`bpy.types`.\n"
745 "   :type type: class";
746 PyObject *BPy_PointerProperty(PyObject *self, PyObject *args, PyObject *kw)
747 {
748         StructRNA *srna;
749
750         if (PyTuple_GET_SIZE(args) > 0) {
751                 PyErr_SetString(PyExc_ValueError, "all args must be keywords");
752                 return NULL;
753         }
754
755         srna= srna_from_self(self);
756         if(srna==NULL && PyErr_Occurred()) {
757                 return NULL; /* self's type was compatible but error getting the srna */
758         }
759         else if(srna) {
760                 static const char *kwlist[] = {"attr", "type", "name", "description", "options", NULL};
761                 char *id=NULL, *name="", *description="";
762                 PropertyRNA *prop;
763                 StructRNA *ptype;
764                 PyObject *type= Py_None;
765                 PyObject *pyopts= NULL;
766                 int opts=0;
767
768                 if (!PyArg_ParseTupleAndKeywords(args, kw, "sO|ssO!:PointerProperty", (char **)kwlist, &id, &type, &name, &description, &PySet_Type, &pyopts))
769                         return NULL;
770
771                 if(bpy_struct_id_used(srna, id)) {
772                         // PyErr_Format(PyExc_TypeError, "PointerProperty(): '%s' already defined.", id);
773                         // return NULL;
774                         Py_RETURN_NONE;
775                 }
776
777                 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "PointerProperty(options={...}):"))
778                         return NULL;
779
780                 ptype= pointer_type_from_py(type);
781                 if(!ptype)
782                         return NULL;
783
784                 prop= RNA_def_pointer_runtime(srna, id, ptype, name, description);
785                 if(pyopts) {
786                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
787                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
788                 }
789                 RNA_def_property_duplicate_pointers(prop);
790                 Py_RETURN_NONE;
791         }
792         else { /* operators defer running this function */
793                 return bpy_prop_deferred_return((void *)BPy_PointerProperty, kw);
794         }
795         return NULL;
796 }
797
798 static char BPy_CollectionProperty_doc[] =
799 ".. function:: CollectionProperty(items, type=\"\", description=\"\", default=\"\", options={'ANIMATABLE'})\n"
800 "\n"
801 "   Returns a new collection property definition.\n"
802 "\n"
803 "   :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
804 "   :type options: set\n"
805 "   :arg type: Dynamic type from :mod:`bpy.types`.\n"
806 "   :type type: class";
807 PyObject *BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw)
808 {
809         StructRNA *srna;
810
811         if (PyTuple_GET_SIZE(args) > 0) {
812                 PyErr_SetString(PyExc_ValueError, "all args must be keywords");
813                 return NULL;
814         }
815
816         srna= srna_from_self(self);
817         if(srna==NULL && PyErr_Occurred()) {
818                 return NULL; /* self's type was compatible but error getting the srna */
819         }
820         else if(srna) {
821                 static const char *kwlist[] = {"attr", "type", "name", "description", "options", NULL};
822                 char *id=NULL, *name="", *description="";
823                 PropertyRNA *prop;
824                 StructRNA *ptype;
825                 PyObject *type= Py_None;
826                 PyObject *pyopts= NULL;
827                 int opts=0;
828
829                 if (!PyArg_ParseTupleAndKeywords(args, kw, "sO|ssO!:CollectionProperty", (char **)kwlist, &id, &type, &name, &description, &PySet_Type, &pyopts))
830                         return NULL;
831
832                 if(bpy_struct_id_used(srna, id)) {
833                         // PyErr_Format(PyExc_TypeError, "CollectionProperty(): '%s' already defined.", id);
834                         // return NULL;
835                         Py_RETURN_NONE;
836                 }
837
838                 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "CollectionProperty(options={...}):"))
839                         return NULL;
840
841                 ptype= pointer_type_from_py(type);
842                 if(!ptype)
843                         return NULL;
844
845                 prop= RNA_def_collection_runtime(srna, id, ptype, name, description);
846                 if(pyopts) {
847                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
848                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
849                 }
850                 RNA_def_property_duplicate_pointers(prop);
851                 Py_RETURN_NONE;
852         }
853         else { /* operators defer running this function */
854                 return bpy_prop_deferred_return((void *)BPy_CollectionProperty, kw);
855         }
856         return NULL;
857 }
858
859 static struct PyMethodDef props_methods[] = {
860         {"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS|METH_KEYWORDS, BPy_BoolProperty_doc},
861         {"BoolVectorProperty", (PyCFunction)BPy_BoolVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_BoolVectorProperty_doc},
862         {"IntProperty", (PyCFunction)BPy_IntProperty, METH_VARARGS|METH_KEYWORDS, BPy_IntProperty_doc},
863         {"IntVectorProperty", (PyCFunction)BPy_IntVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_IntVectorProperty_doc},
864         {"FloatProperty", (PyCFunction)BPy_FloatProperty, METH_VARARGS|METH_KEYWORDS, BPy_FloatProperty_doc},
865         {"FloatVectorProperty", (PyCFunction)BPy_FloatVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_FloatVectorProperty_doc},
866         {"StringProperty", (PyCFunction)BPy_StringProperty, METH_VARARGS|METH_KEYWORDS, BPy_StringProperty_doc},
867         {"EnumProperty", (PyCFunction)BPy_EnumProperty, METH_VARARGS|METH_KEYWORDS, BPy_EnumProperty_doc},
868         {"PointerProperty", (PyCFunction)BPy_PointerProperty, METH_VARARGS|METH_KEYWORDS, BPy_PointerProperty_doc},
869         {"CollectionProperty", (PyCFunction)BPy_CollectionProperty, METH_VARARGS|METH_KEYWORDS, BPy_CollectionProperty_doc},
870         {NULL, NULL, 0, NULL}
871 };
872
873 static struct PyModuleDef props_module = {
874         PyModuleDef_HEAD_INIT,
875         "bpy.props",
876         "This module defines properties to extend blenders internal data, the result of these functions"
877         " is used to assign properties to classes registered with blender and can't be used directly.",
878         -1,/* multiple "initialization" just copies the module dict. */
879         props_methods,
880         NULL, NULL, NULL, NULL
881 };
882
883 PyObject *BPY_rna_props( void )
884 {
885         PyObject *submodule;
886         submodule= PyModule_Create(&props_module);
887         PyDict_SetItemString(PySys_GetObject("modules"), props_module.m_name, submodule);
888
889         /* INCREF since its its assumed that all these functions return the
890          * module with a new ref like PyDict_New, since they are passed to
891           * PyModule_AddObject which steals a ref */
892         Py_INCREF(submodule);
893
894         return submodule;
895 }