ANOTHER QUESTION ON PAGE 3 :P Project LearnMorePython

I don’t know if it was a good or bad idea to post a whole thread on this…Anyway…

I am starting this small python project.

Goal- To learn more PYTHON :slight_smile:

Here is what’s going down:

I will model a basic human model and rig it and so forth

I will model a small scene to put it in.

I will script as much of it as possible.

The player will be able to:

Walk (DONE!) (I will change it to servo motion later when I finish some other stuff)
Run (DONE!)
Jump (DONE!)
grab onto a ledge and pull himself up(DONE!)
swing on a rope

All this will be done in python script. Any questions I have will be posted here. (Which will probably be a lot.)

What will also be done with python scripting:

Enemy AI: Move/Idle/Attack/Health

Day/Night system

Gold system

Shop system

(That’s all I can think of at the moment.)

I’ll follow you! If this thread works out well, it can help alot of people new to python scripting

I have a basic character, a box for his physics, and an armature. If I wanted him to move AND play his walking animation at the same time using python, would I need two different scripts for that? And exactly how would I make him play his animation using python?

@Sonic - No, you wouldn’t need two scripts for this. You can get the object’s actuators (attached to the script) with cont.actuators[‘ActuatorName’]. After that, you can activate the script with cont.actuators[‘ActuatorName’].activate(). Of course, you can store variables to make it easier to type, like walkanim = cont.actuators[‘Walk’]. Moving can be done the same way - apply the variables to the actuator (move = cont.actuators[‘Move’], move.linV = [0.0, 0.1, 0.0], for example), and then activate the actuator.

EDIT: In answer to your question, use the above method to play the animation stored in an actuator (the Action actuator) attached to the Python controller running the script.

@SolarLune, Thanks!


I feel like this happens to me a lot but… I can’t seem to get my script to work. When I press the w key, nothing happens. :frowning:


Now that’s what I don’t understand about python. What’s the point if you still have to use the logic? I could do the same thing set up with an (and) controller, and not have to use the script. Then to move. You still need to add another actuator for the movement part of the script.
You still almost always need logic brick for your python.

Sonic, if you go into the help menu there should be “Toggle System Console” in there. Not sure if you already have this up or something, but if you activate the console and then run the game and press W it should display any errors in the console. Knowing the error helps, and if you still need help you can post the error with the code and we should be able to help. It’s ridiculously difficult to debug without the console so make sure to open that whenever you are going to be testing python!

EDIT: when I say press W, I mean it in the way you said it, as in W should trigger the code. the W key itself doesn’t print out errors in the console

@turbomonkey, It would be easier to just put “and” controllers, but I think python helps make things easier whenever you get really complex logic and stuff.

@curlystraw I have a mac, I am using 2.57, and I cannot find the debug console anywhere.

I am downloading 2.58 as I type and I am hoping it will have the debug console. :stuck_out_tongue:

EDIT: Nope, no debug console here either. :frowning:

I got the script to work when I took out the walkanim.activate(). But all that did was make the cube move. Now I need the animation to activate… AND I NEED TO KNOW HOW TO OPEN THE DEBUG CONSOLE ON MAC!!!

  1. You typed logic.getCurrentController - it’s a function, so you need the parentheses at the end: logic.getCurrentController()

  2. It’s cont.activate(walkanim), not walkanim.activate() - My mistake, I pointed you in the wrong direction in my previous post. I apologize. It’s cont.activate(), not actuator.activate().

  3. The console on Mac can be activated (if I recall) by navigating into the Mac executable (where Blender is), into /.blender, and then there should be another Blender executable. Run that, and the console should appear, right next to Blender.

@ Turbomonkey - At a certain point, logic possible in Python is impossible with logic bricks. Far before that, though, Python becomes viable as it can do things that are impossible with logic bricks. For example, once Moguri’s GSoC branch is finalized into the official build of Blender, Sonic’s example here won’t need any logic bricks outside of an Always sensor - the script will be able to play animations, move the object, and detect keyboard input without any additional logic bricks.

EDIT: In addition, Python opens up methods of dealing with AI and portions of your game where attempting to make such AI with logic bricks would slow things to a crawl. For example, LOD can only be done with Python. There’s just no way to do it -efficiently- with logic bricks.

