28.2. Python for games

With Python integrated into the game engine you can influence LogicBricks, change their parameters and react to events triggered by the LogicBricks.

Besides that you can influence the GameObject that carries the Python Controller directly. This means moving it, applying forces or getting information from this object.


In addition to the Python in the game engine, Blender includes Python for modeling and animation tasks.

28.2.1. Basic gamePython

The first step for using gamePython is to add at least a Sensor and a Python Controller to an object. Then add a new text file in the TextWindow. Fill in the name of that text file into the "Script:" field of the Python Controller. You should now have a game logic setup like in Figure 28-2.

Figure 28-2. LogicBricks for a first gamePython script.

Now enter the following script into the TextWindow (you don't need to type the lines starting with "#", these are comments).

Figure 28-3. First Script

  1         # first gamePython script
  2          # gets the position of the owning object
  3          # and prints it on the console
  5          import GameLogic
  7          controller = GameLogic.getCurrentController()
  8          owner = controller.getOwner()
 10          print owner.getPosition()

The "print" command and errors from the Python interpreter will appear on the console from which you started Blender from, or in the DOS window, when running Blender under Windows. So it is helpful to size the Blender window in such a way that you can see the console window while programming Python.

This basic script only prints the position of the object that owns the Python Controller. Move your object and then restart the game engine with the PKEY to see the results changing.

Now we explain the function of the script line by line. Line five is maybe the most important line here. We import the "GameLogic" module which is the basis for all game Python in Blender.

In line seven we get the Controller, which executes the script and assigns it to the variable "controller".

In line eight we use the controller we got in line seven to get the owner, the GameObject carrying the LogicBrick. You can see we use the method "getOwner()" to get the owner of our controller.

We now have the owner and we can use its methods to do things with it. Here in line 10 we use the "getPosition()" method to print the position of the gameObject as a matrix of the X, Y and Z values.

You may now wonder what other methods the PythonObjects have. Of course this is part of this documentation, but Python is "self" documenting, so we have other ways to get that information.

Add the following line to the end of the script from Figure 28-3:

  1 print dir(owner)

Start the game engine again, stop it and look at the console window. You will see the following output:

    [0.0, 0.0, 0.0] 
    ['applyImpulse', 'disableRigidBody','enableRigidBody', 'getLinearVelocity', 'getMass',
    'getOrientation', 'getPosition', 'getReactionForce','getVelocity', 'restoreDynamics', 'setOrientation',
    'setPosition', 'setVisible', 'suspendDynamics']

The first line shows the position of the object, the next lines show the methods, that the "owner" provides. For example you see a 'getMass' method, which will return the mass of a dynamic object. With the knowledge of the "dir()" function you can ask Python objects for information, without consulting external documentation.