/** * $Id$ * * ***** BEGIN GPL LICENSE BLOCK ***** * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Contributor(s): Campbell Barton * * ***** END GPL LICENSE BLOCK ***** */ #include "bpy_props.h" #include "bpy_rna.h" #include "bpy_util.h" #include "RNA_access.h" #include "RNA_define.h" /* for defining our own rna */ #include "MEM_guardedalloc.h" #include "float.h" /* FLT_MIN/MAX */ /* operators use this so it can store the args given but defer running * it until the operator runs where these values are used to setup the * default args for that operator instance */ static PyObject *bpy_prop_deferred_return(void *func, PyObject *kw) { PyObject *ret = PyTuple_New(2); PyTuple_SET_ITEM(ret, 0, PyCObject_FromVoidPtr(func, NULL)); PyTuple_SET_ITEM(ret, 1, kw); Py_INCREF(kw); return ret; } /* Function that sets RNA, NOTE - self is NULL when called from python, but being abused from C so we can pass the srna allong * This isnt incorrect since its a python object - but be careful */ static char BPy_BoolProperty_doc[] = ".. function:: BoolProperty(name=\"\", description=\"\", default=False, hidden=False)\n" "\n" " Returns a new boolean property definition.."; PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw) { StructRNA *srna; if (PyTuple_GET_SIZE(args) > 0) { PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this. return NULL; } srna= srna_from_self(self); if(srna==NULL && PyErr_Occurred()) { return NULL; /* self's type was compatible but error getting the srna */ } else if(srna) { static char *kwlist[] = {"attr", "name", "description", "default", "hidden", NULL}; char *id=NULL, *name="", *description=""; int def=0, hidden=0; PropertyRNA *prop; if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssii:BoolProperty", kwlist, &id, &name, &description, &def, &hidden)) return NULL; prop= RNA_def_boolean(srna, id, def, name, description); if(hidden) RNA_def_property_flag(prop, PROP_HIDDEN); RNA_def_property_duplicate_pointers(prop); Py_RETURN_NONE; } else { /* operators defer running this function */ return bpy_prop_deferred_return((void *)BPy_BoolProperty, kw); } } static char BPy_BoolVectorProperty_doc[] = ".. function:: BoolVectorProperty(name=\"\", description=\"\", default=(False, False, False), hidden=False, size=3)\n" "\n" " Returns a new vector boolean property definition."; PyObject *BPy_BoolVectorProperty(PyObject *self, PyObject *args, PyObject *kw) { StructRNA *srna; if (PyTuple_GET_SIZE(args) > 0) { PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this. return NULL; } srna= srna_from_self(self); if(srna==NULL && PyErr_Occurred()) { return NULL; /* self's type was compatible but error getting the srna */ } else if(srna) { static char *kwlist[] = {"attr", "name", "description", "default", "hidden", "size", NULL}; char *id=NULL, *name="", *description=""; int def[PYRNA_STACK_ARRAY]={0}; int hidden=0, size=3; PropertyRNA *prop; PyObject *pydef= NULL; if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssOii:BoolVectorProperty", kwlist, &id, &name, &description, &pydef, &hidden, &size)) return NULL; if(size < 1 || size > PYRNA_STACK_ARRAY) { PyErr_Format(PyExc_TypeError, "BoolVectorProperty(size=%d): size must be between 0 and %d.", size, PYRNA_STACK_ARRAY); return NULL; } if(pydef && BPyAsPrimitiveArray(def, pydef, size, &PyBool_Type, "BoolVectorProperty(default=sequence)") < 0) return NULL; prop= RNA_def_boolean_array(srna, id, size, pydef ? def:NULL, name, description); if(hidden) RNA_def_property_flag(prop, PROP_HIDDEN); RNA_def_property_duplicate_pointers(prop); Py_RETURN_NONE; } else { /* operators defer running this function */ return bpy_prop_deferred_return((void *)BPy_BoolVectorProperty, kw); } } static char BPy_IntProperty_doc[] = ".. function:: IntProperty(name=\"\", description=\"\", default=0, min=-sys.maxint, max=sys.maxint, soft_min=-sys.maxint, soft_max=sys.maxint, step=1, hidden=False)\n" "\n" " Returns a new int property definition."; PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw) { StructRNA *srna; if (PyTuple_GET_SIZE(args) > 0) { PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this. return NULL; } srna= srna_from_self(self); if(srna==NULL && PyErr_Occurred()) { return NULL; /* self's type was compatible but error getting the srna */ } else if(srna) { static char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "hidden", NULL}; char *id=NULL, *name="", *description=""; int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def=0; int hidden=0; PropertyRNA *prop; if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssiiiiiii:IntProperty", kwlist, &id, &name, &description, &def, &min, &max, &soft_min, &soft_max, &step, &hidden)) return NULL; prop= RNA_def_int(srna, id, def, min, max, name, description, soft_min, soft_max); RNA_def_property_ui_range(prop, min, max, step, 0); if(hidden) RNA_def_property_flag(prop, PROP_HIDDEN); RNA_def_property_duplicate_pointers(prop); Py_RETURN_NONE; } else { /* operators defer running this function */ return bpy_prop_deferred_return((void *)BPy_IntProperty, kw); } } static char BPy_IntVectorProperty_doc[] = ".. function:: IntVectorProperty(name=\"\", description=\"\", default=(0, 0, 0), min=-sys.maxint, max=sys.maxint, soft_min=-sys.maxint, soft_max=sys.maxint, hidden=False, size=3)\n" "\n" " Returns a new vector int property definition."; PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw) { StructRNA *srna; if (PyTuple_GET_SIZE(args) > 0) { PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this. return NULL; } srna= srna_from_self(self); if(srna==NULL && PyErr_Occurred()) { return NULL; /* self's type was compatible but error getting the srna */ } else if(srna) { static char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "hidden", "size", NULL}; char *id=NULL, *name="", *description=""; int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def[PYRNA_STACK_ARRAY]={0}; int hidden=0, size=3; PropertyRNA *prop; PyObject *pydef= NULL; if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssOiiiiii:IntVectorProperty", kwlist, &id, &name, &description, &pydef, &min, &max, &soft_min, &soft_max, &hidden, &size)) return NULL; if(size < 1 || size > PYRNA_STACK_ARRAY) { PyErr_Format(PyExc_TypeError, "IntVectorProperty(size=%d): size must be between 0 and %d.", size, PYRNA_STACK_ARRAY); return NULL; } if(pydef && BPyAsPrimitiveArray(def, pydef, size, &PyLong_Type, "IntVectorProperty(default=sequence)") < 0) return NULL; prop= RNA_def_int_array(srna, id, size, pydef ? def:NULL, min, max, name, description, soft_min, soft_max); RNA_def_property_ui_range(prop, min, max, step, 0); if(hidden) RNA_def_property_flag(prop, PROP_HIDDEN); RNA_def_property_duplicate_pointers(prop); Py_RETURN_NONE; } else { /* operators defer running this function */ return bpy_prop_deferred_return((void *)BPy_IntVectorProperty, kw); } } static char BPy_FloatProperty_doc[] = ".. 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, hidden=False)\n" "\n" " Returns a new float property definition."; PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw) { StructRNA *srna; if (PyTuple_GET_SIZE(args) > 0) { PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this. return NULL; } srna= srna_from_self(self); if(srna==NULL && PyErr_Occurred()) { return NULL; /* self's type was compatible but error getting the srna */ } else if(srna) { static char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "hidden", NULL}; char *id=NULL, *name="", *description=""; float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def=0.0f; int precision= 2, hidden=0; PropertyRNA *prop; if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssffffffii:FloatProperty", kwlist, &id, &name, &description, &def, &min, &max, &soft_min, &soft_max, &step, &precision, &hidden)) return NULL; prop= RNA_def_float(srna, id, def, min, max, name, description, soft_min, soft_max); RNA_def_property_ui_range(prop, min, max, step, precision); if(hidden) RNA_def_property_flag(prop, PROP_HIDDEN); RNA_def_property_duplicate_pointers(prop); Py_RETURN_NONE; } else { /* operators defer running this function */ return bpy_prop_deferred_return((void *)BPy_FloatProperty, kw); } } static char BPy_FloatVectorProperty_doc[] = ".. 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, hidden=False, size=3)\n" "\n" " Returns a new vector float property definition."; PyObject *BPy_FloatVectorProperty(PyObject *self, PyObject *args, PyObject *kw) { StructRNA *srna; if (PyTuple_GET_SIZE(args) > 0) { PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this. return NULL; } srna= srna_from_self(self); if(srna==NULL && PyErr_Occurred()) { return NULL; /* self's type was compatible but error getting the srna */ } else if(srna) { static char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "hidden", "size", NULL}; char *id=NULL, *name="", *description=""; float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def[PYRNA_STACK_ARRAY]={0.0f}; int precision= 2, hidden=0, size=3; PropertyRNA *prop; PyObject *pydef= NULL; if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssOfffffiii:FloatVectorProperty", kwlist, &id, &name, &description, &pydef, &min, &max, &soft_min, &soft_max, &step, &precision, &hidden, &size)) return NULL; if(size < 1 || size > PYRNA_STACK_ARRAY) { PyErr_Format(PyExc_TypeError, "FloatVectorProperty(size=%d): size must be between 0 and %d.", size, PYRNA_STACK_ARRAY); return NULL; } if(pydef && BPyAsPrimitiveArray(def, pydef, size, &PyFloat_Type, "FloatVectorProperty(default=sequence)") < 0) return NULL; prop= RNA_def_float_array(srna, id, size, pydef ? def:NULL, min, max, name, description, soft_min, soft_max); RNA_def_property_ui_range(prop, min, max, step, precision); if(hidden) RNA_def_property_flag(prop, PROP_HIDDEN); RNA_def_property_duplicate_pointers(prop); Py_RETURN_NONE; } else { /* operators defer running this function */ return bpy_prop_deferred_return((void *)BPy_FloatVectorProperty, kw); } } static char BPy_StringProperty_doc[] = ".. function:: StringProperty(name=\"\", description=\"\", default=\"\", maxlen=0, hidden=False)\n" "\n" " Returns a new string property definition."; PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw) { StructRNA *srna; if (PyTuple_GET_SIZE(args) > 0) { PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this. return NULL; } srna= srna_from_self(self); if(srna==NULL && PyErr_Occurred()) { return NULL; /* self's type was compatible but error getting the srna */ } else if(srna) { static char *kwlist[] = {"attr", "name", "description", "default", "maxlen", "hidden", NULL}; char *id=NULL, *name="", *description="", *def=""; int maxlen=0, hidden=0; PropertyRNA *prop; if (!PyArg_ParseTupleAndKeywords(args, kw, "s|sssii:StringProperty", kwlist, &id, &name, &description, &def, &maxlen, &hidden)) return NULL; prop= RNA_def_string(srna, id, def, maxlen, name, description); if(hidden) RNA_def_property_flag(prop, PROP_HIDDEN); RNA_def_property_duplicate_pointers(prop); Py_RETURN_NONE; } else { /* operators defer running this function */ return bpy_prop_deferred_return((void *)BPy_StringProperty, kw); } } static EnumPropertyItem *enum_items_from_py(PyObject *value, const char *def, int *defvalue) { EnumPropertyItem *items= NULL; PyObject *item; int seq_len, i, totitem= 0; if(!PySequence_Check(value)) { PyErr_SetString(PyExc_TypeError, "expected a sequence of tuples for the enum items"); return NULL; } seq_len = PySequence_Length(value); for(i=0; i