Spaces:
Running on Zero
Running on Zero
Download motion.py from dodoyy/Motion-SVG-Vector: direct link, hf CLI and curl.
- Browser
- Download file 4.32 kB
-
https://huggingface.co/spaces/dodoyy/Motion-SVG-Vector/resolve/main/motion.py
- Command line
-
hf download hf://spaces/dodoyy/Motion-SVG-Vector/motion.py
-
curl -L -o motion.py https://huggingface.co/spaces/dodoyy/Motion-SVG-Vector/resolve/main/motion.py
4.32 kB
| import re | |
| from collections import Counter | |
| from xml.etree import ElementTree as ET | |
| from validator import ANIMATIONS, NS, PAINT, SVGError, parse_svg, seconds, svg_text, view_box | |
| MOTIONS = ("bounce", "float", "rotate", "swing", "pulse", "fade") | |
| HEX_COLOR = re.compile(r"#[0-9a-fA-F]{6}") | |
| def add_motion(root: ET.Element, effect: str, duration: float) -> str: | |
| x, y, width, height = view_box(root) | |
| cx, cy = x + width / 2, y + height / 2 | |
| group = ET.SubElement(root, NS + "g") | |
| for child in list(root)[:-1]: | |
| if child.tag.removeprefix(NS) not in {"defs", "title", "desc", *ANIMATIONS}: | |
| root.remove(child) | |
| group.append(child) | |
| timing = {"dur": f"{duration:g}s", "repeatCount": "indefinite"} | |
| if effect == "fade": | |
| ET.SubElement(group, NS + "animate", { | |
| **timing, "attributeName": "opacity", "values": "1;0.35;1", | |
| }) | |
| return svg_text(root) | |
| if effect == "pulse": | |
| group.set("transform", f"translate({cx:g} {cy:g})") | |
| scaling = ET.SubElement(group, NS + "g") | |
| content = ET.SubElement(scaling, NS + "g", {"transform": f"translate({-cx:g} {-cy:g})"}) | |
| for child in list(group)[:-1]: | |
| group.remove(child) | |
| content.append(child) | |
| group = scaling | |
| effects = { | |
| "bounce": ("translate", f"0 0;0 {-height * 0.10:g};0 0"), | |
| "float": ("translate", f"0 0;0 {-height * 0.045:g};0 0;0 {height * 0.045:g};0 0"), | |
| "rotate": ("rotate", f"0 {cx:g} {cy:g};360 {cx:g} {cy:g}"), | |
| "swing": ("rotate", f"0 {cx:g} {cy:g};12 {cx:g} {cy:g};0 {cx:g} {cy:g};-12 {cx:g} {cy:g};0 {cx:g} {cy:g}"), | |
| "pulse": ("scale", "1;1.08;1"), | |
| } | |
| transform, values = effects[effect] | |
| timing.update({"attributeName": "transform", "type": transform, "values": values}) | |
| if transform != "rotate" or effect == "swing": | |
| segments = values.count(";") | |
| timing.update({ | |
| "calcMode": "spline", | |
| "keyTimes": ";".join(f"{n / segments:g}" for n in range(segments + 1)), | |
| "keySplines": ";".join([".42 0 .58 1"] * segments), | |
| }) | |
| ET.SubElement(group, NS + "animateTransform", timing) | |
| return svg_text(root) | |
| def edit_motion(source: str, speed: float, repeat: str, color: str) -> str: | |
| if not 0.5 <= speed <= 3 or repeat not in {"original", "indefinite", "1", "2", "3", "5"}: | |
| raise SVGError("์๋์ ๋ฐ๋ณต ์ค์ ์ ํ์ธํด ์ฃผ์ธ์.") | |
| root = parse_svg(source, require_animation=True) | |
| color = color.strip() | |
| if color and not HEX_COLOR.fullmatch(color): | |
| raise SVGError("์์์ #7C58DB์ฒ๋ผ 6์๋ฆฌ HEX๋ก ์ ๋ ฅํด ์ฃผ์ธ์.") | |
| palette = Counter( | |
| value.casefold() | |
| for node in root.iter() if node.tag.removeprefix(NS) not in ANIMATIONS | |
| for attr, value in node.attrib.items() | |
| if attr in PAINT and HEX_COLOR.fullmatch(value) | |
| and value.casefold() not in {"#ffffff", "#000000"} | |
| ) | |
| original_color = palette.most_common(1)[0][0] if palette else None | |
| if color and original_color is None: | |
| raise SVGError("์ด SVG์์ ๊ต์ฒดํ 6์๋ฆฌ HEX ์์์ ์ฐพ์ ์ ์์ต๋๋ค.") | |
| for node in root.iter(): | |
| if node.tag.removeprefix(NS) in ANIMATIONS: | |
| if speed != 1: | |
| for attr in ("dur", "begin"): | |
| if node.get(attr) not in (None, "0"): | |
| adjusted = seconds(node.attrib[attr]) / speed | |
| node.set(attr, f"{adjusted:.9f}".rstrip("0").rstrip(".") + "s") | |
| if repeat != "original": | |
| node.set("repeatCount", repeat) | |
| if repeat != "indefinite": | |
| node.set("fill", "freeze") | |
| if color and node.get("attributeName") in PAINT: | |
| for attr in ("values", "from", "to", "by"): | |
| if attr in node.attrib: | |
| node.set(attr, ";".join( | |
| color if part.strip().casefold() == original_color else part | |
| for part in node.attrib[attr].split(";") | |
| )) | |
| elif color: | |
| for attr in PAINT.intersection(node.attrib): | |
| if node.attrib[attr].casefold() == original_color: | |
| node.set(attr, color) | |
| return svg_text(root) | |