MCPcopy Create free account
hub / github.com/TimMcCool/scratchattach / from_json

Method from_json

scratchattach/editor/sprite.py:261–343  ·  view source on GitHub ↗
(data: dict)

Source from the content-addressed store, hash-verified

259
260 @staticmethod
261 def from_json(data: dict):
262 _is_stage = data["isStage"]
263 _name = data["name"]
264 _current_costume = data.get("currentCostume", 1)
265 _layer_order = data.get("layerOrder", 1)
266 _volume = data.get("volume", 100)
267
268 # Read VLB
269 def read_idcomponent(attr_name: str, cls: type[base.IDComponent]):
270 _vlbs = []
271 for _vlb_id, _vlb_data in data.get(attr_name, {}).items():
272 _vlbs.append(cls.from_json((_vlb_id, _vlb_data)))
273 return _vlbs
274
275 _variables = read_idcomponent("variables", vlb.Variable)
276 _lists = read_idcomponent("lists", vlb.List)
277 _broadcasts = read_idcomponent("broadcasts", vlb.Broadcast)
278
279 # Read assets
280 _costumes = []
281 for _costume_data in data.get("costumes", []):
282 _costumes.append(asset.Costume.from_json(_costume_data))
283 _sounds = []
284 for _sound_data in data.get("sounds", []):
285 _sounds.append(asset.Sound.from_json(_sound_data))
286
287 # Read comments
288 _comments = read_idcomponent("comments", comment.Comment)
289
290 # Read blocks/prims
291 _prims = {}
292 _blocks = {}
293 for _block_id, _block_data in data.get("blocks", {}).items():
294 if isinstance(_block_data, list):
295 # Prim
296 _prims[_block_id] = prim.Prim.from_json(_block_data)
297 else:
298 # Block
299 _blocks[_block_id] = block.Block.from_json(_block_data)
300
301 # Stage/sprite specific vars
302 _tempo, _video_state, _video_transparency, _text_to_speech_language = (None,) * 4
303 _visible, _x, _y, _size, _direction, _draggable, _rotation_style = (None,) * 7
304 if _is_stage:
305 _tempo = data.get("tempo", 0)
306 _video_state = data.get("videoState", "off")
307 _video_transparency = data.get("videoTransparency", 0)
308 _text_to_speech_language = data.get("textToSpeechLanguage", "en")
309 else:
310 _visible = data["visible"]
311 _x = data.get("x", 0)
312 _y = data.get("y", 0)
313 _size = data.get("size", 100)
314 _direction = data.get("direction", 90)
315 _draggable = data.get("draggable", False)
316 _rotation_style = data.get("rotationStyle", "all-around")
317
318 return Sprite(

Callers 2

read_idcomponentMethod · 0.45
from_sprite3Method · 0.45

Calls 2

SpriteClass · 0.70
getMethod · 0.45

Tested by

no test coverage detected