Vizard 7 » Tutorials & Examples » Example scripts » Networking » Instant messenger
7.5

Instant messenger

\examples\network\im.py

This script demonstrates how to pass messages to another computer. Type a message in the box and press 'Send' to send the message. When you receive a message it will appear in the box, and the avatars mouth will move.

"""This script demonstrates how to pass messages to another computer.
Type a message in the box below and press the button to send the message.
When you receive a message it will appear in this box,
and the avatars mouth will move."""
import viz
import vizact
import vizinfo
import viztask
import vizinput
from random import random

viz.setMultiSample(4)
viz.fov(60)
viz.go()

# Create the display box for incoming messages
info = vizinfo.InfoPanel(align=viz.ALIGN_RIGHT_TOP)

# Get the target machine name from user input
targetmachine = vizinput.input('Enter the name of the machine you want to connect to').upper()

# Add a mailbox for the target machine
TargetMailBox = viz.addNetwork(targetmachine)

#Add the avatar
avatar = viz.addAvatar('vcc_male2.cfg')
avatar.setPosition([-0.1,0.2,0.4])
avatar.setEuler([165,0,0])
avatar.mouthPos = 0.0

#Get jaw bone
jawBone = avatar.getBone('Bip01 MJaw')
jawBone.lock()

def GetTalkSpeed():
return (random() * 5) + 5

def AnimateAvatarTask(message, sender):

# Set the text display to the message text
    info.setText(message)

# Set the title of the info
    info.setTitle('Message from: ' + sender)

# Show the message
    info.setPanelVisible(True)

# Get talk speed
    talkSpeed = GetTalkSpeed()

# Perform talking animation
    talkTime = viz.getFrameTime() + max(3.0, len(message) / 10.0)
while viz.getFrameTime() < talkTime:

# Animate the avatar talking
        avatar.mouthPos += talkSpeed * viz.getFrameElapsed()
if avatar.mouthPos > 1.0 and talkSpeed > 0:
            talkSpeed *= -1
elif avatar.mouthPos < 0.0 and talkSpeed < 0:
            talkSpeed = GetTalkSpeed()

# Update jaw bone
        jawBone.setEuler([0,avatar.mouthPos*7,0], viz.AVATAR_LOCAL)

# Wait a frame
yield None

# Animate mouth back to 0
while avatar.mouthPos > 0.0:

# Reduce mouth position
        avatar.mouthPos -= viz.getFrameElapsed() * talkSpeed
        avatar.mouthPos = max(avatar.mouthPos, 0.0)

# Update jaw bone
        jawBone.setEuler([0,avatar.mouthPos*7,0], viz.AVATAR_LOCAL)

# Wait a frame
yield None

# Hide the text
    info.setPanelVisible(False)

def NetworkTask():

    animateTask = None

while True:

# Wait for network event
        data = yield viztask.waitNetwork()

# Kill existing animation task
if animateTask:
            animateTask.kill()

# Create animation task
        e = data.events[0]
        animateTask = viztask.schedule(AnimateAvatarTask(e.message, e.sender))

viztask.schedule(NetworkTask())

def EnterTextTask():

# Create info panel for entering text
    sendTextPanel = vizinfo.InfoPanel('', icon=False,title='Send Text',align=viz.ALIGN_LEFT_BOTTOM)
    sendButton = sendTextPanel.addItem(viz.addButtonLabel('Send'))

# Setup handler for send button
def OnSend():

# Get current text message
        message = sendTextPanel.getText()
if message:

# Send the current text to the mailbox
            TargetMailBox.send(message=message)

# Reset the text to nothing
            sendTextPanel.setText('')

    vizact.onbuttondown(sendButton, OnSend)

# Loop that processes keyboard events
while True:

# Wait for a key to be entered
        data = yield viztask.waitKeyDown(None)

# Get current text
        message = sendTextPanel.getText()

# If the length of the key is 1 then
# the input is a normal character
if len(data.key ) == 1:
            message += data.key
elif data.key    == viz.KEY_RETURN:
#If the key is the return key then add the newline character
            message += '\n'
elif data.key    == viz.KEY_BACKSPACE:
#If the key is the backspace key then remove the last character
            message = message[:len(message)-1]

# Update the input text
        sendTextPanel.setText(message)

viztask.schedule(EnterTextTask())

#Disable mouse navigation
viz.mouse(viz.OFF)

#Set the background color
viz.clearcolor(viz.SLATE)