BGE Python API
authorCampbell Barton <ideasman42@gmail.com>
Sun, 12 Apr 2009 09:56:30 +0000 (09:56 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sun, 12 Apr 2009 09:56:30 +0000 (09:56 +0000)
added defines PY_SET_ATTR_FAIL, PY_SET_ATTR_MISSING and PY_SET_ATTR_SUCCESS

This is useful when objects that have user defined attributes (GameObject and Scene)
When calling setattr on the parent, a return value of PY_SET_ATTR_FAIL means the attribute exists but failed to be set, so don't set the custom attribute.

source/gameengine/Expressions/PyObjectPlus.cpp
source/gameengine/Expressions/PyObjectPlus.h
source/gameengine/Expressions/Value.cpp
source/gameengine/Ketsji/KX_ConstraintWrapper.cpp
source/gameengine/Ketsji/KX_Light.cpp
source/gameengine/Ketsji/KX_Scene.h
source/gameengine/Ketsji/KX_VehicleWrapper.cpp
source/gameengine/Ketsji/KX_VertexProxy.cpp

index 4526f8f649bf5395989784030bb525f6595a53ef..33335ebef3e9f7fc65cf279c88b4bed235b00e4e 100644 (file)
@@ -137,7 +137,7 @@ int PyObjectPlus::py_setattro(PyObject *attr, PyObject* value)
        //return PyObject::py_setattro(attr,value);
        //cerr << "Unknown attribute" << endl;
        PyErr_SetString(PyExc_AttributeError, "attribute cant be set");
-       return 1;
+       return PY_SET_ATTR_MISSING;
 }
 
 PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef)
@@ -699,13 +699,13 @@ int PyObjectPlus::py_setattro_self(const PyAttributeDef attrlist[], void *self,
                                attrdef->m_type == KX_PYATTRIBUTE_TYPE_DUMMY)
                        {
                                PyErr_SetString(PyExc_AttributeError, "property is read-only");
-                               return 1;
+                               return PY_SET_ATTR_FAIL;
                        }
                        
                        return py_set_attrdef(self, attrdef, value);
                }
        }
-       return -1;                      
+       return PY_SET_ATTR_MISSING;                     
 }
 #endif
 
index 421684616343ffd9054a6b1a4770886f4468ad52..f61b9f8d0b8ee0b6fcd3da9a3bbee23daa18f5e7 100644 (file)
@@ -124,6 +124,16 @@ static inline void Py_Fatal(const char *M) {
        return NULL;
 
 
+/*
+ * nonzero values are an error for setattr
+ * however because of the nested lookups we need to know if the errors
+ * was because the attribute didnt exits of if there was some problem setting the value
+ */
+
+#define PY_SET_ATTR_FAIL                1
+#define PY_SET_ATTR_MISSING    -1
+#define PY_SET_ATTR_SUCCESS                     0
+
 #define py_setattro_up(Parent) \
        PyObject *descr = PyDict_GetItem(Type.tp_dict, attr); \
         \
@@ -132,14 +142,14 @@ static inline void Py_Fatal(const char *M) {
                        const PyAttributeDef* attrdef= reinterpret_cast<const PyAttributeDef *>(PyCObject_AsVoidPtr(descr)); \
                        if (attrdef->m_access == KX_PYATTRIBUTE_RO) { \
                                PyErr_Format(PyExc_AttributeError, "\"%s\" is read only", PyString_AsString(attr)); \
-                               return -1; \
+                               return PY_SET_ATTR_FAIL; \
                        } \
                        else { \
                                return py_set_attrdef((void *)this, attrdef, value); \
                        } \
                } else { \
                        PyErr_Format(PyExc_AttributeError, "\"%s\" cannot be set", PyString_AsString(attr)); \
-                       return -1; \
+                       return PY_SET_ATTR_FAIL; \
                } \
        } else { \
                PyErr_Clear(); \
index 47f0686c4c31c8abf48b7f0ece15b49a12823c5a..f1e367d6e59d0e834976e9e329421fc68e901141 100644 (file)
@@ -804,11 +804,11 @@ int       CValue::py_setattro(PyObject *attr, PyObject* pyobj)
                vallie->Release();
        } else
        {
-               return 1; /* ConvertPythonToValue sets the error message */
+               return PY_SET_ATTR_FAIL; /* ConvertPythonToValue sets the error message */
        }
        
        //PyObjectPlus::py_setattro(attr,value);
-       return 0;
+       return PY_SET_ATTR_SUCCESS;
 };
 
 PyObject*      CValue::ConvertKeysToPython( void )
