minor improvements (show busy cursor, etc.)
This commit is contained in:
parent
9e9df1e36b
commit
cad1df2f58
1 changed files with 25 additions and 18 deletions
|
@ -50,25 +50,26 @@ def pack():
|
||||||
if not image:
|
if not image:
|
||||||
raise Abort('No texture image selected')
|
raise Abort('No texture image selected')
|
||||||
|
|
||||||
imagesize = image.getSize()
|
imgwidth, imgheight = image.getSize()
|
||||||
if imagesize[0] != imagesize[1]:
|
if imgwidth != imgheight:
|
||||||
Blender.Draw.PupMenu("Warning%t|Image isn't a square!")
|
Blender.Draw.PupMenu("Warning%t|Image isn't a square!")
|
||||||
gap = (float(GAP) / imagesize[0], float(GAP) / imagesize[1])
|
gap = (float(GAP) / imgwidth, float(GAP) / imgheight)
|
||||||
margin = (float(MARGIN) / imagesize[0] - gap[0] * 0.5, float(MARGIN) / imagesize[1] - gap[1] * 0.5)
|
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)):
|
def drawrect(x0, y0, x1, y1, color = (255, 255, 255, 255)):
|
||||||
x0 *= imagesize[0]
|
x0 *= imgwidth
|
||||||
y0 *= imagesize[1]
|
x1 *= imgwidth
|
||||||
x1 *= imagesize[0]
|
y0 *= imgheight
|
||||||
y1 *= imagesize[1]
|
y1 *= imgheight
|
||||||
for u in range(int(x0 + 0.5), int(x1 - 0.5)):
|
for u in range(int(x0 + 0.5), int(x1 - 0.5)):
|
||||||
for v in range(int(y0 + 0.5), int(y1 - 0.5)):
|
for v in range(int(y0 + 0.5), int(y1 - 0.5)):
|
||||||
image.setPixelI(u, v, color)
|
image.setPixelI(u, v, color)
|
||||||
|
|
||||||
|
|
||||||
boxes = []
|
boxes = []
|
||||||
meshes = {}
|
meshes = {}
|
||||||
|
|
||||||
|
BIG = 1<<30
|
||||||
Blender.Window.DrawProgressBar(0.0, "packing")
|
Blender.Window.DrawProgressBar(0.0, "packing")
|
||||||
for o in Blender.Scene.GetCurrent().objects.selected:
|
for o in Blender.Scene.GetCurrent().objects.selected:
|
||||||
if o.type != "Mesh":
|
if o.type != "Mesh":
|
||||||
|
@ -83,8 +84,8 @@ def pack():
|
||||||
meshes[mesh.name] = True
|
meshes[mesh.name] = True
|
||||||
|
|
||||||
print "\tobject '%s'" % o.name
|
print "\tobject '%s'" % o.name
|
||||||
xmin = ymin = 1000.0
|
xmin = ymin = BIG
|
||||||
xmax = ymax = -1000.0
|
xmax = ymax = -BIG
|
||||||
for f in mesh.faces:
|
for f in mesh.faces:
|
||||||
for p in f.uv:
|
for p in f.uv:
|
||||||
xmin = min(xmin, p[0])
|
xmin = min(xmin, p[0])
|
||||||
|
@ -94,21 +95,22 @@ def pack():
|
||||||
|
|
||||||
width = xmax - xmin
|
width = xmax - xmin
|
||||||
height = ymax - ymin
|
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:
|
if not boxes:
|
||||||
raise Abort('No mesh objects selected')
|
raise Abort('No mesh objects selected')
|
||||||
|
|
||||||
|
|
||||||
boxsize = Blender.Geometry.BoxPack2D(boxes)
|
boxwidth, boxheight = Blender.Geometry.BoxPack2D(boxes)
|
||||||
xscale = (1.0 - 2.0 * margin[0]) / max(boxsize[0], boxsize[1])
|
boxmax = max(boxwidth, boxheight)
|
||||||
yscale = (1.0 - 2.0 * margin[1]) / max(boxsize[0], boxsize[1])
|
xscale = (1.0 - 2.0 * margin[0]) / boxmax
|
||||||
|
yscale = (1.0 - 2.0 * margin[1]) / boxmax
|
||||||
|
|
||||||
Blender.Window.DrawProgressBar(0.2, "Erasing texture")
|
Blender.Window.DrawProgressBar(0.2, "Erasing texture")
|
||||||
drawrect(0, 0, 1, 1) # erase texture
|
drawrect(0, 0, 1, 1) # erase texture
|
||||||
for box in boxes:
|
for box in boxes:
|
||||||
xmin = ymin = 1000.0
|
xmin = ymin = BIG
|
||||||
xmax = ymax = -1000.0
|
xmax = ymax = -BIG
|
||||||
for f in box[6].faces:
|
for f in box[6].faces:
|
||||||
for p in f.uv:
|
for p in f.uv:
|
||||||
p[0] = (p[0] - box[4] + box[0] + gap[0] * 0.5 + margin[0]) * xscale
|
p[0] = (p[0] - box[4] + box[0] + gap[0] * 0.5 + margin[0]) * xscale
|
||||||
|
@ -121,6 +123,9 @@ def pack():
|
||||||
|
|
||||||
drawrect(xmin, ymin, xmax, ymax, (random.randint(128, 255), random.randint(128, 255),
|
drawrect(xmin, ymin, xmax, ymax, (random.randint(128, 255), random.randint(128, 255),
|
||||||
random.randint(128, 255), 255))
|
random.randint(128, 255), 255))
|
||||||
|
box[6].update()
|
||||||
|
|
||||||
|
Blender.Window.RedrawAll()
|
||||||
Blender.Window.DrawProgressBar(1.0, "Finished")
|
Blender.Window.DrawProgressBar(1.0, "Finished")
|
||||||
|
|
||||||
|
|
||||||
|
@ -128,15 +133,17 @@ def pack():
|
||||||
editmode = Blender.Window.EditMode()
|
editmode = Blender.Window.EditMode()
|
||||||
if editmode:
|
if editmode:
|
||||||
Blender.Window.EditMode(0)
|
Blender.Window.EditMode(0)
|
||||||
|
Blender.Window.WaitCursor(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print "box packing ..."
|
print "box packing ..."
|
||||||
pack()
|
pack()
|
||||||
print "done\n"
|
print "done\n"
|
||||||
except Abort, e:
|
except Abort, e:
|
||||||
|
print "Error:", e.msg, " -> aborting ...\n"
|
||||||
Blender.Draw.PupMenu("Error%t|" + e.msg)
|
Blender.Draw.PupMenu("Error%t|" + e.msg)
|
||||||
print "Error:", e.msg, " -> aborting ...\n"
|
|
||||||
|
|
||||||
|
Blender.Window.WaitCursor(0)
|
||||||
if editmode:
|
if editmode:
|
||||||
Blender.Window.EditMode(1)
|
Blender.Window.EditMode(1)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue