]> git.mxchange.org Git - flightgear.git/blobdiff - utils/Modeller/uv_pack.py
Merge branch 'ehofman/atc' into next
[flightgear.git] / utils / Modeller / uv_pack.py
index 64fd6c370dc47c3e052137541d5e3c96119caca7..245a429c8e6778f620e44509309b329b455a9854 100644 (file)
@@ -33,11 +33,32 @@ Usage:
 (7) export UV layout to SVG (UVs->Scripts->Save UV Face Layout)
 """
 
+
+#--------------------------------------------------------------------------------
+# 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.
+#--------------------------------------------------------------------------------
+
+
 MARGIN = 10 # px
 GAP = 10    # px
 
 
-import Blender, math, random
+import Blender
+from random import randint as rand
 
 
 class Abort(Exception):
@@ -50,25 +71,26 @@ def pack():
        if not image:
                raise Abort('No texture image selected')
 
-       imagesize = image.getSize()
-       if imagesize[0] != imagesize[1]:
+       imgwidth, imgheight = image.getSize()
+       if imgwidth != imgheight:
                Blender.Draw.PupMenu("Warning%t|Image isn't a square!")
-       gap = (float(GAP) / imagesize[0], float(GAP) / imagesize[1])
-       margin = (float(MARGIN) / imagesize[0] - gap[0] * 0.5, float(MARGIN) / imagesize[1] - gap[1] * 0.5)
-
+       gap = (float(GAP) / imgwidth, float(GAP) / imgheight)
+       margin = (float(MARGIN) / imgwidth - gap[0] * 0.5, float(MARGIN) / imgheight - gap[1] * 0.5)
 
        def drawrect(x0, y0, x1, y1, color = (255, 255, 255, 255)):
-               x0 *= imagesize[0]
-               y0 *= imagesize[1]
-               x1 *= imagesize[0]
-               y1 *= imagesize[1]
+               x0 *= imgwidth
+               x1 *= imgwidth
+               y0 *= imgheight
+               y1 *= imgheight
                for u in range(int(x0 + 0.5), int(x1 - 0.5)):
                        for v in range(int(y0 + 0.5), int(y1 - 0.5)):
                                image.setPixelI(u, v, color)
 
+
        boxes = []
-       meshes = {}
+       unique_meshes = {}
 
+       BIG = 1<<30
        Blender.Window.DrawProgressBar(0.0, "packing")
        for o in Blender.Scene.GetCurrent().objects.selected:
                if o.type != "Mesh":
@@ -77,14 +99,14 @@ def pack():
                mesh = o.getData(mesh = 1)
                if not mesh.faceUV:
                        continue
-               if mesh.name in meshes:
+               if mesh.name in unique_meshes:
                        #print "dropping duplicate mesh", mesh.name, "of object", o.name
                        continue
-               meshes[mesh.name] = True
+               unique_meshes[mesh.name] = True
 
                print "\tobject '%s'" % o.name
-               xmin = ymin = 1000.0
-               xmax = ymax = -1000.0
+               xmin = ymin = BIG
+               xmax = ymax = -BIG
                for f in mesh.faces:
                        for p in f.uv:
                                xmin = min(xmin, p[0])
@@ -94,21 +116,24 @@ def pack():
 
                width = xmax - xmin
                height = ymax - ymin
-               boxes.append([0, 0, width + gap[0], height + gap[1], xmin, ymin, mesh, o.name])
+               boxes.append([0, 0, width + gap[0], height + gap[1], xmin, ymin, mesh])
 
        if not boxes:
                raise Abort('No mesh objects selected')
 
 
-       boxsize = Blender.Geometry.BoxPack2D(boxes)
-       xscale = (1.0 - 2.0 * margin[0]) / max(boxsize[0], boxsize[1])
-       yscale = (1.0 - 2.0 * margin[1]) / max(boxsize[0], boxsize[1])
+       boxwidth, boxheight = Blender.Geometry.BoxPack2D(boxes)
+       boxmax = max(boxwidth, boxheight)
+       xscale = (1.0 - 2.0 * margin[0]) / boxmax
+       yscale = (1.0 - 2.0 * margin[1]) / boxmax
+
+       image.reload()
+       #drawrect(0, 0, 1, 1) # erase texture
 
-       Blender.Window.DrawProgressBar(0.2, "Erasing texture")
-       drawrect(0, 0, 1, 1) # erase texture
-       for box in boxes:
-               xmin = ymin = 1000.0
-               xmax = ymax = -1000.0
+       for i, box in enumerate(boxes):
+               Blender.Window.DrawProgressBar(float(i) * len(boxes), "Drawing")
+               xmin = ymin = BIG
+               xmax = ymax = -BIG
                for f in box[6].faces:
                        for p in f.uv:
                                p[0] = (p[0] - box[4] + box[0] + gap[0] * 0.5 + margin[0]) * xscale
@@ -119,8 +144,10 @@ def pack():
                                ymin = min(ymin, p[1])
                                ymax = max(ymax, p[1])
 
-               drawrect(xmin, ymin, xmax, ymax, (random.randint(128, 255), random.randint(128, 255),
-                               random.randint(128, 255), 255))
+               drawrect(xmin, ymin, xmax, ymax, (rand(128, 255), rand(128, 255), rand(128, 255), 255))
+               box[6].update()
+
+       Blender.Window.RedrawAll()
        Blender.Window.DrawProgressBar(1.0, "Finished")
 
 
@@ -128,15 +155,17 @@ def pack():
 editmode = Blender.Window.EditMode()
 if editmode:
        Blender.Window.EditMode(0)
+Blender.Window.WaitCursor(1)
 
 try:
        print "box packing ..."
        pack()
        print "done\n"
 except Abort, e:
+       print "Error:", e.msg, "  -> aborting ...\n"
        Blender.Draw.PupMenu("Error%t|" + e.msg)
-       print "Error:", e.msg, " -> aborting ...\n"
 
+Blender.Window.WaitCursor(0)
 if editmode:
        Blender.Window.EditMode(1)