Python confusion

If you feel like you're up to the challenge of building your own Ages in Blender or 3ds Max, this is the place for you!

Python confusion

Postby Justintime9 » Sun Mar 15, 2009 6:22 am

I'm currently working on getting a button to be clickable which will trigger an animation. Now, I've been looking at this tutorial: http://www.guildofwriters.com/wiki/How_ ... _Clickable

When I got to the bottom, I assumed I needed to create a python file, since my object wasn't a book. I then went on to the "How to create a python file" tutorial. I did fine until it said to enter the code. In the Clickable tutorial it give me this code:

Code: Select all
http://www.guildofwriters.com/wiki/How_to_make_an_Object_Clickable

Now, It took me quite awhile to make heads or tails of this, but from what I've gathered, I have to put

Code: Select all
if ((id == actClickableObject.id) and state):
            print ('Object %s was clicked' % strObject.value)
            #place your code here

after "def OnNotify(self, state, id, events):". After looking at the top, it occured to me to replace "actClickableObject" with "ptAttribActivator" as it appeared to be telling me to do, as well as "strObject"

Now here's the problem: I don't know what to replace with what, in order to specify which objects it's modifying, telling it that it will trigger an animation, and I don't know what the numbers in parentheses are for. Can anyone help me?
User avatar
Justintime9
 
Posts: 1188
Joined: Sat Sep 29, 2007 5:37 am

Re: Python confusion

Postby Jojon » Sun Mar 15, 2009 7:10 am

If the button is only going to trigger the animation and not, say, remember which end of the animation we are at after playing, you don't need that python scripting. (that would be to remember whether a door is open or closed, for example)

Please note that I can't entirely vouch for the alcscript below - it's been copied, pasted and trimmed and those words just about sums up my scripting:

Code: Select all
My_animated_object:
    animations:
      - name: You_go_object
        autostart: 0
        loop: 0
        loopstart: 0
        loopend: 2        # A 2 second (loopend-loopstart) animation. (60 frames, 2s times 30fps)


My_button_object:
    logic:
        modifiers:
          - cursor: poised
            flags:
              - localelement
            activators:
              - type: objectinvolume
                remote: My_button_region        # Player has to be inside this box, or the button will refuse to highlight
                triggers:
                  - any
            conditions:
              - type: activator
                activators:
                  - type: picking
              - type: objectinbox
                satisfied: true
            actions:
              - type: responder
                ref: :My_olde_list-o-actions
        actions:
          - type: responder
            name: My_olde_list-o-actions
            responder:
                states:
                  - cmds:
                      - type: animcmdmsg
                        params:
                            receivers:
                              - 006D:My_animated_object        # Send the animation message to the object in question
                            animname: You_go_object        # ...and say which animation to play.
                            cmds:
                              - continue        # We want the animation to continue playing from its current position
                        waiton: -1        # Play right away, when button pressed, without waiting for anything else
                    nextstate: 0        # Eg. a door can have two "states": open and closed
                    waittocmd: 0
                curstate: 0
                flags:
                  - detecttrigger


Each states.cmds can contain as many actions as you wish, so you could start two animations simultaneously and switch camera while at it, just add more blocks of the ( - type: to waiton: ) variety.
If you want the button to alternately start and stop the animation, you'd make it looping and introduce a second "state". Then, when you first press the button, the first state would send an animation message, starting the animation, and setting nextstate to eg. 1, for "animation running, next time stop it". Next time you press the button, the actions listed for state 1 will be performed -- this would be an animation message, just like the one that started the animation, but this time stopping it. This state would set the next state to 0, so that next button press, we'll be back to the first actions list starting the animation again.
You can, of course have more than two states, say stop->conveyor_running_left->stop->conveyor_running_right->back_to_start...
Jojon
 
Posts: 1116
Joined: Sun Sep 30, 2007 5:49 am

Re: Python confusion

Postby Justintime9 » Sun Mar 15, 2009 7:42 am

Well, that's still pretty much latin to me, (what the heek are states?), but here's what I want to do: The button will open a wide window, the first time the button is pressed, it will open, the second time, it will close. When the button is clicked, the avatar will do the push button animation.
User avatar
Justintime9
 
Posts: 1188
Joined: Sat Sep 29, 2007 5:37 am

Re: Python confusion

Postby Jojon » Sun Mar 15, 2009 8:14 am

Justintime9 wrote:Well, that's still pretty much latin to me, (what the heek are states?), but here's what I want to do: The button will open a wide window, the first time the button is pressed, it will open, the second time, it will close. When the button is clicked, the avatar will do the push button animation.


