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

#76 2012-08-20 07:46:23

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

Re: kurt — Scratch file format Python library

I think I've nearly fixed converting text to scratch blocks!

I'm working on a compiler that takes a folder structure of scripts and images and outputs an .sb file. You can decompile a project into its parts, edit the scripts in a text editor (and images in an image editor!), then recompile  smile

Gratuitous screenshot:
http://i.imgur.com/18Pvc.png

(It's not very exciting, I'll admit  tongue  — but editing the text file and then recompiling does actually update the scratch file!)


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

Offline

 

#77 2012-08-20 07:48:29

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

Re: kurt — Scratch file format Python library

blob8108 wrote:

I think I've nearly fixed converting text to scratch blocks!

I'm working on a compiler that takes a folder structure of scripts and images and outputs an .sb file. You can decompile a project into its parts, edit the scripts in a text editor (and images in an image editor!), then recompile  smile

Gratuitous screenshot:
http://i.imgur.com/18Pvc.png

(It's not very exciting, I'll admit  tongue  — but editing the text file and then recompiling does actually update the scratch file!)

*gasp* That's amazing!  smile

Hey blob, mind giving me a quick Python primer? I may have to hack a Mac next Monday with it at some competition. Don't ask why...

EDIT: A couple of links will be fine.

Last edited by Hardmath123 (2012-08-20 07:48:44)


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

Offline

 

#78 2012-08-20 07:58:46

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: kurt — Scratch file format Python library

Wanna help integrate into M30W?
(or at least commit to github?  tongue )

Last edited by roijac (2012-08-20 08:06:34)

Offline

 

#79 2012-08-20 08:24:04

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

Re: kurt — Scratch file format Python library

Hardmath123 wrote:

*gasp* That's amazing!  smile

Thank you  smile

Hey blob, mind giving me a quick Python primer? I may have to hack a Mac next Monday with it at some competition. Don't ask why...

EDIT: A couple of links will be fine.

Sounds cool! I assume you mean hacking *on* the Mac, not cracking; not that it matters  tongue

The most obvious thing I can think of:

Last week I helped lead on a Scripture Union holiday called Livewires, where we do loads of techy stuff including teaching people Python, which I helped with. You can download the packages and worksheets we use and go through them. They're intended for people who've never programmed before, which is not you; but it still might be worth trying at least some of the beginner's stuff (sheets 1-5) to get the feel of Python.

At the other extreme — there's also a summary called Python for the impatient (also written by a guy who used to be on LW team) written for programmers, which looks nice, and may suit you better.

Hope either of those help — if not, I can try finding you something else  smile

roijac wrote:

Wanna help integrate into M30W?
(or at least commit to github?   tongue  )

I'll commit to github once I've tested the thing. It's still not quite perfect yet...  tongue


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

Offline

 

#80 2012-08-20 08:35:54

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

Re: kurt — Scratch file format Python library

blob8108 wrote:

Hardmath123 wrote:

*gasp* That's amazing!  smile

Thank you  smile

Hey blob, mind giving me a quick Python primer? I may have to hack a Mac next Monday with it at some competition. Don't ask why...

EDIT: A couple of links will be fine.

Sounds cool! I assume you mean hacking *on* the Mac, not cracking; not that it matters  tongue

The most obvious thing I can think of:

Last week I helped lead on a Scripture Union holiday called Livewires, where we do loads of techy stuff including teaching people Python, which I helped with. You can download the packages and worksheets we use and go through them. They're intended for people who've never programmed before, which is not you; but it still might be worth trying at least some of the beginner's stuff (sheets 1-5) to get the feel of Python.

At the other extreme — there's also a summary called Python for the impatient (also written by a guy who used to be on LW team) written for programmers, which looks nice, and may suit you better.

Hope either of those help — if not, I can try finding you something else  smile

Thanks, I'm obviously an "impatient".  tongue

I'm actually, believe it or not, hacking a Mac emulator from Windows (I think, the competition organizers are very stingy about information). Which is terrible, because I grew up on my Mac ever since I could say "mouse".  hmm  There are rumors of them making you use Ubuntu, though.

Last edited by Hardmath123 (2012-08-20 08:36:07)


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

Offline

 

#81 2012-08-20 08:53:19

s_federici
Scratcher
Registered: 2007-12-18
Posts: 500+

Re: kurt — Scratch file format Python library

blob8108 wrote:

I'm working on a compiler that takes a folder structure of scripts and images and outputs an .sb file

Great! I (and some other scratchers, I'm sure) are ready to test it  smile

blob8108 wrote:

(It's not very exciting, I'll admit  tongue

What do you mean this is not exciting?!? This IS exciting!!!!

Offline

 

#82 2012-08-20 09:45:14

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

Re: kurt — Scratch file format Python library

Hardmath123 wrote:

Thanks, I'm obviously an "impatient".  tongue

I thought you might be...  tongue

I'm actually, believe it or not, hacking a Mac emulator from Windows (I think, the competition organizers are very stingy about information).

That's... interesting. It sounds very difficult.


Just some points I wanted to add to that "Python for the impatient" thing:

He points out that the / operator does integer division by default: so 10 / 3 gives you 3. You can do "from __future__ import division" at the top of your program to fix this; then 10 / 3 gives 3.3333333333333335. Alternatively, just make one of the arguments a float: float(10) / 3.

Traditional C-style "for" loops become something like "for i in range(10):", but most of the time it's easier to iterate over the list/iterable directly, as he suggests. As he says, use xrange for very large ranges, as it returns a generator, not a list. (Generators are cool).

A little more on modules:

To make a Python module, so you can separate your code into separate files, just make a file called "mymodule.py" and then use "import mymodule" from a different .py file in the same folder.

You can alternatively make a folder called "mymodule" with a (can be empty) file called "__init__.py". "import mymodule" then imports "mymodule/__init__.py" as "mymodule" in the local namespace, the same as before. You can also put more .py files inside the folder as submodules, say "mymodule/cheesecake.py", and then access them as "mymodule.cheesecake".

The "string" module he mentions is outdated — there are .lower() , .upper(), .find(), .replace() methods on the str type itself, so you can do things like "fish".upper().

Also, dir() is really useful — it returns a list of the the attributes of an object, eg:

Code:

>>> my_list = []
>>> dir(my_list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

help() on an object gives you similar (sometimes more useful!) output.


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

Offline

 

#83 2012-08-20 09:47:25

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

Re: kurt — Scratch file format Python library

s_federici wrote:

blob8108 wrote:

I'm working on a compiler that takes a folder structure of scripts and images and outputs an .sb file

Great! I (and some other scratchers, I'm sure) are ready to test it  smile

Awesome! I'll test it until it works for me, and then I'll let you know when it's ready  smile

blob8108 wrote:

(It's not very exciting, I'll admit  tongue

What do you mean this is not exciting?!? This IS exciting!!!!

I was referring to the screenshot  tongue  it occurred to me that I could easily have mocked up a text file and identical Scratch file and you'd never know...


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

Offline

 

#84 2012-08-20 12:05:07

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

Re: kurt — Scratch file format Python library

blob8108 wrote:

Hardmath123 wrote:

Thanks, I'm obviously an "impatient".  tongue

I thought you might be...  tongue

I'm actually, believe it or not, hacking a Mac emulator from Windows (I think, the competition organizers are very stingy about information).

That's... interesting. It sounds very difficult.


Just some points I wanted to add to that "Python for the impatient" thing:

He points out that the / operator does integer division by default: so 10 / 3 gives you 3. You can do "from __future__ import division" at the top of your program to fix this; then 10 / 3 gives 3.3333333333333335. Alternatively, just make one of the arguments a float: float(10) / 3.

Traditional C-style "for" loops become something like "for i in range(10):", but most of the time it's easier to iterate over the list/iterable directly, as he suggests. As he says, use xrange for very large ranges, as it returns a generator, not a list. (Generators are cool).

A little more on modules:

To make a Python module, so you can separate your code into separate files, just make a file called "mymodule.py" and then use "import mymodule" from a different .py file in the same folder.

You can alternatively make a folder called "mymodule" with a (can be empty) file called "__init__.py". "import mymodule" then imports "mymodule/__init__.py" as "mymodule" in the local namespace, the same as before. You can also put more .py files inside the folder as submodules, say "mymodule/cheesecake.py", and then access them as "mymodule.cheesecake".

The "string" module he mentions is outdated — there are .lower() , .upper(), .find(), .replace() methods on the str type itself, so you can do things like "fish".upper().

Also, dir() is really useful — it returns a list of the the attributes of an object, eg:

Code:

>>> my_list = []
>>> dir(my_list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

help() on an object gives you similar (sometimes more useful!) output.

Thanks a ton! I'm gonna email myself a link here, it's so useful.  smile

Then again, I'm not logging in to one of those computers; who knows what sort of rotten stuff the previous contestant rigged?  tongue


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

Offline

 

#85 2012-08-20 12:31:02

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

Re: kurt — Scratch file format Python library

Hardmath123 wrote:

Then again, I'm not logging in to one of those computers; who knows what sort of rotten stuff the previous contestant rigged?  tongue

What do you mean by that?  tongue


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

Offline

 

#86 2012-08-20 21:17:13

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

Re: kurt — Scratch file format Python library

blob8108 wrote:

Hardmath123 wrote:

Then again, I'm not logging in to one of those computers; who knows what sort of rotten stuff the previous contestant rigged?  tongue

What do you mean by that?  tongue

I plan to leave a little surprise of my own somewhere on the computer... I always do at these sorts of competitions.  tongue


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

Offline

 

#87 2012-08-21 06:51:54

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

Re: kurt — Scratch file format Python library

s_federici wrote:

blob8108 wrote:

I'm working on a compiler that takes a folder structure of scripts and images and outputs an .sb file

Great! I (and some other scratchers, I'm sure) are ready to test it :)

That's actually really encouraging :)

I've been running some tests: decompiling projects to generate the scratchblocks code, parsing it back to scripts, and then comparing the parsed version with the original.

Here's what I've fixed yesterday/so far:

* trailing newlines at beginning or end
* differentiating variables and built-in reporters ("x position", "timer"... vs "myvariable")
* less than & greater than < >
* C-blocks require special keywords 'forever', 'if', 'else', 'repeat', 'until', 'end' to parse the structure properly
* 'until' is a keyword but is *also* used in normal blocks ("wait until...")
* 'X of Y' — differentiating 'computeFunction:of:' (eg "sqrt of 4") and 'getAttribute:of:' (eg "x position of Sprite1")
* I'd previously added comments starting with hashes # — bad idea (think "costume #"... :P)
* empty inserts () <>

Still to fix:

* obsolete blocks (the online detector I used in my roads game uses these, so they're important to me)
* 'getAttribute:of:' (the "[property] of [sprite]" block) stores a reference to the Sprite *Morph*, not its name. This is a slightly massive architectural problem, as at the moment I read the directories recursively — so I haven't created later Sprite objects when I'm reading a particular script. :/
* recover better from reaching EOF when expecting "end" — 'if <>' on its own is accepted by blocksplugin.js, so should be valid syntax.

You can take a sneak peek at the parser and the lexer to get an idea of how complicated the scratchblocks syntax is to parse...

Last edited by blob8108 (2012-08-21 06:52:24)


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

Offline

 

#88 2012-08-22 18:01:50

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

Re: kurt — Scratch file format Python library

I made a test project by importing a hundred or so frames of my portal stop motion. I exported the frames from Final Cut Pro as an image sequence.

I copied a bunch of them (about 5-700ish) into a new folder, under "portal files/00 Stage/backgrounds". I resized them to 480x360 using Imagemagick: "mogrify -resize 480x360 *.jpg".

The stop motion's done at 12 fps, but the movie's rendered at 24 (it contains some video elements as well). So I used the following Python snippet to delete every other costume in the folder:

Code:

import os

rm = True
for f in sorted(os.listdir('.')):
    if rm:
        os.remove(f)
    rm = not rm

Then I just compiled the folder using Kurt, and I have my Scratch project! It took literally about a second. Compiling lots of JPEGs with Kurt is much faster than drag/dropping them into Scratch — I'm sure you've experienced how slow Scratch is at that. I've waited for whole minutes before...

Unfortunately, Kurt's much slower at importing PNG images, and doesn't compress them, so you get a bigger file. I might try optimising the image processing code, and I can certainly implement the compression algorithm Scratch uses.

A lot of other things are easier, too, like moving 150 costumes from the stage to a separate sprite: just decompile the project, move the images to the new folder, and then compile it again!

The parser is working okay, too. And the cycle of — edit the project as text/images, compile to Scratch, test, save, decompile again, repeat — seems quite nice. Some operations are easier to do in Scratch, for example tweaking things where you want instant feedback; and some operations, like moving around lots of images, and doing lots of copy/pasting of scripts, are easier with the decompiled file/folder structure version.

Hopefully this gives you an idea of what using Kurt is/would be like — what do you think?  smile


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

Offline

 

#89 2012-08-22 18:05:20

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: kurt — Scratch file format Python library

blob8108 wrote:

I made a test project by importing a hundred or so frames of my portal stop motion. I exported the frames from Final Cut Pro as an image sequence.

I copied a bunch of them (about 5-700ish) into a new folder, under "portal files/00 Stage/backgrounds". I resized them to 480x360 using Imagemagick: "mogrify -resize 480x360 *.jpg".

The stop motion's done at 12 fps, but the movie's rendered at 24 (it contains some video elements as well). So I used the following Python snippet to delete every other costume in the folder:

Code:

import os

rm = True
for f in sorted(os.listdir('.')):
    if rm:
        os.remove(f)
    rm = not rm

Then I just compiled the folder using Kurt, and I have my Scratch project! It took literally about a second. Compiling lots of JPEGs with Kurt is much faster than drag/dropping them into Scratch — I'm sure you've experienced how slow Scratch is at that. I've waited for whole minutes before...

Unfortunately, Kurt's much slower at importing PNG images, and doesn't compress them, so you get a bigger file. I might try optimising the image processing code, and I can certainly implement the compression algorithm Scratch uses.

A lot of other things are easier, too, like moving 150 costumes from the stage to a separate sprite: just decompile the project, move the images to the new folder, and then compile it again!

The parser is working okay, too. And the cycle of — edit the project as text/images, compile to Scratch, test, save, decompile again, repeat — seems quite nice. Some operations are easier to do in Scratch, for example tweaking things where you want instant feedback; and some operations, like moving around lots of images, and doing lots of copy/pasting of scripts, are easier with the decompiled file/folder structure version.

Hopefully this gives you an idea of what using Kurt is/would be like — what do you think?  smile

I'm thinking...

Code:

$ git commit -am "here you go"
$ git push

http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#90 2012-08-22 18:34:29

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

Re: kurt — Scratch file format Python library

MathWizz wrote:

I'm thinking...

Code:

$ git commit -am "here you go"
$ git push

...  tongue

You'll find it under the scratchblocks branch (still highly experimental and very messy, of course); it's not on pip yet. Follow the instructions under "Option 3" to install. Enjoy!  smile

WARNING: BACK UP YOUR PROJECTS FIRST, use at your own risk, etc.
---

Parser bugs:
* weird graphic effect [ v] bug
* make sure we integerify number inserts? eg [42]
* this:
    if <(background #) = (54)>
        wait (0.92) secs
    else
        if <(background #) = (73)>
             wait (5.92) secs
        else
        end
    end
* obsolete blocks
* 'getAttribute:of:' (the "[property] of [sprite]" block) stores a reference to the Sprite *Morph*, not its name. This is a slightly massive architectural problem, as at the moment I read the directories recursively — so I haven't created later Sprite objects when I'm reading a particular script.  hmm
* recover better from reaching EOF when expecting "end" — 'if <>' on its own is accepted by blocksplugin.js, so should be valid syntax.
* 'of' block when argument two is a reporter.
Compiler bugs:
* fix sprite list order
* doesn't select blank stage background
* remove image extensions from costume names
* add warning for variables named after blocks
* allow empty stage/sprite/images/scripts folders
* variables
* lists

Restrictions (won't fix):
* variable names:
    * can't have special characters, rather obviously: like any of "[]()<>".
    * can't be named after a block: you can't have a variable called "wait until", as I just discovered: screenshot
    * definitely can't contain special identifiers (like "end", "if", etc.) — FIX
The same possibly applies to broadcasts, etc, not sure.

Last edited by blob8108 (2012-08-23 09:37:32)


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

Offline

 

#91 2012-08-26 08:34:49

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

Re: kurt — Scratch file format Python library

It is finished.  big_smile

I've reached my long-term goal: to be able to decompile my roads game to text/images, compile it again, and have it (mostly) working. Now I can edit projects as text!

There are a few tweaks and bugs left to fix, and general polishing to do, but it's mostly minor stuff. And I'm currently on holiday and don't really have internet -- so I'll hopefully push to Github sometime before next Tuesday evening; that is, a week and a bit -- which should give me time to fix them.  smile

I'm rather excited...


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

Offline

 

#92 2012-08-26 09:02:00

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

Re: kurt — Scratch file format Python library

Nice job!  smile  I've been playing with Python recently, as you know, and I can see how much effort you put into this.


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

Offline

 

#93 2012-08-26 09:09:26

BirdByte
Scratcher
Registered: 2012-07-07
Posts: 1000+

Re: kurt — Scratch file format Python library

Wow, this is amazing! Great job, blob8108!  big_smile


http://i50.tinypic.com/312u714.jpg

Offline

 

#94 2012-08-26 10:13:07

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

Re: kurt — Scratch file format Python library

Thanks, both of you!

Hardmath123 wrote:

I can see how much effort you put into this.

Yeah; I had fun, though  big_smile  And it gets easier. Especially with semi-automated testing. The hardest part is figuring out what it's supposed to do, as you can probably guess...

It does feel amazing when you get near to the end, though  big_smile


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

Offline

 

#95 2012-08-26 10:43:27

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

Re: kurt — Scratch file format Python library

I know... I wrote myself a rudimentary chat program in Python to warm up for tomorrow's competition. It's fun to code it, but just awesome to show it off to your parents.  smile


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

Offline

 

#96 2012-08-26 13:44:22

s_federici
Scratcher
Registered: 2007-12-18
Posts: 500+

Re: kurt — Scratch file format Python library

blob8108 wrote:

I'll hopefully push to Github sometime before next Tuesday evening

What a long time to wait...  sad

Offline

 

#97 2012-08-26 14:41:58

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

Re: kurt — Scratch file format Python library

Hardmath123 wrote:

I know... I wrote myself a rudimentary chat program in Python to warm up for tomorrow's competition. It's fun to code it, but just awesome to show it off to your parents.  smile

Heh  tongue  I'm not entirely sure my parents understand Kurt -- despite the number of times I've mentioned it...

What kind of chat program? And have you tried Twisted yet?  big_smile


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

Offline

 

#98 2012-08-26 20:08:26

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

Re: kurt — Scratch file format Python library

blob8108 wrote:

Hardmath123 wrote:

I know... I wrote myself a rudimentary chat program in Python to warm up for tomorrow's competition. It's fun to code it, but just awesome to show it off to your parents.  smile

Heh  tongue  I'm not entirely sure my parents understand Kurt -- despite the number of times I've mentioned it...

What kind of chat program? And have you tried Twisted yet?  big_smile

Err... an embarrassingly nonsecure socket-based chat.


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

Offline

 

#99 2012-08-27 07:28:56

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: kurt — Scratch file format Python library

blob, why don't you actually use PIL over pypng? (or both)

Offline

 

#100 2012-08-27 08:14:18

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

Re: kurt — Scratch file format Python library

I have no idea. I have been looking at ways to optimize my PNG code... Is PIL faster? If you could find out for me that'd be great  smile


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

Offline

 

Board footer