#! /usr/bin/env python # This tutorial connects us to a verse server and asks the server for any # nodes, it can also create a node if you set the other accept connect # callback. First we define a call back for the network to update if our # connection gets accepted. import verse as v #----- these two callbacks can be used if you want to create some data ------ # this one is for creating a node def cb_send_connect_accept(avatar, address, host_id): print "cb_send_connect_accept: we just go connected to a verse server!" v.send_node_create(~0, v.OBJECT, avatar); v.send_node_create(~0, v.GEOMETRY, avatar); v.send_node_create(~0, v.MATERIAL, avatar); v.send_node_create(~0, v.BITMAP, avatar); mask = 0 for i in range(0, v.NUM_TYPES): mask = mask | (1 << i) v.send_node_index_subscribe(mask) # second we define a call back for the network to update if it wants to informs # us of any nodes def cb_send_node_create_create(node_id, type, owner): # printing the node id print "We found a node! it is of id = " + str(node_id) # did we realy create this node or did some one else? if owner == v.OWNER_MINE: if type == v.OBJECT: print "its a object node!" # lets set the node to emit light! v.send_o_light_set(node_id, 1, 1, 1) # right now we have not stored any ids of any nodes to point to # if we did we could use v.send_o_link_set to set some links # to other nodes elif type == v.GEOMETRY: print "its a geometry node!" # lets create some layers! how about two layers to store uv data in? v.send_g_layer_create(node_id, ~0, "map_u", v.G_LAYER_POLYGON_CORNER_REAL, 0, 0) v.send_g_layer_create(node_id, ~0, "map_v", v.G_LAYER_POLYGON_CORNER_REAL, 0, 0) # lets get wild and create 3 layers to store a color for each vertex too v.send_g_layer_create(node_id, ~0, "col_r", v.G_LAYER_VERTEX_REAL, 0, 0) v.send_g_layer_create(node_id, ~0, "col_g", v.G_LAYER_VERTEX_REAL, 0, 0) v.send_g_layer_create(node_id, ~0, "col_b", v.G_LAYER_VERTEX_REAL, 0, 0) # lets create a few vertices # layer 0 is always the base vertex layer v.send_g_vertex_set_real64_xyz(node_id, 0, 0, 1, 0, 0) v.send_g_vertex_set_real64_xyz(node_id, 1, 0, 0, 1, 0) v.send_g_vertex_set_real64_xyz(node_id, 2, 0, 0, 0, 1) # and why not create a polygon! # layer 1 is always the base polygon layer, the fourth corner is # set to ~0 to create a triangle v.send_g_polygon_set_corner_uint32(node_id, 0, 1, 0, 1, 2, ~0) elif type == v.MATERIAL: print "its a material node!" # lets create a few fragments fragment = v.MFOutput() # the front of this material should start here fragment.front = 4; # the back of this material should black fragment.back = 0 fragment.type = "color" v.send_m_fragment_create(node_id, ~0, v.M_FT_OUTPUT, fragment) fragment = v.MFColor() fragment.red = 1 fragment.green = 0.1 fragment.blue = 0.1 v.send_m_fragment_create(node_id, ~0, v.M_FT_COLOR, fragment) # i want my material to be influenced by the incomming light so lets # create a light fragment fragment = v.MFLight() # we are not going to use any of that ... fragment.brdf = 0 fragment.brdf_r = "" fragment.brdf_g = "" fragment.brdf_b = "" # the surface is perfectly smooth fragment.normal_falloff = 0 # this material is lit by both direct and ambient light fragment.type = v.M_LIGHT_DIRECT_AND_AMBIENT v.send_m_fragment_create(node_id, ~0, v.M_FT_LIGHT, fragment) # ok lets now mix the red color whit the incomming light! fragment = v.MFBlender() # the red color fragment.data_a = 2 # the light fragment.data_b = 3 # this blend mode does not use control fragment.control = 0 # we want the two to be multiplyed together fragment.type = v.M_BLEND_MULTIPLY v.send_m_fragment_create(node_id, ~0, v.M_FT_BLENDER, fragment) # the material example above is not fool prof, to be crrect you # should not as i have done here assume that the fragments will get # the numbers that i have assigned to them, It may work in this # implementation but is wery good. what you shuld do is send the # fragments out and then wait for them to come back to see what is # they got, and after that you can create the fragments that points # to them. This requies quite a lot more round trip and would be # too hard to folow for a simple tutorial as this one. elif type == v.BITMAP: print "its a bitmap node!" # cool we have a bitmap lets first set the size of it, a 2D 256*256 # will be fine v.send_b_dimensions_set(node_id, 256, 256, 1) # i will need 3 chanels (rgb) and just to be cool lets have them # be 32 bit floats v.send_b_layer_create(node_id, ~0, "col_r", v.B_LAYER_REAL32) v.send_b_layer_create(node_id, ~0, "col_g", v.B_LAYER_REAL32) v.send_b_layer_create(node_id, ~0, "col_b", v.B_LAYER_REAL32) # this is the tile we are going to send (tiles are allways 2D) data = [] for i in range(0, v.B_TILE_SIZE * v.B_TILE_SIZE): # lets create some stripes (The air near my fingers) data.append(float(i % 2)) v.send_b_tile_set(node_id, 0, 0, 0, 0, v.B_LAYER_REAL32, data) # again we asumed that one of the three layers would get id 0, # presumably red, but there is no way of knowing so if you write a # real app wait for the v.send_b_layer_create calls to be # confirmed from the host # ----- these two callbacks are to be used of you want to just listen ------- def cb_send_connect_accept_listen(avatar, address, host_id): print "cb_send_connect_accept: we just go connected to a verse server!\n" print "listing nodes:" mask = 0 for i in range(0, v.NUM_TYPES): mask = mask | (1 << i) v.send_node_index_subscribe(mask) def cb_send_node_create_listen(node_id, type, owner): print "We found a node! it is of id = " + str(node_id) if type == v.OBJECT: print "its a object node!" elif type == v.GEOMETRY: print "its a geometry node!" elif type == v.MATERIAL: print "its a material node!" elif type == v.BITMAP: print "its a bitmap node!" elif type == v.TEXT: print "its a text node!" elif type == v.PARTICLE: print "its a particle node!" elif type == v.CURVE: print "its a curve node!" # ---------------------------- the main function --------------------------- def main(): server_address = "localhost" # first we set the callbacks v.callback_set(v.SEND_CONNECT_ACCEPT, cb_send_connect_accept) v.callback_set(v.SEND_NODE_CREATE, cb_send_node_create_create) # set these callbacks if you just want to listen # v.callback_set(SEND_CONNECT_ACCEPT, cb_send_connect_accept_listen) # vll_callback_set(SEND_NODE_CREATE, cb_send_node_create_listen) print "all callbacks set" # now we connect to the server connections = v.send_connect("My_name", "My_password", server_address, 0) print "attempted to connect to server " + server_address # we set the session we want to update (verse can handle more then one!) v.session_set(connections) print "session set" # (almost) infinit loop i = 0 while i < 1000: # listen to the network (wait no more then 10000 microseconds) and update the callbacks v.callback_update(10000); #if v.session_get_size() > 5000: # v.send_connect_terminate("localhost", "Im quiting becouse you dont return my calls! (what ever happend to us?)") # print "Im quiting becouse you dont return my calls! (what ever happend to us?)" # v.session_destroy(connections) # break i += 1 main()