Halloween Costume ideas 2015

OSC / Scratching Blender

In this post, I will talk about random stuff that is not necessarily related to Blender, however, it is in line toward my interest in understanding Blender Game Engine (BGE) and Game Programming with Python.

Or at least the concept of interaction between languages and programs and the real-world mechanic.

MANY LANGUAGES => MANY MACHINES

In recent months, I have been dealing with some programming languages:
  • Python
  • Processing
  • ChucK
  • Scratch
I am also experimenting a little bit with the Raspberry Pi computing (Linux OS, small cheap affordable computer, but can be powerful).

I have not touched much of Arduino board yet, but maybe eventually I am heading toward that direction.

-----------------------------------------------------------------------------------------------------
UPDATE 2014.03.17:
I finally did get encounter with Arduino board and I learned that programming for Arduino is similar to Processing, except that we have more direct connection I/O (input and output) with this hardware microcontroller, which is cool!

Thanks to Powerhouse Museum and Thinker1 Arduino board, I am actually learning some basic foundations. James Oliver is the guy to ask about Thinker1, he is a teacher at ThinkSpace and really good at explaining things.

Anyways, I learn that since Arduino speak like Processing, we could use Processing language to load OSC related library, or you could also use the "serial". Serial is kind of the data being passed from hardware that the computer can then listen to. Arduino board like Thinker1 has Potentiometer (knob) and Button INPUT.

So, if it is in Blender, we could use Python that listen to "serial", if we want Arduino board to control whatever we want in Blender. Or, we could use OSC to control the Arduino board (LED, sound, etc).
-----------------------------------------------------------------------------------------------------

Now, one thing cool about Raspberry Pi is its portability (it is also economic). And moreover, inside the small computer, it has built in Python and Scratch program. It is not capable of running Blender yet, because of the system limitation, maybe in the near future?

All those are not directly related to Blender. However, it is about to get a little bit more interesting once one figure out how to bridge and link all of them together.

Imagine:
- Controlling LED light(s) using Blender
- Controlling Robots motion using Blender.
- Home automation using Blender (if you really want to) --- of course, nowadays is more popular for people to control anything using their smartphone touch or voice assistant like SIRI. But nothing can stop us to use Blender Game interface to control the outside world :)

Well, the above example is probably a bit out of reach, but not impossible! Try something easier first:
- Triggering procedural sound in ChucK using Blender.
- Controlling Blender Game Engine objects, using arbitrary controller
- Using Blender simulation to control real world robot and object.

Again, since I am not from programming, computer science or electronic engineering background, all these are new discovery. I have been always curious on how one program can talk to another program, or how one machine can control another machine. So, if you never program before, we are on the same boat!

Do not be afraid with programming.

Saying that, bridging virtual to real world seems to be something that needs to be explored more and it is going to be a lot easier, only if everyone got the access and tools to do so. Very exciting areas to explore but there are some challenges, no doubt!

BABY STEPS, BABY TALKS

I am very new to OSC (Open Sound Controller) and actually to MIDI controller. Kind of heard about them but not until today that I am really using it.

I am really just a baby in this area. In fact, I feel like in the "kindergarten". However, I remember John Maeda said something like this:
"I’d rather be green and growing than ripe and ready to rot"
http://scholarlykitchen.sspnet.org/2009/09/23/john-maeda-and-the-art-of-staying-green-and-growing/

(John Maeda is one of my hero inspiration.)

With OSC setup, we could use it to control not only sound, but passing the data to drive other things. As much as I am excited when little thing works (and failed), this kind of Bridging stuff can be a bit complicated to setup. But not impossible, just need to follow the steps.

This video kind of talk about OSC in easy to understand manner.
https://www.youtube.com/watch?v=jQD1S27f3dU

I really started with Processing, ChucK, and Scratch as a test bed since those programs already have easy way to use the OSC protocol.

So, the first few steps, I manage to do this "TALK" between apps:
1. iPad => MIDI Controller => Processing => visual trigger
2. iPad => MIDI Controller => ChucK => sound trigger
3. iPad => TouchOSC app => Scratch => trigger some animation, action, sound

I don't need to get into details of all those, but basically, it is quite interesting when we are able to TRIGGER ACTION from and within app and external hardware/controller.

Scratch is the easiest to implement and execute. There are 2 versions of Scratch: 1.4 and newer 2.0. I use the older one:
http://scratch.mit.edu/scratch_1.4/

So, I will use both SCRATCH and BLENDER to help us understand this whole OSC business.

This is one of my inspirator: "How BGE and Scratch can inline"
http://www.youtube.com/watch?v=nCFVJYpz_vk

