Discuss Scratch

aespibr
Scratcher
73 posts

ScratchGen - A Python API for creating Scratch projects

ScratchGen
GitHub - PyPI

I reverse-engineered the Scratch file format to create something like kurt, but revamped for Scratch 3. It features an OOP approach to creating Scratch projects through an easy-to-use Python API where all of the technical details are abstracted away.

ScratchGen can do everything kurt did and more! Due to it being written in a flexible language with many third-party libraries available, you can combine your Python knowledge with your Scratch wisdom and automatically generate anything you need.

ScratchGen also has Markdown documentation available, which lists all of its features and how to use them, complete with code examples. Here are a couple:

  • Text Reversal Engine
from ScratchGen import *
project = Project()
sprite = project.createSprite("Text Reversal Sprite")
sprite.addCostume("assets/scratch logo.png")
reversed_text = project.createGlobalVariable("reversed text")
block = sprite.createCustomBlock("reverse text %s")
text = block.getParameters()
reverse_text = block.setScript(
    SetVariable(reversed_text, ""),
    Repeat(LengthOf(text),
        SetVariable(reversed_text,
            Join(
                LetterOf(
                    Add(LengthOf(reversed_text), 1),
                    text
                ), reversed_text
            )
        )
    )
)
sprite.createScript(WhenFlagClicked(),
    reverse_text("Hello, World!"),
    Say(reversed_text)
)
project.save("Text Reversal Test.sb3")

  • Simple Jetpack Game
from ScratchGen import *
project = Project()
sprite = project.createSprite("Flying Sprite")
sprite.addCostume("assets/linus jetpack.png")
y_velocity = sprite.createVariable("y velocity")
sprite.createScript(WhenFlagClicked(),
    GoToPosition(0, 0),
    SetVariable(y_velocity, 0),
    Forever(
        If(Or(MouseDown(), KeyPressed(SPACE)),
            ChangeVariable(y_velocity, 2)
        ).Else(
            ChangeVariable(y_velocity, -2)
        ),
        ChangeY(y_velocity)
    )
)
project.save("Flying Test.sb3")

* Wildcard imports are safe due to namespace curation.


Scrybe

ScratchGen is also being used in the development of Scrybe, a programming language inspired by goboscript. Scrybe is still in active development, but feel free to try it out!

Last edited by aespibr (April 9, 2025 18:11:24)

turbowarper94
Scratcher
1 post

ScratchGen - A Python API for creating Scratch projects

when green flag clicked
go to x: (0) y: (0)
forever
if <key (right arrow v) pressed?> then
change x by (10)
end
if <key (left arrow v) pressed?> then
change x by (-10)
end
if <key (up arrow v) pressed?> then
change y by (10)
end
if <key (down arrow v) pressed?> then
change y by (-10)
end
end
so yeah
kandita8
Scratcher
22 posts

ScratchGen - A Python API for creating Scratch projects

when green flag clicked
go to x: (0) y: (0)
forever
if <key (right arrow v) pressed?> then
change x by (10)
end
if <key (left arrow v) pressed?> then
change x by (-10)
end
if <key (up arrow v) pressed?> then
change y by (10)
end
if <key (down arrow v) pressed?> then
change y by (-10)
end
end
kandita8
Scratcher
22 posts

ScratchGen - A Python API for creating Scratch projects

project = Project()

sprite = project.createSprite("Flying Sprite")
sprite.addCostume("assets/linus jetpack.png")

y_velocity = sprite.createVariable("y velocity")

sprite.createScript(WhenFlagClicked(),
GoToPosition(0, 0),
SetVariable(y_velocity, 0),

Forever(
If(Or(MouseDown(), KeyPressed(SPACE)),
ChangeVariable(y_velocity, 2)
).Else(
ChangeVariable(y_velocity, -2)
),

ChangeY(y_velocity)
)
)

project.save("Flying Test.sb3")
kandita8
Scratcher
22 posts

ScratchGen - A Python API for creating Scratch projects

project = Project()

sprite = project.createSprite("Text Reversal Sprite")
sprite.addCostume("assets/scratch logo.png")

reversed_text = project.createGlobalVariable("reversed text")

block = sprite.createCustomBlock("reverse text %s")
text = block.getParameters()
reverse_text = block.setScript(
SetVariable(reversed_text, ""),

Repeat(LengthOf(text),
SetVariable(reversed_text,
Join(
LetterOf(
Add(LengthOf(reversed_text), 1),
text
), reversed_text
)
)
)
)

sprite.createScript(WhenFlagClicked(),
reverse_text("Hello, World!"),
Say(reversed_text)
)

project.save("Text Reversal Test.sb3")
MineTurte
Scratcher
1000+ posts

ScratchGen - A Python API for creating Scratch projects

aespibr wrote:

ScratchGen
GitHub - PyPI

I reverse-engineered the Scratch file format to create something like kurt, but revamped for Scratch 3. It features an OOP approach to creating Scratch projects through an easy-to-use Python API where all of the technical details are abstracted away.

ScratchGen can do everything kurt did and more! Due to it being written in a flexible language with many third-party libraries available, you can combine your Python knowledge with your Scratch wisdom and automatically generate anything you need.

ScratchGen also has Markdown documentation available, which lists all of its features and how to use them, complete with code examples. Here are a couple:

  • Text Reversal Engine
from ScratchGen import *
project = Project()
sprite = project.createSprite("Text Reversal Sprite")
sprite.addCostume("assets/scratch logo.png")
reversed_text = project.createGlobalVariable("reversed text")
block = sprite.createCustomBlock("reverse text %s")
text = block.getParameters()
reverse_text = block.setScript(
    SetVariable(reversed_text, ""),
    Repeat(LengthOf(text),
        SetVariable(reversed_text,
            Join(
                LetterOf(
                    Add(LengthOf(reversed_text), 1),
                    text
                ), reversed_text
            )
        )
    )
)
sprite.createScript(WhenFlagClicked(),
    reverse_text("Hello, World!"),
    Say(reversed_text)
)
project.save("Text Reversal Test.sb3")

  • Simple Jetpack Game
from ScratchGen import *
project = Project()
sprite = project.createSprite("Flying Sprite")
sprite.addCostume("assets/linus jetpack.png")
y_velocity = sprite.createVariable("y velocity")
sprite.createScript(WhenFlagClicked(),
    GoToPosition(0, 0),
    SetVariable(y_velocity, 0),
    Forever(
        If(Or(MouseDown(), KeyPressed(SPACE)),
            ChangeVariable(y_velocity, 2)
        ).Else(
            ChangeVariable(y_velocity, -2)
        ),
        ChangeY(y_velocity)
    )
)
project.save("Flying Test.sb3")

* Wildcard imports are safe due to namespace curation.


Scrybe

ScratchGen is also being used in the development of Scrybe, a programming language inspired by goboscript. Scrybe is still in active development, but feel free to try it out!
Hmmm interesting idea but wouldn't it be easier if you just turned scratchblocks or something like that into a scratch project? also small note you can put the code tag with =py for it to format and color correctly ;)

Last edited by MineTurte (April 9, 2025 12:58:05)

aespibr
Scratcher
73 posts

ScratchGen - A Python API for creating Scratch projects

MineTurte wrote:

Hmmm interesting idea but wouldn't it be easier if you just turned scratchblocks or something like that into a scratch project? also small note you can put the code tag with =py for it to format and color correctly ;)

This provides much more than scratchblocks, as you can not only create scripts, but import costumes and sounds. You can also leverage the abilities of Python to speed up certain aspects of creating complex projects. For example, you could write a script that generates a text engine sprite by importing all of the text costumes in Python.

Also, thanks for the formatting tip
MineTurte
Scratcher
1000+ posts

ScratchGen - A Python API for creating Scratch projects

aespibr wrote:

MineTurte wrote:

Hmmm interesting idea but wouldn't it be easier if you just turned scratchblocks or something like that into a scratch project? also small note you can put the code tag with =py for it to format and color correctly ;)

This provides much more than scratchblocks, as you can not only create scripts, but import costumes and sounds. You can also leverage the abilities of Python to speed up certain aspects of creating complex projects. For example, you could write a script that generates a text engine sprite by importing all of the text costumes in Python.

Also, thanks for the formatting tip
Oh okay well yeah that would be pretty cool. I could see some pretty good uses for this. If you need any help lmk

Powered by DjangoBB