Last active
July 15, 2020 22:10
-
-
Save Kif11/1adb6b2ced7a45e07ca838bc10f17b6c to your computer and use it in GitHub Desktop.
Export Houdini poly lines to SVG
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
# Python script to export Houdini poly lines to SVG | |
# Paste content to Python node | |
# Replace EXPORT_PATH_HERE with desired output file location | |
node = hou.pwd() | |
geo = node.geometry() | |
filename = "EXPORT_PATH_HERE" | |
padding = 50 | |
box = geo.boundingBox() | |
minv = box.minvec() | |
size = box.sizevec() | |
width = size.x() * 1000 | |
height = size.y() * 1000 | |
width += padding | |
height += padding | |
def transform_points(points): | |
for p in points: | |
p = hou.Vector2( | |
(p.x() - minv.x()) / size.x() * width, | |
(1.0 - (p.y() - minv.y()) / size.y()) * height | |
) | |
yield p | |
with open(filename, 'w') as fp: | |
fp.write('<?xml version="1.0" standalone="no"?>\n') | |
fp.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n') | |
fp.write('<svg width="{width:.2f}" height="{height:.2f}" viewBox="{vbX:.2f} {vbY:.2f} {vbW:.2f} {vbH:.2f}" version="1.1" xmlns="http://www.w3.org/2000/svg">\n'.format( | |
width = width, | |
height = height, | |
vbX = -padding, | |
vbY = -padding, | |
vbW = width + padding * 2, | |
vbH = height + padding * 2, | |
)) | |
for prim in geo.iterPrims(): | |
if prim.type() != hou.primType.Polygon: | |
continue | |
points = [v.point().position() for v in prim.vertices()] | |
points = list(transform_points(points)) | |
try: | |
a color = prim.attribValue("Cd") | |
except hou.OperationFailed: # Color attribute doest exist | |
color = (0, 0, 0) | |
try: | |
stroke_width = prim.attribValue("swidth") | |
except hou.OperationFailed: # Pscale attribute doest exist | |
stroke_width = 1 | |
if not points: | |
continue | |
data = 'M{} {} '.format(round(points[0].x(), 3), round(points[0].y(), 3)) | |
for p in points[1:]: | |
data += 'L{} {} '.format(round(p.x(), 3), round(p.y(), 3)) | |
# Close path for closed polys | |
closed = prim.intrinsicValueDict()['closed'] | |
if closed: | |
data += 'Z' | |
fp.write('<path d="{}" stroke="rgb({}, {}, {})" stroke-width="{}px" fill="none"/>\n'.format( | |
data, | |
int(round(color[0] * 255)), | |
int(round(color[1] * 255)), | |
int(round(color[2] * 255)), | |
stroke_width, | |
)) | |
fp.write('</svg>') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment