[Verse-dev] PyVerse 0.1

Brecht Van Lommel blendix@pandora.be
29 Dec 2003 16:46:18 +0000


--=-Ff4RQXq1j7177j3OHynF
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi,

PyVerse 0.1 has been released today. You can download it from:
http://projects.blender.org/project/showfiles.php?group_id=41
There's a source package and windows binaries for python 2.2 and python
2.3. Linux (and Mac?) users should compile the source package using this
single command: python setup.py build.

This version supports all verse functions and structs. Any missing
features are not intentional, so notify me if there's anything not
working or missing. At this time, most functions are untested.

The python bindings differ only slightly from the c api. The most
notable changes are:

- new structs (python classes) were defined for the material fragments.
e.g. instead of VMatFrag.output(), you can create a new output fragment
using VMFOutput().
- VNTag *tag in verse_send_tag_create doesn't expect a union but for
example a python boolean, number, string, 3-tuple (vector), string
(blob) or VNTAnimation class.
- verse_send_o_method_create instead of (uint8 param_count, VNOParamType
*param_types, char **param_names), expects a ((name, type), (name,
type), ...) tuple
- verse_send_o_method_send instead of (void *params) expects a (value,
value, ...) tuple
- the verse_(un)pack_method_call functions are not exposed to python, as
they are automatically invoked by the python bindings
- callbacks are set using constants, like this:
verse_callback_set(SEND_ACCEPT_CONNECT, cb_accept_connect)
- there is no (void *user_data) pointer in the verse_callback_set nor in
the callback functions
- array members of classes cannot be accessed like:
fragment.matrix[6] = 0.3
you must do this:
matrix = fragment.matrix
matrix[6] = 0.3
fragment.matrix = matrix

You can see most of these changes in the tutorial (in attachment), which
is a port of the c tutorial included with verse.

You can expect a demo using verse in the blender game engine and
probably some more scripts later this week. A higher level api written
in python will probably also follow.

Happy python coding,
Brecht

--=-Ff4RQXq1j7177j3OHynF
Content-Disposition: attachment; filename=tutorial.py
Content-Type: text/x-python; name=tutorial.py; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

#! /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.

from verse import *

#----- these two callbacks can be used if you want to create some data ----=
--
my_avatar =3D -1

# this one is for creating a node
def cb_send_accept_connect(avatar, address):
	global my_avatar
	my_avatar =3D avatar

	print "cb_send_accept_connect: we just go connected to a verse server!"

	verse_send_node_create(-1, V_NT_OBJECT, avatar);
	verse_send_node_create(-2, V_NT_GEOMETRY, avatar);
	verse_send_node_create(-3, V_NT_MATERIAL, avatar);
	verse_send_node_create(-4, V_NT_BITMAP, avatar);

	mask =3D 0
	for i in range(0, V_NT_NUM_TYPES):
		mask =3D mask | (1 << i)
	verse_send_node_list(mask)

