Skip to content

Commit d540aed

Browse files
committed
Can now access elements from svg by id
1 parent ca0fec7 commit d540aed

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

manim/mobject/svg/svg_mobject.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def __init__(
127127
self.stroke_color = stroke_color
128128
self.stroke_opacity = stroke_opacity # type: ignore[assignment]
129129
self.stroke_width = stroke_width # type: ignore[assignment]
130+
self.id_to_vgroup_dict: dict[str, VGroup] = {}
130131
if self.stroke_width is None:
131132
self.stroke_width = 0
132133

@@ -205,9 +206,11 @@ def generate_mobject(self) -> None:
205206
svg = se.SVG.parse(modified_file_path)
206207
modified_file_path.unlink()
207208

209+
mobjects_dict = self.get_mobjects_from(svg)
210+
for key, value in mobjects_dict.items():
211+
self.id_to_vgroup_dict[key] = value
212+
self.add(value)
208213

209-
mobjects = self.get_mobjects_from(svg)
210-
self.add(*mobjects)
211214
self.flip(RIGHT) # Flip y
212215

213216
def get_file_path(self) -> Path:
@@ -261,7 +264,7 @@ def generate_config_style_dict(self) -> dict[str, str]:
261264
result[svg_key] = str(svg_default_dict[style_key])
262265
return result
263266

264-
def get_mobjects_from(self, svg: se.SVG) -> list[VMobject]:
267+
def get_mobjects_from(self, svg: se.SVG) -> dict[str, VGroup]:
265268
print("<get_mobjects_from>")
266269
"""Convert the elements of the SVG to a list of mobjects.
267270
@@ -290,47 +293,33 @@ def get_mobjects_from(self, svg: se.SVG) -> list[VMobject]:
290293
group_id_number += 1
291294
pass
292295
if isinstance(element, se.Group):
293-
print(" " * depth, f"create a vgroup with the name: '{ group_name }'")
296+
print(" " * depth, f"create a vgroup with the name: '{group_name}'")
294297
vg = VGroup()
295298
vgroups[group_name] = vg
296299
vgroup_names.append(group_name)
297300
vgroup_stack.append(group_name)
298-
print(" " * depth, "depth: %d" % depth)
299-
print(" " * depth, "vgroup_stack: ", vgroup_stack)
300301
parent_name = vgroup_stack[depth - 1]
301-
print(" " * depth, "parent_name: '%s'" % parent_name)
302302
vgroups[parent_name].add(vgroups[group_name])
303-
print(" " * depth, "Added to vgroup '%s'" % parent_name)
304303

305304
if isinstance(element, (se.Group, se.Use)):
306305
for subelement in element[::-1]:
307306
stack.append((subelement, depth + 1))
308307
# Add element to the parent vgroup
309308
try:
310309
parent_name = vgroup_stack[depth - 2]
311-
print(" " * depth, "parent: ", parent_name)
312-
if isinstance(element, se.Path):
313-
mob: VMobject = self.path_to_mobject(element)
314-
self.apply_style_to_mobject(mob, element)
315-
if isinstance(element, se.Transformable) and element.apply:
316-
self.handle_transform(mob, element.transform)
310+
if isinstance(element, (se.Path)):
311+
mob = self.get_mob_from_shape_element(element)
317312
vgroups[parent_name].add(mob)
318-
print(
319-
" " * depth,
320-
"vgroups['%s']" % parent_name,
321-
vgroups[parent_name],
322-
)
323-
print(" " * depth, "Added to vgroup '%s'" % parent_name)
324313

325314
except Exception as e:
326315
print(e)
327316
print("depth: ", depth)
328317

329-
return vgroups["root"], vgroups
318+
return vgroups
330319

331-
def get_mob_from_shape_element(self, shape):
320+
def get_mob_from_shape_element(self, shape: se.SVGElement) -> VMobject:
332321
if isinstance(shape, se.Group): # noqa: SIM114
333-
assert False, "Should never get here"
322+
raise Exception("Should never get here")
334323
elif isinstance(shape, se.Path):
335324
mob: VMobject = self.path_to_mobject(shape)
336325
elif isinstance(shape, se.SimpleLine):
@@ -346,12 +335,12 @@ def get_mob_from_shape_element(self, shape):
346335
elif isinstance(shape, se.Text):
347336
mob = self.text_to_mobject(shape)
348337
elif isinstance(shape, se.Use) or type(shape) is se.SVGElement:
349-
assert False, "Should never get here - se.Use or se.SVGElement"
338+
raise Exception("Should never get here - se.Use or se.SVGElement")
350339
else:
351340
logger.warning(f"Unsupported element type: {type(shape)}")
352-
assert False, f"Unsupported element type: {type(shape)}"
341+
raise Exception(f"Unsupported element type: {type(shape)}")
353342
if mob is None or not mob.has_points():
354-
assert False, f"mob is empty or have no points"
343+
raise Exception("mob is empty or have no points")
355344
self.apply_style_to_mobject(mob, shape)
356345
if isinstance(shape, se.Transformable) and shape.apply:
357346
self.handle_transform(mob, shape.transform)

0 commit comments

Comments
 (0)