X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=utils%2FModeller%2Fuv_export_svg.py;h=af703793cbef6da21065c87815508c71b1758f09;hb=1d097cc57586eedab7b16d6c77f17009615457e3;hp=fd70aca29822547987fdb201d53b3f7ea66d2dff;hpb=580f65fcb2faea65ecbd72d8170bbf5088e60c81;p=flightgear.git diff --git a/utils/Modeller/uv_export_svg.py b/utils/Modeller/uv_export_svg.py index fd70aca29..af703793c 100644 --- a/utils/Modeller/uv_export_svg.py +++ b/utils/Modeller/uv_export_svg.py @@ -1,14 +1,14 @@ #!BPY # """ -# Name: 'SVG: Export UV layout to SVG file' +# Name: 'UV: Export to SVG' # Blender: 245 -# Group: 'UV' +# Group: 'Image' # Tooltip: 'Export selected objects to SVG file' # """ __author__ = "Melchior FRANZ < mfranz # aon : at >" -__url__ = "http://members.aon.at/mfranz/flightgear/" +__url__ = ["http://www.flightgear.org/", "http://cvs.flightgear.org/viewvc/source/utils/Modeller/uv_export_svg.py"] __version__ = "0.1" __bpydoc__ = """\ Saves the UV mappings of all selected objects to an SVG file. The uv_import_svg.py @@ -16,11 +16,30 @@ script can be used to re-import such a file. Each object and each group of adjac faces therein will be made a separate SVG group. """ -ID_SEPARATOR = '_.._' -FILL_COLOR = 'yellow' +#-------------------------------------------------------------------------------- +# Copyright (C) 2008 Melchior FRANZ < mfranz # aon : at > +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +#-------------------------------------------------------------------------------- -import Blender, sys +FILL_COLOR = None # 'yellow' or '#ffa000' e.g. for uni-color, None for random color +ID_SEPARATOR = '_.:._' + + +import Blender, BPyMessages, sys, random class Abort(Exception): @@ -28,106 +47,113 @@ class Abort(Exception): self.msg = msg -def get_adjacent_faces(pool): - if not len(pool): - return [] - - i, face = pool.popitem() - group = [face] - - uvcoords = {} - for c in face.uv: - uvcoords[(c[0], c[1])] = True - - while True: - found = [] - for face in pool.itervalues(): - for c in face.uv: - if (c[0], c[1]) in uvcoords: - for d in face.uv: - uvcoords[(d[0], d[1])] = True - found.append(face) - break - if not found: - break - for face in found: - group.append(face) - del pool[face.index] - - return group - - -def write_svg(filename): +class UVFaceGroups: + def __init__(self, mesh): + faces = dict([(f.index, f) for f in mesh.faces]) + self.groups = [] + while faces: + self.groups.append(self.adjacent(faces)) + + def __len__(self): + return len(self.groups) + + def __iter__(self): + return self.groups.__iter__() + + def adjacent(self, faces): + uvcoords = {} + face = faces.popitem()[1] + group = [face] + for c in face.uv: + uvcoords[tuple(c)] = True + + while True: + found = [] + for face in faces.itervalues(): + for c in face.uv: + if tuple(c) in uvcoords: + for c in face.uv: + uvcoords[tuple(c)] = True + found.append(face) + break + if not found: + return group + for face in found: + group.append(face) + del faces[face.index] + + +def stringcolor(string): + random.seed(hash(string)) + c = [random.randint(220, 255), random.randint(120, 240), random.randint(120, 240)] + random.shuffle(c) + return "#%02x%02x%02x" % tuple(c) + + +def write_svg(path): size = Blender.Draw.PupMenu("Image size%t|128|256|512|1024|2048|4096|8192") if size < 0: raise Abort('no image size chosen') size = 1 << (size + 6) - print "exporting to '%s' (size %d) ... " % (filename, size), - svg = open(filename, "w") + svg = open(path, "w") svg.write('\n') svg.write('\n\n') svg.write('\n' % (size, size, size, size)) - svg.write("\tuv_export_svg.py: %s\n" % filename); + svg.write("\tuv_export_svg.py: %s\n" % path); svg.write('\t\n' % (size, size, 1.0)) - unique_meshes = {} + objects = {} for o in Blender.Scene.GetCurrent().objects.selected: if o.type != "Mesh": continue mesh = o.getData(mesh = 1) - if not mesh.faceUV: - continue - if mesh.name in unique_meshes: - continue - unique_meshes[mesh.name] = True - - svg.write('\t\n' % (FILL_COLOR, o.name, o.name)) + if mesh.faceUV: + objects[mesh.name] = (o.name, mesh) - pool = {} - for f in mesh.faces: - pool[f.index] = f + for meshname, v in objects.iteritems(): + objname, mesh = v + color = FILL_COLOR or stringcolor(meshname) - groups = [] - while len(pool): - groups.append(get_adjacent_faces(pool)) + svg.write('\t\n' % (color, objname, objname)) - for faces in groups: + facegroups = UVFaceGroups(mesh) + for faces in facegroups: indent = '\t\t' - if len(groups) > 1: + if len(facegroups) > 1: svg.write('\t\t\n') indent = '\t\t\t' for f in faces: - svg.write('%s\n') - if len(groups) > 1: + if len(facegroups) > 1: svg.write('\t\t\n') svg.write("\t\n") svg.write('\n') svg.close() - print "done." -def export(filename): - registry = {} - registry[basename] = Blender.sys.basename(filename) - Blender.Registry.SetKey("UVImportExportSVG", registry, False) +def export(path): + if not BPyMessages.Warning_SaveOver(path): + return editmode = Blender.Window.EditMode() if editmode: Blender.Window.EditMode(0) try: - write_svg(filename) + write_svg(path) + Blender.Registry.SetKey("UVImportExportSVG", { "path" : path }, False) + except Abort, e: print "Error:", e.msg, " -> aborting ...\n" Blender.Draw.PupMenu("Error%t|" + e.msg) @@ -136,8 +162,13 @@ def export(filename): Blender.Window.EditMode(1) -active = Blender.Scene.GetCurrent().objects.active -(basename, extname) = Blender.sys.splitext(Blender.Get("filename")) -filename = Blender.sys.basename(basename) + "-" + active.name + ".svg" -Blender.Window.FileSelector(export, "Export to SVG", filename) +registry = Blender.Registry.GetKey("UVImportExportSVG", False) +if registry and "path" in registry: + path = registry["path"] +else: + active = Blender.Scene.GetCurrent().objects.active + basename = Blender.sys.basename(Blender.sys.splitext(Blender.Get("filename"))[0]) + path = basename + "-" + active.name + ".svg" + +Blender.Window.FileSelector(export, "Export to SVG", path)