Need Help Writing Python Script!!!

Okay guys, my friend and i are in the Navy and we’ve got a test to do for our class. We don’t have a decent study guide, so we decided we’d go ahead and make one. We both are really interested in Blender and decided to work with it from there. So here’s the plan.

I wanted to do a simple quiz session with python Interactive GUI scripting. My friend, Smith, wants to do his with the game engine and be able to make the quiz a game in a sense. So, we kind of added both of them together. Basically you have (in the game part) a character (in the beta phase it’ll just be hands with a weapon or something in first person-in “Charlie” [whatever] phase it’ll probably be a complete character in third person) with a single wall (it’ll be one stationary wall in beta and an entire building in “Charlie”) and on this wall is plastered one question with four possible answers under it with a check “circle” (you know the little button that you click on that has a black ball pop up in it when it’s your answer?) next to each possible answer.

On the ground in front of you is a black ball, and the objective is to get the ball in the hole where your selected answer is located. However, your character doesn’t have the physical strength to pick up the ball and put it in the hole, so this is where the Interactive Python GUI comes into view. The game part is located on the right, and the Python part is located on the left hand side in a separate panel. This GUI will also have a question with four possible answers and if you get THAT question right then your character will be given a specific type of weapon that can help you get the ball in the hole. May it be electricity for magnetics, psychic powers, teleportation device, etc.

I’m having a problem with my part because, well, i don’t know how to make a GUI. i had tried a long time ago, but the tutorial i found (hours of searching included) didn’t really help, it just showed me how to create a button, not really how to do anything with it like this. I want to have one question button at the top (really, not even a button, just the printed question), four buttons with random possible answers, and a “continue” button at the bottom. The Continue botton simply randomizes a set of questions (100 to be exact). Then the question randomizes the order of the answers for THAT question. If you click the wrong answer it simply shows your character try to throw the ball and it falling back down and then randomizes another question for you to try again.

if you get it right it gives you a new weapon (these weapons are listed at the bottom right hand of your game screen for you to pick and choose for different functions) and then you can try your chances on the overall question.

i have no idea where to start. My friend knows nothing about python, only the game engine. i want to incorporate python with this because that’s something i like anyways, but i also want to get from this the knowledge my friend has with the game engine so he learns python (as well as me) and i learn game engine and we both get a new study tool.

Also, i heard that the newest version of Blender allows you to export in executable files so that you don’t need blender to run it…is that only in game engines or can it also be with basic python scripts, and if it is only with game engines can i have the python script included in that side panel?



Programming consists of two parts: technical stuff and the right frame of mind. It seems to me that you have a frame-of-mind problem here, preventing you to understand the technical stuff. There are no easy solutions for this, but I will try to help you along.

In designing a GUI, you must think in events. Events are generated by things like buttons: you must define one function, a drawing callback that draws the buttons, and that assigns an event number to every button. Then, there is a second function that you must define, an event callback, which is called by Blender every time an event is generated. Using the event number, you can find out which button generated the event. Then you can do specific scripting in response to the button presses.

I suggest that you just read the Blender.Draw documentation and have a look at the code of some other Blender GUI scripts (several are shipped with Blender). As soon as it all starts to make sense, you can then start making your own.

Blender’s GUI code contains 2 essential components:

(1) A GUI drawing function
(2) An event handling function

When you call Blender.Draw.Register(guiDrawingFunc, handleNonGUIEventsFunc, handleGUIEventsFunction), what happens is something like the following:

while user_did_not_say_quit:
    if there is a non-GUI event:
    if there is a GUI event:

So, in the GUI drawing function, you tell blender what GUI components you need, buttons, drop down menus, etc, and how you want to handle the events generated by these GUI components. For example, if your GUI contains a single button for quiting, the code would look like:

def drawButtons():
   buttonText = 'Quit'
   buttonEventId = 1
   buttonHeight = 50
   buttonWidth = 100
   buttonHorizontalPosition = 100
   buttonVerticalPosition = 100
   buttonHintText ='quit the GUI'

   quitButton = Blender.Draw.PushButton(buttonText,

The important thing about the above function is the buttonEventId, it’s an integer that will be the event generated when the quitButton is pressed.

Then your event handling code would look like the following:

def handleGUIEvent(evt):
    if evt == 1:

To put these two functions together to start the GUI, you use the following code:

Blender.Draw.Register(drawButtons, None, handleGUIEvents)

Notice that there is no function to handle non-GUI events.

Hope it helps.


i’d avise you to make all your stuff only in the game engine
so that you can export it to an executable (.exe or other)

For what you want to do with buttons I would not recomment using any of non-game engine GUI API (the one which uses the python module named Blender)

If you don’t need your game to be exported to some executable and are ok to use Blender, then you could very well use what’s proposed in the previous post.
Basically you’d have to split Blender’s windows into with the mouse (right click on a windows edge, choose split in the menu).
Then in the right view for example you could have you regular Blender GUI script (not a game engine one) running (You just have to choose this right view a text editor, open the regular blender GUI script in it and start it with Alt P). The way this script works is what the post just before mine talks about. And you have details here
And in the left view you could have your game running maybe with little python scripting, this python would be Game Engine specific (using the GameLogic module).
You could use scriptlinks to start up the right and left view automatically, but I don’t know how that works honestly.
It is not possible to control the Blender Game Engine from a script that does not run inside it. So you have to use some communication for both your left and side views to talk to each other.
In order for both the regular blender GUI script and the game engine to communicate, you could use the Registry module :
Basically, for the right side to control the left(3d game) side, you would make the right side to store some value in the registry under some dictionary key name (ex: on some button pressed in the right view, set variable or key name “GameWhatToDo” to “next one”). And the left side would check regularly for those same registry variables to see if a change has come.

But as I would recommend to you, you’d be better off doing everything only in the Game Engine and don’t do anything using the regular Blender UI.
Since you say that you’ve managed to accomplish a tutorial on making one Button in Blender Game Engine, and according to what you said on your UI setup in game : One button at the top, one at the bottom, a variable number of buttons at the middle. That would not be difficult to script all within the Game Engine, though you won’t have all the callbacks and events “clean” scripting all in one script file as in the regular non-game engine GUI tool that’s described by the previous post.
So I’d advise you to try and do a rough buttons generator for the Game Engine fiddling with the first tutorial on button in Game Engine you have done.
Basically, you could say that you want max 5 buttons in the middle, create those five buttons and show only (you can hide and show them using setVisible() in Python I think) those required to have the exact nb of questions for this turn (ex : 3 possible answers this time, ok we show all the buttons except the last two buttons).