Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2009-04-07 15:06:35 +0400
committerCampbell Barton <ideasman42@gmail.com>2009-04-07 15:06:35 +0400
commit5d64dd019e7e8150db40505097d1b4048f4e0153 (patch)
tree65d3ac2f7d7d8a9e3aad5ba5331c43a5d1fca358 /source/gameengine/Expressions
parentc054ea02039e9209095108c1b1f42250a7e85d8f (diff)
BGE Python API
Use each types dictionary to store attributes PyAttributeDef's so it uses pythons hash lookup (which it was already doing for methods) rather then doing a string lookup on the array each time. This also means attributes can be found in the type without having to do a dir() on the instance.
Diffstat (limited to 'source/gameengine/Expressions')
-rw-r--r--source/gameengine/Expressions/PyObjectPlus.cpp949
-rw-r--r--source/gameengine/Expressions/PyObjectPlus.h48
2 files changed, 521 insertions, 476 deletions
diff --git a/source/gameengine/Expressions/PyObjectPlus.cpp b/source/gameengine/Expressions/PyObjectPlus.cpp
index 03afa62a6da..3148dfc2b89 100644
--- a/source/gameengine/Expressions/PyObjectPlus.cpp
+++ b/source/gameengine/Expressions/PyObjectPlus.cpp
@@ -98,6 +98,10 @@ PyMethodDef PyObjectPlus::Methods[] = {
{NULL, NULL} /* Sentinel */
};
+PyAttributeDef PyObjectPlus::Attributes[] = {
+ {NULL} //Sentinel
+};
+
/*------------------------------
* PyObjectPlus Parents -- Every class, even the abstract one should have parents
------------------------------*/
@@ -136,561 +140,574 @@ int PyObjectPlus::py_setattro(PyObject *attr, PyObject* value)
return 1;
}
-PyObject *PyObjectPlus::py_getattro_self(const PyAttributeDef attrlist[], void *self, PyObject *attr)
+PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef)
{
- char *attr_str= PyString_AsString(attr);
-
- const PyAttributeDef *attrdef;
- for (attrdef=attrlist; attrdef->m_name != NULL; attrdef++)
+ if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_DUMMY)
{
- if (!strcmp(attr_str, attrdef->m_name))
+ // fake attribute, ignore
+ return NULL;
+ }
+ if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_FUNCTION)
+ {
+ // the attribute has no field correspondance, handover processing to function.
+ if (attrdef->m_getFunction == NULL)
+ return NULL;
+ return (*attrdef->m_getFunction)(self, attrdef);
+ }
+ char *ptr = reinterpret_cast<char*>(self)+attrdef->m_offset;
+ if (attrdef->m_length > 1)
+ {
+ PyObject* resultlist = PyList_New(attrdef->m_length);
+ for (unsigned int i=0; i<attrdef->m_length; i++)
{
- if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_DUMMY)
+ switch (attrdef->m_type) {
+ case KX_PYATTRIBUTE_TYPE_BOOL:
+ {
+ bool *val = reinterpret_cast<bool*>(ptr);
+ ptr += sizeof(bool);
+ PyList_SetItem(resultlist,i,PyInt_FromLong(*val));
+ break;
+ }
+ case KX_PYATTRIBUTE_TYPE_SHORT:
+ {
+ short int *val = reinterpret_cast<short int*>(ptr);
+ ptr += sizeof(short int);
+ PyList_SetItem(resultlist,i,PyInt_FromLong(*val));
+ break;
+ }
+ case KX_PYATTRIBUTE_TYPE_ENUM:
+ // enum are like int, just make sure the field size is the same
+ if (sizeof(int) != attrdef->m_size)
+ {
+ Py_DECREF(resultlist);
+ return NULL;
+ }
+ // walkthrough
+ case KX_PYATTRIBUTE_TYPE_INT:
+ {
+ int *val = reinterpret_cast<int*>(ptr);
+ ptr += sizeof(int);
+ PyList_SetItem(resultlist,i,PyInt_FromLong(*val));
+ break;
+ }
+ case KX_PYATTRIBUTE_TYPE_FLOAT:
+ {
+ float *val = reinterpret_cast<float*>(ptr);
+ ptr += sizeof(float);
+ PyList_SetItem(resultlist,i,PyFloat_FromDouble(*val));
+ break;
+ }
+ default:
+ // no support for array of complex data
+ Py_DECREF(resultlist);
+ return NULL;
+ }
+ }
+ return resultlist;
+ }
+ else
+ {
+ switch (attrdef->m_type) {
+ case KX_PYATTRIBUTE_TYPE_BOOL:
+ {
+ bool *val = reinterpret_cast<bool*>(ptr);
+ return PyInt_FromLong(*val);
+ }
+ case KX_PYATTRIBUTE_TYPE_SHORT:
+ {
+ short int *val = reinterpret_cast<short int*>(ptr);
+ return PyInt_FromLong(*val);
+ }
+ case KX_PYATTRIBUTE_TYPE_ENUM:
+ // enum are like int, just make sure the field size is the same
+ if (sizeof(int) != attrdef->m_size)
{
- // fake attribute, ignore
return NULL;
}
- if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_FUNCTION)
+ // walkthrough
+ case KX_PYATTRIBUTE_TYPE_INT:
{
- // the attribute has no field correspondance, handover processing to function.
- if (attrdef->m_getFunction == NULL)
- return NULL;
- return (*attrdef->m_getFunction)(self, attrdef);
+ int *val = reinterpret_cast<int*>(ptr);
+ return PyInt_FromLong(*val);
}
- char *ptr = reinterpret_cast<char*>(self)+attrdef->m_offset;
- if (attrdef->m_length > 1)
+ case KX_PYATTRIBUTE_TYPE_FLOAT:
{
- PyObject* resultlist = PyList_New(attrdef->m_length);
- for (unsigned int i=0; i<attrdef->m_length; i++)
- {
- switch (attrdef->m_type) {
- case KX_PYATTRIBUTE_TYPE_BOOL:
- {
- bool *val = reinterpret_cast<bool*>(ptr);
- ptr += sizeof(bool);
- PyList_SetItem(resultlist,i,PyInt_FromLong(*val));
- break;
- }
- case KX_PYATTRIBUTE_TYPE_SHORT:
- {
- short int *val = reinterpret_cast<short int*>(ptr);
- ptr += sizeof(short int);
- PyList_SetItem(resultlist,i,PyInt_FromLong(*val));
- break;
- }
- case KX_PYATTRIBUTE_TYPE_ENUM:
- // enum are like int, just make sure the field size is the same
- if (sizeof(int) != attrdef->m_size)
- {
- Py_DECREF(resultlist);
- return NULL;
- }
- // walkthrough
- case KX_PYATTRIBUTE_TYPE_INT:
- {
- int *val = reinterpret_cast<int*>(ptr);
- ptr += sizeof(int);
- PyList_SetItem(resultlist,i,PyInt_FromLong(*val));
- break;
- }
- case KX_PYATTRIBUTE_TYPE_FLOAT:
- {
- float *val = reinterpret_cast<float*>(ptr);
- ptr += sizeof(float);
- PyList_SetItem(resultlist,i,PyFloat_FromDouble(*val));
- break;
- }
- default:
- // no support for array of complex data
- Py_DECREF(resultlist);
- return NULL;
- }
- }
- return resultlist;
+ float *val = reinterpret_cast<float*>(ptr);
+ return PyFloat_FromDouble(*val);
}
- else
+ case KX_PYATTRIBUTE_TYPE_STRING:
{
- switch (attrdef->m_type) {
- case KX_PYATTRIBUTE_TYPE_BOOL:
- {
- bool *val = reinterpret_cast<bool*>(ptr);
- return PyInt_FromLong(*val);
- }
- case KX_PYATTRIBUTE_TYPE_SHORT:
- {
- short int *val = reinterpret_cast<short int*>(ptr);
- return PyInt_FromLong(*val);
- }
- case KX_PYATTRIBUTE_TYPE_ENUM:
- // enum are like int, just make sure the field size is the same
- if (sizeof(int) != attrdef->m_size)
- {
- return NULL;
- }
- // walkthrough
- case KX_PYATTRIBUTE_TYPE_INT:
- {
- int *val = reinterpret_cast<int*>(ptr);
- return PyInt_FromLong(*val);
- }
- case KX_PYATTRIBUTE_TYPE_FLOAT:
- {
- float *val = reinterpret_cast<float*>(ptr);
- return PyFloat_FromDouble(*val);
- }
- case KX_PYATTRIBUTE_TYPE_STRING:
- {
- STR_String *val = reinterpret_cast<STR_String*>(ptr);
- return PyString_FromString(*val);
- }
- default:
- return NULL;
- }
+ STR_String *val = reinterpret_cast<STR_String*>(ptr);
+ return PyString_FromString(*val);
}
+ default:
+ return NULL;
}
}
- return NULL;
}
-int PyObjectPlus::py_setattro_self(const PyAttributeDef attrlist[], void *self, PyObject *attr, PyObject *value)
+#if 0
+PyObject *PyObjectPlus::py_getattro_self(const PyAttributeDef attrlist[], void *self, PyObject *attr)
{
+ char *attr_str= PyString_AsString(attr);
const PyAttributeDef *attrdef;
+
+ for (attrdef=attrlist; attrdef->m_name != NULL; attrdef++)
+ if (!strcmp(attr_str, attrdef->m_name))
+ return py_get_attrdef(self, attrdef);
+
+ return NULL;
+}
+#endif
+
+int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyObject *value)
+{
void *undoBuffer = NULL;
void *sourceBuffer = NULL;
size_t bufferSize = 0;
- char *attr_str= PyString_AsString(attr);
-
- for (attrdef=attrlist; attrdef->m_name != NULL; attrdef++)
+
+ char *ptr = reinterpret_cast<char*>(self)+attrdef->m_offset;
+ if (attrdef->m_length > 1)
{
- if (!strcmp(attr_str, attrdef->m_name))
+ if (!PySequence_Check(value))
{
- if (attrdef->m_access == KX_PYATTRIBUTE_RO ||
- attrdef->m_type == KX_PYATTRIBUTE_TYPE_DUMMY)
+ PyErr_SetString(PyExc_TypeError, "expected a sequence");
+ return 1;
+ }
+ if (PySequence_Size(value) != attrdef->m_length)
+ {
+ PyErr_SetString(PyExc_TypeError, "incorrect number of elements in sequence");
+ return 1;
+ }
+ switch (attrdef->m_type)
+ {
+ case KX_PYATTRIBUTE_TYPE_FUNCTION:
+ if (attrdef->m_setFunction == NULL)
{
- PyErr_SetString(PyExc_AttributeError, "property is read-only");
+ PyErr_SetString(PyExc_AttributeError, "function attribute without function, report to blender.org");
return 1;
}
- char *ptr = reinterpret_cast<char*>(self)+attrdef->m_offset;
- if (attrdef->m_length > 1)
+ return (*attrdef->m_setFunction)(self, attrdef, value);
+ case KX_PYATTRIBUTE_TYPE_BOOL:
+ bufferSize = sizeof(bool);
+ break;
+ case KX_PYATTRIBUTE_TYPE_SHORT:
+ bufferSize = sizeof(short int);
+ break;
+ case KX_PYATTRIBUTE_TYPE_ENUM:
+ case KX_PYATTRIBUTE_TYPE_INT:
+ bufferSize = sizeof(int);
+ break;
+ case KX_PYATTRIBUTE_TYPE_FLOAT:
+ bufferSize = sizeof(float);
+ break;
+ default:
+ // should not happen
+ PyErr_SetString(PyExc_AttributeError, "Unsupported attribute type, report to blender.org");
+ return 1;
+ }
+ // let's implement a smart undo method
+ bufferSize *= attrdef->m_length;
+ undoBuffer = malloc(bufferSize);
+ sourceBuffer = ptr;
+ if (undoBuffer)
+ {
+ memcpy(undoBuffer, sourceBuffer, bufferSize);
+ }
+ for (int i=0; i<attrdef->m_length; i++)
+ {
+ PyObject *item = PySequence_GetItem(value, i); /* new ref */
+ // we can decrement the reference immediately, the reference count
+ // is at least 1 because the item is part of an array
+ Py_DECREF(item);
+ switch (attrdef->m_type)
{
- if (!PySequence_Check(value))
- {
- PyErr_SetString(PyExc_TypeError, "expected a sequence");
- return 1;
- }
- if (PySequence_Size(value) != attrdef->m_length)
- {
- PyErr_SetString(PyExc_TypeError, "incorrect number of elements in sequence");
- return 1;
- }
- switch (attrdef->m_type)
+ case KX_PYATTRIBUTE_TYPE_BOOL:
{
- case KX_PYATTRIBUTE_TYPE_FUNCTION:
- if (attrdef->m_setFunction == NULL)
+ bool *var = reinterpret_cast<bool*>(ptr);
+ ptr += sizeof(bool);
+ if (PyInt_Check(item))
+ {
+ *var = (PyInt_AsLong(item) != 0);
+ }
+ else if (PyBool_Check(item))
{
- PyErr_SetString(PyExc_AttributeError, "function attribute without function, report to blender.org");
- return 1;
+ *var = (item == Py_True);
+ }
+ else
+ {
+ PyErr_SetString(PyExc_TypeError, "expected an integer or a bool");
+ goto UNDO_AND_ERROR;
}
- return (*attrdef->m_setFunction)(self, attrdef, value);
- case KX_PYATTRIBUTE_TYPE_BOOL:
- bufferSize = sizeof(bool);
- break;
- case KX_PYATTRIBUTE_TYPE_SHORT:
- bufferSize = sizeof(short int);
- break;
- case KX_PYATTRIBUTE_TYPE_ENUM:
- case KX_PYATTRIBUTE_TYPE_INT:
- bufferSize = sizeof(int);
- break;
- case KX_PYATTRIBUTE_TYPE_FLOAT:
- bufferSize = sizeof(float);
break;
- default:
- // should not happen
- PyErr_SetString(PyExc_AttributeError, "Unsupported attribute type, report to blender.org");
- return 1;
}
- // let's implement a smart undo method
- bufferSize *= attrdef->m_length;
- undoBuffer = malloc(bufferSize);
- sourceBuffer = ptr;
- if (undoBuffer)
+ case KX_PYATTRIBUTE_TYPE_SHORT:
{
- memcpy(undoBuffer, sourceBuffer, bufferSize);
- }
- for (int i=0; i<attrdef->m_length; i++)
- {
- PyObject *item = PySequence_GetItem(value, i); /* new ref */
- // we can decrement the reference immediately, the reference count
- // is at least 1 because the item is part of an array
- Py_DECREF(item);
- switch (attrdef->m_type)
+ short int *var = reinterpret_cast<short int*>(ptr);
+ ptr += sizeof(short int);
+ if (PyInt_Check(item))
{
- case KX_PYATTRIBUTE_TYPE_BOOL:
- {
- bool *var = reinterpret_cast<bool*>(ptr);
- ptr += sizeof(bool);
- if (PyInt_Check(item))
- {
- *var = (PyInt_AsLong(item) != 0);
- }
- else if (PyBool_Check(item))
- {
- *var = (item == Py_True);
- }
- else
- {
- PyErr_SetString(PyExc_TypeError, "expected an integer or a bool");
- goto UNDO_AND_ERROR;
- }
- break;
- }
- case KX_PYATTRIBUTE_TYPE_SHORT:
+ long val = PyInt_AsLong(item);
+ if (attrdef->m_clamp)
{
- short int *var = reinterpret_cast<short int*>(ptr);
- ptr += sizeof(short int);
- if (PyInt_Check(item))
- {
- long val = PyInt_AsLong(item);
- if (attrdef->m_clamp)
- {
- if (val < attrdef->m_imin)
- val = attrdef->m_imin;
- else if (val > attrdef->m_imax)
- val = attrdef->m_imax;
- }
- else if (val < attrdef->m_imin || val > attrdef->m_imax)
- {
- PyErr_SetString(PyExc_ValueError, "item value out of range");
- goto UNDO_AND_ERROR;
- }
- *var = (short int)val;
- }
- else
- {
- PyErr_SetString(PyExc_TypeError, "expected an integer");
- goto UNDO_AND_ERROR;
- }
- break;
+ if (val < attrdef->m_imin)
+ val = attrdef->m_imin;
+ else if (val > attrdef->m_imax)
+ val = attrdef->m_imax;
}
- case KX_PYATTRIBUTE_TYPE_ENUM:
- // enum are equivalent to int, just make sure that the field size matches:
- if (sizeof(int) != attrdef->m_size)
+ else if (val < attrdef->m_imin || val > attrdef->m_imax)
{
- PyErr_SetString(PyExc_AttributeError, "attribute size check error, report to blender.org");
+ PyErr_SetString(PyExc_ValueError, "item value out of range");
goto UNDO_AND_ERROR;
}
- // walkthrough
- case KX_PYATTRIBUTE_TYPE_INT:
+ *var = (short int)val;
+ }
+ else
+ {
+ PyErr_SetString(PyExc_TypeError, "expected an integer");
+ goto UNDO_AND_ERROR;
+ }
+ break;
+ }
+ case KX_PYATTRIBUTE_TYPE_ENUM:
+ // enum are equivalent to int, just make sure that the field size matches:
+ if (sizeof(int) != attrdef->m_size)
+ {
+ PyErr_SetString(PyExc_AttributeError, "attribute size check error, report to blender.org");
+ goto UNDO_AND_ERROR;
+ }
+ // walkthrough
+ case KX_PYATTRIBUTE_TYPE_INT:
+ {
+ int *var = reinterpret_cast<int*>(ptr);
+ ptr += sizeof(int);
+ if (PyInt_Check(item))
+ {
+ long val = PyInt_AsLong(item);
+ if (attrdef->m_clamp)
{
- int *var = reinterpret_cast<int*>(ptr);
- ptr += sizeof(int);
- if (PyInt_Check(item))
- {
- long val = PyInt_AsLong(item);
- if (attrdef->m_clamp)
- {
- if (val < attrdef->m_imin)
- val = attrdef->m_imin;
- else if (val > attrdef->m_imax)
- val = attrdef->m_imax;
- }
- else if (val < attrdef->m_imin || val > attrdef->m_imax)
- {
- PyErr_SetString(PyExc_ValueError, "item value out of range");
- goto UNDO_AND_ERROR;
- }
- *var = (int)val;
- }
- else
- {
- PyErr_SetString(PyExc_TypeError, "expected an integer");
- goto UNDO_AND_ERROR;
- }
- break;
+ if (val < attrdef->m_imin)
+ val = attrdef->m_imin;
+ else if (val > attrdef->m_imax)
+ val = attrdef->m_imax;
}
- case KX_PYATTRIBUTE_TYPE_FLOAT:
+ else if (val < attrdef->m_imin || val > attrdef->m_imax)
{
- float *var = reinterpret_cast<float*>(ptr);
- ptr += sizeof(float);
- double val = PyFloat_AsDouble(item);
- if (val == -1.0 && PyErr_Occurred())
- {
- PyErr_SetString(PyExc_TypeError, "expected a float");
- goto UNDO_AND_ERROR;
- }
- else if (attrdef->m_clamp)
- {
- if (val < attrdef->m_fmin)
- val = attrdef->m_fmin;
- else if (val > attrdef->m_fmax)
- val = attrdef->m_fmax;
- }
- else if (val < attrdef->m_fmin || val > attrdef->m_fmax)
- {
- PyErr_SetString(PyExc_ValueError, "item value out of range");
- goto UNDO_AND_ERROR;
- }
- *var = (float)val;
- break;
+ PyErr_SetString(PyExc_ValueError, "item value out of range");
+ goto UNDO_AND_ERROR;
}
- default:
- // should not happen
- PyErr_SetString(PyExc_AttributeError, "attribute type check error, report to blender.org");
+ *var = (int)val;
+ }
+ else
+ {
+ PyErr_SetString(PyExc_TypeError, "expected an integer");
goto UNDO_AND_ERROR;
}
+ break;
}
- // no error, call check function if any
- if (attrdef->m_checkFunction != NULL)
+ case KX_PYATTRIBUTE_TYPE_FLOAT:
{
- if ((*attrdef->m_checkFunction)(self, attrdef) != 0)
+ float *var = reinterpret_cast<float*>(ptr);
+ ptr += sizeof(float);
+ double val = PyFloat_AsDouble(item);
+ if (val == -1.0 && PyErr_Occurred())
{
- // post check returned an error, restore values
- UNDO_AND_ERROR:
- if (undoBuffer)
- {
- memcpy(sourceBuffer, undoBuffer, bufferSize);
- free(undoBuffer);
- }
- return 1;
+ PyErr_SetString(PyExc_TypeError, "expected a float");
+ goto UNDO_AND_ERROR;
}
+ else if (attrdef->m_clamp)
+ {
+ if (val < attrdef->m_fmin)
+ val = attrdef->m_fmin;
+ else if (val > attrdef->m_fmax)
+ val = attrdef->m_fmax;
+ }
+ else if (val < attrdef->m_fmin || val > attrdef->m_fmax)
+ {
+ PyErr_SetString(PyExc_ValueError, "item value out of range");
+ goto UNDO_AND_ERROR;
+ }
+ *var = (float)val;
+ break;
}
+ default:
+ // should not happen
+ PyErr_SetString(PyExc_AttributeError, "attribute type check error, report to blender.org");
+ goto UNDO_AND_ERROR;
+ }
+ }
+ // no error, call check function if any
+ if (attrdef->m_checkFunction != NULL)
+ {
+ if ((*attrdef->m_checkFunction)(self, attrdef) != 0)
+ {
+ // post check returned an error, restore values
+ UNDO_AND_ERROR:
if (undoBuffer)
+ {
+ memcpy(sourceBuffer, undoBuffer, bufferSize);
free(undoBuffer);
- return 0;
+ }
+ return 1;
}
- else // simple attribute value
+ }
+ if (undoBuffer)
+ free(undoBuffer);
+ return 0;
+ }
+ else // simple attribute value
+ {
+ if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_FUNCTION)
+ {
+ if (attrdef->m_setFunction == NULL)
{
- if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_FUNCTION)
+ PyErr_SetString(PyExc_AttributeError, "function attribute without function, report to blender.org");
+ return 1;
+ }
+ return (*attrdef->m_setFunction)(self, attrdef, value);
+ }
+ if (attrdef->m_checkFunction != NULL)
+ {
+ // post check function is provided, prepare undo buffer
+ sourceBuffer = ptr;
+ switch (attrdef->m_type)
+ {
+ case KX_PYATTRIBUTE_TYPE_BOOL:
+ bufferSize = sizeof(bool);
+ break;
+ case KX_PYATTRIBUTE_TYPE_SHORT:
+ bufferSize = sizeof(short);
+ break;
+ case KX_PYATTRIBUTE_TYPE_ENUM:
+ case KX_PYATTRIBUTE_TYPE_INT:
+ bufferSize = sizeof(int);
+ break;
+ case KX_PYATTRIBUTE_TYPE_FLOAT:
+ bufferSize = sizeof(float);
+ break;
+ case KX_PYATTRIBUTE_TYPE_STRING:
+ sourceBuffer = reinterpret_cast<STR_String*>(ptr)->Ptr();
+ if (sourceBuffer)
+ bufferSize = strlen(reinterpret_cast<char*>(sourceBuffer))+1;
+ break;
+ default:
+ PyErr_SetString(PyExc_AttributeError, "unknown attribute type, report to blender.org");
+ return 1;
+ }
+ if (bufferSize)
+ {
+ undoBuffer = malloc(bufferSize);
+ if (undoBuffer)
{
- if (attrdef->m_setFunction == NULL)
- {
- PyErr_SetString(PyExc_AttributeError, "function attribute without function, report to blender.org");
- return 1;
- }
- return (*attrdef->m_setFunction)(self, attrdef, value);
+ memcpy(undoBuffer, sourceBuffer, bufferSize);
}
- if (attrdef->m_checkFunction != NULL)
+ }
+ }
+
+ switch (attrdef->m_type)
+ {
+ case KX_PYATTRIBUTE_TYPE_BOOL:
+ {
+ bool *var = reinterpret_cast<bool*>(ptr);
+ if (PyInt_Check(value))
+ {
+ *var = (PyInt_AsLong(value) != 0);
+ }
+ else if (PyBool_Check(value))
{
- // post check function is provided, prepare undo buffer
- sourceBuffer = ptr;
- switch (attrdef->m_type)
+ *var = (value == Py_True);
+ }
+ else
+ {
+ PyErr_SetString(PyExc_TypeError, "expected an integer or a bool");
+ goto FREE_AND_ERROR;
+ }
+ break;
+ }
+ case KX_PYATTRIBUTE_TYPE_SHORT:
+ {
+ short int *var = reinterpret_cast<short int*>(ptr);
+ if (PyInt_Check(value))
+ {
+ long val = PyInt_AsLong(value);
+ if (attrdef->m_clamp)
{
- case KX_PYATTRIBUTE_TYPE_BOOL:
- bufferSize = sizeof(bool);
- break;
- case KX_PYATTRIBUTE_TYPE_SHORT:
- bufferSize = sizeof(short);
- break;
- case KX_PYATTRIBUTE_TYPE_ENUM:
- case KX_PYATTRIBUTE_TYPE_INT:
- bufferSize = sizeof(int);
- break;
- case KX_PYATTRIBUTE_TYPE_FLOAT:
- bufferSize = sizeof(float);
- break;
- case KX_PYATTRIBUTE_TYPE_STRING:
- sourceBuffer = reinterpret_cast<STR_String*>(ptr)->Ptr();
- if (sourceBuffer)
- bufferSize = strlen(reinterpret_cast<char*>(sourceBuffer))+1;
- break;
- default:
- PyErr_SetString(PyExc_AttributeError, "unknown attribute type, report to blender.org");
- return 1;
+ if (val < attrdef->m_imin)
+ val = attrdef->m_imin;
+ else if (val > attrdef->m_imax)
+ val = attrdef->m_imax;
}
- if (bufferSize)
+ else if (val < attrdef->m_imin || val > attrdef->m_imax)
{
- undoBuffer = malloc(bufferSize);
- if (undoBuffer)
- {
- memcpy(undoBuffer, sourceBuffer, bufferSize);
- }
+ PyErr_SetString(PyExc_ValueError, "value out of range");
+ goto FREE_AND_ERROR;
}
+ *var = (short int)val;
}
-
- switch (attrdef->m_type)
+ else
{
- case KX_PYATTRIBUTE_TYPE_BOOL:
- {
- bool *var = reinterpret_cast<bool*>(ptr);
- if (PyInt_Check(value))
- {
- *var = (PyInt_AsLong(value) != 0);
- }
- else if (PyBool_Check(value))
- {
- *var = (value == Py_True);
- }
- else
- {
- PyErr_SetString(PyExc_TypeError, "expected an integer or a bool");
- goto FREE_AND_ERROR;
- }
- break;
- }
- case KX_PYATTRIBUTE_TYPE_SHORT:
+ PyErr_SetString(PyExc_TypeError, "expected an integer");
+ goto FREE_AND_ERROR;
+ }
+ break;
+ }
+ case KX_PYATTRIBUTE_TYPE_ENUM:
+ // enum are equivalent to int, just make sure that the field size matches:
+ if (sizeof(int) != attrdef->m_size)
+ {
+ PyErr_SetString(PyExc_AttributeError, "attribute size check error, report to blender.org");
+ goto FREE_AND_ERROR;
+ }
+ // walkthrough
+ case KX_PYATTRIBUTE_TYPE_INT:
+ {
+ int *var = reinterpret_cast<int*>(ptr);
+ if (PyInt_Check(value))
+ {
+ long val = PyInt_AsLong(value);
+ if (attrdef->m_clamp)
{
- short int *var = reinterpret_cast<short int*>(ptr);
- if (PyInt_Check(value))
- {
- long val = PyInt_AsLong(value);
- if (attrdef->m_clamp)
- {
- if (val < attrdef->m_imin)
- val = attrdef->m_imin;
- else if (val > attrdef->m_imax)
- val = attrdef->m_imax;
- }
- else if (val < attrdef->m_imin || val > attrdef->m_imax)
- {
- PyErr_SetString(PyExc_ValueError, "value out of range");
- goto FREE_AND_ERROR;
- }
- *var = (short int)val;
- }
- else
- {
- PyErr_SetString(PyExc_TypeError, "expected an integer");
- goto FREE_AND_ERROR;
- }
- break;
+ if (val < attrdef->m_imin)
+ val = attrdef->m_imin;
+ else if (val > attrdef->m_imax)
+ val = attrdef->m_imax;
}
- case KX_PYATTRIBUTE_TYPE_ENUM:
- // enum are equivalent to int, just make sure that the field size matches:
- if (sizeof(int) != attrdef->m_size)
+ else if (val < attrdef->m_imin || val > attrdef->m_imax)
{
- PyErr_SetString(PyExc_AttributeError, "attribute size check error, report to blender.org");
+ PyErr_SetString(PyExc_ValueError, "value out of range");
goto FREE_AND_ERROR;
}
- // walkthrough
- case KX_PYATTRIBUTE_TYPE_INT:
- {
- int *var = reinterpret_cast<int*>(ptr);
- if (PyInt_Check(value))
- {
- long val = PyInt_AsLong(value);
- if (attrdef->m_clamp)
- {
- if (val < attrdef->m_imin)
- val = attrdef->m_imin;
- else if (val > attrdef->m_imax)
- val = attrdef->m_imax;
- }
- else if (val < attrdef->m_imin || val > attrdef->m_imax)
- {
- PyErr_SetString(PyExc_ValueError, "value out of range");
- goto FREE_AND_ERROR;
- }
- *var = (int)val;
- }
- else
- {
- PyErr_SetString(PyExc_TypeError, "expected an integer");
- goto FREE_AND_ERROR;
- }
- break;
- }
- case KX_PYATTRIBUTE_TYPE_FLOAT:
+ *var = (int)val;
+ }
+ else
+ {
+ PyErr_SetString(PyExc_TypeError, "expected an integer");
+ goto FREE_AND_ERROR;
+ }
+ break;
+ }
+ case KX_PYATTRIBUTE_TYPE_FLOAT:
+ {
+ float *var = reinterpret_cast<float*>(ptr);
+ double val = PyFloat_AsDouble(value);
+ if (val == -1.0 && PyErr_Occurred())
+ {
+ PyErr_SetString(PyExc_TypeError, "expected a float");
+ goto FREE_AND_ERROR;
+ }
+ else if (attrdef->m_clamp)
+ {
+ if (val < attrdef->m_fmin)
+ val = attrdef->m_fmin;
+ else if (val > attrdef->m_fmax)
+ val = attrdef->m_fmax;
+ }
+ else if (val < attrdef->m_fmin || val > attrdef->m_fmax)
+ {
+ PyErr_SetString(PyExc_ValueError, "value out of range");
+ goto FREE_AND_ERROR;
+ }
+ *var = (float)val;
+ break;
+ }
+ case KX_PYATTRIBUTE_TYPE_STRING:
+ {
+ STR_String *var = reinterpret_cast<STR_String*>(ptr);
+ if (PyString_Check(value))
+ {
+ char *val = PyString_AsString(value);
+ if (attrdef->m_clamp)
{
- float *var = reinterpret_cast<float*>(ptr);
- double val = PyFloat_AsDouble(value);
- if (val == -1.0 && PyErr_Occurred())
+ if (strlen(val) < attrdef->m_imin)
{
- PyErr_SetString(PyExc_TypeError, "expected a float");
+ // can't increase the length of the string
+ PyErr_SetString(PyExc_ValueError, "string length too short");
goto FREE_AND_ERROR;
}
- else if (attrdef->m_clamp)
- {
- if (val < attrdef->m_fmin)
- val = attrdef->m_fmin;
- else if (val > attrdef->m_fmax)
- val = attrdef->m_fmax;
- }
- else if (val < attrdef->m_fmin || val > attrdef->m_fmax)
+ else if (strlen(val) > attrdef->m_imax)
{
- PyErr_SetString(PyExc_ValueError, "value out of range");
- goto FREE_AND_ERROR;
- }
- *var = (float)val;
- break;
- }
- case KX_PYATTRIBUTE_TYPE_STRING:
- {
- STR_String *var = reinterpret_cast<STR_String*>(ptr);
- if (PyString_Check(value))
- {
- char *val = PyString_AsString(value);
- if (attrdef->m_clamp)
- {
- if (strlen(val) < attrdef->m_imin)
- {
- // can't increase the length of the string
- PyErr_SetString(PyExc_ValueError, "string length too short");
- goto FREE_AND_ERROR;
- }
- else if (strlen(val) > attrdef->m_imax)
- {
- // trim the string
- char c = val[attrdef->m_imax];
- val[attrdef->m_imax] = 0;
- *var = val;
- val[attrdef->m_imax] = c;
- break;
- }
- } else if (strlen(val) < attrdef->m_imin || strlen(val) > attrdef->m_imax)
- {
- PyErr_SetString(PyExc_ValueError, "string length out of range");
- goto FREE_AND_ERROR;
- }
+ // trim the string
+ char c = val[attrdef->m_imax];
+ val[attrdef->m_imax] = 0;
*var = val;
+ val[attrdef->m_imax] = c;
+ break;
}
- else
- {
- PyErr_SetString(PyExc_TypeError, "expected a string");
- goto FREE_AND_ERROR;
- }
- break;
+ } else if (strlen(val) < attrdef->m_imin || strlen(val) > attrdef->m_imax)
+ {
+ PyErr_SetString(PyExc_ValueError, "string length out of range");
+ goto FREE_AND_ERROR;
}
- default:
- // should not happen
- PyErr_SetString(PyExc_AttributeError, "unknown attribute type, report to blender.org");
+ *var = val;
+ }
+ else
+ {
+ PyErr_SetString(PyExc_TypeError, "expected a string");
goto FREE_AND_ERROR;
}
+ break;
}
- // check if post processing is needed
- if (attrdef->m_checkFunction != NULL)
+ default:
+ // should not happen
+ PyErr_SetString(PyExc_AttributeError, "unknown attribute type, report to blender.org");
+ goto FREE_AND_ERROR;
+ }
+ }
+ // check if post processing is needed
+ if (attrdef->m_checkFunction != NULL)
+ {
+ if ((*attrdef->m_checkFunction)(self, attrdef) != 0)
+ {
+ // restore value
+ RESTORE_AND_ERROR:
+ if (undoBuffer)
{
- if ((*attrdef->m_checkFunction)(self, attrdef) != 0)
+ if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_STRING)
{
- // restore value
- RESTORE_AND_ERROR:
- if (undoBuffer)
- {
- if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_STRING)
- {
- // special case for STR_String: restore the string
- STR_String *var = reinterpret_cast<STR_String*>(ptr);
- *var = reinterpret_cast<char*>(undoBuffer);
- }
- else
- {
- // other field type have direct values
- memcpy(ptr, undoBuffer, bufferSize);
- }
- }
- FREE_AND_ERROR:
- if (undoBuffer)
- free(undoBuffer);
- return 1;
+ // special case for STR_String: restore the string
+ STR_String *var = reinterpret_cast<STR_String*>(ptr);
+ *var = reinterpret_cast<char*>(undoBuffer);
+ }
+ else
+ {
+ // other field type have direct values
+ memcpy(ptr, undoBuffer, bufferSize);
}
}
+ FREE_AND_ERROR:
if (undoBuffer)
free(undoBuffer);
- return 0;
+ return 1;
+ }
+ }
+ if (undoBuffer)
+ free(undoBuffer);
+ return 0;
+}
+
+#if 0
+int PyObjectPlus::py_setattro_self(const PyAttributeDef attrlist[], void *self, PyObject *attr, PyObject *value)
+{
+ const PyAttributeDef *attrdef;
+ char *attr_str= PyString_AsString(attr);
+
+ for (attrdef=attrlist; attrdef->m_name != NULL; attrdef++)
+ {
+ if (!strcmp(attr_str, attrdef->m_name))
+ {
+ if (attrdef->m_access == KX_PYATTRIBUTE_RO ||
+ attrdef->m_type == KX_PYATTRIBUTE_TYPE_DUMMY)
+ {
+ PyErr_SetString(PyExc_AttributeError, "property is read-only");
+ return 1;
+ }
+
+ return py_set_attrdef(self, attrdef, value);
}
}
return -1;
}
+#endif
/*------------------------------
* PyObjectPlus repr -- representations
diff --git a/source/gameengine/Expressions/PyObjectPlus.h b/source/gameengine/Expressions/PyObjectPlus.h
index f178d03e131..41bf72964ce 100644
--- a/source/gameengine/Expressions/PyObjectPlus.h
+++ b/source/gameengine/Expressions/PyObjectPlus.h
@@ -99,20 +99,42 @@ static inline void Py_Fatal(const char *M) {
// to be properly passed up the hierarchy.
#define py_getattro_up(Parent) \
- PyObject *rvalue; \
+ \
PyObject *descr = PyDict_GetItem(Type.tp_dict, attr); \
\
- if (descr == NULL) { \
- PyErr_Clear(); \
- rvalue = Parent::py_getattro(attr); \
+ if(descr) { \
+ if (PyCObject_Check(descr)) { \
+ return py_get_attrdef((void *)this, (const PyAttributeDef*)PyCObject_AsVoidPtr(descr)); \
+ } else { \
+ return PyCFunction_New(((PyMethodDescrObject *)descr)->d_method, (PyObject *)this); \
+ } \
} else { \
- rvalue= PyCFunction_New(((PyMethodDescrObject *)descr)->d_method, (PyObject *)this); \
- } \
- \
- if (strcmp(PyString_AsString(attr), "__dict__")==0) {\
- rvalue = py_getattr_dict(rvalue, Methods, Attributes); \
+ PyErr_Clear(); \
+ PyObject *rvalue= Parent::py_getattro(attr); \
+ \
+ if (strcmp(PyString_AsString(attr), "__dict__")==0) { \
+ return py_getattr_dict(rvalue, Methods, Attributes); \
+ } \
+ \
+ return rvalue; \
} \
- return rvalue; \
+ return NULL;
+
+
+#define py_setattro_up(Parent) \
+ PyObject *descr = PyDict_GetItem(Type.tp_dict, attr); \
+ \
+ if(descr) { \
+ if (PyCObject_Check(descr)) { \
+ return py_set_attrdef((void *)this, (const PyAttributeDef*)PyCObject_AsVoidPtr(descr), value); \
+ } else { \
+ PyErr_Format(PyExc_AttributeError, "\"%s\" cannot be set", PyString_AsString(attr)); \
+ return -1; \
+ } \
+ } else { \
+ PyErr_Clear(); \
+ return Parent::py_setattro(attr, value); \
+ }
/**
@@ -376,8 +398,14 @@ public:
{
return ((PyObjectPlus*) PyObj)->py_getattro(attr);
}
+
+ static PyObject* py_get_attrdef(void *self, const PyAttributeDef *attrdef);
+ static int py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyObject *value);
+
+#if 0
static PyObject *py_getattro_self(const PyAttributeDef attrlist[], void *self, PyObject *attr);
static int py_setattro_self(const PyAttributeDef attrlist[], void *self, PyObject *attr, PyObject *value);
+#endif
virtual int py_delattro(PyObject *attr);
virtual int py_setattro(PyObject *attr, PyObject *value); // py_setattro method