# second we define a call back for the network to update if it wants to inf=
orms=20
# us of any nodes
def cb_send_node_create_create(node_id, type, owner_id):
	global my_avatar

	# printing the node id
	print "We found a node! it is of id =3D " + str(node_id)

	# did we realy create this node or did some one else?
	if owner_id =3D=3D my_avatar:
		if type =3D=3D V_NT_OBJECT:
			print "its a object node!"
			# lets set the node to emit light!
			verse_send_o_set_light(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 verse_send_o_link_set to set some links
			# to other nodes

		elif type =3D=3D V_NT_GEOMETRY:
			print "its a geometry node!"
			# lets create some layers! how about two layers to store uv data in?
			verse_send_g_layer_create(node_id, 1, "tex_u", VN_G_LAYER_POLYGON_CORNER=
_REAL64, 0, 0)
			verse_send_g_layer_create(node_id, 2, "tex_v", VN_G_LAYER_POLYGON_CORNER=
_REAL64, 0, 0)
			# lets get wild and create 3 layers to store a color for each vertex too
			verse_send_g_layer_create(node_id, 3, "col_r", VN_G_LAYER_VERTEX_REAL64,=
 0, 0)
			verse_send_g_layer_create(node_id, 4, "col_g", VN_G_LAYER_VERTEX_REAL64,=
 0, 0)
			verse_send_g_layer_create(node_id, 5, "col_b", VN_G_LAYER_VERTEX_REAL64,=
 0, 0)
			# lets create a few vertices
			# layer one is always the base vertex layer
			verse_send_g_vertex_set_real64_xyz(node_id, 0, 0, 1, 0, 0)
			verse_send_g_vertex_set_real64_xyz(node_id, 1, 0, 0, 1, 0)
			verse_send_g_vertex_set_real64_xyz(node_id, 2, 0, 0, 0, 1)
			# and why not create a polygon!
			# layer two is always the base polygon layer, the fourth corner is
			# set to -1 to create a triangle
			verse_send_g_polygon_set_corner_uinteger32(node_id, 0, 1, 0, 1, 2, -1)

		elif type =3D=3D V_NT_MATERIAL:
			print "its a material node!"
			# lets create a few fragments
			fragment =3D VMFOutput()
			# the front of this material should start here
			fragment.front =3D 4;
			# the back of this material should black
			fragment.back =3D 0
			fragment.type =3D "color"
			verse_send_m_fragment_create(node_id, 1, VN_M_FT_OUTPUT, fragment)

			fragment =3D VMFColor()
			fragment.red =3D 1
			fragment.green =3D 0.1
			fragment.blue =3D 0.1
			verse_send_m_fragment_create(node_id, 2, VN_M_FT_COLOR, fragment)

			# i want my material to be influenced by the incomming light so lets
			# create a light fragment
			fragment =3D VMFLight()
			# we are not going to use any of that ...
			fragment.brdf =3D 0
			fragment.brdf_r =3D ""
			fragment.brdf_g =3D ""
			fragment.brdf_b =3D ""
			# the surface is perfectly smooth
			fragment.normal_falloff =3D 0
			# this material is lit by both direct and ambient light
			fragment.type =3D VN_M_LIGHT_DIRECT_AND_AMBIENT
			verse_send_m_fragment_create(node_id, 3, VN_M_FT_LIGHT, fragment)
				=09
			# ok lets now mix the red color whit the incomming light!
			fragment =3D VMFBlender()
			# the red color
			fragment.data_a =3D 2
			# the light
			fragment.data_b =3D 3
			# this blend mode does not use control
			fragment.control =3D 0
			# we want the two to be multiplyed together
			fragment.type =3D VN_M_BLEND_MULTIPLY
			verse_send_m_fragment_create(node_id, 4, VN_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=20
			# 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 =3D=3D V_NT_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
			verse_send_b_init_dimensions(node_id, 256, 256, 1)
			# i will need 3 chanels (rgb) and just to be cool lets have them
			# be 32 bit floats
			verse_send_b_layer_create(node_id, 1, "col_r", VN_B_LAYER_REAL32)
			verse_send_b_layer_create(node_id, 2, "col_g", VN_B_LAYER_REAL32)
			verse_send_b_layer_create(node_id, 3, "col_b", VN_B_LAYER_REAL32)

			# this is the tile we are going to send (tiles are allways 2D)
			data =3D []
			for i in range(0,  VN_B_TILE_SIZE * VN_B_TILE_SIZE):
				# lets create some stripes (The air near my fingers)
				data.append(float(i % 2))

			verse_send_b_layer_set_tile(node_id, 0, 0, 0, 0, VN_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 verse_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_accept_connect_listen(avatar, address):
	print "cb_send_accept_connect: we just go connected to a verse server!\n"
	print "listing nodes:"
	mask =3D 0
	for i in range(0, V_NT_NUM_TYPES):
		mask =3D mask | (1 << i)
	verse_send_node_list(mask)

def cb_send_node_create_listen(node_id, type, owner_id):
	print "We found a node! it is of id =3D "  + str(node_id)
	if type =3D=3D V_NT_OBJECT:
		print "its a object node!"
	elif type =3D=3D V_NT_GEOMETRY:
		print "its a geometry node!"
	elif type =3D=3D V_NT_MATERIAL:
		print "its a material node!"
	elif type =3D=3D V_NT_BITMAP:
		print "its a bitmap node!"
	elif type =3D=3D V_NT_TEXT:
		print "its a text node!"
	elif type =3D=3D V_NT_PARTICLE:
		print "its a particle node!"
	elif type =3D=3D V_NT_CURVE:
		print "its a curve node!"

# ---------------------------- the main function --------------------------=
-

def main():
	server_address =3D "localhost"

	# first we set the callbacks
	verse_callback_set(SEND_ACCEPT_CONNECT,	cb_send_accept_connect)
	verse_callback_set(SEND_NODE_CREATE, cb_send_node_create_create)

	# set these callbacks if you just want to listen
	# verse_callback_set(SEND_ACCEPT_CONNECT, cb_send_accept_connect_listen)
	# vll_callback_set(SEND_NODE_CREATE, cb_send_node_create_listen)

	print "all callbacks set"
=09
	# now we connect to the server
	connections =3D verse_send_connect("My_name", "My_password", server_addres=
s)
	print "attempted to connect to server " + server_address

	# we set the session we want to update (verse can handle more then one!)
	verse_session_set(connections)=20
	print "session set"

	# (almost) infinit loop
	i =3D 0
	while i < 1000:=20
		# listen to the network (wait no more then 100 ms) and update the callbac=
ks
		verse_callback_update(100);=20
		if verse_session_get_size() > 5000:
			verse_send_terminate_connect("Im quiting becouse you dont return my call=
s! (what ever happend to us?)")
			print "Im quiting becouse you dont return my calls! (what ever happend t=
o us?)"
			verse_session_destroy(connections)
		i +=3D 1

main()


--=-Ff4RQXq1j7177j3OHynF--