Actions using Python?(.blend posted)

I’m trying to program my first animated character controls. I figured out the movement controls, but I can’t get the actions to work. Could someone please provide me with a basic outline of how the actions work in python so that I can apply it to my file? This is in blender 2.59 btw.

thanks in advance!

I feel like I’m missing something in my description. :confused: Here’s the code if this helps:

import bge

def main():

cont = bge.logic.getCurrentController()
player = cont.owner
keyboard = bge.logic.keyboard
scene = bge.logic.getCurrentScene()

#properties
movSpd = 0.3
rotSpd = 0.07

#animations - basic
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.UPARROWKEY]:
    player.applyMovement((0, movSpd, 0), True)
    
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.RIGHTARROWKEY]:
    player.applyRotation((0, 0, -rotSpd), True)
    
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.LEFTARROWKEY]:
    player.applyRotation((0, 0, rotSpd), True)

main()

Sorry if it’s too nooby to behold, but that’s what I’ve got. The first IF statement is for the forward movement. I tried adding the playAction function under the player.applyMovement, but it didn’t seem to work.

Here’s how it looked:

import bge

def main():

cont = bge.logic.getCurrentController()
player = cont.owner
keyboard = bge.logic.keyboard
scene = bge.logic.getCurrentScene()

#properties
movSpd = 0.3
rotSpd = 0.07

#animations - basic
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.UPARROWKEY]:
    player.applyMovement((0, movSpd, 0), True)
    player.playAction(Barrot_run, 7, 20, layer=1, priority=5, blendin=5, play_mode=ACT_MODE_LOOP, layer_weight=0.0, ipo_flags=0, speed=1.0)
    
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.RIGHTARROWKEY]:
    player.applyRotation((0, 0, -rotSpd), True)
    
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.LEFTARROWKEY]:
    player.applyRotation((0, 0, rotSpd), True)

main()

What did I do wrong? I didn’t know what to put for ipo_flags and weight, so I kept theme at their default values.

Please help.T.T
^

This should work:



arm = obj.childrenRecursive['PlayerArmature']
arm.playAction('Animationname', startframe, endframe) # This should be called on the first frame that you want to play this animation on, and on no others.


There’s your problem.
You have to tell it how to loop, but in order to do so, you have to use a type.
change ACT_MODE_LOOP
to
bge.logic.KX_ACTION_MODE_LOOP

This is because the playAction() feature is not accepting a string, but a type (method) of playing an animation, which is defined in the types.

A simplified .blend, that clearly illustrates the problem, would help us help you.

Without that, I can’t really say with any certainty, but maybe player.playAction is supposed to be called only once? - I assume you’re running it always because of applyMovement/Rotation which has to be called on every tick in order to perform.

Did you try using the actuator? Does that work?

@Goran - That is indeed the case - the playAction function should be called on the frame that you want the animation to start. Coupling this with my Callbacks class in my BGHelper module is helpful. :smiley:

is useful tell that KX…etc?
I ever believe is to avoid (heavy)
I’m wrong?

applyMovement is not too good(recent discovery : D), cannot make a climb!

Thanks a lot for your help guys! I will try each suggestion and see what happens.

Alright, so my tries ended up with failure (most likely because of my inexperience).:frowning: Could someone please take the .blend file, find out what’s wrong, and tell me how to fix it please?
Here it is:
barrot.blend (1.41 MB)

Okay, I think I’ve got it. It’s generally better to have a collision box performing the logic with the armature parented to it, though.

barrot.blend (1.37 MB)

By the way, nice model. :slight_smile:

You need to use 2.60. 2.59 doesn’t have the Python API for animations.

Thanks a lot SolarLune and Moguri! It didn’t work until I used 2.6.