Crash Course in Blender Python Part 1: Introduction
Posted on: September 1, 2008Welcome to a Crash course in Blender-Python part1, in which i will demonstrate some of the uses and basic functions of Blender-Python.
Why Use Python when you can just use logic bricks?
There are several resons why python is so much better than just using logic bricks to create games in blender. First off, logic bricks can be VERY messy,and hard to work with when you need to produce a bugfix or simply add new features. The more python you use ( epecially if you coded it all yourself ) the easier it is for you to go back and correct mistakes. Sure,logic bricks can appear easier, and sometimes they can be when your goal is to produce a VERY simple game, but in the long run, it wont be as professional and effiecient as it could be. Second, when you are using logic bricks, you are limited to ONLY what blender has built in. You cant create new classes, define new methods, or anything of the sort when ONLY using logic bricks.
Getting Started
You will need to install all these items:
- http://download.blender.org/release/Blender2.46/blender-2.46-windows.exe
- http://www.python.org/ftp/python/2.5.2/python-2.5.2.msi
Aftter you download these and install them we can get started.
Some Useful Python Resources
There are always resources that are very useful when it comes to blender-python or just any programming language in general.
Built in noob BlenderGE docs
http://www.tutorialsforblender3d.com/GameFunctions/GameLogicPage1.html
Python docs
http://www.python.org/doc/
How to Use the BlenderGE Docs
This is how they are used. when you see a Function listed in the BlenderGE noob docs, it will look basically like this.
So as you see, the method name is “getHitObject()”. This is part of the “radar sensor” class in which can be called as in the example shown above. This data basically tells you :
- The class name in which the method is part of
- The method and any parameters it has.
A parameter is data that the method uses in order to return the data that cooresponds to it. In this case, there are no parameters that you can define when calling the method. Lets look at an example method that does use parameters.
As you can see, the method name is “disableRigidBody”. It is part of the “GameObject” class ( in other words, a mesh is a gameobject ) and any gameobject can use or “call” this method. Here is some example code:
# get a list of the objects in the scene
objList = GameLogic.getCurrentScene().getObjectList()
# select the object named Cube from the list
objCube = objList["OBCube"]
# turn off rigid body
objCube.disableRigidBody()
Notice That there are no parameters that you need to supply the method with in order to call it.
“If” and “else” and “elif” Statements
The if, elif and else statements are probably what i would call the most important functions in ALL programming languages. They are what tells the computer what to do and when to do it.
For example:
Say your mom wanted you to take out the trash, but only if the trashcan was full. Well then she would probably say:
“Only take out the trash if the trashcan is full. Thank you!”
And of course, you would only take out the trash if the trashcan was full. Now, lets put this into programming terms. You, are the computer. Your mom, is the programming ( the .exe, or ect…). Your mom, now that she is the programming, would say something like this:
if trashcan.isFull() == True:
Child.emptyTrashcan()
And you, the computer, would empty the trashcan upon it being full.
Lets do some real code!
So, now that you get the basic principle, lets get started with some coding.
Now, you are ready to setup a basic Movement script in Blender.





October 22nd, 2008 at 9:56 am
> if trashcan.isFull() == True:
Ouch! Even though this article is aimed at absolute beginners, please, don’t teach them to program like clueless idiots. The “== True” here does absolutely nothing useful; pretending it has to be there confuses students and makes them worse programmers.
The condition mom stated was “if the trashcan is full”, not “if it is true that the trashcan is full.”