4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * Contributor(s): Campbell Barton
22 * ***** END GPL LICENSE BLOCK *****
25 #include "bpy_props.h"
29 #include "RNA_access.h"
30 #include "RNA_define.h" /* for defining our own rna */
31 #include "RNA_enum_types.h"
33 #include "MEM_guardedalloc.h"
35 #include "float.h" /* FLT_MIN/MAX */
37 EnumPropertyItem property_flag_items[] = {
38 {PROP_HIDDEN, "HIDDEN", 0, "Hidden", ""},
39 {PROP_ANIMATABLE, "ANIMATABLE", 0, "Animateable", ""},
40 {0, NULL, 0, NULL, NULL}};
43 EnumPropertyItem property_subtype_string_items[] = {
44 {PROP_FILEPATH, "FILE_PATH", 0, "File Path", ""},
45 {PROP_DIRPATH, "DIR_PATH", 0, "Directory Path", ""},
46 {PROP_FILENAME, "FILENAME", 0, "Filename", ""},
48 {PROP_NONE, "NONE", 0, "None", ""},
49 {0, NULL, 0, NULL, NULL}};
51 EnumPropertyItem property_subtype_number_items[] = {
52 {PROP_UNSIGNED, "UNSIGNED", 0, "Unsigned", ""},
53 {PROP_PERCENTAGE, "PERCENTAGE", 0, "Percentage", ""},
54 {PROP_FACTOR, "FACTOR", 0, "Factor", ""},
55 {PROP_ANGLE, "ANGLE", 0, "Angle", ""},
56 {PROP_TIME, "TIME", 0, "Time", ""},
57 {PROP_DISTANCE, "DISTANCE", 0, "Distance", ""},
59 {PROP_NONE, "NONE", 0, "None", ""},
60 {0, NULL, 0, NULL, NULL}};
62 EnumPropertyItem property_subtype_array_items[] = {
63 {PROP_COLOR, "COLOR", 0, "Color", ""},
64 {PROP_TRANSLATION, "TRANSLATION", 0, "Translation", ""},
65 {PROP_DIRECTION, "DIRECTION", 0, "Direction", ""},
66 {PROP_VELOCITY, "VELOCITY", 0, "Velocity", ""},
67 {PROP_ACCELERATION, "ACCELERATION", 0, "Acceleration", ""},
68 {PROP_MATRIX, "MATRIX", 0, "Matrix", ""},
69 {PROP_EULER, "EULER", 0, "Euler", ""},
70 {PROP_QUATERNION, "QUATERNION", 0, "Quaternion", ""},
71 {PROP_AXISANGLE, "AXISANGLE", 0, "Axis Angle", ""},
72 {PROP_XYZ, "XYZ", 0, "XYZ", ""},
73 {PROP_COLOR_GAMMA, "COLOR_GAMMA", 0, "Color Gamma", ""},
75 {PROP_NONE, "NONE", 0, "None", ""},
76 {0, NULL, 0, NULL, NULL}};
78 /* operators use this so it can store the args given but defer running
79 * it until the operator runs where these values are used to setup the
80 * default args for that operator instance */
81 static PyObject *bpy_prop_deferred_return(void *func, PyObject *kw)
83 PyObject *ret = PyTuple_New(2);
84 PyTuple_SET_ITEM(ret, 0, PyCapsule_New(func, NULL, NULL));
85 PyTuple_SET_ITEM(ret, 1, kw);
90 /* Function that sets RNA, NOTE - self is NULL when called from python, but being abused from C so we can pass the srna allong
91 * This isnt incorrect since its a python object - but be careful */
92 static char BPy_BoolProperty_doc[] =
93 ".. function:: BoolProperty(name=\"\", description=\"\", default=False, options={'ANIMATABLE'}, subtype='NONE')\n"
95 " Returns a new boolean property definition.\n"
97 " :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
98 " :type options: set\n"
99 " :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
100 " :type subtype: string";
102 PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
106 if (PyTuple_GET_SIZE(args) > 0) {
107 PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
111 srna= srna_from_self(self);
112 if(srna==NULL && PyErr_Occurred()) {
113 return NULL; /* self's type was compatible but error getting the srna */
116 static char *kwlist[] = {"attr", "name", "description", "default", "options", "subtype", NULL};
117 char *id=NULL, *name="", *description="";
120 PyObject *pyopts= NULL;
122 char *pysubtype= NULL;
123 int subtype= PROP_NONE;
125 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssiO!s:BoolProperty", kwlist, &id, &name, &description, &def, &PySet_Type, &pyopts, &pysubtype))
128 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "BoolProperty(options={...}):"))
131 if(pysubtype && RNA_enum_value_from_id(property_subtype_number_items, pysubtype, &subtype)==0) {
132 PyErr_Format(PyExc_TypeError, "BoolProperty(subtype='%s'): invalid subtype.");
136 // prop= RNA_def_boolean(srna, id, def, name, description);
137 prop= RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
138 RNA_def_property_boolean_default(prop, def);
139 RNA_def_property_ui_text(prop, name, description);
142 if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
143 if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
145 RNA_def_property_duplicate_pointers(prop);
148 else { /* operators defer running this function */
149 return bpy_prop_deferred_return((void *)BPy_BoolProperty, kw);
153 static char BPy_BoolVectorProperty_doc[] =
154 ".. function:: BoolVectorProperty(name=\"\", description=\"\", default=(False, False, False), options={'ANIMATABLE'}, subtype='NONE', size=3)\n"
156 " Returns a new vector boolean property definition.\n"
158 " :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
159 " :type options: set\n"
160 " :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'NONE'].\n"
161 " :type subtype: string";
162 PyObject *BPy_BoolVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
166 if (PyTuple_GET_SIZE(args) > 0) {
167 PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
171 srna= srna_from_self(self);
172 if(srna==NULL && PyErr_Occurred()) {
173 return NULL; /* self's type was compatible but error getting the srna */
176 static char *kwlist[] = {"attr", "name", "description", "default", "options", "subtype", "size", NULL};
177 char *id=NULL, *name="", *description="";
178 int def[PYRNA_STACK_ARRAY]={0};
181 PyObject *pydef= NULL;
182 PyObject *pyopts= NULL;
184 char *pysubtype= NULL;
185 int subtype= PROP_NONE;
187 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssOO!si:BoolVectorProperty", kwlist, &id, &name, &description, &pydef, &PySet_Type, &pyopts, &pysubtype, &size))
190 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "BoolVectorProperty(options={...}):"))
193 if(pysubtype && RNA_enum_value_from_id(property_subtype_array_items, pysubtype, &subtype)==0) {
194 PyErr_Format(PyExc_TypeError, "BoolVectorProperty(subtype='%s'): invalid subtype.");
198 if(size < 1 || size > PYRNA_STACK_ARRAY) {
199 PyErr_Format(PyExc_TypeError, "BoolVectorProperty(size=%d): size must be between 0 and %d.", size, PYRNA_STACK_ARRAY);
203 if(pydef && BPyAsPrimitiveArray(def, pydef, size, &PyBool_Type, "BoolVectorProperty(default=sequence)") < 0)
206 // prop= RNA_def_boolean_array(srna, id, size, pydef ? def:NULL, name, description);
207 prop= RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
208 RNA_def_property_array(prop, size);
209 if(pydef) RNA_def_property_boolean_array_default(prop, def);
210 RNA_def_property_ui_text(prop, name, description);
213 if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
214 if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
216 RNA_def_property_duplicate_pointers(prop);
219 else { /* operators defer running this function */
220 return bpy_prop_deferred_return((void *)BPy_BoolVectorProperty, kw);
224 static char BPy_IntProperty_doc[] =
225 ".. 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"
227 " Returns a new int property definition.\n"
229 " :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
230 " :type options: set\n"
231 " :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
232 " :type subtype: string";
233 PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
237 if (PyTuple_GET_SIZE(args) > 0) {
238 PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
242 srna= srna_from_self(self);
243 if(srna==NULL && PyErr_Occurred()) {
244 return NULL; /* self's type was compatible but error getting the srna */
247 static char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "options", "subtype", NULL};
248 char *id=NULL, *name="", *description="";
249 int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def=0;
251 PyObject *pyopts= NULL;
253 char *pysubtype= NULL;
254 int subtype= PROP_NONE;
256 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssiiiiiiO!s:IntProperty", kwlist, &id, &name, &description, &def, &min, &max, &soft_min, &soft_max, &step, &PySet_Type, &pyopts, &pysubtype))
259 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "IntProperty(options={...}):"))
262 if(pysubtype && RNA_enum_value_from_id(property_subtype_number_items, pysubtype, &subtype)==0) {
263 PyErr_Format(PyExc_TypeError, "IntProperty(subtype='%s'): invalid subtype.");
267 prop= RNA_def_property(srna, id, PROP_INT, subtype);
268 RNA_def_property_int_default(prop, def);
269 RNA_def_property_range(prop, min, max);
270 RNA_def_property_ui_text(prop, name, description);
271 RNA_def_property_ui_range(prop, soft_min, soft_max, step, 3);
274 if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
275 if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
277 RNA_def_property_duplicate_pointers(prop);
280 else { /* operators defer running this function */
281 return bpy_prop_deferred_return((void *)BPy_IntProperty, kw);
285 static char BPy_IntVectorProperty_doc[] =
286 ".. 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"
288 " Returns a new vector int property definition.\n"
290 " :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
291 " :type options: set\n"
292 " :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'NONE'].\n"
293 " :type subtype: string";
294 PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
298 if (PyTuple_GET_SIZE(args) > 0) {
299 PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
303 srna= srna_from_self(self);
304 if(srna==NULL && PyErr_Occurred()) {
305 return NULL; /* self's type was compatible but error getting the srna */
308 static char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "options", "subtype", "size", NULL};
309 char *id=NULL, *name="", *description="";
310 int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def[PYRNA_STACK_ARRAY]={0};
313 PyObject *pydef= NULL;
314 PyObject *pyopts= NULL;
316 char *pysubtype= NULL;
317 int subtype= PROP_NONE;
319 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssOiiiiO!si:IntVectorProperty", kwlist, &id, &name, &description, &pydef, &min, &max, &soft_min, &soft_max, &PySet_Type, &pyopts, &pysubtype, &size))
322 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "IntVectorProperty(options={...}):"))
325 if(pysubtype && RNA_enum_value_from_id(property_subtype_array_items, pysubtype, &subtype)==0) {
326 PyErr_Format(PyExc_TypeError, "IntVectorProperty(subtype='%s'): invalid subtype.");
330 if(size < 1 || size > PYRNA_STACK_ARRAY) {
331 PyErr_Format(PyExc_TypeError, "IntVectorProperty(size=%d): size must be between 0 and %d.", size, PYRNA_STACK_ARRAY);
335 if(pydef && BPyAsPrimitiveArray(def, pydef, size, &PyLong_Type, "IntVectorProperty(default=sequence)") < 0)
338 prop= RNA_def_property(srna, id, PROP_INT, subtype);
339 RNA_def_property_array(prop, size);
340 if(pydef) RNA_def_property_int_array_default(prop, def);
341 RNA_def_property_range(prop, min, max);
342 RNA_def_property_ui_text(prop, name, description);
343 RNA_def_property_ui_range(prop, soft_min, soft_max, step, 3);
346 if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
347 if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
349 RNA_def_property_duplicate_pointers(prop);
352 else { /* operators defer running this function */
353 return bpy_prop_deferred_return((void *)BPy_IntVectorProperty, kw);
358 static char BPy_FloatProperty_doc[] =
359 ".. 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"
361 " Returns a new float property definition.\n"
363 " :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
364 " :type options: set\n"
365 " :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
366 " :type subtype: string\n"
367 " :arg unit: Enumerator in ['NONE', 'LENGTH', 'AREA', 'VOLUME', 'ROTATION', 'TIME', 'VELOCITY', 'ACCELERATION'].\n"
368 " :type unit: string\n";
369 PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
373 if (PyTuple_GET_SIZE(args) > 0) {
374 PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
378 srna= srna_from_self(self);
379 if(srna==NULL && PyErr_Occurred()) {
380 return NULL; /* self's type was compatible but error getting the srna */
383 static char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", "unit", NULL};
384 char *id=NULL, *name="", *description="";
385 float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def=0.0f;
388 PyObject *pyopts= NULL;
390 char *pysubtype= NULL;
391 int subtype= PROP_NONE;
393 int unit= PROP_UNIT_NONE;
395 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssffffffiO!ss:FloatProperty", kwlist, &id, &name, &description, &def, &min, &max, &soft_min, &soft_max, &step, &precision, &PySet_Type, &pyopts, &pysubtype, &pyunit))
398 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "FloatProperty(options={...}):"))
401 if(pysubtype && RNA_enum_value_from_id(property_subtype_number_items, pysubtype, &subtype)==0) {
402 PyErr_Format(PyExc_TypeError, "FloatProperty(subtype='%s'): invalid subtype.");
406 if(pyunit && RNA_enum_value_from_id(property_unit_items, pyunit, &unit)==0) {
407 PyErr_Format(PyExc_TypeError, "FloatProperty(unit='%s'): invalid unit.");
411 prop= RNA_def_property(srna, id, PROP_FLOAT, subtype | unit);
412 RNA_def_property_float_default(prop, def);
413 RNA_def_property_range(prop, min, max);
414 RNA_def_property_ui_text(prop, name, description);
415 RNA_def_property_ui_range(prop, soft_min, soft_max, step, precision);
418 if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
419 if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
421 RNA_def_property_duplicate_pointers(prop);
424 else { /* operators defer running this function */
425 return bpy_prop_deferred_return((void *)BPy_FloatProperty, kw);
429 static char BPy_FloatVectorProperty_doc[] =
430 ".. 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"
432 " Returns a new vector float property definition.\n"
434 " :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
435 " :type options: set\n"
436 " :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'NONE'].\n"
437 " :type subtype: string";
438 PyObject *BPy_FloatVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
442 if (PyTuple_GET_SIZE(args) > 0) {
443 PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
447 srna= srna_from_self(self);
448 if(srna==NULL && PyErr_Occurred()) {
449 return NULL; /* self's type was compatible but error getting the srna */
452 static char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", "size", NULL};
453 char *id=NULL, *name="", *description="";
454 float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def[PYRNA_STACK_ARRAY]={0.0f};
455 int precision= 2, size=3;
457 PyObject *pydef= NULL;
458 PyObject *pyopts= NULL;
460 char *pysubtype= NULL;
461 int subtype= PROP_NONE;
463 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssOfffffiO!si:FloatVectorProperty", kwlist, &id, &name, &description, &pydef, &min, &max, &soft_min, &soft_max, &step, &precision, &PySet_Type, &pyopts, &pysubtype, &size))
466 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "FloatVectorProperty(options={...}):"))
469 if(pysubtype && RNA_enum_value_from_id(property_subtype_array_items, pysubtype, &subtype)==0) {
470 PyErr_Format(PyExc_TypeError, "FloatVectorProperty(subtype='%s'): invalid subtype.");
474 if(size < 1 || size > PYRNA_STACK_ARRAY) {
475 PyErr_Format(PyExc_TypeError, "FloatVectorProperty(size=%d): size must be between 0 and %d.", size, PYRNA_STACK_ARRAY);
479 if(pydef && BPyAsPrimitiveArray(def, pydef, size, &PyFloat_Type, "FloatVectorProperty(default=sequence)") < 0)
482 prop= RNA_def_property(srna, id, PROP_FLOAT, subtype);
483 RNA_def_property_array(prop, size);
484 if(pydef) RNA_def_property_float_array_default(prop, def);
485 RNA_def_property_range(prop, min, max);
486 RNA_def_property_ui_text(prop, name, description);
487 RNA_def_property_ui_range(prop, soft_min, soft_max, step, precision);
490 if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
491 if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
493 RNA_def_property_duplicate_pointers(prop);
496 else { /* operators defer running this function */
497 return bpy_prop_deferred_return((void *)BPy_FloatVectorProperty, kw);
501 static char BPy_StringProperty_doc[] =
502 ".. function:: StringProperty(name=\"\", description=\"\", default=\"\", maxlen=0, options={'ANIMATABLE'}, subtype='NONE')\n"
504 " Returns a new string property definition.\n"
506 " :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
507 " :type options: set\n"
508 " :arg subtype: Enumerator in ['FILE_PATH', 'DIR_PATH', 'FILENAME', 'NONE'].\n"
509 " :type subtype: string";
510 PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw)
514 if (PyTuple_GET_SIZE(args) > 0) {
515 PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
519 srna= srna_from_self(self);
520 if(srna==NULL && PyErr_Occurred()) {
521 return NULL; /* self's type was compatible but error getting the srna */
524 static char *kwlist[] = {"attr", "name", "description", "default", "maxlen", "options", "subtype", NULL};
525 char *id=NULL, *name="", *description="", *def="";
528 PyObject *pyopts= NULL;
530 char *pysubtype= NULL;
531 int subtype= PROP_NONE;
533 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|sssiO!s:StringProperty", kwlist, &id, &name, &description, &def, &maxlen, &PySet_Type, &pyopts, &pysubtype))
536 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "StringProperty(options={...}):"))
539 if(pysubtype && RNA_enum_value_from_id(property_subtype_string_items, pysubtype, &subtype)==0) {
540 PyErr_Format(PyExc_TypeError, "StringProperty(subtype='%s'): invalid subtype.");
544 prop= RNA_def_property(srna, id, PROP_STRING, subtype);
545 if(maxlen != 0) RNA_def_property_string_maxlength(prop, maxlen);
546 if(def) RNA_def_property_string_default(prop, def);
547 RNA_def_property_ui_text(prop, name, description);
550 if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
551 if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
553 RNA_def_property_duplicate_pointers(prop);
556 else { /* operators defer running this function */
557 return bpy_prop_deferred_return((void *)BPy_StringProperty, kw);
561 static EnumPropertyItem *enum_items_from_py(PyObject *value, const char *def, int *defvalue)
563 EnumPropertyItem *items= NULL;
565 int seq_len, i, totitem= 0;
567 if(!PySequence_Check(value)) {
568 PyErr_SetString(PyExc_TypeError, "expected a sequence of tuples for the enum items");
572 seq_len = PySequence_Length(value);
573 for(i=0; i<seq_len; i++) {
574 EnumPropertyItem tmp= {0, "", 0, "", ""};
576 item= PySequence_GetItem(value, i);
577 if(item==NULL || PyTuple_Check(item)==0) {
578 PyErr_SetString(PyExc_TypeError, "expected a sequence of tuples for the enum items");
579 if(items) MEM_freeN(items);
584 if(!PyArg_ParseTuple(item, "sss", &tmp.identifier, &tmp.name, &tmp.description)) {
585 PyErr_SetString(PyExc_TypeError, "expected an identifier, name and description in the tuple");
591 RNA_enum_item_add(&items, &totitem, &tmp);
593 if(def[0] && strcmp(def, tmp.identifier) == 0)
594 *defvalue= tmp.value;
602 RNA_enum_item_end(&items, &totitem);
607 static char BPy_EnumProperty_doc[] =
608 ".. function:: EnumProperty(items, name=\"\", description=\"\", default=\"\", options={'ANIMATABLE'})\n"
610 " Returns a new enumerator property definition.\n"
612 " :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
613 " :type options: set\n"
614 " :arg items: The items that make up this enumerator.\n"
615 " :type items: sequence of string triplets";
616 PyObject *BPy_EnumProperty(PyObject *self, PyObject *args, PyObject *kw)
620 if (PyTuple_GET_SIZE(args) > 0) {
621 PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
625 srna= srna_from_self(self);
626 if(srna==NULL && PyErr_Occurred()) {
627 return NULL; /* self's type was compatible but error getting the srna */
630 static char *kwlist[] = {"attr", "items", "name", "description", "default", "options", NULL};
631 char *id=NULL, *name="", *description="", *def="";
633 PyObject *items= Py_None;
634 EnumPropertyItem *eitems;
636 PyObject *pyopts= NULL;
639 if (!PyArg_ParseTupleAndKeywords(args, kw, "sO|sssO!:EnumProperty", kwlist, &id, &items, &name, &description, &def, &PySet_Type, &pyopts))
642 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "EnumProperty(options={...}):"))
645 eitems= enum_items_from_py(items, def, &defvalue);
649 prop= RNA_def_enum(srna, id, eitems, defvalue, name, description);
651 if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
652 if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
654 RNA_def_property_duplicate_pointers(prop);
659 else { /* operators defer running this function */
660 return bpy_prop_deferred_return((void *)BPy_EnumProperty, kw);
664 static StructRNA *pointer_type_from_py(PyObject *value)
668 srna= srna_from_self(value);
670 PyErr_SetString(PyExc_SystemError, "expected an RNA type derived from IDPropertyGroup");
674 if(!RNA_struct_is_a(srna, &RNA_IDPropertyGroup)) {
675 PyErr_SetString(PyExc_SystemError, "expected an RNA type derived from IDPropertyGroup");
682 static char BPy_PointerProperty_doc[] =
683 ".. function:: PointerProperty(items, type=\"\", description=\"\", default=\"\", options={'ANIMATABLE'})\n"
685 " Returns a new pointer property definition.\n"
687 " :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n"
688 " :type options: set\n"
689 " :arg type: Dynamic type from :mod:`bpy.types`.\n"
690 " :type type: class";
691 PyObject *BPy_PointerProperty(PyObject *self, PyObject *args, PyObject *kw)
695 if (PyTuple_GET_SIZE(args) > 0) {
696 PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
700 srna= srna_from_self(self);
701 if(srna==NULL && PyErr_Occurred()) {
702 return NULL; /* self's type was compatible but error getting the srna */
705 static char *kwlist[] = {"attr", "type", "name", "description", "options", NULL};
706 char *id=NULL, *name="", *description="";
709 PyObject *type= Py_None;
710 PyObject *pyopts= NULL;
713 if (!PyArg_ParseTupleAndKeywords(args, kw, "sO|ssO!:PointerProperty", kwlist, &id, &type, &name, &description, &PySet_Type, &pyopts))
716 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "PointerProperty(options={...}):"))
719 ptype= pointer_type_from_py(type);
723 prop= RNA_def_pointer_runtime(srna, id, ptype, name, description);
725 if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
726 if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
728 RNA_def_property_duplicate_pointers(prop);
731 else { /* operators defer running this function */
732 return bpy_prop_deferred_return((void *)BPy_PointerProperty, kw);
737 static char BPy_CollectionProperty_doc[] =
738 ".. function:: CollectionProperty(items, type=\"\", description=\"\", default=\"\", options={'ANIMATABLE'})\n"
740 " Returns a new collection property definition.\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_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw)
750 if (PyTuple_GET_SIZE(args) > 0) {
751 PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
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 */
760 static char *kwlist[] = {"attr", "type", "name", "description", "options", NULL};
761 char *id=NULL, *name="", *description="";
764 PyObject *type= Py_None;
765 PyObject *pyopts= NULL;
768 if (!PyArg_ParseTupleAndKeywords(args, kw, "sO|ssO!:CollectionProperty", kwlist, &id, &type, &name, &description, &PySet_Type, &pyopts))
771 if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "CollectionProperty(options={...}):"))
774 ptype= pointer_type_from_py(type);
778 prop= RNA_def_collection_runtime(srna, id, ptype, name, description);
780 if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
781 if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
783 RNA_def_property_duplicate_pointers(prop);
786 else { /* operators defer running this function */
787 return bpy_prop_deferred_return((void *)BPy_CollectionProperty, kw);
792 static struct PyMethodDef props_methods[] = {
793 {"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS|METH_KEYWORDS, BPy_BoolProperty_doc},
794 {"BoolVectorProperty", (PyCFunction)BPy_BoolVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_BoolVectorProperty_doc},
795 {"IntProperty", (PyCFunction)BPy_IntProperty, METH_VARARGS|METH_KEYWORDS, BPy_IntProperty_doc},
796 {"IntVectorProperty", (PyCFunction)BPy_IntVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_IntVectorProperty_doc},
797 {"FloatProperty", (PyCFunction)BPy_FloatProperty, METH_VARARGS|METH_KEYWORDS, BPy_FloatProperty_doc},
798 {"FloatVectorProperty", (PyCFunction)BPy_FloatVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_FloatVectorProperty_doc},
799 {"StringProperty", (PyCFunction)BPy_StringProperty, METH_VARARGS|METH_KEYWORDS, BPy_StringProperty_doc},
800 {"EnumProperty", (PyCFunction)BPy_EnumProperty, METH_VARARGS|METH_KEYWORDS, BPy_EnumProperty_doc},
801 {"PointerProperty", (PyCFunction)BPy_PointerProperty, METH_VARARGS|METH_KEYWORDS, BPy_PointerProperty_doc},
802 {"CollectionProperty", (PyCFunction)BPy_CollectionProperty, METH_VARARGS|METH_KEYWORDS, BPy_CollectionProperty_doc},
803 {NULL, NULL, 0, NULL}
806 static struct PyModuleDef props_module = {
807 PyModuleDef_HEAD_INIT,
809 "This module defines properties to extend blenders internal data, the result of these functions"
810 " is used to assign properties to classes registered with blender and can't be used directly.",
811 -1,/* multiple "initialization" just copies the module dict. */
813 NULL, NULL, NULL, NULL
816 PyObject *BPY_rna_props( void )
819 submodule= PyModule_Create(&props_module);
820 PyDict_SetItemString(PySys_GetObject("modules"), props_module.m_name, submodule);
822 /* INCREF since its its assumed that all these functions return the
823 * module with a new ref like PyDict_New, since they are passed to
824 * PyModule_AddObject which steals a ref */
825 Py_INCREF(submodule);