index edd6e991e9f6295d11fee839b1601fdd36e6e31f..6f4d970c568a44dd20f841cb29357885eec98573 100644 (file)
@@ -101,7 +101,7 @@ PyObject*   KX_ConstraintWrapper::py_getattro(PyObject *attr)
 int    KX_ConstraintWrapper::py_setattro(PyObject *attr,PyObject* pyobj)
 {
        int result = 1;
-
+       /* what the heck is this supposed to do?, needs attention */
        if (PyList_Check(pyobj))
        {
                result = 0;
index f996f86d751239d1d966093c4a8972d51cfb085a..059345ea8deb6bb0ace4f9bd86ac24b3f4d00231 100644 (file)
@@ -225,14 +225,14 @@ int       KX_LightObject::py_setattro(PyObject *attr, PyObject *pyvalue)
                if (!strcmp(attr_str, "layer"))
                {
                        m_lightobj.m_layer = value;
-                       return 0;
+                       return PY_SET_ATTR_SUCCESS;
                }
                
                if (!strcmp(attr_str, "type"))
                {
                        if (value >= RAS_LightObject::LIGHT_SPOT && value <= RAS_LightObject::LIGHT_NORMAL)
                                m_lightobj.m_type = (RAS_LightObject::LightType) value;
-                       return 0;
+                       return PY_SET_ATTR_SUCCESS;
                }
        }
        
@@ -242,37 +242,37 @@ int       KX_LightObject::py_setattro(PyObject *attr, PyObject *pyvalue)
                if (!strcmp(attr_str, "energy"))
                {
                        m_lightobj.m_energy = value;
-                       return 0;
+                       return PY_SET_ATTR_SUCCESS;
                }
        
                if (!strcmp(attr_str, "distance"))
                {
                        m_lightobj.m_distance = value;
-                       return 0;
+                       return PY_SET_ATTR_SUCCESS;
                }
                
                if (!strcmp(attr_str, "lin_attenuation"))
                {
                        m_lightobj.m_att1 = value;
-                       return 0;
+                       return PY_SET_ATTR_SUCCESS;
                }
                
                if (!strcmp(attr_str, "quad_attenuation"))
                {
                        m_lightobj.m_att2 = value;
-                       return 0;
+                       return PY_SET_ATTR_SUCCESS;
                }
                
                if (!strcmp(attr_str, "spotsize"))
                {
                        m_lightobj.m_spotsize = value;
-                       return 0;
+                       return PY_SET_ATTR_SUCCESS;
                }
                
                if (!strcmp(attr_str, "spotblend"))
                {
                        m_lightobj.m_spotblend = value;
-                       return 0;
+                       return PY_SET_ATTR_SUCCESS;
                }
        }
 
@@ -286,16 +286,16 @@ int       KX_LightObject::py_setattro(PyObject *attr, PyObject *pyvalue)
                                m_lightobj.m_red = color[0];
                                m_lightobj.m_green = color[1];
                                m_lightobj.m_blue = color[2];
-                               return 0;
+                               return PY_SET_ATTR_SUCCESS;
                        }
-                       return 1;
+                       return PY_SET_ATTR_FAIL;
                }
        }
        
        if (!strcmp(attr_str, "SPOT") || !strcmp(attr_str, "SUN") || !strcmp(attr_str, "NORMAL"))
        {
                PyErr_Format(PyExc_RuntimeError, "Attribute %s is read only.", attr_str);
-               return 1;
+               return PY_SET_ATTR_FAIL;
        }
        
        return KX_GameObject::py_setattro(attr, pyvalue);
index 1bdfbd45d20cdfbce34eed2af5539406c2d834be..b70c6d3e8d3113b770b764c14a6a42ba907442f6 100644 (file)
@@ -597,13 +597,13 @@ public:
                
                int ret= ((PyObjectPlus*) self)->py_setattro(attr, value);
                
-               if (ret) {
-                       if (!PyDict_SetItem(((KX_Scene *) self)->m_attrlist, attr, value)) {
+               if (ret==PY_SET_ATTR_MISSING) {
+                       if (PyDict_SetItem(((KX_Scene *) self)->m_attrlist, attr, value)==0) {
                                PyErr_Clear();
-                               ret= 0;
+                               ret= PY_SET_ATTR_SUCCESS;
                        }
                        else {
-                               ret= -1;
+                               ret= PY_SET_ATTR_FAIL;
                        }
                }
                
index 315b60922b7f20877fd1c617fbdb7d3139f4f77c..98f5a1ea87deb7e90b53fd7399e0839f4e19571d 100644 (file)
@@ -331,7 +331,7 @@ PyObject*   KX_VehicleWrapper::py_getattro(PyObject *attr)
 
 int    KX_VehicleWrapper::py_setattro(PyObject *attr,PyObject* pyobj)
 {
-       
+       /* TODO - strange setattr, needs updating */
        PyTypeObject* type = pyobj->ob_type;
        int result = 1;
 
index bb12f4053326aa9d505e5249fee96917d3439438..6a160dff7b70b9e1e7a9454e77a6ad8251daf717 100644 (file)
@@ -171,9 +171,9 @@ int    KX_VertexProxy::py_setattro(PyObject *attr, PyObject *pyvalue)
                {
                        m_vertex->SetXYZ(vec);
                        m_mesh->SetMeshModified(true);
-                       return 0;
+                       return PY_SET_ATTR_SUCCESS;
                }
-               return 1;
+               return PY_SET_ATTR_FAIL;
        }
        
        if (!strcmp(attr_str, "UV"))
@@ -183,9 +183,9 @@ int    KX_VertexProxy::py_setattro(PyObject *attr, PyObject *pyvalue)
                {
                        m_vertex->SetUV(vec);
                        m_mesh->SetMeshModified(true);
-                       return 0;
+                       return PY_SET_ATTR_SUCCESS;
                }
-               return 1;
+               return PY_SET_ATTR_FAIL;
        }
        
        if (!strcmp(attr_str, "color") || !strcmp(attr_str, "colour"))
@@ -195,9 +195,9 @@ int    KX_VertexProxy::py_setattro(PyObject *attr, PyObject *pyvalue)
                {
                        m_vertex->SetRGBA(vec);
                        m_mesh->SetMeshModified(true);
-                       return 0;
+                       return PY_SET_ATTR_SUCCESS;
                }
-               return 1;
+               return PY_SET_ATTR_FAIL;
        }
        
        if (!strcmp(attr_str, "normal"))
@@ -207,9 +207,9 @@ int    KX_VertexProxy::py_setattro(PyObject *attr, PyObject *pyvalue)
                {
                        m_vertex->SetNormal(vec);
                        m_mesh->SetMeshModified(true);
-                       return 0;
+                       return PY_SET_ATTR_SUCCESS;
                }
-               return 1;
+               return PY_SET_ATTR_FAIL;
        }
   }
   
@@ -223,7 +223,7 @@ int    KX_VertexProxy::py_setattro(PyObject *attr, PyObject *pyvalue)
                pos.x() = val;
                m_vertex->SetXYZ(pos);
                m_mesh->SetMeshModified(true);
-               return 0;
+               return PY_SET_ATTR_SUCCESS;
        }
        
        if (!strcmp(attr_str, "y"))
@@ -231,7 +231,7 @@ int    KX_VertexProxy::py_setattro(PyObject *attr, PyObject *pyvalue)
                pos.y() = val;
                m_vertex->SetXYZ(pos);
                m_mesh->SetMeshModified(true);
-               return 0;
+               return PY_SET_ATTR_SUCCESS;
        }
        
        if (!strcmp(attr_str, "z"))
@@ -239,7 +239,7 @@ int    KX_VertexProxy::py_setattro(PyObject *attr, PyObject *pyvalue)
                pos.z() = val;
                m_vertex->SetXYZ(pos);
                m_mesh->SetMeshModified(true);
-               return 0;
+               return PY_SET_ATTR_SUCCESS;
        }
        
        // uv
@@ -249,7 +249,7 @@ int    KX_VertexProxy::py_setattro(PyObject *attr, PyObject *pyvalue)
                uv[0] = val;
                m_vertex->SetUV(uv);
                m_mesh->SetMeshModified(true);
-               return 0;
+               return PY_SET_ATTR_SUCCESS;
        }
 
        if (!strcmp(attr_str, "v"))
@@ -257,7 +257,7 @@ int    KX_VertexProxy::py_setattro(PyObject *attr, PyObject *pyvalue)
                uv[1] = val;
                m_vertex->SetUV(uv);
                m_mesh->SetMeshModified(true);
-               return 0;
+               return PY_SET_ATTR_SUCCESS;
        }
 
        // uv
@@ -275,7 +275,7 @@ int    KX_VertexProxy::py_setattro(PyObject *attr, PyObject *pyvalue)
                uv[1] = val;
                m_vertex->SetUV2(uv);
                m_mesh->SetMeshModified(true);
-               return 0;
+               return PY_SET_ATTR_SUCCESS;
        }
        
        // col
@@ -287,28 +287,28 @@ int    KX_VertexProxy::py_setattro(PyObject *attr, PyObject *pyvalue)
                cp[0] = (unsigned char) val;
                m_vertex->SetRGBA(icol);
                m_mesh->SetMeshModified(true);
-               return 0;
+               return PY_SET_ATTR_SUCCESS;
        }
        if (!strcmp(attr_str, "g"))
        {
                cp[1] = (unsigned char) val;
                m_vertex->SetRGBA(icol);
                m_mesh->SetMeshModified(true);
-               return 0;
+               return PY_SET_ATTR_SUCCESS;
        }
        if (!strcmp(attr_str, "b"))
        {
                cp[2] = (unsigned char) val;
                m_vertex->SetRGBA(icol);
                m_mesh->SetMeshModified(true);
-               return 0;
+               return PY_SET_ATTR_SUCCESS;
        }
        if (!strcmp(attr_str, "a"))
        {
                cp[3] = (unsigned char) val;
                m_vertex->SetRGBA(icol);
                m_mesh->SetMeshModified(true);
-               return 0;
+               return PY_SET_ATTR_SUCCESS;
        }
   }