42
42
#include "KX_PythonInit.h"
43
//python physics binding
44
#include "KX_PyConstraintBinding.h"
46
#include "KX_KetsjiEngine.h"
44
48
#include "SCA_IInputDevice.h"
45
49
#include "SCA_PropertySensor.h"
46
50
#include "SCA_RandomActuator.h"
47
51
#include "KX_ConstraintActuator.h"
48
52
#include "KX_IpoActuator.h"
53
#include "KX_SoundActuator.h"
54
#include "BL_ActionActuator.h"
49
55
#include "RAS_IRasterizer.h"
50
56
#include "RAS_ICanvas.h"
51
57
#include "MT_Vector3.h"
84
93
static PyObject* ErrorObject;
85
94
STR_String gPyGetRandomFloat_doc="getRandomFloat returns a random floating point value in the range [0..1)";
87
static PyObject* gPyGetRandomFloat(PyObject* self,
96
static PyObject* gPyGetRandomFloat(PyObject*,
91
100
return PyFloat_FromDouble(MT_random());
96
void GlobalConvertPythonPylist(PyObject* pylist, MT_Vector3 &pos)
99
if (pylist->ob_type == &CListValue::Type)
101
CListValue* listval = (CListValue*) pylist;
102
unsigned int numitems = listval->GetCount();
105
for (unsigned int index=0;index<numitems;index++)
107
pos[index] = listval->GetValue(index)->GetNumber();
117
// assert the list is long enough...
118
unsigned int numitems = PyList_Size(pylist);
121
for (unsigned int index=0;index<numitems;index++)
123
pos[index] = PyFloat_AsDouble(PyList_GetItem(pylist,index));
134
void GlobalConvertPythonPylist(PyObject* pylist, MT_Vector4 &vec)
137
if (pylist->ob_type == &CListValue::Type)
139
CListValue* listval = (CListValue*) pylist;
140
unsigned int numitems = listval->GetCount();
143
for (unsigned index=0;index<numitems;index++)
145
vec[index] = listval->GetValue(index)->GetNumber();
154
// assert the list is long enough...
155
unsigned int numitems = PyList_Size(pylist);
158
for (unsigned index=0;index<numitems;index++)
160
vec[index] = PyFloat_AsDouble(PyList_GetItem(pylist,index));
172
void GlobalConvertPythonVectorArg(PyObject* args, MT_Vector3 &pos)
175
PyArg_ParseTuple(args,"O",&pylist);
177
GlobalConvertPythonPylist(pylist, pos);
180
void GlobalConvertPythonVectorArg(PyObject* args, MT_Vector4 &vec)
183
PyArg_ParseTuple(args,"O",&pylist);
185
GlobalConvertPythonPylist(pylist, vec);
189
static PyObject* gPySetGravity(PyObject* self,
103
static PyObject* gPySetGravity(PyObject*,
193
107
MT_Vector3 vec = MT_Vector3(0., 0., 0.);
194
GlobalConvertPythonVectorArg(args, vec);
197
gp_KetsjiScene->SetGravity(vec);
108
if (PyVecArgTo(args, vec))
111
gp_KetsjiScene->SetGravity(vec);
203
120
static bool usedsp = false;
205
122
// this gets a pointer to an array filled with floats
206
static PyObject* gPyGetSpectrum(PyObject* self,
123
static PyObject* gPyGetSpectrum(PyObject*,
210
127
SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
263
182
audiodevice->StopUsingDSP();
190
static PyObject* gPySetLogicTicRate(PyObject*,
195
if (PyArg_ParseTuple(args, "f", &ticrate))
197
KX_KetsjiEngine::SetTicRate(ticrate);
204
static PyObject* gPyGetLogicTicRate(PyObject*, PyObject*, PyObject*)
206
return PyFloat_FromDouble(KX_KetsjiEngine::GetTicRate());
209
static PyObject* gPySetPhysicsTicRate(PyObject*,
214
if (PyArg_ParseTuple(args, "f", &ticrate))
217
PHY_GetActiveEnvironment()->setFixedTimeStep(true,ticrate);
224
static PyObject* gPyGetPhysicsTicRate(PyObject*, PyObject*, PyObject*)
226
return PyFloat_FromDouble(PHY_GetActiveEnvironment()->getFixedTimeStep());
229
static STR_String gPyGetCurrentScene_doc =
230
"getCurrentScene()\n"
231
"Gets a reference to the current scene.\n";
232
static PyObject* gPyGetCurrentScene(PyObject* self,
236
Py_INCREF(gp_KetsjiScene);
237
return (PyObject*) gp_KetsjiScene;
271
242
static struct PyMethodDef game_methods[] = {
272
243
{"getCurrentController",
273
244
(PyCFunction) SCA_PythonController::sPyGetCurrentController,
274
245
METH_VARARGS, SCA_PythonController::sPyGetCurrentController__doc__},
246
{"getCurrentScene", (PyCFunction) gPyGetCurrentScene,
247
METH_VARARGS, gPyGetCurrentScene_doc.Ptr()},
275
248
{"addActiveActuator",(PyCFunction) SCA_PythonController::sPyAddActiveActuator,
276
249
METH_VARARGS, SCA_PythonController::sPyAddActiveActuator__doc__},
277
250
{"getRandomFloat",(PyCFunction) gPyGetRandomFloat,
279
252
{"setGravity",(PyCFunction) gPySetGravity, METH_VARARGS,"set Gravitation"},
280
253
{"getSpectrum",(PyCFunction) gPyGetSpectrum, METH_VARARGS,"get audio spectrum"},
281
254
{"stopDSP",(PyCFunction) gPyStopDSP, METH_VARARGS,"stop using the audio dsp (for performance reasons)"},
255
{"getLogicTicRate", (PyCFunction) gPyGetLogicTicRate, METH_VARARGS, "Gets the logic tic rate"},
256
{"setLogicTicRate", (PyCFunction) gPySetLogicTicRate, METH_VARARGS, "Sets the logic tic rate"},
257
{"getPhysicsTicRate", (PyCFunction) gPyGetPhysicsTicRate, METH_VARARGS, "Gets the physics tic rate"},
258
{"setPhysicsTicRate", (PyCFunction) gPySetPhysicsTicRate, METH_VARARGS, "Sets the physics tic rate"},
282
259
{NULL, (PyCFunction) NULL, 0, NULL }
286
static PyObject* gPyGetWindowHeight(PyObject* self,
263
static PyObject* gPyGetWindowHeight(PyObject*,
290
267
int height = (gp_Canvas ? gp_Canvas->GetHeight() : 0);
372
static PyObject* gPySetBackgroundColor(PyObject* self,
347
static PyObject* gPySetEyeSeparation(PyObject*,
352
if (PyArg_ParseTuple(args, "f", &sep))
355
gp_Rasterizer->SetEyeSeparation(sep);
363
static PyObject* gPyGetEyeSeparation(PyObject*, PyObject*, PyObject*)
366
return PyFloat_FromDouble(gp_Rasterizer->GetEyeSeparation());
371
static PyObject* gPySetFocalLength(PyObject*,
376
if (PyArg_ParseTuple(args, "f", &focus))
379
gp_Rasterizer->SetFocalLength(focus);
386
static PyObject* gPyGetFocalLength(PyObject*, PyObject*, PyObject*)
389
return PyFloat_FromDouble(gp_Rasterizer->GetFocalLength());
393
static PyObject* gPySetBackgroundColor(PyObject*,
377
398
MT_Vector4 vec = MT_Vector4(0., 0., 0.3, 0.);
378
GlobalConvertPythonVectorArg(args, vec);
399
if (PyVecArgTo(args, vec))
382
gp_Rasterizer->SetBackColor(vec[0], vec[1], vec[2], vec[3]);
403
gp_Rasterizer->SetBackColor(vec[0], vec[1], vec[2], vec[3]);
389
static PyObject* gPySetMistColor(PyObject* self,
413
static PyObject* gPySetMistColor(PyObject*,
394
418
MT_Vector3 vec = MT_Vector3(0., 0., 0.);
395
GlobalConvertPythonVectorArg(args, vec);
419
if (PyVecArgTo(args, vec))
399
gp_Rasterizer->SetFogColor(vec[0], vec[1], vec[2]);
423
gp_Rasterizer->SetFogColor(vec[0], vec[1], vec[2]);
406
static PyObject* gPySetMistStart(PyObject* self,
433
static PyObject* gPySetMistStart(PyObject*,
481
508
{"setMistStart",(PyCFunction)gPySetMistStart,METH_VARARGS,"set Mist Start(rgb)"},
482
509
{"setMistEnd",(PyCFunction)gPySetMistEnd,METH_VARARGS,"set Mist End(rgb)"},
511
{"setEyeSeparation", (PyCFunction) gPySetEyeSeparation, METH_VARARGS, "set the eye separation for stereo mode"},
512
{"getEyeSeparation", (PyCFunction) gPyGetEyeSeparation, METH_VARARGS, "get the eye separation for stereo mode"},
513
{"setFocalLength", (PyCFunction) gPySetFocalLength, METH_VARARGS, "set the focal length for stereo mode"},
514
{"getFocalLength", (PyCFunction) gPyGetFocalLength, METH_VARARGS, "get the focal length for stereo mode"},
484
515
{ NULL, (PyCFunction) NULL, 0, NULL }
560
591
KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_NORMAL, SCA_RandomActuator::KX_RANDOMACT_FLOAT_NORMAL);
561
592
KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL, SCA_RandomActuator::KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL);
594
/* 6. Sound actuator */
595
KX_MACRO_addTypesToDict(d, KX_SOUNDACT_PLAYSTOP, KX_SoundActuator::KX_SOUNDACT_PLAYSTOP);
596
KX_MACRO_addTypesToDict(d, KX_SOUNDACT_PLAYEND, KX_SoundActuator::KX_SOUNDACT_PLAYEND);
597
KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPSTOP, KX_SoundActuator::KX_SOUNDACT_LOOPSTOP);
598
KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPEND, KX_SoundActuator::KX_SOUNDACT_LOOPEND);
599
KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPBIDIRECTIONAL, KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL);
600
KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP, KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP);
602
/* 7. Action actuator */
603
KX_MACRO_addTypesToDict(d, KX_ACTIONACT_PLAY, BL_ActionActuator::KX_ACT_ACTION_PLAY);
604
KX_MACRO_addTypesToDict(d, KX_ACTIONACT_FLIPPER, BL_ActionActuator::KX_ACT_ACTION_FLIPPER);
605
KX_MACRO_addTypesToDict(d, KX_ACTIONACT_LOOPSTOP, BL_ActionActuator::KX_ACT_ACTION_LOOPSTOP);
606
KX_MACRO_addTypesToDict(d, KX_ACTIONACT_LOOPEND, BL_ActionActuator::KX_ACT_ACTION_LOOPEND);
607
KX_MACRO_addTypesToDict(d, KX_ACTIONACT_PROPERTY, BL_ActionActuator::KX_ACT_ACTION_PROPERTY);
563
609
// Check for errors
564
610
if (PyErr_Occurred())
716
* Python is not initialised.
718
PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level)
720
STR_String pname = progname;
721
Py_SetProgramName(pname.Ptr());
726
//importBlenderModules()
730
PyObject* moduleobj = PyImport_AddModule("__main__");
731
return PyModule_GetDict(moduleobj);
734
void exitGamePlayerPythonScripting()
740
* Python is already initialized.
671
742
PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level)
673
744
STR_String pname = progname;
674
745
Py_SetProgramName(pname.Ptr());
677
#ifndef USE_BLENDER_PYTHON
682
749
setSandbox(level);
684
751
PyObject* moduleobj = PyImport_AddModule("__main__");