With Blender, I am sure someone out there is already doing some works related to OSC and automation. And since Blender speaks Python, there must be Python library we can use.

For most of these experimentation, I realized how often we still need to be dependent on someone else's code (more experienced coder) and previous research.

I guess this is totally fine, we really do not need to unnecessary re-invent the wheel, as long we give credits back to the coder and understand what is happening to then use it for our own goal and usage. This is indirect collaboration and totally cool.

BRIDGING BGE, PYTHON and OSC

We know that Blender speaks Python 3.3 and BGE (Blender Game Engine) can do real-time and dynamic value changing and triggering things. So... this is exactly the topic and keyword I use when researching using Google. A little bit advanced topic, we need to tackle it step by step.

THE CHALLENGE:
Make Blender to talk to Scratch using Python.
http://wiki.scratch.mit.edu/wiki/Remote_Sensor_Connections

BLENDER PYTHON => OSC => SCRATCH
(not yet BGE, but eventually go that way).

Let's see how this journey actually goes...

1. Scratch bridge via OSC:
Scratch has built in Remote Sensor and this means, as long we can create OSC bridge from external, we can trigger things inside Scratch, easily.
http://www.kasperkamperman.com/blog/scratch-osc-bridge/

I tried using iPad TouchOSC app following the above method and it works. The "Scratch OSC Bridge" program, which is created in Processing (Java) by KasperKamperman is a handy little helper.

2. Scratch can communicate via Python 2 and 3 and OSC (indirect method)
We always need to be aware of the Python Library we use, to make sure the version will work with Blender Python 3.3.

Scratch and Python 2.7
http://wiki.scratch.mit.edu/wiki/Communicating_to_Scratch_via_Python_with_a_GUI

Scratch and Python 3.3 (didn't work as smoothly)

I thought that I could just copy paste and use the Python 3.3 script inside Blender to send message to Scratch. Somehow, it failed.

So, I am using a different Python Library, luckily below works, otherwise I am stucked.

4. Python scripting can help with OSC sending and receiving stuff.
Python is really the ULTIMATE KEY here, when we want to talk to and from Blender to the outside app or external hardware. OSC is also the SECOND KEY. We want to be able to send message to specific ADDRESS and PORT.

Dealga McArdle (Blender Scripting blog master) gave me a really nice hint: Try Python OSC Library for Python 3.3.
https://pypi.python.org/pypi/python-osc#downloads

>> INSTALLING 3RD PARTY EXTERNAL PYTHON PACKAGE AND MODULE
I have probably touched this topic at some point, but just to refresh again. If you want to bring in 3rd party Python Library from outside to be used inside Blender, simply place it here:
C:\Program Files\Blender Foundation\Blender\2.69\python\lib\site-packages

So, remember: site-packages is the folder where you want to put outside Python script.

Restart Blender, and then Blender can then source and import the Python library (module) as needed.
from blah import blah

So, I tried and I manage to make Blender talks to Scratch, receiving OSC Path and Message.

Thanks Dealga for example code, this is the original code (I think Dealga is playing with Pure Data or ChucK)
https://gist.github.com/zeffii/7264092

EXAMPLE CODE: (Blender talk to Scratch to randomly move the cat in X and Y)
import time
import random
from pythonosc import osc_message_builder
from pythonosc import udp_client

tick_duration = 0.1

ip = "127.0.0.1"
port = 8000
client = udp_client.UDPClient(ip, port)
osc_msg = osc_message_builder.OscMessageBuilder

for j in range(50):
    value_a = random.randrange(-240,240)
    value_b = random.randrange(-180,180)
    value_c = 3
    
    message_out_list = [value_a, value_b, value_c]
    
    msg = osc_msg(address = "/BLAH")
    for i in message_out_list:
        msg.add_arg(i)

    print(message_out_list)
    msg = msg.build()
    client.send(msg)
    time.sleep(tick_duration)

print("-" * 50)


SCRATCH CONTROLLING ROBOT...

If Scratch ables to control Robot, Blender can too...
http://www.dexterindustries.com/BrickPi/program-it/scratch/
http://www.dexterindustries.com/BrickPi/

Python...

If there is Python, we can probably control it using Blender Game Engine (BGE).

Something like this:

But how?

TO BE CONTINUED .....

LINKS

Blender and Scratch
http://www.youtube.com/watch?v=nCFVJYpz_vk

Blender BGE Python Tutorial
Kris Occhipinti
http://www.youtube.com/watch?v=g6CjnC1T0lA

http://www.surreal.asturnazari.es/node/178

PureData and Blender: (In French language)
http://wiki.labomedia.org/index.php/Communication_entre_Pure-data_et_Blender_en_OSC

Post a Comment

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget