2006-05-19 15:56:14 +00:00
|
|
|
#!BPY
|
|
|
|
|
|
|
|
"""
|
2007-02-15 12:13:13 +00:00
|
|
|
Name: 'Consolidate into one image'
|
|
|
|
Blender: 243
|
2006-07-28 02:38:02 +00:00
|
|
|
Group: 'Image'
|
2006-05-19 15:56:14 +00:00
|
|
|
Tooltip: 'Pack all texture images into 1 image and remap faces.'
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = "Campbell Barton"
|
|
|
|
__url__ = ("blender", "blenderartists.org")
|
2007-02-15 12:13:13 +00:00
|
|
|
__version__ = "1.1 2007/02/15"
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
__bpydoc__ = """\
|
|
|
|
This script makes a new image from the used areas of all the images mapped to the selected mesh objects.
|
|
|
|
Image are packed into 1 new image that is assigned to the original faces.
|
|
|
|
This is usefull for game models where 1 image is faster then many, and saves the labour of manual texture layout in an image editor.
|
|
|
|
|
|
|
|
"""
|
|
|
|
# --------------------------------------------------------------------------
|
|
|
|
# Auto Texture Layout v1.0 by Campbell Barton (AKA Ideasman)
|
|
|
|
# --------------------------------------------------------------------------
|
|
|
|
# ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
#
|
|
|
|
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
2007-02-14 01:03:32 +00:00
|
|
|
#
|
2006-05-19 15:56:14 +00:00
|
|
|
# ***** END GPL LICENCE BLOCK *****
|
|
|
|
# --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
# Function to find all the images we use
|
|
|
|
import Blender as B
|
2006-05-28 10:48:50 +00:00
|
|
|
from Blender.Mathutils import Vector, RotationMatrix
|
2006-05-19 15:56:14 +00:00
|
|
|
from Blender.Scene import Render
|
2006-05-28 10:48:50 +00:00
|
|
|
import BPyMathutils
|
2006-05-30 10:15:01 +00:00
|
|
|
BIGNUM= 1<<30
|
2007-02-15 12:13:13 +00:00
|
|
|
TEXMODE= B.Mesh.FaceModes.TEX
|
2006-05-28 10:48:50 +00:00
|
|
|
|
2006-05-30 10:15:01 +00:00
|
|
|
def pointBounds(points):
|
|
|
|
'''
|
|
|
|
Takes a list of points and returns the
|
|
|
|
area, center, bounds
|
|
|
|
'''
|
|
|
|
ymax= xmax= -BIGNUM
|
|
|
|
ymin= xmin= BIGNUM
|
|
|
|
|
|
|
|
for p in points:
|
|
|
|
x= p.x
|
|
|
|
y= p.y
|
|
|
|
|
|
|
|
if x>xmax: xmax=x
|
|
|
|
if y>ymax: ymax=y
|
|
|
|
|
|
|
|
if x<xmin: xmin=x
|
|
|
|
if y<ymin: ymin=y
|
|
|
|
|
|
|
|
# area and center
|
|
|
|
return\
|
|
|
|
(xmax-xmin) * (ymax-ymin),\
|
|
|
|
Vector((xmin+xmax)/2, (ymin+ymax)/2),\
|
|
|
|
(xmin, ymin, xmax, ymax)
|
2006-05-28 10:48:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def bestBoundsRotation(current_points):
|
2006-05-30 10:15:01 +00:00
|
|
|
'''
|
|
|
|
Takes a list of points and returns the best rotation for those points
|
|
|
|
so they fit into the samllest bounding box
|
|
|
|
'''
|
|
|
|
|
|
|
|
current_area, cent, bounds= pointBounds(current_points)
|
2006-05-28 10:48:50 +00:00
|
|
|
|
2006-05-30 10:15:01 +00:00
|
|
|
total_rot_angle= 0.0
|
|
|
|
rot_angle= 45
|
2006-05-28 10:48:50 +00:00
|
|
|
while rot_angle > 0.1:
|
|
|
|
mat_pos= RotationMatrix( rot_angle, 2)
|
|
|
|
mat_neg= RotationMatrix( -rot_angle, 2)
|
|
|
|
|
|
|
|
new_points_pos= [v*mat_pos for v in current_points]
|
2006-05-30 10:15:01 +00:00
|
|
|
area_pos, cent_pos, bounds_pos= pointBounds(new_points_pos)
|
|
|
|
|
|
|
|
# 45d rotations only need to be tested in 1 direction.
|
|
|
|
if rot_angle == 45:
|
|
|
|
area_neg= area_pos
|
|
|
|
else:
|
|
|
|
new_points_neg= [v*mat_neg for v in current_points]
|
|
|
|
area_neg, cent_neg, bounds_neg= pointBounds(new_points_neg)
|
|
|
|
|
2006-05-28 10:48:50 +00:00
|
|
|
|
|
|
|
# Works!
|
|
|
|
#print 'Testing angle', rot_angle, current_area, area_pos, area_neg
|
|
|
|
|
|
|
|
best_area= min(area_pos, area_neg, current_area)
|
|
|
|
if area_pos == best_area:
|
|
|
|
current_area= area_pos
|
2006-05-30 10:15:01 +00:00
|
|
|
cent= cent_pos
|
|
|
|
bounds= bounds_pos
|
2006-05-28 10:48:50 +00:00
|
|
|
current_points= new_points_pos
|
2006-05-30 10:15:01 +00:00
|
|
|
total_rot_angle+= rot_angle
|
|
|
|
elif rot_angle != 45 and area_neg == best_area:
|
2006-05-28 10:48:50 +00:00
|
|
|
current_area= area_neg
|
2006-05-30 10:15:01 +00:00
|
|
|
cent= cent_neg
|
|
|
|
bounds= bounds_neg
|
2006-05-28 10:48:50 +00:00
|
|
|
current_points= new_points_neg
|
2006-05-30 10:15:01 +00:00
|
|
|
total_rot_angle-= rot_angle
|
2006-05-28 10:48:50 +00:00
|
|
|
|
|
|
|
rot_angle *= 0.5
|
|
|
|
|
|
|
|
# Return the optimal rotation.
|
2006-05-30 10:15:01 +00:00
|
|
|
return total_rot_angle
|
|
|
|
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
class faceGroup(object):
|
2006-05-30 10:15:01 +00:00
|
|
|
'''
|
|
|
|
A Group of faces that all use the same image, each group has its UVs packed into a square.
|
|
|
|
'''
|
2006-05-19 15:56:14 +00:00
|
|
|
__slots__= 'xmax', 'ymax', 'xmin', 'ymin',\
|
2006-05-28 10:48:50 +00:00
|
|
|
'image', 'faces', 'box_pack', 'size', 'ang', 'rot_mat', 'cent'\
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
def __init__(self, mesh_list, image, size, PREF_IMAGE_MARGIN):
|
|
|
|
self.image= image
|
|
|
|
self.size= size
|
2007-02-15 12:13:13 +00:00
|
|
|
self.faces= [f for me in mesh_list for f in me.faces if f.mode & TEXMODE and f.image == image]
|
2006-05-28 10:48:50 +00:00
|
|
|
|
|
|
|
# Find the best rotation.
|
2006-05-30 10:15:01 +00:00
|
|
|
all_points= [uv for f in self.faces for uv in f.uv]
|
|
|
|
bountry_indicies= BPyMathutils.convexHull(all_points)
|
|
|
|
bountry_points= [all_points[i] for i in bountry_indicies]
|
2006-05-28 10:48:50 +00:00
|
|
|
|
2006-05-30 10:15:01 +00:00
|
|
|
# Pre Rotation bounds
|
|
|
|
self.cent= pointBounds(bountry_points)[1]
|
2006-05-28 10:48:50 +00:00
|
|
|
|
2006-05-30 10:15:01 +00:00
|
|
|
# Get the optimal rotation angle
|
|
|
|
self.ang= bestBoundsRotation(bountry_points)
|
|
|
|
self.rot_mat= RotationMatrix(self.ang, 2), RotationMatrix(-self.ang, 2)
|
2006-05-28 10:48:50 +00:00
|
|
|
|
2006-05-30 10:15:01 +00:00
|
|
|
# Post rotation bounds
|
|
|
|
bounds= pointBounds([\
|
|
|
|
((uv-self.cent) * self.rot_mat[0]) + self.cent\
|
|
|
|
for uv in bountry_points])[2]
|
2006-05-19 15:56:14 +00:00
|
|
|
|
2006-05-30 10:15:01 +00:00
|
|
|
# Break the bounds into useable values.
|
|
|
|
xmin, ymin, xmax, ymax= bounds
|
2006-05-28 10:48:50 +00:00
|
|
|
|
2006-05-30 10:15:01 +00:00
|
|
|
# Store the bounds, include the margin.
|
2006-05-28 10:48:50 +00:00
|
|
|
# The bounds rect will need to be rotated to the rotation angle.
|
2006-05-19 15:56:14 +00:00
|
|
|
self.xmax= xmax + (PREF_IMAGE_MARGIN/size[0])
|
|
|
|
self.xmin= xmin - (PREF_IMAGE_MARGIN/size[0])
|
|
|
|
self.ymax= ymax + (PREF_IMAGE_MARGIN/size[1])
|
|
|
|
self.ymin= ymin - (PREF_IMAGE_MARGIN/size[1])
|
|
|
|
|
|
|
|
self.box_pack=[\
|
2007-07-09 17:33:38 +00:00
|
|
|
0.0, 0.0,\
|
2006-05-19 15:56:14 +00:00
|
|
|
size[0]*(self.xmax - self.xmin),\
|
2007-07-09 17:33:38 +00:00
|
|
|
size[1]*(self.ymax - self.ymin),\
|
|
|
|
image.name]
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
# default.
|
|
|
|
self.scale= 1.0
|
|
|
|
|
|
|
|
def set_worldspace_scale(self):
|
|
|
|
scale_uv= 0.0
|
|
|
|
scale_3d= 0.0
|
|
|
|
for f in self.faces:
|
|
|
|
for i in xrange(len(f.v)):
|
|
|
|
scale_uv+= (f.uv[i]-f.uv[i-1]).length * 0.1
|
|
|
|
scale_3d+= (f.v[i].co-f.v[i-1].co).length * 0.1
|
|
|
|
self.scale= scale_3d/scale_uv
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def move2packed(self, width, height):
|
|
|
|
'''
|
|
|
|
Moves the UV coords to their packed location
|
|
|
|
using self.box_pack as the offset, scaler.
|
|
|
|
box_pack must be set to its packed location.
|
|
|
|
width and weight are the w/h of the overall packed area's bounds.
|
|
|
|
'''
|
|
|
|
# packedLs is a list of [(anyUniqueID, left, bottom, width, height)...]
|
|
|
|
# Width and height in float pixel space.
|
|
|
|
|
|
|
|
# X Is flipped :/
|
|
|
|
#offset_x= (1-(self.box_pack[1]/d)) - (((self.xmax-self.xmin) * self.image.size[0])/d)
|
2007-07-09 17:33:38 +00:00
|
|
|
offset_x= self.box_pack[0]/width
|
|
|
|
offset_y= self.box_pack[1]/height
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
for f in self.faces:
|
|
|
|
for uv in f.uv:
|
2006-05-28 10:48:50 +00:00
|
|
|
uv_rot= ((uv-self.cent) * self.rot_mat[0]) + self.cent
|
|
|
|
uv.x= offset_x+ (((uv_rot.x-self.xmin) * self.size[0])/width)
|
|
|
|
uv.y= offset_y+ (((uv_rot.y-self.ymin) * self.size[1])/height)
|
2006-05-19 15:56:14 +00:00
|
|
|
|
2007-02-15 12:13:13 +00:00
|
|
|
def consolidate_mesh_images(mesh_list, scn, PREF_IMAGE_PATH, PREF_IMAGE_SIZE, PREF_KEEP_ASPECT, PREF_IMAGE_MARGIN): #, PREF_SIZE_FROM_UV=True):
|
2007-07-09 17:33:38 +00:00
|
|
|
'''
|
|
|
|
Main packing function
|
|
|
|
|
|
|
|
All meshes from mesh_list must have faceUV else this function will fail.
|
|
|
|
'''
|
2006-05-19 15:56:14 +00:00
|
|
|
face_groups= {}
|
|
|
|
|
|
|
|
for me in mesh_list:
|
|
|
|
for f in me.faces:
|
2007-02-15 12:13:13 +00:00
|
|
|
if f.mode & TEXMODE:
|
|
|
|
image= f.image
|
|
|
|
if image:
|
2006-05-19 15:56:14 +00:00
|
|
|
try:
|
2007-02-15 12:13:13 +00:00
|
|
|
face_groups[image.name] # will fail if teh groups not added.
|
2006-05-19 15:56:14 +00:00
|
|
|
except:
|
2007-02-15 12:13:13 +00:00
|
|
|
try:
|
|
|
|
size= image.size
|
|
|
|
except:
|
|
|
|
B.Draw.PupMenu('Aborting: Image cold not be loaded|' + image.name)
|
|
|
|
return
|
|
|
|
|
|
|
|
face_groups[image.name]= faceGroup(mesh_list, image, size, PREF_IMAGE_MARGIN)
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
if not face_groups:
|
2007-07-09 17:33:38 +00:00
|
|
|
B.Draw.PupMenu('No Images found in mesh(es). Aborting!')
|
2006-05-19 15:56:14 +00:00
|
|
|
return
|
|
|
|
|
2006-05-30 10:15:01 +00:00
|
|
|
if len(face_groups)<2:
|
2007-07-09 17:33:38 +00:00
|
|
|
B.Draw.PupMenu('Only 1 image found|Select a mesh(es) using 2 or more images.')
|
2006-05-19 15:56:14 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
'''
|
|
|
|
if PREF_SIZE_FROM_UV:
|
|
|
|
for fg in face_groups.itervalues():
|
|
|
|
fg.set_worldspace_scale()
|
|
|
|
'''
|
|
|
|
|
|
|
|
# RENDER THE FACES.
|
|
|
|
render_scn= B.Scene.New()
|
|
|
|
render_scn.makeCurrent()
|
2006-05-28 10:48:50 +00:00
|
|
|
render_context= render_scn.getRenderingContext()
|
2006-05-30 10:15:01 +00:00
|
|
|
render_context.setRenderPath('') # so we can ignore any existing path and save to the abs path.
|
2006-05-28 02:12:49 +00:00
|
|
|
|
2006-05-30 10:15:01 +00:00
|
|
|
PREF_IMAGE_PATH_EXPAND= B.sys.expandpath(PREF_IMAGE_PATH) + '.png'
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
# TEST THE FILE WRITING.
|
|
|
|
try:
|
|
|
|
# Can we write to this file???
|
|
|
|
f= open(PREF_IMAGE_PATH_EXPAND, 'w')
|
|
|
|
f.close()
|
|
|
|
except:
|
2007-02-15 12:13:13 +00:00
|
|
|
B.Draw.PupMenu('Error%t|Could not write to path|' + PREF_IMAGE_PATH_EXPAND)
|
2006-05-19 15:56:14 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
render_context.imageSizeX(PREF_IMAGE_SIZE)
|
|
|
|
render_context.imageSizeY(PREF_IMAGE_SIZE)
|
|
|
|
render_context.enableOversampling(True)
|
|
|
|
render_context.setOversamplingLevel(16)
|
|
|
|
render_context.setRenderWinSize(100)
|
|
|
|
render_context.setImageType(Render.PNG)
|
|
|
|
render_context.enableExtensions(True)
|
2008-11-05 04:48:08 +00:00
|
|
|
render_context.enablePremultiply() # No alpha needed.
|
|
|
|
render_context.enableRGBAColor()
|
2007-02-15 12:13:13 +00:00
|
|
|
render_context.threads = 2
|
2006-05-19 15:56:14 +00:00
|
|
|
|
2006-05-28 10:48:50 +00:00
|
|
|
#Render.EnableDispView() # Broken??
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
# New Mesh and Object
|
|
|
|
render_mat= B.Material.New()
|
|
|
|
render_mat.mode |= B.Material.Modes.SHADELESS
|
|
|
|
render_mat.mode |= B.Material.Modes.TEXFACE
|
2008-11-05 04:48:08 +00:00
|
|
|
render_mat.mode |= B.Material.Modes.ZTRANSP
|
|
|
|
render_mat.setAlpha(0.0)
|
|
|
|
|
2006-05-19 15:56:14 +00:00
|
|
|
render_me= B.Mesh.New()
|
2006-05-28 10:48:50 +00:00
|
|
|
render_me.verts.extend([Vector(0,0,0)]) # Stupid, dummy vert, preverts errors. when assigning UV's/
|
2006-05-19 15:56:14 +00:00
|
|
|
render_ob= B.Object.New('Mesh')
|
|
|
|
render_ob.link(render_me)
|
|
|
|
render_scn.link(render_ob)
|
|
|
|
render_me.materials= [render_mat]
|
|
|
|
|
|
|
|
|
|
|
|
# New camera and object
|
|
|
|
render_cam_data= B.Camera.New('ortho')
|
|
|
|
render_cam_ob= B.Object.New('Camera')
|
|
|
|
render_cam_ob.link(render_cam_data)
|
|
|
|
render_scn.link(render_cam_ob)
|
2007-07-09 17:33:38 +00:00
|
|
|
render_scn.objects.camera = render_cam_ob
|
2006-05-19 15:56:14 +00:00
|
|
|
|
2007-01-09 00:36:42 +00:00
|
|
|
render_cam_data.type= 'ortho'
|
2006-05-19 15:56:14 +00:00
|
|
|
render_cam_data.scale= 1.0
|
|
|
|
|
|
|
|
|
|
|
|
# Position the camera
|
2006-05-30 10:15:01 +00:00
|
|
|
render_cam_ob.LocZ= 1.0
|
|
|
|
render_cam_ob.LocX= 0.5
|
|
|
|
render_cam_ob.LocY= 0.5
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
# List to send to to boxpack function.
|
|
|
|
boxes2Pack= [ fg.box_pack for fg in face_groups.itervalues()]
|
2007-07-09 17:33:38 +00:00
|
|
|
packWidth, packHeight = B.Geometry.BoxPack2D(boxes2Pack)
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
if PREF_KEEP_ASPECT:
|
|
|
|
packWidth= packHeight= max(packWidth, packHeight)
|
|
|
|
|
|
|
|
|
|
|
|
# packedLs is a list of [(anyUniqueID, left, bottom, width, height)...]
|
|
|
|
# Re assign the face groups boxes to the face_group.
|
2007-07-09 17:33:38 +00:00
|
|
|
for box in boxes2Pack:
|
|
|
|
face_groups[ box[4] ].box_pack= box # box[4] is the ID (image name)
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Add geometry to the mesh
|
|
|
|
for fg in face_groups.itervalues():
|
|
|
|
# Add verts clockwise from the bottom left.
|
2007-07-09 17:33:38 +00:00
|
|
|
_x= fg.box_pack[0] / packWidth
|
|
|
|
_y= fg.box_pack[1] / packHeight
|
|
|
|
_w= fg.box_pack[2] / packWidth
|
|
|
|
_h= fg.box_pack[3] / packHeight
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
render_me.verts.extend([\
|
|
|
|
Vector(_x, _y, 0),\
|
|
|
|
Vector(_x, _y +_h, 0),\
|
|
|
|
Vector(_x + _w, _y +_h, 0),\
|
|
|
|
Vector(_x + _w, _y, 0),\
|
|
|
|
])
|
|
|
|
|
|
|
|
render_me.faces.extend([\
|
|
|
|
render_me.verts[-1],\
|
|
|
|
render_me.verts[-2],\
|
|
|
|
render_me.verts[-3],\
|
|
|
|
render_me.verts[-4],\
|
|
|
|
])
|
|
|
|
|
|
|
|
target_face= render_me.faces[-1]
|
|
|
|
target_face.image= fg.image
|
2007-02-15 12:13:13 +00:00
|
|
|
target_face.mode |= TEXMODE
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
# Set the UV's, we need to flip them HOZ?
|
|
|
|
target_face.uv[0].x= target_face.uv[1].x= fg.xmax
|
|
|
|
target_face.uv[2].x= target_face.uv[3].x= fg.xmin
|
|
|
|
|
|
|
|
target_face.uv[0].y= target_face.uv[3].y= fg.ymin
|
|
|
|
target_face.uv[1].y= target_face.uv[2].y= fg.ymax
|
|
|
|
|
2006-05-28 10:48:50 +00:00
|
|
|
for uv in target_face.uv:
|
|
|
|
uv_rot= ((uv-fg.cent) * fg.rot_mat[1]) + fg.cent
|
|
|
|
uv.x= uv_rot.x
|
|
|
|
uv.y= uv_rot.y
|
2006-05-19 15:56:14 +00:00
|
|
|
|
2006-05-30 10:15:01 +00:00
|
|
|
render_context.render()
|
2006-05-28 10:48:50 +00:00
|
|
|
Render.CloseRenderWindow()
|
2006-05-30 10:15:01 +00:00
|
|
|
render_context.saveRenderedImage(PREF_IMAGE_PATH_EXPAND)
|
2006-05-28 10:48:50 +00:00
|
|
|
|
2006-05-30 10:15:01 +00:00
|
|
|
#if not B.sys.exists(PREF_IMAGE_PATH_EXPAND):
|
2006-05-19 15:56:14 +00:00
|
|
|
# raise 'Error!!!'
|
2006-05-28 10:48:50 +00:00
|
|
|
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
# NOW APPLY THE SAVED IMAGE TO THE FACES!
|
2006-05-28 02:12:49 +00:00
|
|
|
#print PREF_IMAGE_PATH_EXPAND
|
2006-05-30 10:15:01 +00:00
|
|
|
try:
|
|
|
|
target_image= B.Image.Load(PREF_IMAGE_PATH_EXPAND)
|
|
|
|
except:
|
|
|
|
B.Draw.PupMenu('Error: Could not render or load the image at path|' + PREF_IMAGE_PATH_EXPAND)
|
|
|
|
return
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
# Set to the 1 image.
|
|
|
|
for me in mesh_list:
|
|
|
|
for f in me.faces:
|
2007-02-15 12:13:13 +00:00
|
|
|
if f.mode & TEXMODE and f.image:
|
2006-05-19 15:56:14 +00:00
|
|
|
f.image= target_image
|
|
|
|
|
|
|
|
for fg in face_groups.itervalues():
|
|
|
|
fg.move2packed(packWidth, packHeight)
|
2007-07-09 17:33:38 +00:00
|
|
|
|
2006-05-19 15:56:14 +00:00
|
|
|
scn.makeCurrent()
|
2006-05-30 10:15:01 +00:00
|
|
|
render_me.verts= None # free a tiny amount of memory.
|
2007-07-09 17:33:38 +00:00
|
|
|
B.Scene.Unlink(render_scn)
|
|
|
|
target_image.makeCurrent()
|
2006-05-19 15:56:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
scn= B.Scene.GetCurrent()
|
2007-02-15 12:13:13 +00:00
|
|
|
scn_objects = scn.objects
|
|
|
|
ob= scn_objects.active
|
2006-05-19 15:56:14 +00:00
|
|
|
|
2006-12-28 05:00:35 +00:00
|
|
|
if not ob or ob.type != 'Mesh':
|
2006-05-19 15:56:14 +00:00
|
|
|
B.Draw.PupMenu('Error, no active mesh object, aborting.')
|
|
|
|
return
|
|
|
|
|
|
|
|
# Create the variables.
|
|
|
|
# Filename without path or extension.
|
|
|
|
newpath= B.Get('filename').split('/')[-1].split('\\')[-1].replace('.blend', '')
|
|
|
|
|
|
|
|
PREF_IMAGE_PATH = B.Draw.Create('//%s_grp' % newpath)
|
2006-05-30 10:15:01 +00:00
|
|
|
PREF_IMAGE_SIZE = B.Draw.Create(1024)
|
2006-05-19 15:56:14 +00:00
|
|
|
PREF_IMAGE_MARGIN = B.Draw.Create(6)
|
2006-05-30 10:15:01 +00:00
|
|
|
PREF_KEEP_ASPECT = B.Draw.Create(0)
|
2006-05-19 15:56:14 +00:00
|
|
|
PREF_ALL_SEL_OBS = B.Draw.Create(0)
|
|
|
|
|
|
|
|
pup_block = [\
|
2006-05-30 10:15:01 +00:00
|
|
|
'Image Path: (no ext)',\
|
2006-05-19 15:56:14 +00:00
|
|
|
('', PREF_IMAGE_PATH, 3, 100, 'Path to new Image. "//" for curent blend dir.'),\
|
|
|
|
'Image Options',
|
|
|
|
('Pixel Size:', PREF_IMAGE_SIZE, 64, 4096, 'Image Width and Height.'),\
|
2007-02-15 12:13:13 +00:00
|
|
|
('Pixel Margin:', PREF_IMAGE_MARGIN, 0, 64, 'Use a margin to stop mipmapping artifacts.'),\
|
|
|
|
('Keep Aspect', PREF_KEEP_ASPECT, 'If disabled, will stretch the images to the bounds of the texture'),\
|
2006-05-19 15:56:14 +00:00
|
|
|
'Texture Source',\
|
2007-02-15 12:13:13 +00:00
|
|
|
('All Sel Objects', PREF_ALL_SEL_OBS, 'Combine all selected objects into 1 texture, otherwise active object only.'),\
|
2006-05-19 15:56:14 +00:00
|
|
|
]
|
|
|
|
|
2007-02-15 12:13:13 +00:00
|
|
|
if not B.Draw.PupBlock('Consolidate images...', pup_block):
|
2006-05-19 15:56:14 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
PREF_IMAGE_PATH= PREF_IMAGE_PATH.val
|
|
|
|
PREF_IMAGE_SIZE= PREF_IMAGE_SIZE.val
|
|
|
|
PREF_IMAGE_MARGIN= PREF_IMAGE_MARGIN.val
|
|
|
|
PREF_KEEP_ASPECT= PREF_KEEP_ASPECT.val
|
|
|
|
PREF_ALL_SEL_OBS= PREF_ALL_SEL_OBS.val
|
|
|
|
|
|
|
|
if PREF_ALL_SEL_OBS:
|
2007-02-15 12:13:13 +00:00
|
|
|
mesh_list= [ob.getData(mesh=1) for ob in scn_objects.context if ob.type=='Mesh']
|
2006-05-19 15:56:14 +00:00
|
|
|
# Make sure we have no doubles- dict by name, then get the values back.
|
2007-07-09 17:33:38 +00:00
|
|
|
|
|
|
|
for me in mesh_list: me.tag = False
|
|
|
|
|
|
|
|
mesh_list_new = []
|
|
|
|
for me in mesh_list:
|
|
|
|
if me.faceUV and me.tag==False:
|
|
|
|
me.tag = True
|
|
|
|
mesh_list_new.append(me)
|
|
|
|
|
|
|
|
# replace list with possible doubles
|
|
|
|
mesh_list = mesh_list_new
|
2007-02-15 12:13:13 +00:00
|
|
|
|
2006-05-19 15:56:14 +00:00
|
|
|
else:
|
|
|
|
mesh_list= [ob.getData(mesh=1)]
|
2007-07-09 17:33:38 +00:00
|
|
|
if not mesh_list[0].faceUV:
|
|
|
|
B.Draw.PupMenu('Error, active mesh has no images, Aborting!')
|
|
|
|
return
|
2006-05-19 15:56:14 +00:00
|
|
|
|
2007-02-15 12:13:13 +00:00
|
|
|
consolidate_mesh_images(mesh_list, scn, PREF_IMAGE_PATH, PREF_IMAGE_SIZE, PREF_KEEP_ASPECT, PREF_IMAGE_MARGIN)
|
2006-05-30 10:15:01 +00:00
|
|
|
B.Window.RedrawAll()
|
|
|
|
|
2006-05-19 15:56:14 +00:00
|
|
|
if __name__=='__main__':
|
|
|
|
main()
|