UPBGE, start timeline

Hey there :slight_smile:

I would like to make a simple setup for architectural walkthroughs with UPBGE. This is a minimal example in order to move the camera with wsad and look around using the mouse.

As you can see: There are two objects with keyframes in the scene. When pressing spacebar in 3D-View, donut and cube are starting to move. I would like to do the same during the game. Is there an easy way to start the the timeline during the game when pressing a predefined key? I am able to trigger the actions one by one using the logic bricks. Unfortunately, this is not handy when dealing with a lot of animated objects in a scene. The idea would be to group the actions or simple start the whole animation just like pressing spacebar in 3d-View.

Does anyone have experience in this topic?
Thanks in advance!

fps_look.blend (923.4 KB)

if you use logic bricks, why not just use the same keyboard button to trigger all of them (spacebar for example)

1 Like

Thanks for your reply. Yes, this is an option of course. The problem is that my scene contains a lot of objects. I was searching for a method to copy the sensor to all of the objects but I have not found a solution. Is there some python snipped to use, in order to iterate trough all objects and assign its own action? Are there any other methods to automate this process?

sure …

play this script only once at boot

import bge

scene = bge.logic.getCurrentScene()

#### seems it crashes with a loop ... there's a " __default__cam__"  object that is not happy to play an animation .. 
for obj in scene.objects :  print(obj.name)

#### so .. you gonna have to list the objects u want to pick and 
### list their specific animation to play anyway ..
## so do it simply like this
    
scene.objects["Suzanne"].playAction('SuzanneAction',0,60)
scene.objects["Norris"].playAction('TigerUpperCut',0,60)

bgepython.tutorialsforblender3d.com/Action

you will probably want to make for complex things later . But give a try to this already

Ofc, if you really want to loop on all objects, you can filter on the name . Here if you want to create a group of objects u may want to use later in code. It’s better than spamming “if” all along the code

objs = [obj for obj in scene.objects if "zozo" in obj.name ]
for obj in objs : obj.playAction('SuzanneAction',0,60)
1 Like

If you’d rather go the BPY api route instead of using simple and easy BGE api as blenderaptor mentioned, you can use BPY code, but it’s pretty limited. Here’s why:

  1. You have to have Viewport Render option enabled in your Render Properties tab in your Properties Editor. This will restrict all BGE updates and only show Blender / BPY driven simulations.
  2. When the timeline plays, any animations that are active on an object in the active scene collection will be played. That means any actions, simulations or drivers will play on all of those objects, globally.

Here’s some BPY code to play the animation timeline:

import bpy
FRAME_END = 60
currentFrame = bpy.data.scenes["Scene"].frame_current
# Play once only
#bpy.data.scenes["Scene"].frame_set(currentFrame + 1)
# Play looped
bpy.data.scenes["Scene"].frame_set((currentFrame + 1) % FRAME_END)

Here’s how to use a custom start - end timeline setup with BPY:

import bpy

FRAME_START = 100
FRAME_END = 200

currentFrame = bpy.data.scenes["Scene"].frame_current
setFrame = bpy.data.scenes["Scene"].frame_set

if currentFrame < FRAME_START or currentFrame > FRAME_END:
    bpy.data.scenes["Scene"].frame_set(FRAME_START)
elif currentFrame >= FRAME_START and currentFrame < FRAME_END:
    setFrame((currentFrame + 1) % FRAME_END)

Here’s two of my videos demonstrating real-time physic simulations.

1 Like

RParkour, he wants the beginning of an answer, not a display of your science. I personally think that a “simple” and “easy” solution is the best to start with as it will put him on track quickly . :slightly_smiling_face:

3 Likes

Thank you very much! You are great :slight_smile:
This solution is working perfectly for my case.
Only one small adaption:

import bge
import bpy

scene = bge.logic.getCurrentScene()

get start and end of timeline

start = bpy.context.scene.frame_start
end = bpy.context.scene.frame_end

create list of objects that should be animated (in collection “animated”)

animated_objs =
for obj in bpy.data.objects:
if obj.users_collection[0] == bpy.data.collections[‘animated’]:
name = obj.name_full
animated_objs.append(name)

append action

for obj in scene.objects:
if obj.name in animated_objs:
print(“add action of:”, obj.name)
action = obj.name + “Action”
obj.playAction(action, start, end)

fps_look.blend (949.5 KB)