Terribly sorry. I honestly tried to make it as legible as I could, speaking about something I don't really understand myself. I'll begin with the states bit:

Ok, we're kind of dealing with metaphors here... You do physics at school, right? Now a body of water can be in different "states", or maybe one could say "ways of being", like "Solid state" (frozen), "Liquid state" (fluid) and "Gaseous state" (steam). Likewise, a ball sitting on a tabletop is said to be in a "static" or "resting" state, push it and it is in a rolling state, only to go into a falling state as it goes over the edge. :P
You have just defined two states for your window: "Open state" and "Closed state".

Our metaphorical representation of this, in alcscript, is two lists:
"what do do if the user presses the button while the window is closed" and
"what to do if the user presses the button while the window is open".

You can see one such list in the example alcscript I pasted, although it contains only a single entry.

Disregarding, for the time being, any other effects you might want in there, like the button lighting up, as you push it, each of your two "states" lists of actions would consist of two messages sent:

1) a "onestagemod", which makes the avatar perform an action
2) an animation message, sent to you window

The onestagemod will send a "callback" message, at a certain frame of the avatar animation (when hand makes contact with button) saying "Ok, I've pressed the button now" and you will use this to make the animation message, that opens the window, not start until the avatar is done with exercising her/his impressive buttonpressing skills. You do this by assigning the "callback" a number and then use that number on the "waiton" line of the window animation message.

Oh man.. too wordy again. :P


Whether studying the alcscript above tells you anything or not, you'd do well to study the "original" from which I harvested it. :P Nadnerb's AnimatedDoor example age, found at the bottom of the animations wiki page:

http://www.guildofwriters.com/wiki/Animations

It does what you want and more.


Good luck. :7
Jojon
 
Posts: 1116
Joined: Sun Sep 30, 2007 5:49 am

Re: Python confusion

Postby D'Lanor » Sun Mar 15, 2009 9:54 am

My stateanimation quickscript was created for 2 state animations. The trunk version of prp_QuickScripts is outdated though so you need to get the latest version here.

I could have sworn I made a wiki article about it because I have the concept right here... but apparently I did not. Or maybe I was going to do that after the merge with the trunk (which never happened). Anyway, here is the syntax:

Code: Select all
<clickable object>:
    quickscript:
        stateanimation:
            objectname: <animated object>
            region: <clickable region>
            sdlname: <sdl variable>
            facevalue: <tolerance for facing condition>
            clicktimeout: <time to disable clickable>
            avatar:
                animation: <avatar animation>
                animtarget: <avatar animation target>
                marker: <callback marker>
            forewards:
                animation: <opening animation>
                animsound: <opening sound emitter>
                clickanim: <clickable opening animation>
                clicksound: <clickable opening sound emitter>
            backwards:
                animation: <closing animation>
                animsound: <closing sound emitter>
                clickanim: <clickable closing animation>
                clickreverse: <true | false>
                clicksound: <clickable closing sound emitter>


And the explanation (which was supposed to go into the wiki):

General info: Leave out the <>. This just means it is a variable. Also leave out the |. That means you must use one of the choices given (true or false).

The variables:

objectname: This is your animated object. Since we are using a clickable to activate the animation we have to make sure it does not start automatically or keeps looping once it has started.

<animated object>:
animations:
- name: <opening animation>
autostart: 0
loop: 0

For more information about setting up animations see http://www.guildofwriters.com/wiki/Animations

region: The region around your clickable object. See also http://www.guildofwriters.com/wiki/How_ ... _Clickable

sdlname: The name of your SDL variable. If your animated object is a door you probably should call it something like DoorOpen.

VAR BOOL DoorOpen[1] DEFAULT=0

DEFAULT=0 means that the door is initially closed.

See also http://www.guildofwriters.com/wiki/Using_SDL_States

facevalue: The tolerance for the facing condition. Optional, default=no facing condition. This was thrown in for completeness sake but you can leave it out if you don't need it. The facing condition is discussed here: http://www.guildofwriters.com/wiki/How_ ... _condition

clicktimeout: Disables the hotspot of the clickable object during the time you set here. Optional, default=no time out. Leave this out if you do not need it. The time is a float value (for example 3.5).

avatar: Here you can set parameters for the avatar animation. These are optional. So again you can leave this out of your script if you do not need it.

animation: The name of the avatar animation without the Male/Female prefix.

