Creating Your Own Animations

From Multiverse

Jump to: navigation, search

Contents

Overview

This tutorial will cover how to take single animations that you've created for your character and turn them into a slash command. For general information on command handlers (slash commands), see Defining Command Handlers.

Procedure

The general procedure is:

  1. Define the command handler in a server Python script.
  2. Set the length of the animation in client Python script.
  3. Optionally, add a sound.

Defining your command handler

The first step is to add your server slash command to config/worldname/extensions_proxy.py. For example:

class WaveCommand (ProxyPlugin.CommandParser):
   def parse(self, cmdEvent):
       playerOid = cmdEvent.getObjectOid()
       Log.debug("/wave: oid=" + str(playerOid))
       AnimationClient.playSingleAnimation(playerOid, "wave")
...
proxyPlugin.registerCommand("/wave", WaveCommand())

Copy this code and change "wave" to the name of your animation.

This code will call the playSingleAnimation() method of multiverse.mars.plugins.AnimationClient, which sends a message which is caught by AnimationPlugin.AnimReqHook.

Setting animation length in client code

Then, AnimReqHook invokes the effect in worldname/scripts/AnimationEffects.py. Look at ExecuteEffect in that file.

   def ExecuteEffect(self, sourceOID, animName):

       caster = ClientAPI.GetObjectByOID(sourceOID)
       casterLoc = caster.Position

       # attach and play the sound

       if not animName in caster.Model.AnimationNames:
           return

       # play animation
       caster.SetProperty('client.animationoverride', True)
       caster.QueueAnimation(animName)

       # wait for the duration of the animation to turn off the animation override        
       yield int(caster.Model.AnimationLength(animName) * 1000)
       
       caster.SetProperty('client.animationoverride', False)
       
       # clean up sounds
       #target.ClearSounds()

# register the effect
ClientAPI.RegisterEffect("PlayAnimation", PlayAnimation)

This will play any animation called if it belongs to that model.

The Model.AnimationLength() method enables you to get the length of an animation. With this method, you do not need to hard-code the time to yield, but instead can use the following call to wait for the duration of the animation:

yield int(caster.Model.AnimationLength(animName) * 1000)

where animName is the name of the animation.

Adding Sounds

You can add a sound to play with your animation:

# attach and play the sound
sound = ClientAPI.GetSoundSource('swordhit.wav', targetLoc)
target.AttachSound(sound)
sound.Play()

You can use this code from AttackEffect.py and replace the name of the sound with the sound you want to play.

Loading the script on startup

If you have your own version of startup.py make sure you load the AnimationEffects script by adding:

import AnimationEffects.py
Personal tools