Blender for Robotics | Part 4 | Programming in the GameEngine
TOC
- Part 1 | Introduction and Modelling
- Part 2 | Parenting and Rigging
- Part 3 | Interfacing with the Game Engine
- Part 4 | Programming in the Game Engine
- Part 5 | Game Engine and The Kinect
Well, you possibly just went through Tutorial Part 3, where you learned how to animate and trigger a robot model in the Blender Game Engine. I also talked about programming there...To be fair, no programming so far — BUT — we will start right now.
It's extremly important, at least for unexperienced users, to follow the installation instructions as presented in the 3rd part of this tutorial series. In this step, you will learn how to use the Python BGE API [1] to interact/interface with the Game Engine in a "pythonic" way. Additionally, I will introduce to the concept of a middleware [2] in the next part.
First Script in the BGE
How do you execute Python scripts in the BGE? Well, this is quite easy, and if you finished the third part, you might have an idea already...
A few things upfront: Blender exposes it's functionality via different APIs. First of all, there are the "Application Modules" [3] which give you access to the "construction" part of Blender. With the "Application Modules" [3] you can create or edit meshes programmatically, access objects and edit them, and so on. Then, there are the "Standalone Modules" [3] which provide geometry and math functions, font rendering, and audio access. Last, but not least, there are the "Game Engine Modules" [3] which let you access the Game Engine objects and features. We will deal with the latter modules now! Open a console and fire up blender with first file provided with this tutorial.
/opt/blendertutorial/bin/blender PATH/TO/FILE/BFR_tutorial_warp1337_GE_4_scripting.blend
As usual you can watch the video first about what will be described in detail below.
Now, look at the bottom left corner and select the "Text Editor". This is where all the magic happens. You will actually write code in the text editor, save it, and execute it in the BGE. If you just opened the Text Editor, select "New".
Now, name it BGE-Script.py and select the three little buttons next to the naming space, which will give you line wrapping, line numbers, and code highlighting.
In the text editor add the following lines. You can also copy and paste them from this file. Be advised, Python features no line terminal symbols, so you need to work with indents.
### Start of Imports ###
# STD Imports
import sys
import time
import os
import random
import select
from math import *
import mathutils
import bge
### End of Imports ###
# Get the whole bge scene
scene = bge.logic.getCurrentScene()
# Helper vars for convenience
source = scene.objects
# Get the whole Armature
main_arm = source.get('Armature')
# Print some Info about the Armature
print(main_arm.channels)
What does all this do? First of all, standard Python libraries, named modules, as well as Blender specific modules, are imported see: sys and time, but also bge (Blender Game Engine).
# Get the whole bge scene
scene = bge.logic.getCurrentScene()
# Helper vars for convenience
source = scene.objects
This little snippet loads the whole BGE scene, based on that ALL objects in the scene are saved in source field.
# Get the whole Armature
main_arm = source.get('Armature')
# Print some Info about the Armature
print(main_arm.channels)
The last piece of code grabs the "Armature" object, remember the one we created earlier, and prints all the information about its channels [5]. The channel object holds information about all bones within an Armature object. We will come to that later on. You can find detailed descriptions of these API calls here [4]. All this should now look like this:
Okay, now...switch to the Logic Editor again, select the Armature and add another Sensor (Always Sensor), name it Python. Add a Controller, and select "Python". You need to click on the "Or" expression — a menu will come up showing a Python option. As soon as you select the Python option, you can select the recently written BGE-Script.py. Do it!. That's what it is supposed to look like:
If you start the Game Engine (press "p"), the console should show you lots of output. The script is executed every rendered frame, which is 60Hz by default. All the steps described above are, as usual, compiled in this blend file. You just need to open it and press "p".
Finally here's an example how to access the robots behavior in the BGE. At first open this blend file. You will see three additional lines.
# Print angles in radians
print("Right Upper Arm ",main_arm.channels['right_upper_arm'].joint_rotation)
print("Right Lower Arm ",main_arm.channels['right_lower_arm'].joint_rotation)
print("Right Hand ",main_arm.channels['right_hand'].joint_rotation)
These lines print the current joint states of robots the left arm IK chain [6] in radians. You may use math.degrees(x) to convert them to degree. Be advised, I "renamed" the bones in the first place, that way I can access them via their string identifier in the code above.
You can "name" bones in "Pose Mode" only. Select a bone and go to the "Bone Menu" (highlighed blue box in the image above) — type in a suitable name. Now, with the lastest blend file up to this point press "p" and move the IK chain via your cursor keys. You should see a difference in the output vectors in your console. Great!
In the last blend file you can see how to move an Armature programmatically by copying joint rotations. Just fire up the Game Engine and move the arm as ususal.
Please remember to always open the files via console, otherwise you won't see the output ;) Just saying...
Hope this helps!
License
This tutorial is partially based on CC content, therefore I decided to publish my content under CC too.

[1] http://www.blender.org/documentation/blender_python_api_2_57_release/bge...
[2] http://en.wikipedia.org/wiki/Middleware
[3] http://www.blender.org/documentation/blender_python_api_2_57_release/con...
[4] http://www.blender.org/documentation/blender_python_api_2_57_release/bge...
[5] http://www.blender.org/documentation/blender_python_api_2_57_release/bge...
[6] http://www.blender.org/documentation/blender_python_api_2_57_release/bge... (see joint_rotation)
| Attachment | Size |
|---|---|
| BGE-Script.zip | 407 bytes |
| BFR_tutorial_warp1337_GE_4_scripting.zip | 95.68 KB |
| BFR_tutorial_warp1337_GE_4_scripting2.zip | 97.17 KB |
| BFR_tutorial_warp1337_GE_4_scripting3.zip | 97.72 KB |
| BFR_tutorial_warp1337_GE_4_scripting4.zip | 97.74 KB |
- fl0's blog
- 1690 reads
Recent blog posts
- How to use CTest with Jenkins xUnit or JUnit Plugin
- MacBook Pro Retina NVIDIA Problems | EVO DMA push buffer
- PatrolBot in MORSE-1.0 [to be merged]
- Adding additional SSL Certificate to default ca bundle | SSL Certificate Chain Verification | ca-bundle.crt
- AR.Drone 2.0 Install SDK and Fly in 2 Minutes | Ubuntu Linux 12.10
- Simultaneous NAO (Choregraphe) and Blender Motion Control via XBox Kinect
- Blender for Robotics | Part 5 | Game Engine and the Kinect
- Blender for Robotics | Part 4 | Programming in the GameEngine
- Blender for Robotics | Part 3 | Interfacing with the GameEngine
- Finally I got my hands on a PR2 | MORSE ROS
What is Flattr ? Flattr is the worlds first social micro-payment system The idea had already been initiated in 2007, but the first release was in 2010 due to typical geeky laziness. Flattr was founded to help people share money, not just content. https://flattr.com/about
























Comments
Post new comment