remove python2.x support
[blender.git] / source / gameengine / Ketsji / KX_GameObject.cpp
index 30b3cb201a7104b3977d4a349ff726bfb63a5710..146b83abd17f47914048e45c9ab0ea2da2315def 100644 (file)
@@ -66,6 +66,7 @@ typedef unsigned long uint_ptr;
 #include "KX_PythonInit.h"
 #include "KX_PyMath.h"
 #include "KX_PythonSeq.h"
+#include "KX_ConvertPhysicsObject.h"
 #include "SCA_IActuator.h"
 #include "SCA_ISensor.h"
 #include "SCA_IController.h"
@@ -100,7 +101,6 @@ KX_GameObject::KX_GameObject(
        m_bOccluder(false),
        m_pPhysicsController1(NULL),
        m_pGraphicController(NULL),
-       m_pPhysicsEnvironment(NULL),
        m_xray(false),
        m_pHitObject(NULL),
        m_isDeformable(false),
@@ -1387,8 +1387,9 @@ PyMethodDef KX_GameObject::Methods[] = {
        {"getChildrenRecursive", (PyCFunction)KX_GameObject::sPyGetChildrenRecursive,METH_NOARGS},
        {"getPhysicsId", (PyCFunction)KX_GameObject::sPyGetPhysicsId,METH_NOARGS},
        {"getPropertyNames", (PyCFunction)KX_GameObject::sPyGetPropertyNames,METH_NOARGS},
-       {"replaceMesh",(PyCFunction) KX_GameObject::sPyReplaceMesh, METH_O},
+       {"replaceMesh",(PyCFunction) KX_GameObject::sPyReplaceMesh, METH_VARARGS},
        {"endObject",(PyCFunction) KX_GameObject::sPyEndObject, METH_NOARGS},
+       {"reinstancePhysicsMesh", (PyCFunction)KX_GameObject::sPyReinstancePhysicsMesh,METH_VARARGS},
        
        KX_PYMETHODTABLE(KX_GameObject, rayCastTo),
        KX_PYMETHODTABLE(KX_GameObject, rayCast),
@@ -1464,15 +1465,21 @@ bool KX_GameObject::ConvertPythonVectorArgs(PyObject* args,
 }
 */
 
-PyObject* KX_GameObject::PyReplaceMesh(PyObject* value)
+PyObject* KX_GameObject::PyReplaceMesh(PyObject* args)
 {
        KX_Scene *scene = KX_GetActiveScene();
-       RAS_MeshObject* new_mesh;
+       
+       PyObject *value;
+       int use_gfx= 1, use_phys= 0;
+       RAS_MeshObject *new_mesh;
+       
+       if (!PyArg_ParseTuple(args,"O|ii:replaceMesh", &value, &use_gfx, &use_phys))
+               return NULL;
        
        if (!ConvertPythonToMesh(value, &new_mesh, false, "gameOb.replaceMesh(value): KX_GameObject"))
                return NULL;
        
-       scene->ReplaceMesh(this, new_mesh);
+       scene->ReplaceMesh(this, new_mesh, (bool)use_gfx, (bool)use_phys);
        Py_RETURN_NONE;
 }
 
@@ -1486,6 +1493,28 @@ PyObject* KX_GameObject::PyEndObject()
 
 }
 
+PyObject* KX_GameObject::PyReinstancePhysicsMesh(PyObject* args)
+{
+       KX_GameObject *gameobj= NULL;
+       RAS_MeshObject *mesh= NULL;
+       
+       PyObject *gameobj_py= NULL;
+       PyObject *mesh_py= NULL;
+
+       if (    !PyArg_ParseTuple(args,"|OO:reinstancePhysicsMesh",&gameobj_py, &mesh_py) ||
+                       (gameobj_py && !ConvertPythonToGameObject(gameobj_py, &gameobj, true, "gameOb.reinstancePhysicsMesh(obj, mesh): KX_GameObject")) || 
+                       (mesh_py && !ConvertPythonToMesh(mesh_py, &mesh, true, "gameOb.reinstancePhysicsMesh(obj, mesh): KX_GameObject"))
+               ) {
+               return NULL;
+       }
+       
+       /* gameobj and mesh can be NULL */
+       if(KX_ReInstanceBulletShapeFromMesh(this, gameobj, mesh))
+               Py_RETURN_TRUE;
+
+       Py_RETURN_FALSE;
+}
+
 
 PyObject* KX_GameObject::PyGetPosition()
 {
@@ -1496,7 +1525,7 @@ PyObject* KX_GameObject::PyGetPosition()
 static PyObject *Map_GetItem(PyObject *self_v, PyObject *item)
 {
        KX_GameObject* self= static_cast<KX_GameObject*>BGE_PROXY_REF(self_v);
-       const char *attr_str= PyString_AsString(item);
+       const char *attr_str= _PyUnicode_AsString(item);
        CValue* resultattr;
        PyObject* pyconvert;
        
@@ -1530,7 +1559,7 @@ static PyObject *Map_GetItem(PyObject *self_v, PyObject *item)
 static int Map_SetItem(PyObject *self_v, PyObject *key, PyObject *val)
 {
        KX_GameObject* self= static_cast<KX_GameObject*>BGE_PROXY_REF(self_v);
-       const char *attr_str= PyString_AsString(key);
+       const char *attr_str= _PyUnicode_AsString(key);
        if(attr_str==NULL)
                PyErr_Clear();
        
@@ -1562,7 +1591,7 @@ static int Map_SetItem(PyObject *self_v, PyObject *key, PyObject *val)
                int set= 0;
                
                /* as CValue */
-               if(attr_str && BGE_PROXY_CHECK_TYPE(val)==0) /* dont allow GameObjects for eg to be assigned to CValue props */
+               if(attr_str && PyObject_TypeCheck(val, &PyObjectPlus::Type)==0) /* dont allow GameObjects for eg to be assigned to CValue props */
                {
                        CValue* vallie = self->ConvertPythonToValue(val, ""); /* error unused */
                        
@@ -1624,7 +1653,7 @@ static int Seq_Contains(PyObject *self_v, PyObject *value)
                return -1;
        }
        
-       if(PyString_Check(value) && self->GetProperty(PyString_AsString(value)))
+       if(PyUnicode_Check(value) && self->GetProperty(_PyUnicode_AsString(value)))
                return 1;
        
        if (self->m_attr_dict && PyDict_GetItem(self->m_attr_dict, value))
@@ -1652,41 +1681,37 @@ PySequenceMethods KX_GameObject::Sequence = {
 };
 
 PyTypeObject KX_GameObject::Type = {
-#if (PY_VERSION_HEX >= 0x02060000)
        PyVarObject_HEAD_INIT(NULL, 0)
-#else
-       /* python 2.5 and below */
-       PyObject_HEAD_INIT( NULL )  /* required py macro */
-       0,                          /* ob_size */
-#endif
-               "KX_GameObject",
-               sizeof(PyObjectPlus_Proxy),
-               0,
-               py_base_dealloc,
-               0,
-               0,
-               0,
-               0,
-               py_base_repr,
-               0,
-               &Sequence,
-               &Mapping,
-               0,0,0,
-               NULL, //py_base_getattro,
-               NULL, //py_base_setattro,
-               0,
-               Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
-               0,0,0,0,0,0,0,
-               Methods,
-               0,
-               0,
-               &SCA_IObject::Type
+       "KX_GameObject",
+       sizeof(PyObjectPlus_Proxy),
+       0,
+       py_base_dealloc,
+       0,
+       0,
+       0,
+       0,
+       py_base_repr,
+       0,
+       &Sequence,
+       &Mapping,
+       0,0,0,
+       NULL,
+       NULL,
+       0,
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+       0,0,0,0,0,0,0,
+       Methods,
+       0,
+       0,
+       &SCA_IObject::Type,
+       0,0,0,0,0,0,
+       py_base_new
 };
 
 PyObject* KX_GameObject::pyattr_get_name(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
-       return PyString_FromString(self->GetName().ReadPtr());
+       return PyUnicode_FromString(self->GetName().ReadPtr());
 }
 
 PyObject* KX_GameObject::pyattr_get_parent(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
@@ -1959,13 +1984,13 @@ PyObject* KX_GameObject::pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
        int state = 0;
        state |= self->GetState();
-       return PyInt_FromLong(state);
+       return PyLong_FromSsize_t(state);
 }
 
 int KX_GameObject::pyattr_set_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
 {
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
-       int state_i = PyInt_AsLong(value);
+       int state_i = PyLong_AsSsize_t(value);
        unsigned int state = 0;
        
        if (state_i == -1 && PyErr_Occurred()) {
@@ -2179,7 +2204,7 @@ PyObject* KX_GameObject::PySetOcclusion(PyObject* args)
 PyObject* KX_GameObject::PyGetVisible()
 {
        ShowDeprecationWarning("getVisible()", "the visible property");
-       return PyInt_FromLong(m_bVisible);      
+       return PyLong_FromSsize_t(m_bVisible);  
 }
 
 PyObject* KX_GameObject::PyGetState()
@@ -2187,13 +2212,13 @@ PyObject* KX_GameObject::PyGetState()
        ShowDeprecationWarning("getState()", "the state property");
        int state = 0;
        state |= GetState();
-       return PyInt_FromLong(state);
+       return PyLong_FromSsize_t(state);
 }
 
 PyObject* KX_GameObject::PySetState(PyObject* value)
 {
        ShowDeprecationWarning("setState()", "the state property");
-       int state_i = PyInt_AsLong(value);
+       int state_i = PyLong_AsSsize_t(value);
        unsigned int state = 0;
        
        if (state_i == -1 && PyErr_Occurred()) {
@@ -2499,7 +2524,7 @@ PyObject* KX_GameObject::PyGetPhysicsId()
        {
                physid= (uint_ptr)ctrl->GetUserData();
        }
-       return PyInt_FromLong((long)physid);
+       return PyLong_FromSsize_t((long)physid);
 }
 
 PyObject* KX_GameObject::PyGetPropertyNames()
@@ -2661,8 +2686,7 @@ KX_PYMETHODDEF_DOC(KX_GameObject, rayCastTo,
                toDir.normalize();
                toPoint = fromPoint + (dist) * toDir;
        }
-
-       PHY_IPhysicsEnvironment* pe = GetPhysicsEnvironment();
+       PHY_IPhysicsEnvironment* pe = KX_GetActiveScene()->GetPhysicsEnvironment();
        KX_IPhysicsController *spc = GetPhysicsController();
        KX_GameObject *parent = GetParent();
        if (!spc && parent)
@@ -2789,7 +2813,7 @@ KX_PYMETHODDEF_DOC(KX_GameObject, rayCast,
                return none_tuple_3();
        }
        
-       PHY_IPhysicsEnvironment* pe = GetPhysicsEnvironment();
+       PHY_IPhysicsEnvironment* pe = KX_GetActiveScene()->GetPhysicsEnvironment();
        KX_IPhysicsController *spc = GetPhysicsController();
        KX_GameObject *parent = GetParent();
        if (!spc && parent)
@@ -2875,8 +2899,8 @@ PyObject* KX_GameObject::Pyget(PyObject *args)
                return NULL;
        
        
-       if(PyString_Check(key)) {
-               CValue *item = GetProperty(PyString_AsString(key));
+       if(PyUnicode_Check(key)) {
+               CValue *item = GetProperty(_PyUnicode_AsString(key));
                if (item) {
                        ret = item->ConvertValueToPython();
                        if(ret)
@@ -2945,13 +2969,13 @@ bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py
                }
        }
        
-       if (PyString_Check(value)) {
-               *object = (KX_GameObject*)SCA_ILogicBrick::m_sCurrentLogicManager->GetGameObjectByName(STR_String( PyString_AsString(value) ));
+       if (PyUnicode_Check(value)) {
+               *object = (KX_GameObject*)SCA_ILogicBrick::m_sCurrentLogicManager->GetGameObjectByName(STR_String( _PyUnicode_AsString(value) ));
                
                if (*object) {
                        return true;
                } else {
-                       PyErr_Format(PyExc_ValueError, "%s, requested name \"%s\" did not match any KX_GameObject in this scene", error_prefix, PyString_AsString(value));
+                       PyErr_Format(PyExc_ValueError, "%s, requested name \"%s\" did not match any KX_GameObject in this scene", error_prefix, _PyUnicode_AsString(value));
                        return false;
                }
        }