animtarget: The name of an empty object placed at the position where you want the avatar animation to start.

marker: You can optionally specify the name of a callback marker within the avatar animation you use. By default the callback will be the end of the animation.

forewards: This is where you set the variables for the "opening" or "forwards" animation. The spelling of "forewards" is not an error but is kept consistent with the full Alcscript which in turn conforms to Cyan's spelling.

animation: The name of your opening animation.

animsound: The name of the emitter object for the opening sound. Optional. See also http://www.guildofwriters.com/wiki/Adding_Sounds_I

clickanim: The name of the opening animation for the clickable object. Optional.

clicksound: The name of the emitter object for the opening sound of the clickable. Optional.

backwards: This is where you set the variables for the "closing" or "backwards" animation.

animation: The name of your closing animation. If it is the same as the opening animation that animation will be automatically reversed.

animsound: The name of the emitter object for the closing sound. Optional. Can be the same as the opening sound.

clickanim: The name of the closing animation for the clickable object. Optional. Can be the same as the opening animation.

clickreverse: Optional. The closing animation for the clickable object will not be reversed by default if it is the same as the opening animation.
Setting this to true will play the opening animation backwards. Setting this to false is effectively similar to simply leaving this line out. This setting is ignored if opening and closing animations are different.

clicksound: The name of the emitter object for the closing sound of the clickable. Optional. It can be the same as the opening sound.


Edit: Oh, and I almost forgot. Using quickscripts may be easier but it will not teach you the underlying principles. So I would recommend trying the full Alcscript first.
"It is in self-limitation that a master first shows himself." - Goethe
User avatar
D'Lanor
 
Posts: 1980
Joined: Sat Sep 29, 2007 4:24 am

Re: Python confusion

Postby Justintime9 » Sun Mar 15, 2009 11:57 am

Wow, that looks much easier than the normal AlcScript, the tutorial is a bit confusing (since it deals with any clickables, and I didn't know if I needed a Python file or not.) Also, what do I do to get the SDL file?
User avatar
Justintime9
 
Posts: 1188
Joined: Sat Sep 29, 2007 5:37 am

Re: Python confusion

Postby D'Lanor » Sun Mar 15, 2009 12:19 pm

What I always do is make a text file with the name of my age, for example MyAge.sdl.txt. Then when I am done editing I encrypt it with UruFun which saves it as MyAge.sdl without the .txt extension.
"It is in self-limitation that a master first shows himself." - Goethe
User avatar
D'Lanor
 
Posts: 1980
Joined: Sat Sep 29, 2007 4:24 am

Re: Python confusion

Postby Justintime9 » Sun Mar 15, 2009 12:31 pm

What would I put in the .txt file tho? And how do I Encrypt something? when it asks for the SDL variable, is there something I have to name in the SDL file that corresponds to this?
User avatar
Justintime9
 
Posts: 1188
Joined: Sat Sep 29, 2007 5:37 am

Re: Python confusion

Postby D'Lanor » Sun Mar 15, 2009 1:58 pm

sdlname: The name of your SDL variable. If your animated object is a window you could call it WindowOpen. This is what a typical sdl file looks like.

Code: Select all
#
# State Description Language for <replace with name of your age>
#

STATEDESC <replace with name of your age>
{
    VERSION 1

    VAR BOOL    WindowOpen[1] DEFAULT=0

}

DEFAULT=0 means that the window is initially closed.
"It is in self-limitation that a master first shows himself." - Goethe
User avatar
D'Lanor
 
Posts: 1980
Joined: Sat Sep 29, 2007 4:24 am

Re: Python confusion

Postby Justintime9 » Sun Mar 15, 2009 2:44 pm

Well, I added all the settings, but for some reason when I linked there I crashed. here's the Script (the top one is to make it not loop)

Code: Select all
BayWindow:
animations:
- name: WinOpen
autostart: 0
loop: 0
   
WindowButton:
    quickscript:
        stateanimation:
            objectname: BayWindow
            region: WinClReg
            sdlname: VAR BOOL    WindowOpen[1] DEFAULT=0
            clicktimeout: 2.100
            avatar:
                animation: DoorButtonTouch
                animtarget: AvatarEmpty
            forewards:
                animation: WinOpen
            backwards:
                animation: WinOpen
User avatar
Justintime9
 
Posts: 1188
Joined: Sat Sep 29, 2007 5:37 am

Next

Return to Building

Who is online

Users browsing this forum: No registered users and 0 guests

cron