diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-04-19 16:46:39 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-04-19 16:46:39 +0400 |
commit | 8d2cb5bea44f4245dd17f2d82cbd0251d8090fd5 (patch) | |
tree | b28e45f1edaf083c15d2176079836a4497685e57 /source/gameengine/Ketsji/KX_SceneActuator.cpp | |
parent | 92cea7c1b1540d11ed9729bacfabd23ccb7a79c7 (diff) |
BGE Python API
This changes how the BGE classes and Python work together, which hasnt changed since blender went opensource.
The main difference is PyObjectPlus - the base class for most game engine classes, no longer inherit from PyObject, and cannot be cast to a PyObject.
This has the advantage that the BGE does not have to keep 2 reference counts valid for C++ and Python.
Previously C++ classes would never be freed while python held a reference, however this reference could be problematic eg: a GameObject that isnt in a scene anymore should not be used by python, doing so could even crash blender in some cases.
Instead PyObjectPlus has a member "PyObject *m_proxy" which is lazily initialized when python needs it. m_proxy reference counts are managed by python, though it should never be freed while the C++ class exists since it holds a reference to avoid making and freeing it all the time.
When the C++ class is free'd it sets the m_proxy reference to NULL, If python accesses this variable it will raise a RuntimeError, (check the isValid attribute to see if its valid without raising an error).
- This replaces the m_zombie bool and IsZombie() tests added recently.
In python return values that used to be..
return value->AddRef();
Are now
return value->GetProxy();
or...
return value->NewProxy(true); // true means python owns this C++ value which will be deleted when the PyObject is freed
Diffstat (limited to 'source/gameengine/Ketsji/KX_SceneActuator.cpp')
-rw-r--r-- | source/gameengine/Ketsji/KX_SceneActuator.cpp | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/source/gameengine/Ketsji/KX_SceneActuator.cpp b/source/gameengine/Ketsji/KX_SceneActuator.cpp index 2d3022a68f7..b52cc81f68b 100644 --- a/source/gameengine/Ketsji/KX_SceneActuator.cpp +++ b/source/gameengine/Ketsji/KX_SceneActuator.cpp @@ -291,23 +291,39 @@ PyObject* KX_SceneActuator::pyattr_get_camera(void *self, const struct KX_PYATTR KX_SceneActuator* actuator = static_cast<KX_SceneActuator*>(self); if (!actuator->m_camera) Py_RETURN_NONE; - actuator->m_camera->AddRef(); - return actuator->m_camera; + + return actuator->m_camera->GetProxy(); } int KX_SceneActuator::pyattr_set_camera(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value) { KX_SceneActuator* actuator = static_cast<KX_SceneActuator*>(self); KX_Camera *camOb; - + + if(value==Py_None) + { + if (actuator->m_camera) + actuator->m_camera->UnregisterActuator(actuator); + + actuator->m_camera= NULL; + return 0; + } + if (PyObject_TypeCheck(value, &KX_Camera::Type)) { - camOb = static_cast<KX_Camera*>(value); + KX_Camera *camOb= static_cast<KX_Camera*>BGE_PROXY_REF(value); + + if(camOb==NULL) + { + PyErr_SetString(PyExc_RuntimeError, BGE_PROXY_ERROR_MSG); + return 1; + } + if (actuator->m_camera) actuator->m_camera->UnregisterActuator(actuator); + actuator->m_camera = camOb; - if (actuator->m_camera) - actuator->m_camera->RegisterActuator(actuator); + actuator->m_camera->RegisterActuator(actuator); return 0; } @@ -423,11 +439,21 @@ PyObject* KX_SceneActuator::PySetCamera(PyObject* self, 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_RuntimeError, BGE_PROXY_ERROR_MSG); + return NULL; + } + if (m_camera) m_camera->UnregisterActuator(this); - m_camera = (KX_Camera*) cam; - if (m_camera) - m_camera->RegisterActuator(this); + + m_camera= new_camera; + + m_camera->RegisterActuator(this); Py_RETURN_NONE; } PyErr_Clear(); |