Skip to content

Commit f568749

Browse files
committed
fix: allow projects to export
1 parent bc36270 commit f568749

5 files changed

Lines changed: 28 additions & 13 deletions

File tree

scratchattach/editor/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
from .asset import Asset, Costume, Sound
6+
from .blockshape import BlockShapes
67
from .project import Project
78
from .extension import Extensions, Extension
89
from .mutation import Mutation, Argument, ArgumentType, parse_proc_code, construct_proccode, ArgTypes

scratchattach/editor/block.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,17 @@ def id(self) -> str:
152152
Work out the id of this block by searching through the sprite dictionary
153153
"""
154154
if self._id:
155-
return self.id
155+
return self._id
156156
# warnings.warn(f"Using block IDs can cause consistency issues and is not recommended")
157157
# This property is used when converting comments to JSON (we don't want random warning when exporting a project)
158158
for _block_id, _block in self.sprite.blocks.items():
159159
if _block is self:
160160
self._id = _block_id
161-
return self.id
161+
return self._id
162162

163163
# Let's just automatically assign ourselves an id
164164
self.sprite.add_block(self)
165-
return self.id
165+
return self._id
166166

167167
@id.setter
168168
def id(self, value: str) -> None:

scratchattach/editor/monitor.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
from typing import Optional, TYPE_CHECKING
44

55
from typing_extensions import deprecated
6+
import warnings
67

78
if TYPE_CHECKING:
89
from . import project
910

10-
from . import base
11+
from . import base, block
1112

1213

1314
class Monitor(base.ProjectSubcomponent):
@@ -33,6 +34,9 @@ def __init__(self, reporter: Optional[base.NamedIDComponent] = None,
3334
"""
3435
assert isinstance(reporter, base.SpriteSubComponent) or reporter is None
3536

37+
if sprite_name is None and hasattr(reporter, 'sprite'):
38+
sprite_name = None if reporter.sprite.is_stage else reporter.sprite.name
39+
3640
self.reporter_id = reporter_id
3741
"""
3842
ID referencing the VLB being referenced. Replaced with None during project instantiation, where the reporter attribute is updated
@@ -142,14 +146,14 @@ def link_using_project(self):
142146
# todo: consider reimplementing this
143147
@deprecated("This method does not work correctly (This may be fixed in the future)")
144148
@staticmethod
145-
def from_reporter(reporter: Block, _id: str = None, mode: str = "default",
149+
def from_reporter(reporter: block.Block, _id: str = None, mode: str = "default",
146150
opcode: str = None, sprite_name: str = None, value=0, width: int | float = 0,
147151
height: int | float = 0,
148152
x: int | float = 5, y: int | float = 5, visible: bool = False, slider_min: int | float = 0,
149153
slider_max: int | float = 100, is_discrete: bool = True, params: dict = None):
150-
if "reporter" not in reporter.stack_type:
154+
if not reporter.block_shape.is_reporter:
151155
warnings.warn(f"{reporter} is not a reporter block; the monitor will return '0'")
152-
elif "(menu)" in reporter.stack_type:
156+
elif reporter.block_shape.is_menu:
153157
warnings.warn(f"{reporter} is a menu block; the monitor will return '0'")
154158
# Maybe add note that length of list doesn't work fsr?? idk
155159
if _id is None:
@@ -166,7 +170,7 @@ def from_reporter(reporter: Block, _id: str = None, mode: str = "default",
166170
params[field.id] = field.value, field.value_id
167171

168172
return Monitor(
169-
_id,
173+
reporter,
170174
mode,
171175
opcode,
172176

scratchattach/editor/project.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(self, _name: Optional[str] = None, _meta: Optional[meta.Meta] = Non
3131
self.name = _name
3232

3333
self.meta = _meta
34-
self.extensions = _extensions
34+
self.extensions: list[extension.Extension] = list(_extensions)
3535
self.monitors = list(_monitors)
3636
self.sprites = list(_sprites)
3737

@@ -69,11 +69,12 @@ def __repr__(self):
6969
return _ret
7070

7171
@property
72-
def stage(self) -> sprite.Sprite:
72+
def stage(self) -> Optional[sprite.Sprite]:
7373
for _sprite in self.sprites:
7474
if _sprite.is_stage:
7575
return _sprite
7676
warnings.warn(f"Could not find stage for {self.name}")
77+
return None
7778

7879
def to_json(self) -> dict:
7980
_json = {
@@ -221,19 +222,23 @@ def from_id(project_id: int, _name: Optional[str] = None):
221222
return _proj
222223

223224
def find_vlb(self, value: str | None, by: str = "name",
224-
multiple: bool = False) -> vlb.Variable | vlb.List | vlb.Broadcast | list[
225-
vlb.Variable | vlb.List | vlb.Broadcast]:
226-
_ret = []
225+
multiple: bool = False) -> Optional[vlb.Variable | vlb.List | vlb.Broadcast | list[
226+
vlb.Variable | vlb.List | vlb.Broadcast]]:
227+
228+
_ret: list[vlb.Variable | vlb.List | vlb.Broadcast] = []
227229
for _sprite in self.sprites:
228230
val = _sprite.find_vlb(value, by, multiple)
229231
if multiple:
230232
_ret += val
231233
else:
232234
if val is not None:
233235
return val
236+
234237
if multiple:
235238
return _ret
236239

240+
return None
241+
237242
def find_sprite(self, value: str | None, by: str = "name",
238243
multiple: bool = False) -> sprite.Sprite | list[sprite.Sprite]:
239244
_ret = []

scratchattach/editor/sprite.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ def link_subcomponents(self):
102102
self.link_prims()
103103
self.link_blocks()
104104
self.link_comments()
105+
self.link_vlbs()
106+
107+
def link_vlbs(self):
108+
for _vlb in self.vlbs:
109+
_vlb.sprite = self
105110

106111
def link_prims(self):
107112
"""

0 commit comments

Comments
 (0)