def __init__(self, mesh_list, image, size, PREF_IMAGE_MARGIN): self.image = image self.size = size self.faces = [ f for me in mesh_list for f in me.faces if f.mode & TEXMODE and f.image == image ] # Find the best rotation. 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] # Pre Rotation bounds self.cent = pointBounds(bountry_points)[1] # Get the optimal rotation angle self.ang = bestBoundsRotation(bountry_points) self.rot_mat = RotationMatrix(self.ang, 2), RotationMatrix(-self.ang, 2) # Post rotation bounds bounds= pointBounds([\ ((uv-self.cent) * self.rot_mat[0]) + self.cent\ for uv in bountry_points])[2] # Break the bounds into useable values. xmin, ymin, xmax, ymax = bounds # Store the bounds, include the margin. # The bounds rect will need to be rotated to the rotation angle. 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=[\ 0.0, 0.0,\ size[0]*(self.xmax - self.xmin),\ size[1]*(self.ymax - self.ymin),\ image.name]
def __init__(self, mesh_list, image, size, PREF_IMAGE_MARGIN): self.image= image self.size= size self.faces= [f for me in mesh_list for f in me.faces if f.mode & TEXMODE and f.image == image] # Find the best rotation. 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] # Pre Rotation bounds self.cent= pointBounds(bountry_points)[1] # Get the optimal rotation angle self.ang= bestBoundsRotation(bountry_points) self.rot_mat= RotationMatrix(self.ang, 2), RotationMatrix(-self.ang, 2) # Post rotation bounds bounds= pointBounds([\ ((uv-self.cent) * self.rot_mat[0]) + self.cent\ for uv in bountry_points])[2] # Break the bounds into useable values. xmin, ymin, xmax, ymax= bounds # Store the bounds, include the margin. # The bounds rect will need to be rotated to the rotation angle. 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=[\ 0.0, 0.0,\ size[0]*(self.xmax - self.xmin),\ size[1]*(self.ymax - self.ymin),\ image.name]
def save_billboard(PREF_IMAGE_PATH): Blender.Window.WaitCursor(1) # remove png, add it later PREF_IMAGE_PATH = PREF_IMAGE_PATH.replace('.png', '') ob_sel = GLOBALS['ob_sel'] me_ob = GLOBALS['me_ob'] me_data = GLOBALS['me_data'] time = Blender.sys.time() me_mat = me_ob.matrixWorld # Render images for all faces face_data = [] # Store faces, images etc boxes2Pack = [] me_data.faceUV = True for i, f in enumerate(me_data.faces): no = f.no # Offset the plane by the zoffset on the faces normal plane = [v.co * me_mat for v in f] # Horizontal stacking, make sure 0,1 and 2,3 are the longest if\ (plane[0]-plane[1]).length + (plane[2]-plane[3]).length < \ (plane[1]-plane[2]).length + (plane[3]-plane[0]).length: plane.append(plane.pop(0)) rot90 = True else: rot90 = False no = Blender.Mathutils.QuadNormal(*plane) plane = [v + no * PREF_Z_OFFSET.val for v in plane] cent = (plane[0] + plane[1] + plane[2] + plane[3]) / 4.0 camera_matrix = BPyMathutils.plane2mat(plane) tmp_path = '%s_%d' % (PREF_IMAGE_PATH, i) img = BPyRender.imageFromObjectsOrtho(ob_sel, tmp_path, PREF_TILE_RES.val, PREF_TILE_RES.val, PREF_AA.val, PREF_ALPHA.val, camera_matrix) img.reload() #img.pack() # se we can keep overwriting the path #img.filename= "" if rot90: f.uv = Vector(1, 1), Vector(0, 1), Vector(0, 0), Vector(1, 0) else: f.uv = Vector(0, 1), Vector(0, 0), Vector(1, 0), Vector(1, 1) if not PREF_IMG_PACK.val: f.mode |= Mesh.FaceModes.TEX f.image = img if PREF_ALPHA.val: f.transp |= Mesh.FaceTranspModes.ALPHA else: w = ((plane[0] - plane[1]).length + (plane[2] - plane[3]).length) / 2 h = ((plane[1] - plane[2]).length + (plane[3] - plane[0]).length) / 2 face_data.append((f, img)) boxes2Pack.append([0.0, 0.0, h, w, i]) if PREF_IMG_PACK.val: # pack the quads into a square packWidth, packHeight = Blender.Geometry.BoxPack2D(boxes2Pack) render_obs = [] render_mat = alpha_mat(img) # Add geometry to the mesh for box in boxes2Pack: i = box[4] orig_f, img = face_data[i] # New Mesh and Object render_me = bpy.data.meshes.new() render_ob = Blender.Object.New('Mesh') render_me.materials = [render_mat] render_ob.link(render_me) render_obs.append(render_ob) # Add verts clockwise from the bottom left. _x = box[0] / packWidth _y = box[1] / packHeight _w = box[2] / packWidth _h = box[3] / packHeight 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(list(render_me.verts)) render_me.faceUV = True render_me.faces[0].uv = [ Vector(0, 0), Vector(0, 1), Vector(1, 1), Vector(1, 0) ] render_me.faces[0].image = img # Set the UV's, we need to flip them HOZ? for uv in orig_f.uv: uv.x = _x + (uv.x * _w) uv.y = _y + (uv.y * _h) target_image = BPyRender.imageFromObjectsOrtho( render_obs, PREF_IMAGE_PATH, PREF_RES.val, PREF_RES.val, PREF_AA.val, PREF_ALPHA.val, None) target_image.reload() # incase your overwriting an existing image. # Set to the 1 image. for f in me_data.faces: f.image = target_image if PREF_ALPHA.val: f.transp |= Mesh.FaceTranspModes.ALPHA # Free the images data and remove for data in face_data: img = data[1] os.remove(img.filename) img.reload() # Finish pack me_data.update() me_ob.makeDisplayList() Blender.Window.WaitCursor(0) print '%.2f secs taken' % (Blender.sys.time() - time)
def save_billboard(PREF_IMAGE_PATH): Blender.Window.WaitCursor(1) # remove png, add it later PREF_IMAGE_PATH= PREF_IMAGE_PATH.replace('.png', '') ob_sel= GLOBALS['ob_sel'] me_ob = GLOBALS['me_ob'] me_data = GLOBALS['me_data'] time= Blender.sys.time() me_mat= me_ob.matrixWorld # Render images for all faces face_data= [] # Store faces, images etc boxes2Pack= [] me_data.faceUV= True for i, f in enumerate(me_data.faces): no= f.no # Offset the plane by the zoffset on the faces normal plane= [v.co * me_mat for v in f] # Horizontal stacking, make sure 0,1 and 2,3 are the longest if\ (plane[0]-plane[1]).length + (plane[2]-plane[3]).length < \ (plane[1]-plane[2]).length + (plane[3]-plane[0]).length: plane.append(plane.pop(0)) rot90= True else: rot90= False no= Blender.Mathutils.QuadNormal(*plane) plane= [v + no*PREF_Z_OFFSET.val for v in plane] cent= (plane[0]+plane[1]+plane[2]+plane[3] ) /4.0 camera_matrix= BPyMathutils.plane2mat(plane) tmp_path= '%s_%d' % (PREF_IMAGE_PATH, i) img= BPyRender.imageFromObjectsOrtho(ob_sel, tmp_path, PREF_TILE_RES.val, PREF_TILE_RES.val, PREF_AA.val, PREF_ALPHA.val, camera_matrix) img.reload() #img.pack() # se we can keep overwriting the path #img.filename= "" if rot90: f.uv=Vector(1,1), Vector(0,1), Vector(0,0), Vector(1,0) else: f.uv= Vector(0,1), Vector(0,0), Vector(1,0), Vector(1,1) if not PREF_IMG_PACK.val: f.mode |= Mesh.FaceModes.TEX f.image = img if PREF_ALPHA.val: f.transp |= Mesh.FaceTranspModes.ALPHA else: w= ((plane[0]-plane[1]).length + (plane[2]-plane[3]).length)/2 h= ((plane[1]-plane[2]).length + (plane[3]-plane[0]).length)/2 face_data.append( (f, img) ) boxes2Pack.append( [0.0,0.0,h, w, i] ) if PREF_IMG_PACK.val: # pack the quads into a square packWidth, packHeight = Blender.Geometry.BoxPack2D(boxes2Pack) render_obs= [] render_mat= alpha_mat(img) # Add geometry to the mesh for box in boxes2Pack: i= box[4] orig_f, img= face_data[i] # New Mesh and Object render_me= bpy.data.meshes.new() render_ob= Blender.Object.New('Mesh') render_me.materials= [render_mat] render_ob.link(render_me) render_obs.append(render_ob) # Add verts clockwise from the bottom left. _x= box[0] / packWidth _y= box[1] / packHeight _w= box[2] / packWidth _h= box[3] / packHeight 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(list(render_me.verts)) render_me.faceUV= True render_me.faces[0].uv = [Vector(0,0), Vector(0,1), Vector(1,1), Vector(1,0)] render_me.faces[0].image = img # Set the UV's, we need to flip them HOZ? for uv in orig_f.uv: uv.x = _x + (uv.x * _w) uv.y = _y + (uv.y * _h) target_image= BPyRender.imageFromObjectsOrtho(render_obs, PREF_IMAGE_PATH, PREF_RES.val, PREF_RES.val, PREF_AA.val, PREF_ALPHA.val, None) target_image.reload() # incase your overwriting an existing image. # Set to the 1 image. for f in me_data.faces: f.image= target_image if PREF_ALPHA.val: f.transp |= Mesh.FaceTranspModes.ALPHA # Free the images data and remove for data in face_data: img= data[1] os.remove(img.filename) img.reload() # Finish pack me_data.update() me_ob.makeDisplayList() Blender.Window.WaitCursor(0) print '%.2f secs taken' % (Blender.sys.time()-time)
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # ***** END GPL LICENCE BLOCK ***** # -------------------------------------------------------------------------- #Run discombobulator.py, not this. import Blender from Blender import NMesh,Object,Material from Blender.NMesh import Vert,Face from Blender.Mathutils import * import BPyMathutils from BPyMathutils import genrand a = BPyMathutils.sgenrand(4859) #Create random numbers def randnum(low,high): num = genrand() num = num*(high-low) num = num+low return num face = Face() xmin = Vector([0,0,0]) xmax = Vector([0,0,0]) ymin = Vector([0,0,0]) ymax = Vector([0,0,0]) mxmin = Vector([0,0,0]) mxmax = Vector([0,0,0])