2nd try to merge sim_physics with trunk rev 19825
[blender.git] / source / gameengine / Ketsji / KX_GameObject.cpp
index bea0fcff2af2dbbc080a5466203a1eef5ac4717f..9fe7ab75267413db5fb52754201605ceb26ab17d 100644 (file)
@@ -1182,12 +1182,22 @@ bool KX_GameObject::ConvertPythonVectorArgs(PyObject* args,
 PyObject* KX_GameObject::PyReplaceMesh(PyObject* value)
 {
        KX_Scene *scene = KX_GetActiveScene();
-       RAS_MeshObject* new_mesh;
+       char* meshname;
+       void* mesh_pt;
+
+       meshname = PyString_AsString(value);
+       if (meshname==NULL) {
+               PyErr_SetString(PyExc_ValueError, "Expected a mesh name");
+               return NULL;
+       }
+       mesh_pt = SCA_ILogicBrick::m_sCurrentLogicManager->GetMeshByName(STR_String(meshname));
        
-       if (!ConvertPythonToMesh(value, &new_mesh, false, "gameOb.replaceMesh(value): KX_GameObject"))
+       if (mesh_pt==NULL) {
+               PyErr_SetString(PyExc_ValueError, "The mesh name given does not exist");
                return NULL;
+       }
+       scene->ReplaceMesh(this, (class RAS_MeshObject*)mesh_pt);
        
-       scene->ReplaceMesh(this, new_mesh);
        Py_RETURN_NONE;
 }
 
@@ -1249,8 +1259,8 @@ PyObject *KX_GameObject::Map_GetItem(PyObject *self_v, PyObject *item)
                return pyconvert;
        }
        else {
-               if(attr_str)    PyErr_Format(PyExc_KeyError, "value = gameOb[key]: KX_GameObject, key \"%s\" does not exist", attr_str);
-               else                    PyErr_SetString(PyExc_KeyError, "value = gameOb[key]: KX_GameObject, key does not exist");
+               if(attr_str)    PyErr_Format(PyExc_KeyError, "KX_GameObject key \"%s\" does not exist", attr_str);
+               else                    PyErr_SetString(PyExc_KeyError, "KX_GameObject key does not exist");
                return NULL;
        }
                
@@ -1280,8 +1290,8 @@ int KX_GameObject::Map_SetItem(PyObject *self_v, PyObject *key, PyObject *val)
                        del |= (PyDict_DelItem(self->m_attr_dict, key)==0) ? 1:0;
                
                if (del==0) {
-                       if(attr_str)    PyErr_Format(PyExc_KeyError, "gameOb[key] = value: KX_GameObject, key \"%s\" could not be set", attr_str);
-                       else                    PyErr_SetString(PyExc_KeyError, "gameOb[key] = value: KX_GameObject, key could not be set");
+                       if(attr_str)    PyErr_Format(PyExc_KeyError, "KX_GameObject key \"%s\" not found", attr_str);
+                       else                    PyErr_SetString(PyExc_KeyError, "KX_GameObject key not found");
                        return -1;
                }
                else if (self->m_attr_dict) {
@@ -1332,8 +1342,8 @@ int KX_GameObject::Map_SetItem(PyObject *self_v, PyObject *key, PyObject *val)
                                set= 1;
                        }
                        else {
-                               if(attr_str)    PyErr_Format(PyExc_KeyError, "gameOb[key] = value: KX_GameObject, key \"%s\" not be added to internal dictionary", attr_str);
-                               else                    PyErr_SetString(PyExc_KeyError, "gameOb[key] = value: KX_GameObject, key not be added to internal dictionary");
+                               if(attr_str)    PyErr_Format(PyExc_KeyError, "KX_GameObject key \"%s\" not be added to internal dictionary", attr_str);
+                               else                    PyErr_SetString(PyExc_KeyError, "KX_GameObject key not be added to internal dictionary");
                        }
                }
                
