Created
July 21, 2015 00:02
-
-
Save anonymous/cd8eaa3e1d55b1399846 to your computer and use it in GitHub Desktop.
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
bl_info = { | |
"name": "Viewport to SVG", | |
"author": "Zeffi, modified by eppo", | |
"version": (0, 1, 2), | |
"blender": (2, 75, 0), | |
"location": "View3D > Properties", | |
"description": "Generate an SVG file from selected objects", | |
"warning": "Objects should have LocRotScale applied -> Ctrl-A menu", | |
"tracker_url": "http://blenderartists.org/forum/showthread.php?375100-Export-%28a-lot-of%29-curves-to-svg", | |
"category": "Render"} | |
import bpy | |
from bpy_extras.view3d_utils import location_3d_to_region_2d as loc3d2d | |
import os | |
def write_svg(context, region): | |
wm = bpy.context.window_manager | |
width, height = region.width, region.height | |
rv3d = context.space_data.region_3d | |
print(wm.path_fn) | |
file_to_write = open(wm.path_fn, 'w') | |
base_string = """\ | |
<svg xmlns="http://www.w3.org/2000/svg" | |
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" | |
xmlns:xlink="http://www.w3.org/1999/xlink" | |
width="%spx" height="%spx" id="svg2" version="1.1">""" % (width, height) | |
file_to_write.write(base_string) | |
for obj in bpy.context.selected_objects: | |
if obj.type == 'MESH': | |
print (obj.name) | |
bpy.context.scene.objects.active = obj | |
vertlist = obj.data.vertices | |
edge_list = [] | |
for edge in obj.data.edges: | |
idx1, idx2 = edge.vertices[:] | |
co1, co2 = vertlist[idx1].co, vertlist[idx2].co | |
co2d_1 = loc3d2d(region, rv3d, co1) | |
co2d_2 = loc3d2d(region, rv3d, co2) | |
edge_list.append((co2d_1, co2d_2)) | |
print("len:",len(edge_list)) | |
group = """ | |
<g id="%s">""" % (obj.name) | |
file_to_write.write(group) | |
nones = 0 | |
for idx, edge in enumerate(edge_list): | |
path_name="path"+str(idx) | |
#print(idx,edge) | |
co1, co2 = edge | |
if type(co1)==type(None) or type(co2)==type(None): | |
nones = nones+1 | |
continue | |
details = """ | |
<path style=" fill:none; | |
stroke:#000000; | |
stroke-width:1; | |
stroke-linecap:butt; | |
stroke-linejoin:miter; | |
stroke-opacity:1; | |
stroke-miterlimit:4; | |
stroke-dasharray:none" | |
d="M %s,%s %s,%s" | |
id="%s"/>""" % ( co1.x, height-co1.y, | |
co2.x, height-co2.y, | |
path_name) | |
file_to_write.write(details) | |
file_to_write.write("</g>") | |
end_file = """</svg>""" | |
file_to_write.write(end_file) | |
file_to_write.close() | |
print("This run had %s Nones" % nones) | |
def draw_data(self, context): | |
region = context.region | |
#obj = context.active_object | |
write_svg(context, region) | |
return | |
class RenderButton(bpy.types.Operator): | |
"""Defines a button""" | |
bl_idname = "svg.render" | |
bl_label = "Renders to svg" | |
country = bpy.props.StringProperty() | |
def execute(self, context): | |
obname = context.active_object.name | |
print('rendering %s' % obname) | |
draw_data(self, context) | |
return{'FINISHED'} | |
class SVGPanel(bpy.types.Panel): | |
"""Creates a Panel in the Object properties window""" | |
bl_label = "Render SVG" | |
bl_idname = "OBJECT_PT_render" | |
bl_space_type = "VIEW_3D" | |
bl_region_type = "UI" | |
bl_context = "object" | |
def draw(self, context): | |
wm = bpy.context.window_manager | |
layout = self.layout | |
obj = context.object | |
row = layout.row() | |
sel=[] | |
for ob in bpy.context.selected_objects: | |
sel.append(ob.name) | |
if len(sel)>3: | |
sel=[] | |
sel.append("A whole lot of Objects") | |
row.label(text="Current Selection: " + str(sel)) | |
# Path selector | |
column = layout.column() | |
column.prop(wm, 'path_fn') | |
# display button | |
self.layout.operator("svg.render", text='Render') | |
classes = [SVGPanel, RenderButton] | |
bpy.types.WindowManager.path_fn=bpy.props.StringProperty(name='', subtype='FILE_PATH', | |
default='\\tmp\\exported.svg', description='Save the SVG file - use absolute path') | |
def register(): | |
for i in classes: | |
bpy.utils.register_class(i) | |
def unregister(): | |
for i in classes: | |
bpy.utils.unregister_class(i) | |
if __name__ == "__main__": | |
register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment