Tuesday, October 20, 2026

#2 Create .sb3 files from python using chatbot and using in scratch

Prompt :  Write python script for writing generate code blocks in scratch

Response : To generate Scratch code blocks programmatically using Python, the most reliable approach is to generate a .sb3 file. A .sb3 file is actually just a standard ZIP archive containing a project.json file (which holds all the block code, coordinates, and logic) along with asset files like images and sounds.

You can write a Python script that formats this JSON structure correctly and packages it. The most popular library built specifically for this is scratchattach [1], but you can also build the project.json natively using Python's built-in json and zipfile modules.
Here is a complete Python script that dynamically generates a valid Scratch project (.sb3) containing a basic movement script (when green flag clicked → move 10 steps).
Python Script to Generate Scratch Blocks
python
import json
import zipfile
import io

def create_scratch_project(output_filename="generated_project.sb3"):
    # 1. Define the block architecture inside project.json
    project_data = {
        "targets": [
            {
                "isStage": True,
                "name": "Stage",
                "variables": {},
                "lists": {},
                "broadcasts": {},
                "blocks": {},
                "comments": {},
                "currentCostume": 0,
                "costumes": [
                    {
                        "name": "backdrop1",
                        "dataFormat": "svg",
                        "assetId": "cd2151ac61ecaa654d0aa79046a8041c",
                        "md5ext": "cd2151ac61ecaa654d0aa79046a8041c.svg",
                        "rotationCenterX": 240,
                        "rotationCenterY": 180
                    }
                ],
                "sounds": [],
                "volume": 100,
                "layerOrder": 0,
                "tempo": 60,
                "videoState": "on",
                "videoTransparency": 50,
                "textToSpeechLanguage": None
            },
            {
                "isStage": False,
                "name": "Sprite1",
                "variables": {},
                "lists": {},
                "broadcasts": {},
                # This is where your generated blocks live
                "blocks": {
                    "hat_block_id": {
                        "opcode": "event_whenflagclicked",
                        "next": "move_block_id",
                        "parent": None,
                        "inputs": {},
                        "fields": {},
                        "shadow": False,
                        "topLevel": True,
                        "x": 100,
                        "y": 100
                    },
                    "move_block_id": {
                        "opcode": "motion_movesteps",
                        "next": None,
                        "parent": "hat_block_id",
                        "inputs": {
                            "STEPS": [1, [4, "10"]]  # Opcode 4 represents a number input string
                        },
                        "fields": {},
                        "shadow": False,
                        "topLevel": False
                    }
                },
                "comments": {},
                "currentCostume": 0,
                "costumes": [
                    {
                        "name": "costume1",
                        "dataFormat": "svg",
                        "assetId": "bcf52853d2f35015e34f26088f11bda5",
                        "md5ext": "bcf52853d2f35015e34f26088f11bda5.svg",
                        "rotationCenterX": 48,
                        "rotationCenterY": 50
                    }
                ],
                "sounds": [],
                "volume": 100,
                "layerOrder": 1,
                "visible": True,
                "x": 0,
                "y": 0,
                "size": 100,
                "direction": 90,
                "draggable": False,
                "rotationStyle": "all around"
            }
        ],
        "monitors": [],
        "extensions": [],
        "meta": {
            "semver": "3.0.0",
            "vm": "0.2.0",
            "agent": "Python Scratch Generator"
        }
    }

    # 2. Minimal blank SVG data strings for required assets
    blank_svg = '<svg version="1.1" xmlns="http://w3.org" width="1" height="1"></svg>'

    # 3. Compile everything directly into a .sb3 (ZIP) package
    with zipfile.ZipFile(output_filename, 'w', zipfile.ZIP_DEFLATED) as sb3:
        # Write the manifest file containing all the blocks
        sb3.writestr('project.json', json.dumps(project_data, indent=2))
        # Write dummy assets so Scratch doesn't crash on load
        sb3.writestr('cd2151ac61ecaa654d0aa79046a8041c.svg', blank_svg)
        sb3.writestr('bcf52853d2f35015e34f26088f11bda5.svg', blank_svg)

    print(f"Successfully created '{output_filename}'! Go to scratch.mit.edu -> File -> Load from your computer.")

if __name__ == "__main__":
    create_scratch_project()
Use code with caution.
How to use this generated file:
  1. Run the script above in your local Python environment to create generated_project.sb3.
  2. Head over to the official online Scratch Editor.
  3. In the top navigation bar, click File → Load from your computer.
  4. Select your generated .sb3 file. You will see your Sprite with the when green flag clicked and move 10 steps blocks automatically snapped together in the workspace!
Would you like me to expand this script to dynamically map complex mathematical algorithms into loops (repeat, forever) or variable assignments inside Scratch? Let me know what logic you are trying to automate!

No comments:

Post a Comment

#4 Convert sb3 files to exe files

You can convert a Scratch file ( .sb3 ) into a standalone Windows executable ( .exe ) using a free web tool called TurboWarp Packager . This...