Last active
May 12, 2022 16:47
-
-
Save zeffii/2fdd78613801e5e137874136b1773781 to your computer and use it in GitHub Desktop.
flatterer_decomposer_to_pydata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
>in objects o | |
>in FilePath FP | |
out verts v | |
out edges s | |
""" | |
import pathlib | |
import flatterer | |
from flatterer import svg_export, mesh_analysis, _pack_sort_items | |
from sverchok.core.handlers import get_sv_depsgraph | |
# this can be uncommented if no more edits are being made. | |
exec(bpy.data.texts['svg_decomposer.py'].as_string()) | |
from sverchok.utils import decompose_flatterer_svg | |
depsgraph = get_sv_depsgraph() | |
out_path = pathlib.Path(FilePath[0][0]) | |
# see params in docs/code | |
options = mesh_analysis.Options( | |
laser_width = 0.16, # float | |
material_width = 300.0, # float | |
shape_padding = 3.0, # float | |
reduce_waste = False, # bool | |
pack_sort = _pack_sort_items[0][0], # str | |
pack_may_rotate = True, # bool | |
shape_table = False # bool | |
) | |
svg_export.write(depsgraph, out_path, objects, options) | |
verts, edges = decompose_flatterer_svg(FilePath[0][0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import sverchok | |
import xmltodict | |
import sverchok | |
from sverchok.data_structure import get_edge_loop | |
from ast import literal_eval | |
def decompose_flatterer_svg(path): | |
verts, edges = [], [] | |
def rotate_v2l_f(vlist, f): | |
if f == 0: | |
return vlist | |
vlist = [vtx[:2] for vtx in vlist] | |
theta = np.radians(f) | |
r = np.array(( | |
(np.cos(theta), -np.sin(theta)), | |
(np.sin(theta), np.cos(theta)) | |
)) | |
new_array = np.round(r.dot(np.array(vlist).T).T) | |
new_array = np.hstack((new_array, np.zeros((len(new_array), 1)))) | |
return new_array.tolist() | |
def translate(factor_str, vlist): | |
factor = factor_str.replace("translate", '') | |
x, y = literal_eval(factor) | |
return [(v[0]+x, v[1]+y, v[2]) for v in vlist] | |
def rotate(factor_str, vlist): | |
factor = factor_str.replace("rotate", '') | |
degree = literal_eval(factor) | |
return rotate_v2l_f(vlist, degree) | |
def convert_coords(coords): | |
return [(float(x), float(y), 0.0) for x, y in [xy.split(',') for xy in coords]] | |
def verts_and_edges_from_points(shape, translate_factor, rotate_factor): | |
local_verts = convert_coords(shape['@points'].split(' ')) | |
local_verts = rotate(rotate_factor, local_verts) | |
local_verts = translate(translate_factor, local_verts) | |
local_edges = get_edge_loop(len(local_verts)) | |
return local_verts, local_edges | |
with open(path) as xml: | |
doc = xmltodict.parse(xml.read()) | |
for group in doc['svg']['g']['g']: | |
group_xform = group['@transform'] # <--- not applied yet. | |
translate_factor, rotate_factor = group_xform.split(' ')[:2] | |
polygons = group['polygon'] | |
if isinstance(polygons, dict): # single shape | |
v, e = verts_and_edges_from_points(polygons, translate_factor, rotate_factor) | |
verts.append(v) | |
edges.append(e) | |
elif isinstance(polygons, list): | |
for shape in polygons: | |
v, e = verts_and_edges_from_points(shape, translate_factor, rotate_factor) | |
verts.append(v) | |
edges.append(e) | |
return verts, edges | |
sverchok.utils.decompose_flatterer_svg = decompose_flatterer_svg |
Author
zeffii
commented
May 12, 2022
svg parsing using xml module was failing because of inkscape properties of the svg.
import re
import xml.etree.ElementTree as ET
file_path = r"D:\DOWNLOADS\Bug_raycaster_2022_04_28_17_11\borker.svg"
with open(file_path) as ofile:
file_string = re.sub("inkscape:", '', ofile.read())
root = ET.fromstring(file_string)
for item in root.findall("g"):
for inner_item in item.findall("g"):
a = inner_item.get("transform")
print(a)
for polygon in inner_item.findall("polygon"):
print(polygon.get("points"))
works
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment