Make materials use PyAttributeDef's
authorCampbell Barton <ideasman42@gmail.com>
Sun, 5 Apr 2009 07:41:03 +0000 (07:41 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sun, 5 Apr 2009 07:41:03 +0000 (07:41 +0000)
source/gameengine/Ketsji/KX_PolygonMaterial.cpp
source/gameengine/Ketsji/KX_PolygonMaterial.h

index 6b9a6201d5c668b1d38e7ae716642919c814a85f..c5bddd6d1668a36903ca569ac1c4cbf4ebd612b3 100644 (file)
@@ -181,6 +181,28 @@ PyMethodDef KX_PolygonMaterial::Methods[] = {
 };
 
 PyAttributeDef KX_PolygonMaterial::Attributes[] = {
+       KX_PYATTRIBUTE_RO_FUNCTION("texture",   KX_PolygonMaterial, pyattr_get_texture),
+       KX_PYATTRIBUTE_RO_FUNCTION("material",  KX_PolygonMaterial, pyattr_get_material), /* should probably be .name ? */
+       
+       KX_PYATTRIBUTE_INT_RW("tile", INT_MIN, INT_MAX, true, KX_PolygonMaterial, m_tile),
+       KX_PYATTRIBUTE_INT_RW("tilexrep", INT_MIN, INT_MAX, true, KX_PolygonMaterial, m_tilexrep),
+       KX_PYATTRIBUTE_INT_RW("tileyrep", INT_MIN, INT_MAX, true, KX_PolygonMaterial, m_tileyrep),
+       KX_PYATTRIBUTE_INT_RW("drawingmode", INT_MIN, INT_MAX, true, KX_PolygonMaterial, m_drawingmode),        
+       KX_PYATTRIBUTE_INT_RW("lightlayer", INT_MIN, INT_MAX, true, KX_PolygonMaterial, m_lightlayer),
+
+       KX_PYATTRIBUTE_BOOL_RW("transparent", KX_PolygonMaterial, m_alpha),
+       KX_PYATTRIBUTE_BOOL_RW("zsort", KX_PolygonMaterial, m_zsort),
+       
+       KX_PYATTRIBUTE_FLOAT_RW("shininess", 0.0f, 1000.0f, KX_PolygonMaterial, m_shininess),
+       KX_PYATTRIBUTE_FLOAT_RW("specularity", 0.0f, 1000.0f, KX_PolygonMaterial, m_specularity),
+       
+       KX_PYATTRIBUTE_RW_FUNCTION("diffuse", KX_PolygonMaterial, pyattr_get_texture, pyattr_set_diffuse),
+       KX_PYATTRIBUTE_RW_FUNCTION("specular",KX_PolygonMaterial, pyattr_get_specular, pyattr_set_specular),    
+       
+       KX_PYATTRIBUTE_RO_FUNCTION("tface",     KX_PolygonMaterial, pyattr_get_tface), /* How the heck is this even useful??? - Campbell */
+       KX_PYATTRIBUTE_RO_FUNCTION("gl_texture", KX_PolygonMaterial, pyattr_get_gl_texture), /* could be called 'bindcode' */
+       
+       /* triangle used to be an attribute, removed for 2.49, nobody should be using it */
        { NULL }        //Sentinel
 };
 
@@ -211,151 +233,20 @@ PyParentObject KX_PolygonMaterial::Parents[] = {
 
 PyObject* KX_PolygonMaterial::py_getattro(PyObject *attr)
 {
-       char *attr_str= PyString_AsString(attr);
-       if (!strcmp(attr_str, "texture"))
-               return PyString_FromString(m_texturename.ReadPtr());
-       if (!strcmp(attr_str, "material"))
-               return PyString_FromString(m_materialname.ReadPtr());
-               
-       if (!strcmp(attr_str, "tface"))
-               return PyCObject_FromVoidPtr(m_tface, NULL);
-               
-       if (!strcmp(attr_str, "gl_texture"))
-       {
-               Image *ima = m_tface->tpage;
-               int bind = 0;
-               if (ima)
-                       bind = ima->bindcode;
-               
-               return PyInt_FromLong(bind);
-       }
-       
-       if (!strcmp(attr_str, "tile"))
-               return PyInt_FromLong(m_tile);
-       if (!strcmp(attr_str, "tilexrep"))
-               return PyInt_FromLong(m_tilexrep);
-       if (!strcmp(attr_str, "tileyrep"))
-               return PyInt_FromLong(m_tileyrep);
-       
-       if (!strcmp(attr_str, "drawingmode"))
-               return PyInt_FromLong(m_drawingmode);
-       if (!strcmp(attr_str, "transparent"))
-               return PyInt_FromLong(m_alpha);
-       if (!strcmp(attr_str, "zsort"))
-               return PyInt_FromLong(m_zsort);
-       if (!strcmp(attr_str, "lightlayer"))
-               return PyInt_FromLong(m_lightlayer);
-       if (!strcmp(attr_str, "triangle"))
-               // deprecated, triangle/quads shouldn't have been a material property
-               return 0;
-               
-       if (!strcmp(attr_str, "diffuse"))
-               return PyObjectFrom(m_diffuse);
-       if (!strcmp(attr_str, "shininess"))
-               return PyFloat_FromDouble(m_shininess);
-       if (!strcmp(attr_str, "specular"))
-               return PyObjectFrom(m_specular);
-       if (!strcmp(attr_str, "specularity"))
-               return PyFloat_FromDouble(m_specularity);
+       PyObject* object = py_getattro_self(Attributes, this, attr);
+       if (object != NULL)
+               return object;
        
        py_getattro_up(PyObjectPlus);
 }
 
-int KX_PolygonMaterial::py_setattro(PyObject *attr, PyObject *pyvalue)
+int KX_PolygonMaterial::py_setattro(PyObject *attr, PyObject *value)
 {
-       char *attr_str= PyString_AsString(attr);
-       if (PyFloat_Check(pyvalue))
-       {
-               float value = PyFloat_AsDouble(pyvalue);
-               if (!strcmp(attr_str, "shininess"))
-               {
-                       m_shininess = value;
-                       return 0;
-               }
-               
-               if (!strcmp(attr_str, "specularity"))
-               {
-                       m_specularity = value;
-                       return 0;
-               }
-       }
-       
-       if (PyInt_Check(pyvalue))
-       {
-               int value = PyInt_AsLong(pyvalue);
-               if (!strcmp(attr_str, "tile"))
-               {
-                       m_tile = value;
-                       return 0;
-               }
-               
-               if (!strcmp(attr_str, "tilexrep"))
-               {
-                       m_tilexrep = value;
-                       return 0;
-               }
-               
-               if (!strcmp(attr_str, "tileyrep"))
-               {
-                       m_tileyrep = value;
-                       return 0;
-               }
-               
-               if (!strcmp(attr_str, "drawingmode"))
-               {
-                       m_drawingmode = value;
-                       return 0;
-               }
-               
-               if (!strcmp(attr_str, "transparent"))
-               {
-                       m_alpha = value;
-                       return 0;
-               }
-               
-               if (!strcmp(attr_str, "zsort"))
-               {
-                       m_zsort = value;
-                       return 0;
-               }
-               
-               if (!strcmp(attr_str, "lightlayer"))
-               {
-                       m_lightlayer = value;
-                       return 0;
-               }
-               
-               // This probably won't work...
-               if (!strcmp(attr_str, "triangle"))
-               {
-                       // deprecated, triangle/quads shouldn't have been a material property
-                       return 0;
-               }
-       }
-       
-       if (PySequence_Check(pyvalue))
-       {
-               if (PySequence_Size(pyvalue) == 3)
-               {
-                       MT_Vector3 value;
-                       if (PyVecTo(pyvalue, value))
-                       {
-                               if (!strcmp(attr_str, "diffuse"))
-                               {
-                                       m_diffuse = value;
-                                       return 0;
-                               }
-                               
-                               if (!strcmp(attr_str, "specular"))
-                               {
-                                       m_specular = value;
-                                       return 0;
-                               }
-                       }
-               }
-       }
+       int ret = py_setattro_self(Attributes, this, attr, value);
+       if (ret >= 0)
+               return ret;
 
-       return PyObjectPlus::py_setattro(attr, pyvalue);
+       return PyObjectPlus::py_setattro(attr, value);
 }
 
 KX_PYMETHODDEF_DOC(KX_PolygonMaterial, setCustomMaterial, "setCustomMaterial(material)")
@@ -419,3 +310,66 @@ KX_PYMETHODDEF_DOC(KX_PolygonMaterial, activate, "activate(rasty, cachingInfo)")
        
        return NULL;
 }
+
+PyObject* KX_PolygonMaterial::pyattr_get_texture(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
+       return PyString_FromString(self->m_texturename.ReadPtr());
+}
+
+PyObject* KX_PolygonMaterial::pyattr_get_material(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
+       return PyString_FromString(self->m_materialname.ReadPtr());
+}
+
+/* this does not seem useful */
+PyObject* KX_PolygonMaterial::pyattr_get_tface(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
+       return PyCObject_FromVoidPtr(self->m_tface, NULL);
+}
+
+PyObject* KX_PolygonMaterial::pyattr_get_gl_texture(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
+       Image *ima = self->m_tface->tpage;
+       return PyInt_FromLong(ima ? ima->bindcode:0);
+}
+
+
+PyObject* KX_PolygonMaterial::pyattr_get_diffuse(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
+       return PyObjectFrom(self->m_diffuse);
+}
+
+int KX_PolygonMaterial::pyattr_set_diffuse(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
+       MT_Vector3 vec;
+       
+       if (!PyVecTo(value, vec))
+               return -1;
+       
+       self->m_diffuse= vec;
+       return 0;
+}
+
+PyObject* KX_PolygonMaterial::pyattr_get_specular(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
+       return PyObjectFrom(self->m_specular);
+}
+
+int KX_PolygonMaterial::pyattr_set_specular(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
+       MT_Vector3 vec;
+       
+       if (!PyVecTo(value, vec))
+               return -1;
+       
+       self->m_specular= vec;
+       return 0;
+}
index f3eb1979f34b57c5a05502b35747db878776c138..1e8ce5c33678d6382facc2a133dd4b3ea400f114 100644 (file)
@@ -117,6 +117,18 @@ public:
 
        virtual PyObject* py_getattro(PyObject *attr);
        virtual int       py_setattro(PyObject *attr, PyObject *pyvalue);
+       
+       
+       static PyObject*        pyattr_get_texture(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       static PyObject*        pyattr_get_material(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       
+       static PyObject*        pyattr_get_tface(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       static PyObject*        pyattr_get_gl_texture(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       
+       static PyObject*        pyattr_get_diffuse(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);    
+       static int                      pyattr_set_diffuse(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
+       static PyObject*        pyattr_get_specular(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);   
+       static int                      pyattr_set_specular(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
 };
 
 #endif // __KX_POLYGONMATERIAL_H__