2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r19323...
[blender.git] / source / gameengine / Ketsji / KX_PolygonMaterial.cpp
index bbaf697..46d0448 100644 (file)
@@ -98,8 +98,7 @@ bool KX_PolygonMaterial::Activate(RAS_IRasterizer* rasty, TCachingInfo& cachingI
        {
                PyObject *pyRasty = PyCObject_FromVoidPtr((void*)rasty, NULL);  /* new reference */
                PyObject *pyCachingInfo = PyCObject_FromVoidPtr((void*) &cachingInfo, NULL); /* new reference */
-               
-               PyObject *ret = PyObject_CallMethod(m_pymaterial, "activate", "(NNO)", pyRasty, pyCachingInfo, (PyObject*) this);
+               PyObject *ret = PyObject_CallMethod(m_pymaterial, "activate", "(NNO)", pyRasty, pyCachingInfo, (PyObject*) this->m_proxy);
                if (ret)
                {
                        bool value = PyInt_AsLong(ret);
@@ -109,6 +108,8 @@ bool KX_PolygonMaterial::Activate(RAS_IRasterizer* rasty, TCachingInfo& cachingI
                else
                {
                        PyErr_Print();
+                       PyErr_Clear();
+                       PySys_SetObject( (char *)"last_traceback", NULL);
                }
        }
        else
@@ -181,181 +182,70 @@ 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
 };
 
 PyTypeObject KX_PolygonMaterial::Type = {
-       PyObject_HEAD_INIT(&PyType_Type)
+       PyObject_HEAD_INIT(NULL)
                0,
                "KX_PolygonMaterial",
-               sizeof(KX_PolygonMaterial),
+               sizeof(PyObjectPlus_Proxy),
+               0,
+               py_base_dealloc,
+               0,
                0,
-               PyDestructor,
                0,
-               __getattr,
-               __setattr,
-               0, //&MyPyCompare,
-               __repr,
-               0 //&cvalue_as_number,
+               0,
+               py_base_repr,
+               0,0,0,0,0,0,
+               py_base_getattro,
+               py_base_setattro,
+               0,0,0,0,0,0,0,0,0,
+               Methods
 };
 
 PyParentObject KX_PolygonMaterial::Parents[] = {
-       &PyObjectPlus::Type,
        &KX_PolygonMaterial::Type,
+       &PyObjectPlus::Type,
        NULL
 };
 
-PyObject* KX_PolygonMaterial::_getattr(const char *attr)
-{
-       if (!strcmp(attr, "texture"))
-               return PyString_FromString(m_texturename.ReadPtr());
-       if (!strcmp(attr, "material"))
-               return PyString_FromString(m_materialname.ReadPtr());
-               
-       if (!strcmp(attr, "tface"))
-               return PyCObject_FromVoidPtr(m_tface, NULL);
-               
-       if (!strcmp(attr, "gl_texture"))
-       {
-               Image *ima = m_tface->tpage;
-               int bind = 0;
-               if (ima)
-                       bind = ima->bindcode;
-               
-               return PyInt_FromLong(bind);
-       }
-       
-       if (!strcmp(attr, "tile"))
-               return PyInt_FromLong(m_tile);
-       if (!strcmp(attr, "tilexrep"))
-               return PyInt_FromLong(m_tilexrep);
-       if (!strcmp(attr, "tileyrep"))
-               return PyInt_FromLong(m_tileyrep);
-       
-       if (!strcmp(attr, "drawingmode"))
-               return PyInt_FromLong(m_drawingmode);
-       if (!strcmp(attr, "transparent"))
-               return PyInt_FromLong(m_alpha);
-       if (!strcmp(attr, "zsort"))
-               return PyInt_FromLong(m_zsort);
-       if (!strcmp(attr, "lightlayer"))
-               return PyInt_FromLong(m_lightlayer);
-       if (!strcmp(attr, "triangle"))
-               // deprecated, triangle/quads shouldn't have been a material property
-               return 0;
-               
-       if (!strcmp(attr, "diffuse"))
-               return PyObjectFrom(m_diffuse);
-       if (!strcmp(attr, "shininess"))
-               return PyFloat_FromDouble(m_shininess);
-       if (!strcmp(attr, "specular"))
-               return PyObjectFrom(m_specular);
-       if (!strcmp(attr, "specularity"))
-               return PyFloat_FromDouble(m_specularity);
-       
-       _getattr_up(PyObjectPlus);
+PyObject* KX_PolygonMaterial::py_getattro(PyObject *attr)
+{      
+       py_getattro_up(PyObjectPlus);
 }
 
-int KX_PolygonMaterial::_setattr(const char *attr, PyObject *pyvalue)
+int KX_PolygonMaterial::py_setattro(PyObject *attr, PyObject *value)
 {
-       if (PyFloat_Check(pyvalue))
-       {
-               float value = PyFloat_AsDouble(pyvalue);
-               if (!strcmp(attr, "shininess"))
-               {
-                       m_shininess = value;
-                       return 0;
-               }
-               
-               if (!strcmp(attr, "specularity"))
-               {
-                       m_specularity = value;
-                       return 0;
-               }
-       }
-       
-       if (PyInt_Check(pyvalue))
-       {
-               int value = PyInt_AsLong(pyvalue);
-               if (!strcmp(attr, "tile"))
-               {
-                       m_tile = value;
-                       return 0;
-               }
-               
-               if (!strcmp(attr, "tilexrep"))
-               {
-                       m_tilexrep = value;
-                       return 0;
-               }
-               
-               if (!strcmp(attr, "tileyrep"))
-               {
-                       m_tileyrep = value;
-                       return 0;
-               }
-               
-               if (!strcmp(attr, "drawingmode"))
-               {
-                       m_drawingmode = value;
-                       return 0;
-               }
-               
-               if (!strcmp(attr, "transparent"))
-               {
-                       m_alpha = value;
-                       return 0;
-               }
-               
-               if (!strcmp(attr, "zsort"))
-               {
-                       m_zsort = value;
-                       return 0;
-               }
-               
-               if (!strcmp(attr, "lightlayer"))
-               {
-                       m_lightlayer = value;
-                       return 0;
-               }
-               
-               // This probably won't work...
-               if (!strcmp(attr, "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, "diffuse"))
-                               {
-                                       m_diffuse = value;
-                                       return 0;
-                               }
-                               
-                               if (!strcmp(attr, "specular"))
-                               {
-                                       m_specular = value;
-                                       return 0;
-                               }
-                       }
-               }
-       }
-
-       return PyObjectPlus::_setattr(attr, pyvalue);
+       py_setattro_up(PyObjectPlus);
 }
 
 KX_PYMETHODDEF_DOC(KX_PolygonMaterial, setCustomMaterial, "setCustomMaterial(material)")
 {
        PyObject *material;
-       if (PyArg_ParseTuple(args, "O", &material))
+       if (PyArg_ParseTuple(args, "O:setCustomMaterial", &material))
        {
                if (m_pymaterial) {
                        Py_DECREF(m_pymaterial);
@@ -371,7 +261,7 @@ KX_PYMETHODDEF_DOC(KX_PolygonMaterial, setCustomMaterial, "setCustomMaterial(mat
 KX_PYMETHODDEF_DOC(KX_PolygonMaterial, updateTexture, "updateTexture(tface, rasty)")
 {
        PyObject *pyrasty, *pytface;
-       if (PyArg_ParseTuple(args, "O!O!", &PyCObject_Type, &pytface, &PyCObject_Type, &pyrasty))
+       if (PyArg_ParseTuple(args, "O!O!:updateTexture", &PyCObject_Type, &pytface, &PyCObject_Type, &pyrasty))
        {
                MTFace *tface = (MTFace*) PyCObject_AsVoidPtr(pytface);
                RAS_IRasterizer *rasty = (RAS_IRasterizer*) PyCObject_AsVoidPtr(pyrasty);
@@ -387,7 +277,7 @@ KX_PYMETHODDEF_DOC(KX_PolygonMaterial, updateTexture, "updateTexture(tface, rast
 KX_PYMETHODDEF_DOC(KX_PolygonMaterial, setTexture, "setTexture(tface)")
 {
        PyObject *pytface;
-       if (PyArg_ParseTuple(args, "O!", &PyCObject_Type, &pytface))
+       if (PyArg_ParseTuple(args, "O!:setTexture", &PyCObject_Type, &pytface))
        {
                MTFace *tface = (MTFace*) PyCObject_AsVoidPtr(pytface);
                GPU_set_tpage(tface);
@@ -400,7 +290,7 @@ KX_PYMETHODDEF_DOC(KX_PolygonMaterial, setTexture, "setTexture(tface)")
 KX_PYMETHODDEF_DOC(KX_PolygonMaterial, activate, "activate(rasty, cachingInfo)")
 {
        PyObject *pyrasty, *pyCachingInfo;
-       if (PyArg_ParseTuple(args, "O!O!", &PyCObject_Type, &pyrasty, &PyCObject_Type, &pyCachingInfo))
+       if (PyArg_ParseTuple(args, "O!O!:activate", &PyCObject_Type, &pyrasty, &PyCObject_Type, &pyCachingInfo))
        {
                RAS_IRasterizer *rasty = static_cast<RAS_IRasterizer*>(PyCObject_AsVoidPtr(pyrasty));
                TCachingInfo *cachingInfo = static_cast<TCachingInfo*>(PyCObject_AsVoidPtr(pyCachingInfo));
@@ -413,3 +303,69 @@ 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);
+       int bindcode= 0;
+       if (self->m_tface && self->m_tface->tpage)
+               bindcode= self->m_tface->tpage->bindcode;
+       
+       return PyInt_FromLong(bindcode);
+}
+
+
+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;
+}