Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active July 16, 2022 18:52
Show Gist options
  • Save zeffii/ab5b0fcb122aae88de91c77fa835f954 to your computer and use it in GitHub Desktop.
Save zeffii/ab5b0fcb122aae88de91c77fa835f954 to your computer and use it in GitHub Desktop.
blender (sverchok) nodetree to svg
import os
import re
import bpy
import sverchok
from dataclasses import dataclass
from xml.etree import ElementTree as et
@dataclass
class NodeProxy():
name: str
label: str
abs_location: tuple
width: float
num_linked_inputs: dict
num_linked_outputs: dict
nt = bpy.data.node_groups['NodeTree']
nt_dict = {}
for n in nt.nodes:
inputs = {s.name: (s.index, s.is_linked) for s in n.inputs}
outputs = {s.name: (s.index, s.is_linked) for s in n.outputs}
nt_dict[n.name] = NodeProxy(n.name, n.label, tuple(int(i) for i in n.absolute_location), n.width, inputs, outputs)
doc = et.Element('svg', width='1480', height='960', version='1.1', xmlns='http://www.w3.org/2000/svg')
gdoc = et.SubElement(doc, "g", transform=f"translate({430}, {330})")
ldoc = et.SubElement(doc, "g", transform=f"translate({430}, {330})")
for k, v in nt_dict.items():
g = et.SubElement(gdoc, "g", transform=f"translate{v.abs_location}")
m = et.SubElement(g, "rect", width=str(v.width), height=str(40), fill='rgb(74, 177, 231)')
t = et.SubElement(g, "text", fill="#333", y="-2", x="3")
t.text = v.name
# style="fill-opacity: .25;")
# https://github.com/nortikin/sverchok/issues/2360
for link in nt.links:
n1, s1, n2, s2 = link.from_node, link.from_socket, link.to_node, link.to_socket
(x1, y1), (x2, y2) = n1.absolute_location, n2.absolute_location
ctrl_1 = int(x1) + n1.width + 180, int(y1)
knot_1 = int(x1) + n1.width + 20, int(y1)
knot_2 = int(x2) - 20, int(y2)
ctrl_2 = int(x2) - 180, int(y2)
dpath = f"M{knot_1} C{ctrl_1} {ctrl_2} {knot_2}"
dpath = re.sub("\(|\)", "", dpath)
path = et.SubElement(ldoc, "path", d=dpath, stroke_width="3.0", stroke="#333", fill="transparent")
svg_filename = "wooooop"
svg_path = os.path.join(bpy.path.abspath('//'), svg_filename + '.svg')
with open(svg_path, 'w') as f:
f.write('\n\n')
f.write(et.tostring(doc).decode())
@zeffii
Copy link
Author

zeffii commented Jul 16, 2022

this shows how to

  • add a group,
  • transform the group,
  • add svg elements to a group.
  • add text to text element
  • add fill
  • how to write to disk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment