This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-12-27 19:42:04

pquiza
Scratcher
Registered: 2010-05-25
Posts: 7

A Python module for Scratch

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:

Code:

>>> 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:

Code:

>>> s.broadcast("Hello, Scratch!")
>>>

The broadcast() method can also accept a list of messages.

Send a sensor update to Scratch:

Code:

>>> 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:

Code:

>>> 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  smile .

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

 

#2 2011-12-27 19:59:48

nickbrickmaster
Scratcher
Registered: 2010-02-02
Posts: 500+

Re: A Python module for Scratch

Cheese! I was making one of these for Konnected. I still will though. I just thought I was the first to do so.


Ask me what I'm doing, wait an hour than roll a die, if it's 4-6, I'm playing Skyrim, if it's 1, I'm eating, if it's 2-3 I'm programming.
Steam: nickbrickmaster | RotMG: PwnThemAll | Minecraft: nickbrickmaster | League Of Legends: BaneOfTitans

Offline

 

#3 2011-12-27 20:41:06

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: A Python module for Scratch

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.

Offline

 

#4 2011-12-27 22:08:50

pquiza
Scratcher
Registered: 2010-05-25
Posts: 7

Re: A Python module for Scratch

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

 

#5 2012-03-14 20:58:41

jet_pilot
Scratcher
Registered: 2008-10-02
Posts: 35

Re: A Python module for Scratch

How did you connect it to BYOB? It works perfectly for scratch but I don't know how to connect it to BYOB.


laser314's alt account.

Offline

 

#6 2012-04-13 15:43:37

pquiza
Scratcher
Registered: 2010-05-25
Posts: 7

Re: A Python module for Scratch

In BYOB under the "Share" menu, click "Host a mesh" and it should be able to connect and communicate with BYOB.

Offline

 

#7 2012-12-28 14:14:03

pquiza
Scratcher
Registered: 2010-05-25
Posts: 7

Re: A Python module for Scratch

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

Code:

# pip install scratchpy

Or easy_install

Code:

# 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

 

#8 2012-12-29 22:29:38

pquiza
Scratcher
Registered: 2010-05-25
Posts: 7

Re: A Python module for Scratch

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

 

#9 2012-12-30 09:01:07

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: A Python module for Scratch

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!


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#10 2012-12-30 13:05:10

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: A Python module for Scratch

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.  big_smile


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#11 2012-12-31 01:26:42

pquiza
Scratcher
Registered: 2010-05-25
Posts: 7

Re: A Python module for Scratch

Haha, that's crazy cool! Didn't think of that either. Sounds like a good idea.

Offline

 

#12 2012-12-31 02:38:10

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: A Python module for Scratch

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.  big_smile

tongue  I love your attitude.


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#13 2012-12-31 03:54:59

nathanprocks
Scratcher
Registered: 2011-04-14
Posts: 1000+

Re: A Python module for Scratch

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.  big_smile

Excellent for programming languages made in Scratch!


http://carrot.cassiedragonandfriends.org/Scratch_Signature/randomsig.php
http://trinary.site40.net/images/scratchrank.php?username=nathanprocks&display=small

Offline

 

#14 2012-12-31 08:30:01

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: A Python module for Scratch

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.  big_smile

tongue  I love your attitude.

Indeed.  tongue

nathanprocks wrote:

Excellent for programming languages made in Scratch!

...right, what should the syntax be?  big_smile


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#15 2012-12-31 08:44:52

nathanprocks
Scratcher
Registered: 2011-04-14
Posts: 1000+

Re: A Python module for Scratch

blob8108 wrote:

Hardmath123 wrote:

blob8108 wrote:


That is a completely ridiculous idea! I can't believe it's never occurred to me before.  big_smile

tongue  I love your attitude.

Indeed.  tongue

nathanprocks wrote:

Excellent for programming languages made in Scratch!

...right, what should the syntax be?  big_smile

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.


http://carrot.cassiedragonandfriends.org/Scratch_Signature/randomsig.php
http://trinary.site40.net/images/scratchrank.php?username=nathanprocks&display=small

Offline

 

#16 2013-01-01 01:00:13

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: A Python module for Scratch

nathanprocks wrote:

blob8108 wrote:

Hardmath123 wrote:


tongue  I love your attitude.

Indeed.  tongue

nathanprocks wrote:

Excellent for programming languages made in Scratch!

...right, what should the syntax be?  big_smile

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:

Code:

PRINT
"starting program...
LABELAS
#start
SET
$x
5
PRINT
$X
JUMPTO
#start

It's remarkably powerful, I calculated pi to 10 decimal places!  big_smile

It's called QuickSilver.


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#17 2013-01-01 05:38:32

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: A Python module for Scratch

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...  tongue


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#18 2013-01-01 05:45:18

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: A Python module for Scratch

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!  tongue


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

Board footer