@@ -1414,7 +1424,7 @@ int KX_GameObject::pyattr_set_mass(void *self_v, const KX_PYATTRIBUTE_DEF *attrd
        KX_IPhysicsController *spc = self->GetPhysicsController();
        MT_Scalar val = PyFloat_AsDouble(value);
        if (val < 0.0f) { /* also accounts for non float */
-               PyErr_SetString(PyExc_AttributeError, "gameOb.mass = float: KX_GameObject, expected a float zero or above");
+               PyErr_SetString(PyExc_AttributeError, "expected a float zero or above");
                return 1;
        }
 
@@ -1437,7 +1447,7 @@ int KX_GameObject::pyattr_set_lin_vel_min(void *self_v, const KX_PYATTRIBUTE_DEF
        KX_IPhysicsController *spc = self->GetPhysicsController();
        MT_Scalar val = PyFloat_AsDouble(value);
        if (val < 0.0f) { /* also accounts for non float */
-               PyErr_SetString(PyExc_AttributeError, "gameOb.linVelocityMin = float: KX_GameObject, expected a float zero or above");
+               PyErr_SetString(PyExc_AttributeError, "expected a float zero or above");
                return 1;
        }
 
@@ -1460,7 +1470,7 @@ int KX_GameObject::pyattr_set_lin_vel_max(void *self_v, const KX_PYATTRIBUTE_DEF
        KX_IPhysicsController *spc = self->GetPhysicsController();
        MT_Scalar val = PyFloat_AsDouble(value);
        if (val < 0.0f) { /* also accounts for non float */
-               PyErr_SetString(PyExc_AttributeError, "gameOb.linVelocityMax = float: KX_GameObject, expected a float zero or above");
+               PyErr_SetString(PyExc_AttributeError, "expected a float zero or above");
                return 1;
        }
 
@@ -1482,7 +1492,7 @@ int KX_GameObject::pyattr_set_visible(void *self_v, const KX_PYATTRIBUTE_DEF *at
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
        int param = PyObject_IsTrue( value );
        if (param == -1) {
-               PyErr_SetString(PyExc_AttributeError, "gameOb.visible = bool: KX_GameObject, expected True or False");
+               PyErr_SetString(PyExc_AttributeError, "expected True or False");
                return 1;
        }
 
@@ -1558,15 +1568,49 @@ PyObject* KX_GameObject::pyattr_get_localOrientation(void *self_v, const KX_PYAT
 int KX_GameObject::pyattr_set_localOrientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
 {
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
-       
-       /* if value is not a sequence PyOrientationTo makes an error */
+       if (!PySequence_Check(value)) {
+               PyErr_SetString(PyExc_AttributeError, "'orientation' attribute needs to be a sequence");
+               return 1;
+       }
+
        MT_Matrix3x3 rot;
-       if (!PyOrientationTo(value, rot, "gameOb.orientation = sequence: KX_GameObject, "))
-               return NULL;
 
-       self->NodeSetLocalOrientation(rot);
-       self->NodeUpdateGS(0.f);
-       return 0;
+       if (PyMatTo(value, rot))
+       {
+               self->NodeSetLocalOrientation(rot);
+               self->NodeUpdateGS(0.f);
+               return 0;
+       }
+       PyErr_Clear();
+
+       if (PySequence_Size(value) == 4)
+       {
+               MT_Quaternion qrot;
+               if (PyVecTo(value, qrot))
+               {
+                       rot.setRotation(qrot);
+                       self->NodeSetLocalOrientation(rot);
+                       self->NodeUpdateGS(0.f);
+                       return 0;
+               }
+               return 1;
+       }
+
+       if (PySequence_Size(value) == 3)
+       {
+               MT_Vector3 erot;
+               if (PyVecTo(value, erot))
+               {
+                       rot.setEuler(erot);
+                       self->NodeSetLocalOrientation(rot);
+                       self->NodeUpdateGS(0.f);
+                       return 0;
+               }
+               return 1;
+       }
+
+       PyErr_SetString(PyExc_AttributeError, "could not set the orientation from a 3x3 matrix, quaternion or euler sequence");
+       return 1;
 }
 
 PyObject* KX_GameObject::pyattr_get_worldScaling(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
@@ -1614,7 +1658,7 @@ int KX_GameObject::pyattr_set_timeOffset(void *self_v, const KX_PYATTRIBUTE_DEF
                MT_Scalar val = PyFloat_AsDouble(value);
                SG_Node* sg_parent= self->GetSGNode()->GetSGParent();
                if (val < 0.0f) { /* also accounts for non float */
-                       PyErr_SetString(PyExc_AttributeError, "gameOb.timeOffset = float: KX_GameObject, expected a float zero or above");
+                       PyErr_SetString(PyExc_AttributeError, "expected a float zero or above");
                        return 1;
                }
                if (sg_parent && sg_parent->IsSlowParent())
@@ -1638,13 +1682,13 @@ int KX_GameObject::pyattr_set_state(void *self_v, const KX_PYATTRIBUTE_DEF *attr
        unsigned int state = 0;
        
        if (state_i == -1 && PyErr_Occurred()) {
-               PyErr_SetString(PyExc_TypeError, "gameOb.state = int: KX_GameObject, expected an int bit field");
+               PyErr_SetString(PyExc_TypeError, "expected an int bit field");
                return 1;
        }
        
        state |= state_i;
        if ((state & ((1<<30)-1)) == 0) {
-               PyErr_SetString(PyExc_AttributeError, "gameOb.state = int: KX_GameObject, state bitfield was not between 0 and 30 (1<<0 and 1<<29)");
+               PyErr_SetString(PyExc_AttributeError, "The state bitfield was not between 0 and 30 (1<<0 and 1<<29)");
                return 1;
        }
        self->SetState(state);
@@ -1657,7 +1701,7 @@ PyObject* KX_GameObject::pyattr_get_meshes(void *self_v, const KX_PYATTRIBUTE_DE
        PyObject *meshes= PyList_New(self->m_meshes.size());
        int i;
        
-       for(i=0; i < self->m_meshes.size(); i++)
+       for(i=0; i < (int)self->m_meshes.size(); i++)
        {
                KX_MeshProxy* meshproxy = new KX_MeshProxy(self->m_meshes[i]);
                PyList_SET_ITEM(meshes, i, meshproxy->GetProxy());
@@ -1803,7 +1847,7 @@ int KX_GameObject::py_setattro(PyObject *attr, PyObject *value)   // py_setattro m
                        ret= PY_SET_ATTR_SUCCESS;
                }
                else {
-                       PyErr_Format(PyExc_AttributeError, "gameOb.myAttr = value: KX_GameObject, failed assigning value to internal dictionary");
+                       PyErr_Format(PyExc_AttributeError, "failed assigning value to KX_GameObject internal dictionary");
                        ret= PY_SET_ATTR_FAIL;
                }
        }
@@ -1822,7 +1866,7 @@ int       KX_GameObject::py_delattro(PyObject *attr)
        if (m_attr_dict && (PyDict_DelItem(m_attr_dict, attr) == 0))
                return 0;
        
-       PyErr_Format(PyExc_AttributeError, "del gameOb.myAttr: KX_GameObject, attribute \"%s\" dosnt exist", attr_str);
+       PyErr_Format(PyExc_AttributeError, "attribute \"%s\" dosnt exist", attr_str);
        return 1;
 }
 
@@ -2083,7 +2127,7 @@ PyObject* KX_GameObject::PyGetParent()
 PyObject* KX_GameObject::PySetParent(PyObject* value)
 {
        KX_GameObject *obj;
-       if (!ConvertPythonToGameObject(value, &obj, false, "gameOb.setParent(value): KX_GameObject"))
+       if (!ConvertPythonToGameObject(value, &obj, false))
                return NULL;
        
        this->SetParent(KX_GetActiveScene(), obj);
@@ -2318,7 +2362,7 @@ KX_PYMETHODDEF_DOC_O(KX_GameObject, getDistanceTo,
        PyErr_Clear();
        
        KX_GameObject *other;
-       if (ConvertPythonToGameObject(value, &other, false, "gameOb.getDistanceTo(value): KX_GameObject"))
+       if (ConvertPythonToGameObject(value, &other, false))
        {
                return PyFloat_FromDouble(NodeGetWorldPosition().distance(other->NodeGetWorldPosition()));
        }
@@ -2341,12 +2385,12 @@ KX_PYMETHODDEF_DOC_O(KX_GameObject, getVectTo,
                PyErr_Clear();
                
                KX_GameObject *other;
-               if (ConvertPythonToGameObject(value, &other, false, "")) /* error will be overwritten */
+               if (ConvertPythonToGameObject(value, &other, false))
                {
                        toPoint = other->NodeGetWorldPosition();
                } else
                {
-                       PyErr_SetString(PyExc_TypeError, "gameOb.getVectTo(other): KX_GameObject, expected a 3D Vector or KX_GameObject type");
+                       PyErr_SetString(PyExc_TypeError, "Expected a 3D Vector or GameObject type");
                        return NULL;
                }
        }
@@ -2435,12 +2479,12 @@ KX_PYMETHODDEF_DOC(KX_GameObject, rayCastTo,
                KX_GameObject *other;
                PyErr_Clear();
                
-               if (ConvertPythonToGameObject(pyarg, &other, false, "")) /* error will be overwritten */
+               if (ConvertPythonToGameObject(pyarg, &other, false))
                {
                        toPoint = other->NodeGetWorldPosition();
                } else
                {
-                       PyErr_SetString(PyExc_TypeError, "gameOb.rayCastTo(other,dist,prop): KX_GameObject, the first argument to rayCastTo must be a vector or a KX_GameObject");
+                       PyErr_SetString(PyExc_TypeError, "the first argument to rayCastTo must be a vector or a KX_GameObject");
                        return NULL;
                }
        }
@@ -2511,7 +2555,7 @@ KX_PYMETHODDEF_DOC(KX_GameObject, rayCast,
        {
                PyErr_Clear();
                
-               if (ConvertPythonToGameObject(pyto, &other, false, ""))  /* error will be overwritten */
+               if (ConvertPythonToGameObject(pyto, &other, false))
                {
                        toPoint = other->NodeGetWorldPosition();
                } else
@@ -2528,12 +2572,12 @@ KX_PYMETHODDEF_DOC(KX_GameObject, rayCast,
        {
                PyErr_Clear();
                
-               if (ConvertPythonToGameObject(pyfrom, &other, false, "")) /* error will be overwritten */
+               if (ConvertPythonToGameObject(pyfrom, &other, false))
                {
                        fromPoint = other->NodeGetWorldPosition();
                } else
                {
-                       PyErr_SetString(PyExc_TypeError, "gameOb.rayCast(to,from,dist,prop,face,xray,poly): KX_GameObject, the second optional argument to rayCast must be a vector or a KX_GameObject");
+                       PyErr_SetString(PyExc_TypeError, "the second optional argument to rayCast must be a vector or a KX_GameObject");
                        return NULL;
                }
        }
@@ -2641,10 +2685,10 @@ void KX_GameObject::Relink(GEN_Map<GEN_HashedPtr, void*> *map_parameter)
        }
 }
 
-bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py_none_ok, const char *error_prefix)
+bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py_none_ok)
 {
        if (value==NULL) {
-               PyErr_Format(PyExc_TypeError, "%s, python pointer NULL, should never happen", error_prefix);
+               PyErr_SetString(PyExc_TypeError, "Error in ConvertPythonToGameObject, python pointer NULL, should never happen");
                *object = NULL;
                return false;
        }
@@ -2655,7 +2699,7 @@ bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py
                if (py_none_ok) {
                        return true;
                } else {
-                       PyErr_Format(PyExc_TypeError, "%s, expected KX_GameObject or a KX_GameObject name, None is invalid", error_prefix);
+                       PyErr_SetString(PyExc_TypeError, "Expected KX_GameObject or a string for a name of a KX_GameObject, None is invalid");
                        return false;
                }
        }
@@ -2666,7 +2710,7 @@ bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py
                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_SetString(PyExc_ValueError, "Requested name did not match any KX_GameObject");
                        return false;
                }
        }
@@ -2676,7 +2720,7 @@ bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py
                
                /* sets the error */
                if (*object==NULL) {
-                       PyErr_Format(PyExc_RuntimeError, "%s, " BGE_PROXY_ERROR_MSG, error_prefix);
+                       PyErr_SetString(PyExc_RuntimeError, BGE_PROXY_ERROR_MSG);
                        return false;
                }
                
@@ -2686,9 +2730,9 @@ bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py
        *object = NULL;
        
        if (py_none_ok) {
-               PyErr_Format(PyExc_TypeError, "%s, expect a KX_GameObject, a string or None", error_prefix);
+               PyErr_SetString(PyExc_TypeError, "Expect a KX_GameObject, a string or None");
        } else {
-               PyErr_Format(PyExc_TypeError, "%s, expect a KX_GameObject or a string", error_prefix);
+               PyErr_SetString(PyExc_TypeError, "Expect a KX_GameObject or a string");
        }
        
        return false;