Whilst developing my BSc dissertation in Audio Engineering I was working with Acoustic Simulation in virtual environments. The short story being I have a First Person level in Blender with different dimension rooms with different textures in and with the use of filters and room mode calculations create a dynamic model that constantly updates. The feedback to the user is an acoustic environment that adjusts to whatever room the player/avatar/user is in. (you get the jist of the project)
Now to the tutorial. I needed to send the information of the following to PureData:
- Player Position
- Surface hitPosition on X, Y & Z ± axis (gauging distance from player to wall)
- Surface texture
Let's work with one of these tasks for this tutorial, the player position;
Within Blender, we create an Always sensor (in the Logic Editor) and connect it to a Python script Controller. Open up the Text Editor and create a new file, name it along the lines of "WorldPosition.py". This is going to be our Python script that will SEND data from Blender to PureData. Below is the code for getting the avatars/players worldPosition within Blender.
—————————————————————————————————
import socket
Game2 = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
cont = bge.logic.getCurrentController() # who runs the script?
own = cont.owner # KX_GameObject
Location=own.worldPosition # get position vector
add='/3'
dx=str(Location[0]) # convert to string
dy=str(Location[1])
dz=str(Location[2])
z=add+' '+dx+' '+dy+' '+dz # make a list
y=str.encode(z) # encode so sendto() understands the message
Game2.sendto(y,('localhost',2001))
—————————————————————————————————
NOTES: firstly I thank whomever's source code it is I built my code from, I cannot recall from the top of my head but I developed my code from theirs.
2001 refers to the local Port in which this script will be sending its data out of, Lets say our worldPosition is as follows: X=4, Y=9 & Z=6. The current message being sent out of Blender is a stream of bytes that translate to: "/3 4 9 6"
NOW we open up a new canvas in PureData and build patch found attached to this post (saves me having to draw it out in text).
Note that we have imported [mrpeach] and [moocow], mrpeach is the library that contains the [udpreceive] objects and OSC functions allowing PureData to receive the data that Blender is putting on port 2001. moocow enables us to convert the stream of bytes into useable information via the [bytes2any] object.
Although i'm not 100% on why /3 is required it helps route the string from Blender into our [unpack f f f] and hey presto we have the player/avatar worldPosition coordinates X,Y & Z updating in real time within PureData (make sure you've hit 'p' to begin the game)
—————————————————————————————————
—————————————————————————————————
I hope that this helps some if they too are looking to communicate between bge and PureData. I'll gladly answer any queries because I believe my writing is making more sense to me than I'm portraying.
thanks guys and hope it helps
Rob