Using custom python script for mathematical calculations

Hi all! I’ve been working on a physics project at home for a couple of days an I’ve come up with some formulas which I would like to test using Blender game engine and Bullet physics. For that purpose I have set up a simple interactive scene in Blender 2.53 beta using standard sensors controllers and actuators. The problem is that I need to have one objects rotation calculated and updated every frame through a rather complex mathematical formula, and I don’t know how to achieve this.

In short:

  • How do I get positions from various objects in the scene?
  • How do I set an objects rotation?
  • How do I use mathematical functions like arcsine, square root and such?
  • How do I “connect” the finished python script with a standard sensor?

Thanks in advance!

I hashed this together, haven’t tested:

import math
from math import *
number = 12
x = sqrt(number)

and you use logic bricks to connect a sensor and actuator to a python controller
then in python use


import bge
controller = bge.logic.getCurrentController()
#or to save writing bge.logic use "from bge import logic as logic", then use 
#sensor1 = logic.sensors["sensor1"].positive
##########

sensor1 = bge.logic.sensors["sensor1"].positive
actuator1 = bge.logic.sensors["actuator1"]

if sensor1:
   controller.activate(actuator1)

it may be glitchy (haven’t tested) but have a go

Firstly to connect a python script simple add a controller, set its type to python than its sub-type to script. Select the text file you wish to use (script only uses internal text files, module acts more as an import operation and can use external modules) than connect sensors to it. The script will be run on a single positive pulse from any sensor and will only send pulses to actuators if explicitly stated in the script (see agoose77’s post).

The math library provides access to many math functions such as:
math.degrees()
math.radians()
math.pi
math.asin()
math.acos()
math.atan()
which may be of use to you. Note that all trigonometric functions will assume radian parameters and return radian values.

Position and orientation data can be accessed by using the .worldPosition and .worldOrientation properties of a game object within a script such as:

import bge # top-level GE module

cont = bge.logic.getCurrentController() # the python controller logic brick

own = cont.owner # the object that "owns" the contorller

# getting the data
pos = own.worldPosition
ori = own.worldOrientation

# setting the data
own.worldPosition = [0,0,0]
own.worldOrientation = [[0,0,0], [0,0,0], [0,0,0]]

#alternatively, position data for each axis can be accessed
own.worldPosition.z = 0
own.worldPosition.x += 5

worldPosition returns a mathtuils.Vector object (3D) and worldOrientation returns a mathutils.Matrix object (3x3). These classes make it relatively easy to manipulate the position and rotation. For example, the vector class support vector addition and subtraction:

import bge
from mathutils import Vector

cont = bge.logic.getCurrentController()
pos = own.worldPosition

relative_pos = Vector([10,10,10]) - pos

The matrix class supports conversion from matrices to Euler angle lists as well as aligning vectors with a matrix:

import math

import bge
from mathutils import Vector

cont = bge.getCurrentController()

pos = own.worldPosition
ori = own.worldOrientation

# Move an object along a local axis
pos = pos + Vector([0,5,0]) * ori # move the object 5 units along its local +y axis

# Set an objects rotation:
eul = ori.to_euler() # euler angles are 'basic' angles, essentially easier to edit
eul.z = math.pi/2

own.worldOrientation = eul.to_matrix() # convert back to matrix and set

# Rotate an object
own.applyRotation([0,0,math.pi/2]) # Note: am unsure if radians or degrees are assumed

In all these example the “own” variable is a bge.types.KX_GameObject. For more information on the bge API visit the docs:
http://www.blender.org/documentation/250PythonDoc/

Andrew-101, that is a littler gem. I have spent days googling to try and find a usable summary of those functions. I have both copied and book marked it

Thank you very much! I will make sure to read python documentation well in the future. Happy blending!