University of Southern CaliforniaUSC
USC ICT TwitterUSC ICT FacebookUSC ICT YouTube

How to register a custom event type so it can be triggered from BML? | General SmartBody Discussion | Forum

Avatar

Please consider registering
guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed Topic RSS sp_TopicIcon
How to register a custom event type so it can be triggered from BML?
November 21, 2014
12:52 am
Avatar
Member
Members
Forum Posts: 29
Member Since:
September 30, 2014
sp_UserOfflineSmall Offline

Hi,

I'm using SmartBody as a Python library with the Panda 3D game engine. Here is what I'm trying to do: I want to trigger a message at the end of an action using BML.

I have read all about it in the documentation and on the forum. I found information about how to settup an event handler and how to trigger an event from BML, but one point remains unclear to me:

How do I register the handler to take care of BML events?

I have tryed the following:

1) create a function that triggers an event of the desired type in a seperate python file

def triggerEoBEvent():
    eventManager = scene.getEventManager()
    event = eventManager.createEvent("EndOfBehavior", "")
    eventManager.hanldeEvent(event, 0)

I then call scene.run on the script containing that function to register it in the environment of SmartBody. Then I use the following BML tag: <head id="a" type="NOD" /><event start="a:end" message="triggerEoBEvent()"/>

2) putting equivalent instructions to the code above directly as the message argument of the BML event tag.

In both cases I get the same result: a "nameError" exception saying that the "scene" attribute doesn't exist.

By the way, this is an issue I also had when trying to run some of the init scripts that directly refer to the "scene" variable. I had to copy-paste their content in an equivalent function. Unless this time I don't have a choice but to use this.

I also need to send BML commands through VHMessage, bml probably won't be found either for the same reason.

I read in the documentation that the "scene", "bml" and a few other variables are always within the SmartBody context. For me it is not the case... How do I make sure they are loaded?

If there is an alternative solution, could someone please tell me how do I register the triggerEoBEvent to the SmartBody environment so I can catch it with a handler that will be accessible outside of SmartBody?

Here is how I defined my event handler:

from SmartBody import SBEventHandler

class EOBEventHandler(SBEventHandler):
    def executeAction(self, event):
        print "Behavior ended"

 

Here is how I register it in my wrapper class for SmartBody:

import SmartBody

from EOBEventHandler import EOBEventHandler

class SmartBodyScene():

    def __init__(self):

        #get the SmartBody scene
        self.scene = SmartBody.getScene()

        #get the even manager
        self.eventManager = self.scene.getEventManager()

        self.eventManager.addEventHandler("EndOfBehavior", EOBEventHandler())

Thanks,

Pierre

November 21, 2014
7:58 am
Avatar
Member
Members
Forum Posts: 29
Member Since:
September 30, 2014
sp_UserOfflineSmall Offline

Hi,

My mistake, I had not initialized the SmartBody environment so that it contains the "scene" and "bml" attributes. I did it by running an init script.

That being said, the function "triggerEoBEvent" is called but it doesn't seem that the handler is called. But maybe I don't need to.

Can I call any external function from a function defined in the same manner, for instance to play a sound?

Thanks,

Pierre

November 22, 2014
12:39 am
Avatar
Admin
Forum Posts: 983
Member Since:
December 1, 2011
sp_UserOfflineSmall Offline

In the other post I wrote how you can change the code so that an event is triggered when a sound is played. Is that what you are looking for?

 

Ari

November 22, 2014
10:53 am
Avatar
Member
Members
Forum Posts: 29
Member Since:
September 30, 2014
sp_UserOfflineSmall Offline

Hi Ari,

Yes, I haven't tried it yet but that is what I need. Thanks a lot for your very helpfull answers. I wanted to use events for two things: I use a remote controller that monitors a person using a Kinect and sends BML commands to SmartBody. I wanted to raise an event to give feedback that a action was completed. I suceeded in sending a vhmsg when an action is over. Now I have to try your solution for the sound.

Regards,

Pierre

December 14, 2014
9:26 am
Avatar
New Member
Members
Forum Posts: 1
Member Since:
December 14, 2014
sp_UserOfflineSmall Offline

thanks Ari, your answer help me also