Wednesday, 6 March 2013

SmartBody Example:

SmartBody characters are usually controlled via python scripts. These scripts are responsible for loading all of the features need for the character animation and then seting up the character in the environment and last functions to control the actions of the character(s).

This is a common exmaple, there are more advanced features than what is discussed in this post.

The first part of the python script to control a SmartBody character should be calls to add any assests that are need for the character animations that will be used later in the script.

smartBodyRelitivePath = "../../smartbody/data/"

# Add asset paths
scene.addAssetPath('script',smartBodyRelitivePath + 'sbm-common/scripts')
scene.addAssetPath('mesh', smartBodyRelitivePath + 'mesh')
scene.addAssetPath('mesh', smartBodyRelitivePath + 'retarget/mesh')
scene.addAssetPath('motion', smartBodyRelitivePath + 'ChrBrad')
scene.addAssetPath('motion', smartBodyRelitivePath + 'ChrRachel')
scene.addAssetPath('motion', smartBodyRelitivePath + 'retarget\motion')
scene.addAssetPath('motion', smartBodyRelitivePath + 'sbm-common/common-sk')

The above code first defines the relative path to the directory that contain all of the assest for character animation. These include meshes, skeletal meshes, joint maps, etc. These are usually stored in the data folder in SmartBody. As can bee seen assets for the mesh for a character is added as well as common script for that character and motion data that will be used to create the motion of the character.

The next section of the program Will configure the scene. This is much more OpenGL like and is to define the setting for the camera.

# Set scene parameters and camera
print 'Configuring scene parameters and camera'
scene.setBoolAttribute('internalAudio', True)'')
camera = getCamera()
camera.setEye(0, 2.87, 11.67)
camera.setCenter(0, 2.14, 9.81)
camera.setUpVector(SrVec(0, 1, 0))
scene.getPawn('camera').setPosition(SrVec(0, -5, 0))

The next section is a little less obvious. It first runs a script, this script loads a joint map. After a joint map is loaded it can be applied to a character in SmartBody.

# Set joint map for Brad
print 'Setting up joint map for Brad''')
zebra2Map = scene.getJointMapManager().getJointMap('zebra2')
bradSkeleton = scene.getSkeleton('')

Next two more scripts are run to setup the motion animation that the character can do and next activates the parametric animation that is used for the characters.

# Retarget setup'')
# Animation setup'')

Now everything that needs to be done to be able to use animations is complete The last step that needs to be done so that a character can be used in the scene is described below.

# Set up Brads

print 'Adding characters into scene'
posX = -200
for i in range(1): # Only add one brad
    baseName = 'ChrBrad%s' % i
    brad = scene.createCharacter(baseName, '')
    bradSkeleton = scene.createSkeleton('')
    # Set position
    bradPos = SrVec((posX + (i * 200))/100, 0, 0)
    # Set up standard controllers
    # Set deformable mesh
    brad.setDoubleAttribute('deformableMeshScale', .01)
    brad.setStringAttribute('deformableMesh', 'ChrBrad')
    # Play idle animation
    bml.execBML(baseName, '')
    # Retarget character
    retargetCharacter(baseName, '', False)

# Turn on GPU deformable geometry for all
for name in scene.getCharacterNames():
    scene.command("char %s viewer deformableGPU" % name)

To add a character to the scene it needs to have a unique name ( will be seen later why ) and must be assigned a skeleton. THe rest of the code sets up the initial conditions for the character Brad: initial position, standard controllers, scale and turn on deformable geometry (by default a characters geometry is not displayed) and the initial posture for brad. A posture is the stance the character will take when no other animations are in effect.

The last part of the script is the part that you must write. After the characters have been initialized properly it is up to you to control the character as you want. For this example I wrote a simple controller that accepts information from a TCP connection so that I can control the character with another program.

class LocomotionDemo(SBScript):
    def update(self, time):
        # Select is a creative way of checking to see if a service has data ready to read,
        # ready to write to or an exceptional condition.
        r, w, e =[service], [], [], 0.0)
        if r: # There is something to read
            data = service.recv(256)
            character = 'ChrBrad0'
            # The last 0 in the target is zero just so the character stays on the plane
            bmlMessage = ''
            print "BML Message for " + character + ": " + bmlMessage
            bml.execBML(character, bmlMessage)

To add a controlling script you need to write a class and have a method called update in that class. This update method will be called every frame and you can check the characters and scene for events of interest to further animate your characters. THe code include some notes on some features of Python.

Last the script that you have created need to be given control. This is done by explicitely removing the script name if it already exsist and then creating an object that can be used to reference your update function.

NOTE: this script name is for the class that is created not the name of script file that is be used.

# Run the update script
locomotiondemo = LocomotionDemo()
scene.addScript('locomotiondemo', locomotiondemo)


No comments:

Post a Comment