import json import copy import json import copy def generate_blocks_from_opcodes(opcode_counts, all_block_definitions): """ Generates a dictionary of Scratch-like blocks based on a list of opcodes and a reference block definition. It now correctly links parent and menu blocks using their generated unique keys, and handles various block categories. It ensures that menu blocks are only generated as children of their respective parent blocks. Args: opcode_counts (list): An array of objects, each with an 'opcode' and 'count' property. Example: [{"opcode": "motion_gotoxy", "count": 1}] all_block_definitions (dict): A comprehensive dictionary containing definitions for all block types. Returns: dict: A JSON object where keys are generated block IDs and values are the block definitions. """ generated_blocks = {} opcode_occurrences = {} # To keep track of how many times each opcode (main or menu) has been used for unique keys # Define explicit parent-menu relationships for linking purposes # This maps main_opcode -> list of (input_field_name, menu_opcode) explicit_menu_links = { "motion_goto": [("TO", "motion_goto_menu")], "motion_glideto": [("TO", "motion_glideto_menu")], "motion_pointtowards": [("TOWARDS", "motion_pointtowards_menu")], "sensing_keypressed": [("KEY_OPTION", "sensing_keyoptions")], "sensing_of": [("OBJECT", "sensing_of_object_menu")], "sensing_touchingobject": [("TOUCHINGOBJECTMENU", "sensing_touchingobjectmenu")], "control_create_clone_of": [("CLONE_OPTION", "control_create_clone_of_menu")], "sound_play": [("SOUND_MENU", "sound_sounds_menu")], "sound_playuntildone": [("SOUND_MENU", "sound_sounds_menu")], "looks_switchcostumeto": [("COSTUME", "looks_costume")], "looks_switchbackdropto": [("BACKDROP", "looks_backdrops")], } # --- Step 1: Process explicitly requested opcodes and generate their instances and associated menus --- for item in opcode_counts: opcode = item.get("opcode") count = item.get("count", 1) if not opcode: print("Warning: Skipping item with missing 'opcode'.") continue if opcode not in all_block_definitions: print(f"Warning: Opcode '{opcode}' not found in all_block_definitions. Skipping.") continue for _ in range(count): # Increment occurrence count for the current main opcode opcode_occurrences[opcode] = opcode_occurrences.get(opcode, 0) + 1 main_block_instance_num = opcode_occurrences[opcode] main_block_unique_key = f"{opcode}_{main_block_instance_num}" # Create a deep copy of the main block definition main_block_data = copy.deepcopy(all_block_definitions[opcode]) # Set properties for a top-level main block main_block_data["parent"] = None main_block_data["next"] = None main_block_data["topLevel"] = True main_block_data["shadow"] = False # Main blocks are typically not shadows generated_blocks[main_block_unique_key] = main_block_data # If this main block has associated menus, generate and link them now if opcode in explicit_menu_links: for input_field_name, menu_opcode_type in explicit_menu_links[opcode]: if menu_opcode_type in all_block_definitions: # Increment the occurrence for the menu block type opcode_occurrences[menu_opcode_type] = opcode_occurrences.get(menu_opcode_type, 0) + 1 menu_block_instance_num = opcode_occurrences[menu_opcode_type] menu_block_data = copy.deepcopy(all_block_definitions[menu_opcode_type]) # Generate a unique key for this specific menu instance menu_unique_key = f"{menu_opcode_type}_{menu_block_instance_num}" # Set properties for a shadow menu block menu_block_data["shadow"] = True menu_block_data["topLevel"] = False menu_block_data["next"] = None menu_block_data["parent"] = main_block_unique_key # Link menu to its parent instance # Update the main block's input to point to this unique menu instance if input_field_name in main_block_data.get("inputs", {}) and \ isinstance(main_block_data["inputs"][input_field_name], list) and \ len(main_block_data["inputs"][input_field_name]) > 1 and \ main_block_data["inputs"][input_field_name][0] == 1: main_block_data["inputs"][input_field_name][1] = menu_unique_key generated_blocks[menu_unique_key] = menu_block_data return generated_blocks # --- Consolidated Block Definitions from all provided JSONs --- all_block_definitions = { # motion_block.json "motion_movesteps": { "opcode": "motion_movesteps", "next": None, "parent": None, "inputs": {"STEPS": [1, [4, "10"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 464, "y": -416 }, "motion_turnright": { "opcode": "motion_turnright", "next": None, "parent": None, "inputs": {"DEGREES": [1, [4, "15"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 467, "y": -316 }, "motion_turnleft": { "opcode": "motion_turnleft", "next": None, "parent": None, "inputs": {"DEGREES": [1, [4, "15"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 464, "y": -210 }, "motion_goto": { "opcode": "motion_goto", "next": None, "parent": None, "inputs": {"TO": [1, "motion_goto_menu"]}, "fields": {}, "shadow": False, "topLevel": True, "x": 465, "y": -95 }, "motion_goto_menu": { "opcode": "motion_goto_menu", "next": None, "parent": "motion_goto", "inputs": {}, "fields": {"TO": ["_random_", None]}, "shadow": True, "topLevel": False }, "motion_gotoxy": { "opcode": "motion_gotoxy", "next": None, "parent": None, "inputs": {"X": [1, [4, "0"]], "Y": [1, [4, "0"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 468, "y": 12 }, "motion_glideto": { "opcode": "motion_glideto", "next": None, "parent": None, "inputs": {"SECS": [1, [4, "1"]], "TO": [1, "motion_glideto_menu"]}, "fields": {}, "shadow": False, "topLevel": True, "x": 470, "y": 129 }, "motion_glideto_menu": { "opcode": "motion_glideto_menu", "next": None, "parent": "motion_glideto", "inputs": {}, "fields": {"TO": ["_random_", None]}, "shadow": True, "topLevel": False }, "motion_glidesecstoxy": { "opcode": "motion_glidesecstoxy", "next": None, "parent": None, "inputs": {"SECS": [1, [4, "1"]], "X": [1, [4, "0"]], "Y": [1, [4, "0"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 476, "y": 239 }, "motion_pointindirection": { "opcode": "motion_pointindirection", "next": None, "parent": None, "inputs": {"DIRECTION": [1, [8, "90"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 493, "y": 361 }, "motion_pointtowards": { "opcode": "motion_pointtowards", "next": None, "parent": None, "inputs": {"TOWARDS": [1, "motion_pointtowards_menu"]}, "fields": {}, "shadow": False, "topLevel": True, "x": 492, "y": 463 }, "motion_pointtowards_menu": { "opcode": "motion_pointtowards_menu", "next": None, "parent": "motion_pointtowards", "inputs": {}, "fields": {"TOWARDS": ["_mouse_", None]}, "shadow": True, "topLevel": False }, "motion_changexby": { "opcode": "motion_changexby", "next": None, "parent": None, "inputs": {"DX": [1, [4, "10"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 851, "y": -409 }, "motion_setx": { "opcode": "motion_setx", "next": None, "parent": None, "inputs": {"X": [1, [4, "0"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 864, "y": -194 }, "motion_changeyby": { "opcode": "motion_changeyby", "next": None, "parent": None, "inputs": {"DY": [1, [4, "10"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 861, "y": -61 }, "motion_sety": { "opcode": "motion_sety", "next": None, "parent": None, "inputs": {"Y": [1, [4, "0"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 864, "y": 66 }, "motion_ifonedgebounce": { "opcode": "motion_ifonedgebounce", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 1131, "y": -397 }, "motion_setrotationstyle": { "opcode": "motion_setrotationstyle", "next": None, "parent": None, "inputs": {}, "fields": {"STYLE": ["left-right", None]}, "shadow": False, "topLevel": True, "x": 1128, "y": -287 }, "motion_xposition": { "opcode": "motion_xposition", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 1193, "y": -136 }, "motion_yposition": { "opcode": "motion_yposition", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 1181, "y": -64 }, "motion_direction": { "opcode": "motion_direction", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 1188, "y": 21 }, # control_block.json "control_wait": { "opcode": "control_wait", "next": None, "parent": None, "inputs": {"DURATION": [1, [5, "1"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 337, "y": 129 }, "control_repeat": { "opcode": "control_repeat", "next": None, "parent": None, "inputs": {"TIMES": [1, [6, "10"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 348, "y": 265 }, "control_forever": { "opcode": "control_forever", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 334, "y": 439 }, "control_if": { "opcode": "control_if", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 331, "y": 597 }, "control_if_else": { "opcode": "control_if_else", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 335, "y": 779 }, "control_wait_until": { "opcode": "control_wait_until", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 676, "y": 285 }, "control_repeat_until": { "opcode": "control_repeat_until", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 692, "y": 381 }, "control_stop": { "opcode": "control_stop", "next": None, "parent": None, "inputs": {}, "fields": {"STOP_OPTION": ["all", None]}, "shadow": False, "topLevel": True, "x": 708, "y": 545, "mutation": {"tagName": "mutation", "children": [], "hasnext": "false"} }, "control_start_as_clone": { "opcode": "control_start_as_clone", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 665, "y": 672 }, "control_create_clone_of": { "opcode": "control_create_clone_of", "next": None, "parent": None, "inputs": {"CLONE_OPTION": [1, "control_create_clone_of_menu"]}, "fields": {}, "shadow": False, "topLevel": True, "x": 648, "y": 797 }, "control_create_clone_of_menu": { "opcode": "control_create_clone_of_menu", "next": None, "parent": "control_create_clone_of", "inputs": {}, "fields": {"CLONE_OPTION": ["_myself_", None]}, "shadow": True, "topLevel": False }, "control_delete_this_clone": { "opcode": "control_delete_this_clone", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 642, "y": 914 }, # data_block.json "data_setvariableto": { "opcode": "data_setvariableto", "next": None, "parent": None, "inputs": {"VALUE": [1, [10, "0"]]}, "fields": {"VARIABLE": ["my variable", "`jEk@4|i[#Fk?(8x)AV.-my variable"]}, "shadow": False, "topLevel": True, "x": 348, "y": 241 }, "data_changevariableby": { "opcode": "data_changevariableby", "next": None, "parent": None, "inputs": {"VALUE": [1, [4, "1"]]}, "fields": {"VARIABLE": ["my variable", "`jEk@4|i[#Fk?(8x)AV.-my variable"]}, "shadow": False, "topLevel": True, "x": 313, "y": 363 }, "data_showvariable": { "opcode": "data_showvariable", "next": None, "parent": None, "inputs": {}, "fields": {"VARIABLE": ["my variable", "`jEk@4|i[#Fk?(8x)AV.-my variable"]}, "shadow": False, "topLevel": True, "x": 415, "y": 473 }, "data_hidevariable": { "opcode": "data_hidevariable", "next": None, "parent": None, "inputs": {}, "fields": {"VARIABLE": ["my variable", "`jEk@4|i[#Fk?(8x)AV.-my variable"]}, "shadow": False, "topLevel": True, "x": 319, "y": 587 }, "data_addtolist": { "opcode": "data_addtolist", "next": None, "parent": None, "inputs": {"ITEM": [1, [10, "thing"]]}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True, "x": 385, "y": 109 }, "data_deleteoflist": { "opcode": "data_deleteoflist", "next": None, "parent": None, "inputs": {"INDEX": [1, [7, "1"]]}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True, "x": 384, "y": 244 }, "data_deletealloflist": { "opcode": "data_deletealloflist", "next": None, "parent": None, "inputs": {}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True, "x": 387, "y": 374 }, "data_insertatlist": { "opcode": "data_insertatlist", "next": None, "parent": None, "inputs": {"ITEM": [1, [10, "thing"]], "INDEX": [1, [7, "1"]]}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True, "x": 366, "y": 527 }, "data_replaceitemoflist": { "opcode": "data_replaceitemoflist", "next": None, "parent": None, "inputs": {"INDEX": [1, [7, "1"]], "ITEM": [1, [10, "thing"]]}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True, "x": 365, "y": 657 }, "data_itemoflist": { "opcode": "data_itemoflist", "next": None, "parent": None, "inputs": {"INDEX": [1, [7, "1"]]}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True, "x": 862, "y": 117 }, "data_itemnumoflist": { "opcode": "data_itemnumoflist", "next": None, "parent": None, "inputs": {"ITEM": [1, [10, "thing"]]}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True, "x": 883, "y": 238 }, "data_lengthoflist": { "opcode": "data_lengthoflist", "next": None, "parent": None, "inputs": {}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True, "x": 876, "y": 342 }, "data_listcontainsitem": { "opcode": "data_listcontainsitem", "next": None, "parent": None, "inputs": {"ITEM": [1, [10, "thing"]]}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True, "x": 871, "y": 463 }, "data_showlist": { "opcode": "data_showlist", "next": None, "parent": None, "inputs": {}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True, "x": 931, "y": 563 }, "data_hidelist": { "opcode": "data_hidelist", "next": None, "parent": None, "inputs": {}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True, "x": 962, "y": 716 }, # event_block.json "event_whenflagclicked": { "opcode": "event_whenflagclicked", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 166, "y": -422 }, "event_whenkeypressed": { "opcode": "event_whenkeypressed", "next": None, "parent": None, "inputs": {}, "fields": {"KEY_OPTION": ["space", None]}, "shadow": False, "topLevel": True, "x": 151, "y": -329 }, "event_whenthisspriteclicked": { "opcode": "event_whenthisspriteclicked", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 156, "y": -223 }, "event_whenbackdropswitchesto": { "opcode": "event_whenbackdropswitchesto", "next": None, "parent": None, "inputs": {}, "fields": {"BACKDROP": ["backdrop1", None]}, "shadow": False, "topLevel": True, "x": 148, "y": -101 }, "event_whengreaterthan": { "opcode": "event_whengreaterthan", "next": None, "parent": None, "inputs": {"VALUE": [1, [4, "10"]]}, "fields": {"WHENGREATERTHANMENU": ["LOUDNESS", None]}, "shadow": False, "topLevel": True, "x": 150, "y": 10 }, "event_whenbroadcastreceived": { "opcode": "event_whenbroadcastreceived", "next": None, "parent": None, "inputs": {}, "fields": {"BROADCAST_OPTION": ["message1", "5O!nei;S$!c!=hCT}0:a"]}, "shadow": False, "topLevel": True, "x": 141, "y": 118 }, "event_broadcast": { "opcode": "event_broadcast", "next": None, "parent": None, "inputs": {"BROADCAST_INPUT": [1, [11, "message1", "5O!nei;S$!c!=hCT}0:a"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 151, "y": 229 }, "event_broadcastandwait": { "opcode": "event_broadcastandwait", "next": None, "parent": None, "inputs": {"BROADCAST_INPUT": [1, [11, "message1", "5O!nei;S$!c!=hCT}0:a"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 157, "y": 340 }, # look_block.json "looks_sayforsecs": { "opcode": "looks_sayforsecs", "next": None, "parent": None, "inputs": {"MESSAGE": [1, [10, "Hello!"]], "SECS": [1, [4, "2"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 408, "y": 91 }, "looks_say": { "opcode": "looks_say", "next": None, "parent": None, "inputs": {"MESSAGE": [1, [10, "Hello!"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 413, "y": 213 }, "looks_thinkforsecs": { "opcode": "looks_thinkforsecs", "next": None, "parent": None, "inputs": {"MESSAGE": [1, [10, "Hmm..."]], "SECS": [1, [4, "2"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 413, "y": 317 }, "looks_think": { "opcode": "looks_think", "next": None, "parent": None, "inputs": {"MESSAGE": [1, [10, "Hmm..."]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 412, "y": 432 }, "looks_switchcostumeto": { "opcode": "looks_switchcostumeto", "next": None, "parent": None, "inputs": {"COSTUME": [1, "8;bti4wv(iH9nkOacCJ|"]}, "fields": {}, "shadow": False, "topLevel": True, "x": 411, "y": 555 }, "looks_costume": { "opcode": "looks_costume", "next": None, "parent": "Q#a,6LPWHqo9-0Nu*[SV", "inputs": {}, "fields": {"COSTUME": ["costume2", None]}, "shadow": True, "topLevel": False }, "looks_nextcostume": { "opcode": "looks_nextcostume", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 419, "y": 687 }, "looks_switchbackdropto": { "opcode": "looks_switchbackdropto", "next": None, "parent": None, "inputs": {"BACKDROP": [1, "-?yeX}29V*wd6W:unW0i"]}, "fields": {}, "shadow": False, "topLevel": True, "x": 901, "y": 91 }, "looks_backdrops": { "opcode": "looks_backdrops", "next": None, "parent": "`Wm^p~l[(IWzc1|wNv*.", "inputs": {}, "fields": {"BACKDROP": ["backdrop1", None]}, "shadow": True, "topLevel": False }, "looks_changesizeby": { "opcode": "looks_changesizeby", "next": None, "parent": None, "inputs": {"CHANGE": [1, [4, "10"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 895, "y": 192 }, "looks_setsizeto": { "opcode": "looks_setsizeto", "next": None, "parent": None, "inputs": {"SIZE": [1, [4, "100"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 896, "y": 303 }, "looks_changeeffectby": { "opcode": "looks_changeeffectby", "next": None, "parent": None, "inputs": {"CHANGE": [1, [4, "25"]]}, "fields": {"EFFECT": ["COLOR", None]}, "shadow": False, "topLevel": True, "x": 892, "y": 416 }, "looks_seteffectto": { "opcode": "looks_seteffectto", "next": None, "parent": None, "inputs": {"VALUE": [1, [4, "0"]]}, "fields": {"EFFECT": ["COLOR", None]}, "shadow": False, "topLevel": True, "x": 902, "y": 527 }, "looks_cleargraphiceffects": { "opcode": "looks_cleargraphiceffects", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 902, "y": 638 }, "looks_show": { "opcode": "looks_show", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 908, "y": 758 }, "looks_hide": { "opcode": "looks_hide", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 455, "y": 861 }, "looks_gotofrontback": { "opcode": "looks_gotofrontback", "next": None, "parent": None, "inputs": {}, "fields": {"FRONT_BACK": ["front", None]}, "shadow": False, "topLevel": True, "x": 853, "y": 878 }, "looks_goforwardbackwardlayers": { "opcode": "looks_goforwardbackwardlayers", "next": None, "parent": None, "inputs": {"NUM": [1, [7, "1"]]}, "fields": {"FORWARD_BACKWARD": ["forward", None]}, "shadow": False, "topLevel": True, "x": 851, "y": 999 }, "looks_costumenumbername": { "opcode": "looks_costumenumbername", "next": None, "parent": None, "inputs": {}, "fields": {"NUMBER_NAME": ["number", None]}, "shadow": False, "topLevel": True, "x": 458, "y": 1007 }, "looks_backdropnumbername": { "opcode": "looks_backdropnumbername", "next": None, "parent": None, "inputs": {}, "fields": {"NUMBER_NAME": ["number", None]}, "shadow": False, "topLevel": True, "x": 1242, "y": 753 }, "looks_size": { "opcode": "looks_size", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 1249, "y": 876 }, # operator_block.json "operator_add": { "opcode": "operator_add", "next": None, "parent": None, "inputs": {"NUM1": [1, [4, ""]], "NUM2": [1, [4, ""]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 128, "y": 153 }, "operator_subtract": { "opcode": "operator_subtract", "next": None, "parent": None, "inputs": {"NUM1": [1, [4, ""]], "NUM2": [1, [4, ""]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 134, "y": 214 }, "operator_multiply": { "opcode": "operator_multiply", "next": None, "parent": None, "inputs": {"NUM1": [1, [4, ""]], "NUM2": [1, [4, ""]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 134, "y": 278 }, "operator_divide": { "opcode": "operator_divide", "next": None, "parent": None, "inputs": {"NUM1": [1, [4, ""]], "NUM2": [1, [4, ""]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 138, "y": 359 }, "operator_random": { "opcode": "operator_random", "next": None, "parent": None, "inputs": {"FROM": [1, [4, "1"]], "TO": [1, [4, "10"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 311, "y": 157 }, "operator_gt": { "opcode": "operator_gt", "next": None, "parent": None, "inputs": {"OPERAND1": [1, [10, ""]], "OPERAND2": [1, [10, "50"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 348, "y": 217 }, "operator_lt": { "opcode": "operator_lt", "next": None, "parent": None, "inputs": {"OPERAND1": [1, [10, ""]], "OPERAND2": [1, [10, "50"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 345, "y": 286 }, "operator_equals": { "opcode": "operator_equals", "next": None, "parent": None, "inputs": {"OPERAND1": [1, [10, ""]], "OPERAND2": [1, [10, "50"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 345, "y": 372 }, "operator_and": { "opcode": "operator_and", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 701, "y": 158 }, "operator_or": { "opcode": "operator_or", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 705, "y": 222 }, "operator_not": { "opcode": "operator_not", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 734, "y": 283 }, "operator_join": { "opcode": "operator_join", "next": None, "parent": None, "inputs": {"STRING1": [1, [10, "apple "]], "STRING2": [1, [10, "banana"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 663, "y": 378 }, "operator_letter_of": { "opcode": "operator_letter_of", "next": None, "parent": None, "inputs": {"LETTER": [1, [6, "1"]], "STRING": [1, [10, "apple"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 664, "y": 445 }, "operator_length": { "opcode": "operator_length", "next": None, "parent": None, "inputs": {"STRING": [1, [10, "apple"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 664, "y": 521 }, "operator_contains": { "opcode": "operator_contains", "next": None, "parent": None, "inputs": {"STRING1": [1, [10, "apple"]], "STRING2": [1, [10, "a"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 634, "y": 599 }, "operator_mod": { "opcode": "operator_mod", "next": None, "parent": None, "inputs": {"NUM1": [1, [4, ""]], "NUM2": [1, [4, ""]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 295, "y": 594 }, "operator_round": { "opcode": "operator_round", "next": None, "parent": None, "inputs": {"NUM": [1, [4, ""]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 307, "y": 674 }, "operator_mathop": { "opcode": "operator_mathop", "next": None, "parent": None, "inputs": {"NUM": [1, [4, ""]]}, "fields": {"OPERATOR": ["abs", None]}, "shadow": False, "topLevel": True, "x": 280, "y": 754 }, # sensing_block.json "sensing_touchingobject": { "opcode": "sensing_touchingobject", "next": None, "parent": None, "inputs": {"TOUCHINGOBJECTMENU": [1, "sensing_touchingobjectmenu"]}, "fields": {}, "shadow": False, "topLevel": True, "x": 359, "y": 116 }, "sensing_touchingobjectmenu": { "opcode": "sensing_touchingobjectmenu", "next": None, "parent": "sensing_touchingobject", "inputs": {}, "fields": {"TOUCHINGOBJECTMENU": ["_mouse_", None]}, "shadow": True, "topLevel": False }, "sensing_touchingcolor": { "opcode": "sensing_touchingcolor", "next": None, "parent": None, "inputs": {"COLOR": [1, [9, "#55b888"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 360, "y": 188 }, "sensing_coloristouchingcolor": { "opcode": "sensing_coloristouchingcolor", "next": None, "parent": None, "inputs": {"COLOR": [1, [9, "#d019f2"]], "COLOR2": [1, [9, "#2b0de3"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 348, "y": 277 }, "sensing_askandwait": { "opcode": "sensing_askandwait", "next": None, "parent": None, "inputs": {"QUESTION": [1, [10, "What's your name?"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 338, "y": 354 }, "sensing_answer": { "opcode": "sensing_answer", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 782, "y": 111 }, "sensing_keypressed": { "opcode": "sensing_keypressed", "next": None, "parent": None, "inputs": {"KEY_OPTION": [1, "sensing_keyoptions"]}, "fields": {}, "shadow": False, "topLevel": True, "x": 762, "y": 207 }, "sensing_keyoptions": { "opcode": "sensing_keyoptions", "next": None, "parent": "sensing_keypressed", "inputs": {}, "fields": {"KEY_OPTION": ["space", None]}, "shadow": True, "topLevel": False }, "sensing_mousedown": { "opcode": "sensing_mousedown", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 822, "y": 422 }, "sensing_mousex": { "opcode": "sensing_mousex", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 302, "y": 528 }, "sensing_mousey": { "opcode": "sensing_mousey", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 668, "y": 547 }, "sensing_setdragmode": { "opcode": "sensing_setdragmode", "next": None, "parent": None, "inputs": {}, "fields": {"DRAG_MODE": ["draggable", None]}, "shadow": False, "topLevel": True, "x": 950, "y": 574 }, "sensing_loudness": { "opcode": "sensing_loudness", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 658, "y": 703 }, "sensing_timer": { "opcode": "sensing_timer", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 459, "y": 671 }, "sensing_resettimer": { "opcode": "sensing_resettimer", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 462, "y": 781 }, "sensing_of": { "opcode": "sensing_of", "next": None, "parent": None, "inputs": {"OBJECT": [1, "sensing_of_object_menu"]}, "fields": {"PROPERTY": ["backdrop #", None]}, "shadow": False, "topLevel": True, "x": 997, "y": 754 }, "sensing_of_object_menu": { "opcode": "sensing_of_object_menu", "next": None, "parent": "sensing_of", "inputs": {}, "fields": {"OBJECT": ["_stage_", None]}, "shadow": True, "topLevel": False }, "sensing_current": { "opcode": "sensing_current", "next": None, "parent": None, "inputs": {}, "fields": {"CURRENTMENU": ["YEAR", None]}, "shadow": False, "topLevel": True, "x": 627, "y": 884 }, "sensing_dayssince2000": { "opcode": "sensing_dayssince2000", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 959, "y": 903 }, "sensing_username": { "opcode": "sensing_username", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 833, "y": 757 }, # sound_block.json "sound_playuntildone": { "opcode": "sound_playuntildone", "next": None, "parent": None, "inputs": {"SOUND_MENU": [1, "sound_sounds_menu"]}, "fields": {}, "shadow": False, "topLevel": True, "x": 253, "y": 17 }, "sound_sounds_menu": { "opcode": "sound_sounds_menu", "next": None, "parent": "sound_playuntildone and sound_play", "inputs": {}, "fields": {"SOUND_MENU": ["Meow", None]}, "shadow": True, "topLevel": False }, "sound_play": { "opcode": "sound_play", "next": None, "parent": None, "inputs": {"SOUND_MENU": [1, "sound_sounds_menu"]}, "fields": {}, "shadow": False, "topLevel": True, "x": 245, "y": 122 }, "sound_stopallsounds": { "opcode": "sound_stopallsounds", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 253, "y": 245 }, "sound_changeeffectby": { "opcode": "sound_changeeffectby", "next": None, "parent": None, "inputs": {"VALUE": [1, [4, "10"]]}, "fields": {"EFFECT": ["PITCH", None]}, "shadow": False, "topLevel": True, "x": 653, "y": 14 }, "sound_seteffectto": { "opcode": "sound_seteffectto", "next": None, "parent": None, "inputs": {"VALUE": [1, [4, "100"]]}, "fields": {"EFFECT": ["PITCH", None]}, "shadow": False, "topLevel": True, "x": 653, "y": 139 }, "sound_cleareffects": { "opcode": "sound_cleareffects", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 651, "y": 242 }, "sound_changevolumeby": { "opcode": "sound_changevolumeby", "next": None, "parent": None, "inputs": {"VOLUME": [1, [4, "-10"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 645, "y": 353 }, "sound_setvolumeto": { "opcode": "sound_setvolumeto", "next": None, "parent": None, "inputs": {"VOLUME": [1, [4, "100"]]}, "fields": {}, "shadow": False, "topLevel": True, "x": 1108, "y": 5 }, "sound_volume": { "opcode": "sound_volume", "next": None, "parent": None, "inputs": {}, "fields": {}, "shadow": False, "topLevel": True, "x": 1136, "y": 123 }, } # #Example input with opcodes from various categories # input_opcodes = [ # {"opcode": "sound_play", "count": 2}, # New: Sound block with menu # {"opcode": "sound_playuntildone", "count": 2}, # New: Sound block with menu # ] # Example input with opcodes from various categories input_opcodes = [ {"opcode": "sound_play", "count": 2}, {"opcode": "sound_playuntildone", "count": 2}, {"opcode":"motion_goto","count":2}, {"opcode":"motion_glideto","count":2}, {"opcode":"looks_switchbackdropto","count":2}, {"opcode":"looks_switchcostumeto","count":2}, {"opcode":"control_create_clone_of","count":2}, {"opcode":"sensing_touchingobject","count":2}, {"opcode":"sensing_of","count":2}, {"opcode":"sensing_keypressed","count":2}, {"opcode":"motion_pointtowards","count":2}, ] generated_output = generate_blocks_from_opcodes(input_opcodes, all_block_definitions) print(json.dumps(generated_output, indent=2))