Hi everyone,
I wrote a simple Python module that allows Python to send and receive broadcasts messages and sensor updates. The code is located here: https://github.com/pilliq/scratchpy.
Here are some examples to run from the Python interactive shell. Make sure remote sensor connections are enabled in Scratch, or for BYOB users, select Share->Host Mesh.
Ensure that scratch.py is in your current directory, or in Python's module search path. Then connect Python to Scratch:
>>> import scratch >>> s = scratch.Scratch(host='localhost') >>>
The above code creates a connection to Scratch on your local machine and saves that connection to the variable 's'. Now you can call different methods on 's' like the following:
Broadcast a message to Scratch:
>>> s.broadcast("Hello, Scratch!") >>>
The broadcast() method can also accept a list of messages.
Send a sensor update to Scratch:
>>> s.sensorupdate({'foo': 'bar', 'hello': 'world'}) >>>
This creates two new sensors in Scratch: foo and world. They can be found in the "Sensing" category in the dropdown menu of the "sensor value" block.
First, create a variable named foo. The receive a message from Scratch:
>>> s.receive() {'broadcast': [], 'sensor-update': {'foo': '0'}} >>>
The receive() method blocks until data is received from Scratch. It returns a dictionary that contains a list of broadcast messages received and another dictionary that contains global variables and their updated values.
Note: the sensorupdate() method and the key 'sensor-update' in the dictionary returned by receive() deal with different data in Scratch. sensorupdate() updates sensors in the "Sensing" category, while the 'sensor-update' key refers to variables in the "Variables" category. I'm not sure if there is a way to update variables in the "Variables" category outside of Scratch.
Right now the module can't handle variable names and values that contain double quotes ("). This is because of my regular expresssion; I need a better one .
That covers most of the functionality. I've used this module at Scratch Day @ Packer where my team and I connected BYOB to a bunch of home appliances and allowed people to control the devices over a mesh. It was exciting to see our lamps blink hundreds of times a second, and people crashing the two roombas we had into each other.
Cheers,
Phillip Quiza
Offline
Cheese! I was making one of these for Konnected. I still will though. I just thought I was the first to do so.
Offline
Wow, amazing! Does it support being able to parse multiple broadcasts being sent? If multiple broadcasts are sent within 0.2 seconds, then it puts them together like so:
0000broadcast "test", broadcast "test"
So when parsing, a script may not work correctly.
Offline
Magnie wrote:
Wow, amazing! Does it support being able to parse multiple broadcasts being sent? If multiple broadcasts are sent within 0.2 seconds, then it puts them together like so:
Code:
0000broadcast "test", broadcast "test"So when parsing, a script may not work correctly.
Yes, it supports multiple broadcasts and multiple variable updates also.
Offline
Hi everyone,
I updated and rewrote most of the code for the module. This update fixed a lot of bugs including the double quote parsing bug I mentioned above, and some critical bugs when reading and writing to Scratch. I also included more documentation for the module. You can find the code in the same place http://bit.ly/RWr96u
Also, you can now install the module using pip
# pip install scratchpy
Or easy_install
# easy_install scratchpy
(you probably need to use sudo for both of them).
If you have any problems, issues, or questions, you can post them to the issues page for the project on github https://github.com/pilliq/scratchpy/issues
Thanks again for using this!
Phillip
Last edited by pquiza (2012-12-29 22:27:15)
Offline
Oh, and I also forgot to mention that the receive() function has changed considerably. Basically it returns a tuple instead of a dictionary. You can read more about it here https://github.com/pilliq/scratchpy#receiving
Offline
Cool, this looks quite interesting! You can do awesome stuff, like draw an image file with the pen + PyPng or something; or getting access to filesystem and other goodies from Scratch.
You should try to integrate it with Kurt, which is a Python library to parse Scratch projects. Then you should be able to dynamically generate a Scratch project from another Scratch project — meta-Scratching!
Offline
Hardmath123 wrote:
You should try to integrate it with Kurt, which is a Python library to parse Scratch projects. Then you should be able to dynamically generate a Scratch project from another Scratch project — meta-Scratching!
That is a completely ridiculous idea! I can't believe it's never occurred to me before.
Offline
blob8108 wrote:
Hardmath123 wrote:
You should try to integrate it with Kurt, which is a Python library to parse Scratch projects. Then you should be able to dynamically generate a Scratch project from another Scratch project — meta-Scratching!
That is a completely ridiculous idea! I can't believe it's never occurred to me before.
I love your attitude.
Offline
blob8108 wrote:
Hardmath123 wrote:
You should try to integrate it with Kurt, which is a Python library to parse Scratch projects. Then you should be able to dynamically generate a Scratch project from another Scratch project — meta-Scratching!
That is a completely ridiculous idea! I can't believe it's never occurred to me before.
Excellent for programming languages made in Scratch!
Offline
Hardmath123 wrote:
blob8108 wrote:
Hardmath123 wrote:
You should try to integrate it with Kurt, which is a Python library to parse Scratch projects. Then you should be able to dynamically generate a Scratch project from another Scratch project — meta-Scratching!
That is a completely ridiculous idea! I can't believe it's never occurred to me before.
I love your attitude.
Indeed.
nathanprocks wrote:
Excellent for programming languages made in Scratch!
...right, what should the syntax be?
Offline
blob8108 wrote:
Hardmath123 wrote:
blob8108 wrote:
That is a completely ridiculous idea! I can't believe it's never occurred to me before.I love your attitude.
Indeed.
nathanprocks wrote:
Excellent for programming languages made in Scratch!
...right, what should the syntax be?
I don't know. I have made a programming language in Scratch before, but it is very slow because it reads each line in a list one character at a time. It works fine in turbo mode though. The commands were something like "Sprite1.Goto#10&10;", where "Sprite1.Goto" is broadcasted and the args such as x and y are store in temporary variables.
Offline
nathanprocks wrote:
blob8108 wrote:
Hardmath123 wrote:
I love your attitude.Indeed.
nathanprocks wrote:
Excellent for programming languages made in Scratch!
...right, what should the syntax be?
I don't know. I have made a programming language in Scratch before, but it is very slow because it reads each line in a list one character at a time. It works fine in turbo mode though. The commands were something like "Sprite1.Goto#10&10;", where "Sprite1.Goto" is broadcasted and the args such as x and y are store in temporary variables.
I made a more complex one which was a low-level line-by-line thing, something like this:
PRINT "starting program... LABELAS #start SET $x 5 PRINT $X JUMPTO #start
It's remarkably powerful, I calculated pi to 10 decimal places!
It's called QuickSilver.
Offline
Hardmath123 wrote:
It's called QuickSilver.
Right, challenge: compile it to scratchblocks code, then feed each it line-by-line through scratchpy into Kurt's compiler...
Offline
Well, Scratch doesn't have label/jump, so you'd have an awful lot of broadcasts. But other than that, I really don't see why not!
Offline