X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=utils%2FModeller%2Fuv_import_svg.py;h=b366d41255d848a87035d99a97c0ed298653d631;hb=50415490e602bb0cb638e528b1ca1db16cf7a2f0;hp=e934be5eeeb227e90b0e446dbc006d51a0ae44f8;hpb=615d5ef4d8244e7ab3da8908d84f46593157d4c6;p=flightgear.git diff --git a/utils/Modeller/uv_import_svg.py b/utils/Modeller/uv_import_svg.py index e934be5ee..b366d4125 100755 --- a/utils/Modeller/uv_import_svg.py +++ b/utils/Modeller/uv_import_svg.py @@ -1,32 +1,49 @@ #!BPY # """ -# Name: 'SVG: Re-Import UV layout from SVG file' +# Name: 'UV: (Re)Import UV from SVG' # Blender: 245 -# Group: 'UV' +# Group: 'Image' # Tooltip: 'Re-import UV layout from SVG file' # """ __author__ = "Melchior FRANZ < mfranz # aon : at >" -__url__ = "http://members.aon.at/mfranz/flightgear/" -__version__ = "0.1" +__url__ = ["http://www.flightgear.org/", "http://cvs.flightgear.org/viewvc/source/utils/Modeller/uv_import_svg.py"] +__version__ = "0.2" __bpydoc__ = """\ Imports an SVG file containing UV maps, which has been saved by the uv_export.svg script. This allows to move, scale, and rotate object -mapping in SVG editors like Inkscape. Note that all contained UV maps +mappings in SVG editors like Inkscape. Note that all contained UV maps will be set, no matter which objects are actually selected at the moment. The choice has been made when the file was saved! """ -ID_SEPARATOR = '_____' +#-------------------------------------------------------------------------------- +# 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, math, re -from xml.sax import saxexts +ID_SEPARATOR = '_.:._' + + +import Blender, BPyMessages, sys, math, re, xml.sax -registry = {} numwsp = re.compile('(?<=[\d.])\s+(?=[-+.\d])') commawsp = re.compile('\s+|\s*,\s*') istrans = re.compile('^\s*(skewX|skewY|scale|translate|rotate|matrix)\s*\(([^\)]*)\)\s*') @@ -46,18 +63,16 @@ class Matrix: return "[Matrix %f %f %f %f %f %f]" % (self.a, self.b, self.c, self.d, self.e, self.f) def multiply(self, mat): - a = self.a * mat.a + self.c * mat.b - b = self.b * mat.a + self.d * mat.b - c = self.a * mat.c + self.c * mat.d - d = self.b * mat.c + self.d * mat.d - e = self.a * mat.e + self.c * mat.f + self.e - f = self.b * mat.e + self.d * mat.f + self.f + a = mat.a * self.a + mat.c * self.b + b = mat.b * self.a + mat.d * self.b + c = mat.a * self.c + mat.c * self.d + d = mat.b * self.c + mat.d * self.d + e = mat.a * self.e + mat.c * self.f + mat.e + f = mat.b * self.e + mat.d * self.f + mat.f self.a = a; self.b = b; self.c = c; self.d = d; self.e = e; self.f = f def transform(self, u, v): - x = u * self.a + v * self.c + self.e - y = u * self.b + v * self.d + self.f - return (x, y) + return u * self.a + v * self.c + self.e, u * self.b + v * self.d + self.f def translate(self, dx, dy): self.multiply(Matrix(1, 0, 0, 1, dx, dy)) @@ -135,7 +150,7 @@ def parse_transform(s): elif cmd == "matrix": if num == 6: - matrix.multiply(Matrix(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5])) + matrix.multiply(Matrix(*arg)) continue else: @@ -149,7 +164,7 @@ def parse_transform(s): return matrix -class import_svg: +class import_svg(xml.sax.handler.ContentHandler): # err_handler def error(self, exception): raise Abort(str(exception)) @@ -182,15 +197,18 @@ class import_svg: def endDocument(self): pass - def characters(self, data, start, length): + def characters(self, data): if not self.scandesc: return - if data[start:start + length].startswith("uv_export_svg.py"): + if data.startswith("uv_export_svg.py"): self.verified = True def ignorableWhitespace(self, data, start, length): pass + def processingInstruction(self, target, data): + pass + def startElement(self, name, attrs): currmat = self.matrices[-1] if "transform" in attrs: @@ -219,7 +237,7 @@ class import_svg: def endElement(self, name): self.scandesc = False - self.matrices = self.matrices[:-1] + self.matrices.pop() def handlePolygon(self, attrs): if not self.verified: @@ -231,14 +249,12 @@ class import_svg: print('bad polygon "%s"' % ident) return - sep = ident.find(ID_SEPARATOR) - if sep < 0: + try: + meshname, num = ident.strip().split(ID_SEPARATOR, 2) + except: print('broken id "%s"' % ident) return - meshname = str(ident[:sep]) - num = int(ident[sep + len(ID_SEPARATOR):]) - if not meshname in self.meshes: print('unknown mesh "%s"' % meshname) return @@ -254,22 +270,23 @@ class import_svg: u, v = matrix.transform(u, v) transuv.append((u / self.width, 1 - v / self.height)) - for i, uv in enumerate(self.meshes[meshname].faces[num].uv): + for i, uv in enumerate(self.meshes[meshname].faces[int(num)].uv): uv[0] = transuv[i][0] uv[1] = transuv[i][1] -def run_parser(filename): +def run_parser(path): + if BPyMessages.Error_NoFile(path): + return + editmode = Blender.Window.EditMode() if editmode: Blender.Window.EditMode(0) Blender.Window.WaitCursor(1) try: - svg = saxexts.ParserFactory().make_parser("xml.sax.drivers.drv_xmlproc") - svg.setDocumentHandler(import_svg()) - svg.setErrorHandler(import_svg()) - svg.parse(filename) + xml.sax.parse(path, import_svg(), import_svg()) + Blender.Registry.SetKey("UVImportExportSVG", { "path" : path }, False) except Abort, e: print "Error:", e.msg, " -> aborting ...\n" @@ -281,13 +298,11 @@ def run_parser(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" - registry = Blender.Registry.GetKey("UVImportExportSVG", False) -if registry and basename in registry: - filename = registry[basename] +if registry and "path" in registry and Blender.sys.exists(Blender.sys.expandpath(registry["path"])): + path = registry["path"] +else: + path = "" -Blender.Window.FileSelector(run_parser, "Import SVG", filename) +Blender.Window.FileSelector(run_parser, "Import SVG", path)