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 lips will move.

 

import viz
import vizact
import vizinfo
import vizinput
from random import random

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

SHRINK = 0
TALK = 1

#add a button to send text message
sendButton = viz.addButton()
sendButton.setPosition([0.95,0.1,0])

#text to send
text = viz.addText('text', viz.SCREEN)
text.setPosition([0.06,0.4,0])
text.setScale([0.5,0.5,0])
text.alignment(viz.ALIGN_LEFT_TOP)

#label for the box that contains text message
label = viz.addText('Send Text', viz.SCREEN)
label.setPosition([0.09,0.5,0])
label.setScale([0.57,0.57,0])
label.color(viz.YELLOW)
label.alignment(viz.ALIGN_LEFT_BASE)

#Create the display box for incoming messages
info = vizinfo.add('This script demonstrates how to pass messages to another computer.\nType a message in the box below and press the button to send the message.\nWhen you recieve a message it will appear in this box,\nand the avatars lips will move.')

#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)

#Variable that will hold the text to send to the user
SendText = ''

#Add the avatar
face = viz.addFace('biohead_talk.vzf',viz.HEAD)
face.setPosition([-0.1,0,0.4])
face.setEuler([165,0,0])
face.mouthPos = 0.0
face.talkSpeed = 0.0

#Create a border for the text field
viz.startLayer(viz.LINE_STRIP)
viz.lineWidth(2)
viz.vertexColor(viz.YELLOW)
viz.vertex([0.08,0.5,0])
viz.vertex([0.02,0.5,0])
viz.vertex([0.02,0.02,0])
viz.vertex([0.9,0.02,0])
viz.vertex([0.9,0.5,0])
viz.vertex([0.3,0.5,0])
viz.endLayer(viz.SCREEN)

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

def mytimer(num):
    if num == TALK:
        #Animate the avatar talking
        face.mouthPos += face.talkSpeed * viz.elapsed()
        if face.mouthPos > 1.0 and face.talkSpeed > 0:
            face.talkSpeed *= -1
        elif face.mouthPos < 0.0 and face.talkSpeed < 0:
            face.talkSpeed = GetTalkSpeed()
        face.setMorph(5,face.mouthPos)
        viz.starttimer(TALK,0.01)
    elif num == SHRINK:
        #Stop the avatar from talking
        viz.killtimer(TALK)
        #Shrink the text
        info.shrink()

def mynetwork(message):
    #Set the text display to the message text
    info.message(message[2])
    #Set the title of the info
    info.title('Message from: '+message[0])
    #Show the message
    info.expand()
    #Kill all shrink timers
    viz.killtimer(SHRINK)
    #Calculate talking time
    talktime = max(3.0,len(message[2]) / 10.0)
    #Start a timer to shrink the text
    viz.starttimer(SHRINK,talktime)
    #Initialize the face talking variables
    face.mouthPos = 0.0
    face.talkSpeed = GetTalkSpeed()
    #Kill all talk timers
    viz.killtimer(TALK)
    #Start a timer to make the avatar talk
    viz.starttimer(TALK,0.01)

def mykeyboard(key):
    global SendText

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

    #Update the input text
    text.message(SendText)

#Create callbacks for timer, network, and keyboard events
viz.callback(viz.TIMER_EVENT,mytimer)
viz.callback(viz.NETWORK_EVENT,mynetwork)
viz.callback(viz.KEYDOWN_EVENT,mykeyboard)

def sendData():
    global SendText

    #Send the current text to the mailbox
    TargetMailBox.send(SendText)
    #Reset the text to nothing
    SendText = ''
    text.message(SendText)

vizact.onbuttondown(sendButton, sendData)


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

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