Vizard 7 » Tutorials & Examples » Networking » Network communication
7.7

Tutorial: Network communication

This tutorial will teach you how to network Vizard worlds so that more than one user can inhabit the same environment. Networking also allows you to create systems in which a monitor can watch and manipulate a world remotely. Either way, two or more users located anywhere in the world can participate in the same virtual experience.

This content is discussed in more detail in the reference section.

Networking computers

For this demonstration we will make a simple world in which two users are represented by two robots in the middle of a maze. The movement and orientation data of each user will be sent to the other user to animate a robot in the other's world. To create this scenario, we will write a script that can run independently on the two separate computers. First let's set up the world:

 

If you have trouble getting the code in this tutorial to work, you can find the complete example script networking.py in the \tutorials\networking directory.

import viz
import vizact
import vizinput
import steve

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

# Use the steve module to represent the other user.
# You will actually have no representation of yourself on your own monitor.
player_matrix = viz.Matrix()
avatar = steve.Steve()
avatar.setTracker(player_matrix)

# Add the world
maze = viz.addChild('maze.osgb')

Now add the code that allows the computers to communicate. Think of this communication as a mail system. First, you'll set up a mailbox to send mail. All Vizard needs to know is the name of the recipient computer.

#Use a prompt to ask the user the network name of the other computer.
target_machine = vizinput.input('Enter the name of the other machine').upper()

#Add a mailbox from which to send messages. This is your outbox.
target_mailbox = viz.addNetwork(target_machine)

Now add the information that you are going to put into that mailbox. We will use a timer to send messages so that the recipient has our current location and orientation.

def sendPosition():

    #Retrieve current transform of viewpoint
    mat = viz.MainView.getMatrix()

    #Send position/rotation to target network object
    target_mailbox.send(action=updatePlayer, quat=mat.getQuat(), pos=mat.getPosition())

# Start a timer that sends out data over the network every frame
vizact.ontimer(0,sendPosition)

That's all it takes to send the information.

 

Now, add the code to receive and process the data using a Vizard event function designed for networking. This event will be running all the time in the background of your world, receiving any messages from other computers:

def updatePlayer(e):
    player_matrix.setPosition(e.pos)
    player_matrix.setQuat(e.quat)

# Listens for any incomming messages
def onNetwork(e):
    if e.sender.upper() == target_machine:
        e.action(e)

# Register network to listen from incomming messages
viz.callback(viz.NETWORK_EVENT, onNetwork)

In the code above, onNetwork(e) is called when event data is received. If the machine that sent the data matches the target machine the following line of code gets executed:

e.action(e)

Here e.action is updatePlayer, the value specified in the action attribute of the send command. The event data is then passed to updatePlayer where Steve's transform matrix gets updated.

 

If you have another computer running Vizard on your network, you are now ready to link the two worlds. Just find out the networks name for both computers and give it a go.

Networking computers

Text messaging