Revision: 20193 http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20193 Author: campbellbarton Date: 2009-05-14 09:59:44 +0200 (Thu, 14 May 2009)
Log Message: ----------- BGE Py API scene.active_camera can now be set so you can more easily set the current camera from python scripts without using an actuator. ConvertPythonToCamera utility function to get a camera from a python string or KX_Camera type. Modified Paths: -------------- trunk/blender/source/gameengine/Ketsji/KX_Camera.cpp trunk/blender/source/gameengine/Ketsji/KX_Camera.h trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp trunk/blender/source/gameengine/Ketsji/KX_Scene.h trunk/blender/source/gameengine/Ketsji/KX_SceneActuator.cpp trunk/blender/source/gameengine/Ketsji/KX_SceneActuator.h trunk/blender/source/gameengine/PyDoc/GameTypes.py Modified: trunk/blender/source/gameengine/Ketsji/KX_Camera.cpp =================================================================== --- trunk/blender/source/gameengine/Ketsji/KX_Camera.cpp 2009-05-14 02:21:50 UTC (rev 20192) +++ trunk/blender/source/gameengine/Ketsji/KX_Camera.cpp 2009-05-14 07:59:44 UTC (rev 20193) @@ -933,3 +933,57 @@ PyObject* KX_Camera::pyattr_get_INTERSECT(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) { return PyInt_FromLong(INTERSECT); } + +bool ConvertPythonToCamera(PyObject * value, KX_Camera **object, bool py_none_ok, const char *error_prefix) +{ + if (value==NULL) { + PyErr_Format(PyExc_TypeError, "%s, python pointer NULL, should never happen", error_prefix); + *object = NULL; + return false; + } + + if (value==Py_None) { + *object = NULL; + + if (py_none_ok) { + return true; + } else { + PyErr_Format(PyExc_TypeError, "%s, expected KX_Camera or a KX_Camera name, None is invalid", error_prefix); + return false; + } + } + + if (PyString_Check(value)) { + STR_String value_str = PyString_AsString(value); + *object = KX_GetActiveScene()->FindCamera(value_str); + + if (*object) { + return true; + } else { + PyErr_Format(PyExc_ValueError, "%s, requested name \"%s\" did not match any KX_Camera in this scene", error_prefix, PyString_AsString(value)); + return false; + } + } + + if (PyObject_TypeCheck(value, &KX_Camera::Type)) { + *object = static_cast<KX_Camera*>BGE_PROXY_REF(value); + + /* sets the error */ + if (*object==NULL) { + PyErr_Format(PyExc_SystemError, "%s, " BGE_PROXY_ERROR_MSG, error_prefix); + return false; + } + + return true; + } + + *object = NULL; + + if (py_none_ok) { + PyErr_Format(PyExc_TypeError, "%s, expect a KX_Camera, a string or None", error_prefix); + } else { + PyErr_Format(PyExc_TypeError, "%s, expect a KX_Camera or a string", error_prefix); + } + + return false; +} \ No newline at end of file Modified: trunk/blender/source/gameengine/Ketsji/KX_Camera.h =================================================================== --- trunk/blender/source/gameengine/Ketsji/KX_Camera.h 2009-05-14 02:21:50 UTC (rev 20192) +++ trunk/blender/source/gameengine/Ketsji/KX_Camera.h 2009-05-14 07:59:44 UTC (rev 20193) @@ -41,6 +41,9 @@ #include "IntValue.h" #include "RAS_CameraData.h" +/* utility conversion function */ +bool ConvertPythonToCamera(PyObject * value, KX_Camera **object, bool py_none_ok, const char *error_prefix); + class KX_Camera : public KX_GameObject { Py_Header; Modified: trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp =================================================================== --- trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp 2009-05-14 02:21:50 UTC (rev 20192) +++ trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp 2009-05-14 07:59:44 UTC (rev 20193) @@ -1819,10 +1819,8 @@ int KX_GameObject::py_setattro(PyObject *attr, PyObject *value) // py_setattro method { - int ret; + int ret= py_setattro__internal(attr, value); - ret= py_setattro__internal(attr, value); - if (ret==PY_SET_ATTR_SUCCESS) { /* remove attribute in our own dict to avoid double ups */ /* NOTE: Annoying that we also do this for setting builtin attributes like mass and visibility :/ */ Modified: trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp =================================================================== --- trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp 2009-05-14 02:21:50 UTC (rev 20192) +++ trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp 2009-05-14 07:59:44 UTC (rev 20193) @@ -1703,13 +1703,27 @@ return self->GetActiveCamera()->GetProxy(); } + +int KX_Scene::pyattr_set_active_camera(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value) +{ + KX_Scene* self= static_cast<KX_Scene*>(self_v); + KX_Camera *camOb; + + if (!ConvertPythonToCamera(value, &camOb, false, "scene.active_camera = value: KX_Scene")) + return PY_SET_ATTR_FAIL; + + self->SetActiveCamera(camOb); + return PY_SET_ATTR_SUCCESS; +} + + PyAttributeDef KX_Scene::Attributes[] = { KX_PYATTRIBUTE_RO_FUNCTION("name", KX_Scene, pyattr_get_name), KX_PYATTRIBUTE_RO_FUNCTION("objects", KX_Scene, pyattr_get_objects), KX_PYATTRIBUTE_RO_FUNCTION("objects_inactive", KX_Scene, pyattr_get_objects_inactive), KX_PYATTRIBUTE_RO_FUNCTION("lights", KX_Scene, pyattr_get_lights), KX_PYATTRIBUTE_RO_FUNCTION("cameras", KX_Scene, pyattr_get_cameras), KX_PYATTRIBUTE_RO_FUNCTION("lights", KX_Scene, pyattr_get_lights), - KX_PYATTRIBUTE_RO_FUNCTION("active_camera", KX_Scene, pyattr_get_active_camera), + KX_PYATTRIBUTE_RW_FUNCTION("active_camera", KX_Scene, pyattr_get_active_camera, pyattr_set_active_camera), KX_PYATTRIBUTE_BOOL_RO("suspended", KX_Scene, m_suspend), KX_PYATTRIBUTE_BOOL_RO("activity_culling", KX_Scene, m_activity_culling), KX_PYATTRIBUTE_FLOAT_RW("activity_culling_radius", 0.5f, FLT_MAX, KX_Scene, m_activity_box_radius), @@ -1717,15 +1731,14 @@ { NULL } //Sentinel }; - PyObject* KX_Scene::py_getattro__internal(PyObject *attr) { py_getattro_up(PyObjectPlus); } -int KX_Scene::py_setattro__internal(PyObject *attr, PyObject *pyvalue) +int KX_Scene::py_setattro__internal(PyObject *attr, PyObject *value) { - return PyObjectPlus::py_setattro(attr, pyvalue); + py_setattro_up(PyObjectPlus); } PyObject* KX_Scene::py_getattro(PyObject *attr) Modified: trunk/blender/source/gameengine/Ketsji/KX_Scene.h =================================================================== --- trunk/blender/source/gameengine/Ketsji/KX_Scene.h 2009-05-14 02:21:50 UTC (rev 20192) +++ trunk/blender/source/gameengine/Ketsji/KX_Scene.h 2009-05-14 07:59:44 UTC (rev 20193) @@ -566,11 +566,12 @@ static PyObject* pyattr_get_lights(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef); static PyObject* pyattr_get_cameras(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef); static PyObject* pyattr_get_active_camera(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef); + static int pyattr_set_active_camera(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value); virtual PyObject* py_getattro(PyObject *attr); /* name, active_camera, gravity, suspended, viewport, framing, activity_culling, activity_culling_radius */ virtual PyObject* py_getattro_dict(); - virtual int py_setattro(PyObject *attr, PyObject *pyvalue); + virtual int py_setattro(PyObject *attr, PyObject *value); virtual int py_delattro(PyObject *attr); virtual PyObject* py_repr(void) { return PyString_FromString(GetName().ReadPtr()); } Modified: trunk/blender/source/gameengine/Ketsji/KX_SceneActuator.cpp =================================================================== --- trunk/blender/source/gameengine/Ketsji/KX_SceneActuator.cpp 2009-05-14 02:21:50 UTC (rev 20192) +++ trunk/blender/source/gameengine/Ketsji/KX_SceneActuator.cpp 2009-05-14 07:59:44 UTC (rev 20193) @@ -264,7 +264,7 @@ //Deprecated functions ------> {"setUseRestart", (PyCFunction) KX_SceneActuator::sPySetUseRestart, METH_VARARGS, (PY_METHODCHAR)SetUseRestart_doc}, {"setScene", (PyCFunction) KX_SceneActuator::sPySetScene, METH_VARARGS, (PY_METHODCHAR)SetScene_doc}, - {"setCamera", (PyCFunction) KX_SceneActuator::sPySetCamera, METH_VARARGS, (PY_METHODCHAR)SetCamera_doc}, + {"setCamera", (PyCFunction) KX_SceneActuator::sPySetCamera, METH_O, (PY_METHODCHAR)SetCamera_doc}, {"getUseRestart", (PyCFunction) KX_SceneActuator::sPyGetUseRestart, METH_NOARGS, (PY_METHODCHAR)GetUseRestart_doc}, {"getScene", (PyCFunction) KX_SceneActuator::sPyGetScene, METH_NOARGS, (PY_METHODCHAR)GetScene_doc}, {"getCamera", (PyCFunction) KX_SceneActuator::sPyGetCamera, METH_NOARGS, (PY_METHODCHAR)GetCamera_doc}, @@ -308,51 +308,21 @@ KX_SceneActuator* actuator = static_cast<KX_SceneActuator*>(self); KX_Camera *camOb; - if(value==Py_None) - { - if (actuator->m_camera) - actuator->m_camera->UnregisterActuator(actuator); - + if (!ConvertPythonToCamera(value, &camOb, true, "actu.camera = value: KX_SceneActuator")) + return PY_SET_ATTR_FAIL; + + if (actuator->m_camera) + actuator->m_camera->UnregisterActuator(actuator); + + if(camOb==NULL) { actuator->m_camera= NULL; - return 0; } - - if (PyObject_TypeCheck(value, &KX_Camera::Type)) - { - KX_Camera *camOb= static_cast<KX_Camera*>BGE_PROXY_REF(value); - - if(camOb==NULL) - { - PyErr_SetString(PyExc_SystemError, BGE_PROXY_ERROR_MSG); - return 1; - } - - if (actuator->m_camera) - actuator->m_camera->UnregisterActuator(actuator); - + else { actuator->m_camera = camOb; actuator->m_camera->RegisterActuator(actuator); - return 0; } - - if (PyString_Check(value)) - { - char *camName = PyString_AsString(value); - - camOb = actuator->FindCamera(camName); - if (camOb) - { - if (actuator->m_camera) - actuator->m_camera->UnregisterActuator(actuator); - actuator->m_camera = camOb; - actuator->m_camera->RegisterActuator(actuator); - return 0; - } - PyErr_SetString(PyExc_TypeError, "not a valid camera name"); - return 1; - } - PyErr_SetString(PyExc_TypeError, "expected a string or a camera object reference"); - return 1; + + return PY_SET_ATTR_SUCCESS; } @@ -431,47 +401,24 @@ "setCamera(camera)\n" "\t- camera: string\n" "\tSet the camera to switch to.\n" ; -PyObject* KX_SceneActuator::PySetCamera(PyObject* args) +PyObject* KX_SceneActuator::PySetCamera(PyObject* value) { ShowDeprecationWarning("setCamera()", "the camera property"); - PyObject *cam; - if (PyArg_ParseTuple(args, "O!:setCamera", &KX_Camera::Type, &cam)) - { - KX_Camera *new_camera; - - new_camera = static_cast<KX_Camera*>BGE_PROXY_REF(cam); - if(new_camera==NULL) - { - PyErr_SetString(PyExc_SystemError, BGE_PROXY_ERROR_MSG); - return NULL; - } - - if (m_camera) - m_camera->UnregisterActuator(this); - - m_camera= new_camera; - - m_camera->RegisterActuator(this); - Py_RETURN_NONE; - } - PyErr_Clear(); - - /* one argument: a scene, ignore the rest */ - char *camName; - if(!PyArg_ParseTuple(args, "s:setCamera", &camName)) - { + KX_Camera *camOb; + + if (!ConvertPythonToCamera(value, &camOb, true, "actu.setCamera(value): KX_SceneActuator")) return NULL; + + if (m_camera) + m_camera->UnregisterActuator(this); + + if(camOb==NULL) { + m_camera= NULL; } - - KX_Camera *camOb = FindCamera(camName); - if (camOb) - { - if (m_camera) - m_camera->UnregisterActuator(this); + else { m_camera = camOb; m_camera->RegisterActuator(this); } - Py_RETURN_NONE; } @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org http://lists.blender.org/mailman/listinfo/bf-blender-cvs