# -*- coding: utf-8 -*- import Image,ImageFont,ImageDraw moji_list_path = "moji_Lv1.txt" font_path = "C:/WINDOWS/Fonts/HGRGM.TTC" font_size = 24 font_color = (0,0,0) font = ImageFont.truetype(font_path, font_size) img_path = 'moji.png' img_width = 1024 img_height = None img_margin_x = 1.0 img_margin_y = 1.0 set_width = 10.0 mesh_size = 5.0/100/font_size def make_moji_scene(moji_list,data): import Blender import bpy #select scene scene = Blender.Scene.GetCurrent() #make material img = Blender.Image.Load(img_path) img_name = img.getName() tex = Blender.Texture.New("mojilist") tex.setType('Image') tex.image = img mat = Blender.Material.New("Moji") mat.setTexture(0,tex,Blender.Texture.TexCo.UV) mat.setMode("Shadeless") #make root object ob_root = scene.objects.new("Empty","Moji") #make moji plane moji_size_list = {} moji_num = len(moji_list) n = _per = 0.0 now_width = now_height = 0.0 for moji in moji_list: per = n/moji_num*100.0 if per > _per+1: _per = per print int(per),"%" n += 1 moji_code = moji.encode("raw_unicode_escape") moji_code = moji_code.replace("\\","#") fsize = font.getsize(moji) #make mesh me_name = "moji" + moji_code me = bpy.data.meshes.new(me_name) me_y = fsize[1] * mesh_size me_x = fsize[0] * mesh_size coords=[ [0,0,0],[me_x,0,0],[me_x,me_y,0],[0,me_y,0] ] faces= [ [3,2,1,0] ] me.verts.extend(coords) me.faces.extend(faces) #make object ob_name = "Moji" + moji_code ob = scene.objects.new(me,ob_name) ob.setLocation(now_width,now_height,0) ob.addProperty("width",me_x,'FLOAT') ob.addProperty("height",me_y,'FLOAT') ob_root.makeParent([ob]) me.flipNormals() #make uv me = ob.getData(0,1) me.addUVLayer("MojiUV") x0,y0 = get_moji_loc(moji_code,data) y0 = img_height - y0 x1,y1 = x0+fsize[0],y0-fsize[1] x0 = x0 / img_width x1 = x1 / img_width y0 = y0 / img_height y1 = y1 / img_height vec0 = Blender.Mathutils.Vector([x0,y0]) vec1 = Blender.Mathutils.Vector([x0,y1]) vec2 = Blender.Mathutils.Vector([x1,y1]) vec3 = Blender.Mathutils.Vector([x1,y0]) new_uv = [vec0,vec1,vec2,vec3] me.faces[0].uv = new_uv me.faces[0].image = img me.materials = [mat] #change location if now_width > set_width: now_width = 0 now_height -= me_y else: now_width += me_x #moji size list moji_size_list[moji_code] = (me_x,me_y) import cPickle f=file("moji_size_list","wb") cPickle.dump(moji_size_list,f) f.close() def get_moji_loc(code,data): for d in data: if d[0] == code: return d[1],d[2] return False def get_moji_list(fname): s = file(fname).read() s = s.decode("utf-8") return s def make_font_img(string): global img_height lines = [] sline = "" img_height = 0 now_width = 0 for s in string: now_width += img_margin_x next_width = now_width + font.getsize(s)[0] if next_width > img_width: img_height += img_margin_y lines.append(sline) img_height += font.getsize(sline)[1] sline = "" now_width = 0 sline += s now_width += font.getsize(s)[0] if len(sline) > 0: lines.append(sline) img_height += font.getsize(sline)[1] size = (img_width,img_height) bgcolor = (1,1,1) img = Image.new('RGBA', size, bgcolor) img.putalpha( Image.new( "L", size, 0 ) ) draw = ImageDraw.Draw(img) data = [] now_x = now_y = 0.0 for line in lines: now_y += img_margin_y for s in line: now_x += img_margin_x code = s.encode("raw_unicode_escape") code = code.replace("\\","#") d = (code,now_x,now_y) data.append(d) size = font.getsize(s) draw.text((now_x,now_y),s,font=font,fill=font_color) now_x += size[0] size = font.getsize(line) now_y += size[1] now_x = 0.0 img.save(img_path,'PNG') return data def main(): s = get_moji_list(moji_list_path) print "making font image..." data = make_font_img(s) print "done" print "making moji texture plane..." make_moji_scene(s,data) print "done" print "Finish!" main()