You may not feel like there’s a need for Python, but you almost definitely will have a hard time making a high-quality game without it.

EDIT 2: @Sonic - In addition, you’ll need to deactivate the walking animation actuator when you let go of the W-key, so you can use cont.deactivate(walkanim).

  1. Ok, thanks again, SolarLune :P.

  2. Not sure how I missed that…

  3. Ummmm… How do you get into mac executable? If you mean terminal, then what should I type in it?

  4. When exactly will Moguri’s GSoC branch be finalized into blender?

No, I think it’s Right-Click > Explore Package, or something like that. Navigate into the Blender ‘executable’ (I think it’s really just a zip file), and inside of that, there’s another Blender folder. Just poke around inside the Blender executable, and you’ll find the one that will launch a Terminal, as well. You might be able to make a shortcut out of it for easy navigation, though I’m not exactly sure.

As for Moguri’s GSoC branch, I’m not sure exactly when it’ll be finalized. However, it seems to be coming along quite nicely - Moguri’s doing a good job on it. You can see his progress on his blog here. If I recall, though, I think that each branch will be finalized into the official builds when they’re finished - in other words, you might see Moguri’s work available officially fairly soon.

Ok, SolarLune, I went to the folder with 2.57 in it. I right clicked on it(blender icon), went to “show package contents” it brought up another folder thing, I clicked on the only icon inside the folder (which was another folder) and it had 2.57 and another different thing. I clicked on the other thing and it opened 2.57 with terminal. YAY! I FOUND IT!

Question: I am using obj.applymovement in my script. Is it simple motion or servo control?

EDIT: I actually went through another folder with some stuff in it and clicked on the folder marked “MacOS” then I went to the blender 2.57 and other thing…

That’s simple motion (or, I think, teleportation, just like using the obj.position property). To use servo motion, I believe you’ll need a Motion actuator. However, I believe that you can use obj.setLinearVelocity() for simple linear velocity.

@Solar…I know that There’s A LOT that Logic can’t do, but let’s say I wanted to make a game with all Python, and not one brick of logic. That can’t be done yet? Mid Drift uses a lot of logic, but also quite a bit of Python. Could I still convert most of my Logic to Python?

Sorry Sonic. Not trying to take over your thread. That was my only question.

Well, you’ll need at least one sensor to drive each Python script. However, at this point, a large amount that can be done with a logic brick can be done with Python without a dedicated logic brick for that purpose (replacing the mesh, polling the mouse or keyboard for input, re-instancing the physics mesh, basic movement (perhaps linear velocity; I have to check on that one), object tracking, for example, all can be done without using a logic brick for those purposes).

New problem arose. Here is my script, whenever I press “W”, the walk animation doesn’t play, whenever I press “S”, the walk animation does play. :S

import bge
from bge import logic
from bge import events

cont = logic.getCurrentController()

obj = cont.owner

key =

walkanim = cont.actuators[‘walk’]

idle = cont.actuators[‘idle’]

Wkey = key[events.WKEY]
Akey = key[events.AKEY]
Skey = key[events.SKEY]
Dkey = key[events.DKEY]

mx = 0.0
my = 0.0

rotate = 0.0

if Wkey == 2:
my = -0.2

if Akey == 2:
rotate = 0.1

if Skey == 2:
my = 0.2

if Dkey == 2:
rotate = -0.1

if Wkey or Skey != 2:

if Wkey or Skey == 2:

obj.applyMovement([mx, my, 0.0], 1)
obj.applyRotation([0.0, 0.0, rotate], 1)

Does the console say anything? Because I don’t see any errors immediately.

Nope. Console says nothing. :frowning:

Oh, I see. There’s an error in your logic. You have a line that says,

if Wkey or Skey != 2:

The line is wrongly written, as it is currently saying that if the W-key is pressed or the S-key isn’t pressed, the Idle animation were played. If it were if Wkey != 2 or Skey != 2, then it would mean that if either key is not pressed, then the animation will idle. You want

if Wkey != 2 and Skey != 2:

Meaning that both the W-key and the S-key have to not be pressed for the idle animation to play. You might just want to check to see if either are pressed, and if so, play the animation. Otherwise (with an else statement), play the idle animation.