Start animation on key event

Hi guys,

I ve got a model animated in Blender and exported to .egg format.

The animation runs, if I put it into the main class. The pose function works on a key event, but I cant make the animation play() or loop() through pressing that key.

What did I miss out?

Cheers

from pandac.PandaModules import *
from direct.actor.Actor import Actor
from direct.interval.ActorInterval import ActorInterval
loadPrcFileData("", "audio-library-name p3openal_audio")


class Stage():

    def __init__(self, scenery):

        self.scenery = scenery
        self.stage = loader.loadModel("stage-stage")
        self.stage.reparentTo(render)

        self.curtain = Actor("stage-curtain" , {"open" : "stage-curtain-open"})
        self.curtain.reparentTo(render)

        self.bgproj = loader.loadModel("stage-bgproj")
        self.bgproj.reparentTo(render)

        self.proj_movie()

        self.curtain.play("open")
        self.curtain.stop()

        

        render.setAntialias(AntialiasAttrib.MAuto)


    def proj_movie(self):

        proj = self.bgproj

        ts = TextureStage("ts")
        ts.setMode(TextureStage.MReplace)
        ts.setSort(1)

        movie = loader.loadTexture("BGProj.mov")
        proj.setTexture(ts, movie)
        proj.setTexScale(TextureStage.getDefault(), movie.getTexScale())


    def update(self, key_map):

        if key_map["open"]:
            self.curtain.play("open")

If I understood correctly, you want the animation to be played from the moment you press the key to the moment you release it. If that’s it, then why not do this:

base.accept("your_key", self.curtain.play, extraArgs=["open"])
base.accept("your_key-up", self.curtain.stop)

Where “your_key” is “w” or “enter” or “f1” or whatever else.

You don’t really need an update method for this, you just once need to tell Panda what you want it to do whenever a key is pressed or released.

Of course, you don’t have to use base. In most cases, you would use your own DirectObject so you can call .ignoreAll() on it conveniently whenever you feel like it.

Thanks coppertop

yes, but it is set in the main script as follows:

#this are the important lines from my main script

self.set_keys()

def set_set_keys(self):
    
    self.key_map = {"open": 0}
    
    self.accept("m", self.set_key, ["open": 1])

def set_key(self, key, value):
    self.key_map[key] = value

def update_scene(self, task):
    self.stage.update(self.key_map)

    return.task.cont

I want to use this setup to control different key events in different modules. It works as far as I use
kinda static events, like pose(“open”, 80) or stop().

The problem here is getting the animation run with play(“open”) or loop(“open”).

You are calling the update function that calls play(“open”) every frame, and it is restarting the animation every frame. This means you are only ever seeing the first frame of animation.

you are right, TeeDee! Thanks, it is working now.

I took the code from an example (as I am newbie in coding :smiley:)

After I took out the updating string task, it is working.

Here the current code:

# main

def set_keys(self):

        self.accept("p", self.stage.proj_movie)
        self.accept("s", self.stage.proj_pic)

and in stage module


class Stage(object):

    def __init__(self, scenery):

        scenery = render.attachNewNode("Scenery")

        self.stage = loader.loadModel("stage-stage")
        self.stage.reparentTo(scenery)

        self.curtain = Actor("stage-curtain" , {"open" : "stage-curtain-open"})
        self.curtain.reparentTo(scenery)
 
        self.bgproj = loader.loadModel("stage-bgproj")
        self.bgproj.reparentTo(scenery)

        

    def proj_movie(self):

        proj = self.bgproj

        ts = TextureStage("ts")
        ts.setMode(TextureStage.MReplace)
        ts.setSort(1)

        movie = loader.loadTexture("BGProj.mov")
        proj.setTexture(ts, movie, 10)
        proj.setTexScale(TextureStage.getDefault(), movie.getTexScale())
        movie.play()
        
    def proj_pic(self):

        proj = self.bgproj
        proj.clearTexture()
        pic = loader.loadTexture("BGProj.jpg")
        proj.setTexture(pic, 10)

Thanks for your help!