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.
This commit is contained in:
2009-05-14 07:59:44 +00:00
parent d2cff7307d
commit d257586fe6
8 changed files with 122 additions and 105 deletions

View File

@@ -933,3 +933,57 @@ PyObject* KX_Camera::pyattr_get_OUTSIDE(void *self_v, const KX_PYATTRIBUTE_DEF *
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;
}

View File

@@ -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;

View File

@@ -1819,9 +1819,7 @@ PyObject* KX_GameObject::py_getattro_dict() {
int KX_GameObject::py_setattro(PyObject *attr, PyObject *value) // py_setattro method
{
int ret;
ret= py_setattro__internal(attr, value);
int ret= py_setattro__internal(attr, value);
if (ret==PY_SET_ATTR_SUCCESS) {
/* remove attribute in our own dict to avoid double ups */

View File

@@ -1703,13 +1703,27 @@ PyObject* KX_Scene::pyattr_get_active_camera(void *self_v, const KX_PYATTRIBUTE_
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 @@ PyAttributeDef KX_Scene::Attributes[] = {
{ 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)

View File

@@ -566,11 +566,12 @@ public:
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()); }

View File

@@ -264,7 +264,7 @@ PyMethodDef KX_SceneActuator::Methods[] =
//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 @@ int KX_SceneActuator::pyattr_set_camera(void *self, const struct KX_PYATTRIBUTE_
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 (!ConvertPythonToCamera(value, &camOb, true, "actu.camera = value: KX_SceneActuator"))
return PY_SET_ATTR_FAIL;
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);
if (actuator->m_camera)
actuator->m_camera->UnregisterActuator(actuator);
if(camOb==NULL) {
actuator->m_camera= NULL;
}
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 @@ const char KX_SceneActuator::SetCamera_doc[] =
"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;
}

View File

@@ -108,7 +108,7 @@ class KX_SceneActuator : public SCA_IActuator
/* 5. getScene: */
KX_PYMETHOD_DOC_NOARGS(KX_SceneActuator,GetScene);
/* 6. setCamera: */
KX_PYMETHOD_DOC_VARARGS(KX_SceneActuator,SetCamera);
KX_PYMETHOD_DOC_O(KX_SceneActuator,SetCamera);
/* 7. getCamera: */
KX_PYMETHOD_DOC_NOARGS(KX_SceneActuator,GetCamera);

View File

@@ -235,7 +235,7 @@ class SCA_IController(SCA_ILogicBrick):
"""
Get the controllers state bitmask, this can be used with the GameObject's state to test if the the controller is active.
This for instance will always be true however you could compare with a previous state to see when the state was activated.
GameLogic.getCurrentController().getState() & GameLogic.getCurrentController().getOwner().getState()
GameLogic.getCurrentController().state & GameLogic.getCurrentController().owner.state
@deprecated: Use the L{state} property
@rtype: int
"""
@@ -2126,7 +2126,7 @@ class KX_LightObject(KX_GameObject):
import GameLogic
co = GameLogic.getCurrentController()
light = co.getOwner()
light = co.owner
light.energy = 1.0
light.colour = [1.0, 0.0, 0.0]
@@ -2187,8 +2187,8 @@ class KX_MeshProxy(SCA_IObject):
The correct method of iterating over every L{KX_VertexProxy} in a game object::
import GameLogic
co = GameLogic.getcurrentController()
obj = co.getOwner()
co = GameLogic.getCurrentController()
obj = co.owner
m_i = 0
mesh = obj.getMesh(m_i) # There can be more than one mesh...
@@ -3177,9 +3177,9 @@ class KX_PolygonMaterial:
self.pass_no = 0
return False
obj = GameLogic.getCurrentController().getOwner()
obj = GameLogic.getCurrentController().owner
mesh = obj.getMesh(0)
mesh = obj.meshes[0]
for mat in mesh.materials:
mat.setCustomMaterial(MyMaterial())
@@ -3596,8 +3596,8 @@ class KX_SCA_ReplaceMeshActuator(SCA_IActuator):
)
co = GameLogic.getCurrentController()
obj = co.getOwner()
act = co.getActuator("LOD." + obj.name)
obj = co.owner
act = co.actuators["LOD." + obj.name]
cam = GameLogic.getCurrentScene().active_camera
def Depth(pos, plane):
@@ -3684,7 +3684,7 @@ class KX_Scene(PyObjectPlus):
# Get the depth of an object in the camera view.
import GameLogic
obj = GameLogic.getCurrentController().getOwner()
obj = GameLogic.getCurrentController().owner
cam = GameLogic.getCurrentScene().active_camera
# Depth is negative and decreasing further from the camera
@@ -3692,19 +3692,20 @@ class KX_Scene(PyObjectPlus):
@bug: All attributes are read only at the moment.
@ivar name: The scene's name
@ivar name: The scene's name, (read-only).
@type name: string
@ivar objects: A list of objects in the scene.
@ivar objects: A list of objects in the scene, (read-only).
@type objects: L{CListValue} of L{KX_GameObject}
@ivar objects_inactive: A list of objects on background layers (used for the addObject actuator).
@ivar objects_inactive: A list of objects on background layers (used for the addObject actuator), (read-only).
@type objects_inactive: L{CListValue} of L{KX_GameObject}
@ivar lights: A list of lights in the scene.
@ivar lights: A list of lights in the scene, (read-only).
@type lights: L{CListValue} of L{KX_LightObject}
@ivar cameras: A list of cameras in the scene.
@ivar cameras: A list of cameras in the scene, (read-only).
@type cameras: L{CListValue} of L{KX_Camera}
@ivar active_camera: The current active camera
@ivar active_camera: The current active camera.
@note: this can be set directly from python to avoid using the L{KX_SceneActuator}
@type active_camera: L{KX_Camera}
@ivar suspended: True if the scene is suspended.
@ivar suspended: True if the scene is suspended, (read-only).
@type suspended: boolean
@ivar activity_culling: True if the scene is activity culling
@type activity_culling: boolean
@@ -5328,7 +5329,7 @@ class KX_Camera(KX_GameObject):
Example::
import GameLogic
co = GameLogic.getCurrentController()
cam = co.GetOwner()
cam = co.owner
# A sphere of radius 4.0 located at [x, y, z] = [1.0, 1.0, 1.0]
if (cam.sphereInsideFrustum([1.0, 1.0, 1.0], 4) != cam.OUTSIDE):
@@ -5344,7 +5345,7 @@ class KX_Camera(KX_GameObject):
Example::
import GameLogic
co = GameLogic.getCurrentController()
cam = co.GetOwner()
cam = co.owner
# Box to test...
box = []
@@ -5374,7 +5375,7 @@ class KX_Camera(KX_GameObject):
Example::
import GameLogic
co = GameLogic.getCurrentController()
cam = co.GetOwner()
cam = co.owner
# Test point [0.0, 0.0, 0.0]
if (cam.pointInsideFrustum([0.0, 0.0, 0.0])):
@@ -5451,7 +5452,7 @@ class KX_Camera(KX_GameObject):
[1.0/cam.scaling[0], 1.0/cam.scaling[1], 1.0/cam.scaling[2], 1.0])
co = GameLogic.getCurrentController()
cam = co.getOwner()
cam = co.owner
cam.setProjectionMatrix(Perspective(cam)))
@type matrix: 4x4 matrix.