def addRegion(self, xoff, yoff, width, height): mesh = self.obj.getData(mesh=True) panelimage = mesh.faces[0].image name = 'PanelRegion' for img in Image.get(): # try to re-use existing deleted panel region if img.size == [ width, height ] and img.source == Image.Sources.GENERATED and img.filename == name and not self.isRegion( img): break else: img = Image.New(name, width, height, 24) for y in range(height): for x in range(width): rgba = panelimage.getPixelI(xoff + x, yoff + y) if not rgba[3]: img.setPixelI(x, y, (102, 102, 255, 255)) # hilite transparent else: img.setPixelI(x, y, rgba[:3] + [255]) img.pack() for n in range(1, PanelRegionHandler.REGIONCOUNT + 1): if mesh.faces[n].image == panelimage: mesh.faces[n].image = img self.obj.addProperty('x%d' % n, xoff) self.obj.addProperty('y%d' % n, yoff) (width, height) = img.size (pwidth, pheight) = panelimage.size xoff = float(xoff) / pwidth yoff = float(yoff) / pheight xscale = float(pwidth) / width yscale = float(pheight) / height # Assign UV mappings from panel image for obj in Scene.GetCurrent().objects: if obj != self.obj and obj.getType() == "Mesh": mesh2 = obj.getData(mesh=True) if mesh2.faceUV: for face in mesh2.faces: if face.image == panelimage: uv = [] for v in face.uv: x = (v.x - xoff) * xscale y = (v.y - yoff) * yscale if not -UV.LIMIT <= x <= 1 + UV.LIMIT or not -UV.LIMIT <= y <= 1 + UV.LIMIT: break uv.append( Vector(min(max(x, 0), 1), min(max(y, 0), 1))) else: face.uv = uv face.image = img mesh2.update() break return img
def createMat(texname, path=""): filename = "texture/" + texname.replace('\\', '/') ffn = "%s%s" % (path, filename) #dn = filename.decode('utf-8') #print filename #print dn # texture texture = Texture.New(texname) texture.setType('Image') # texture.uvlayer = texname img = createImage(ffn) if (img == None): print "Can't find file %s" % ffn nfn = "%s%s" % (path, getFile(filename)) img = createImage(nfn) if (img == None): print "Can't find file %s. You're on your own..." % nfn img = Image.New(getFile(ffn), 10, 10, 1) #try: # img = Image.Load(ffn); # #img = Image.Load(dn); #except: # print ("Can't find texture %s. You'll have to assign it on your own" % ffn) # img = Image.New(ffn, 10, 10, 1) texture.image = img mat = Material.New(texname) mat.setTexture(3, texture, Blender.Texture.TexCo.UV) # TODO: Optimize this for m in mat.getTextures(): if (m != None): m.uvlayer = texname return (mat)
def texture_read(filename): global gMat global gTexObj global gTextureBpp global gTextureWidth global gTextureHeight global gTextureImage try: file = open(filename, "rb") tim_id = struct.unpack("<L", file.read(4))[0] if (tim_id != 0x10): print "%s not a TIM file" % filename file.close() return 0 tim_type = struct.unpack("<L", file.read(4))[0] tim_offset = struct.unpack("<L", file.read(4))[0] file.seek(4, 1) num_colors = struct.unpack("<H", file.read(2))[0] num_palettes = struct.unpack("<H", file.read(2))[0] gTextureBpp = 16 if tim_type == 0x08: gTextureBpp = 4 if tim_type == 0x09: gTextureBpp = 8 txPalettes = [] if gTextureBpp < 16: for i in range(num_palettes): palette = [] for j in range(num_colors): value = struct.unpack("<H", file.read(2))[0] r = (value << 3) & 0xf8 g = (value >> 2) & 0xf8 b = (value >> 7) & 0xf8 r |= r >> 5 g |= g >> 5 b |= b >> 5 r = float(r) / 255.0 g = float(g) / 255.0 b = float(b) / 255.0 palette.append([r, g, b]) txPalettes.append(palette) file.seek(tim_offset + 16) gTextureWidth = struct.unpack("<H", file.read(2))[0] if gTextureBpp == 4: gTextureWidth <<= 2 if gTextureBpp == 8: gTextureWidth <<= 1 gTextureHeight = struct.unpack("<H", file.read(2))[0] #print "Texture: %dx" % gTextureWidth + "%d" % gTextureHeight gTextureImage = Image.New("image", gTextureWidth, gTextureHeight, 24) if gTextureBpp == 4: for y in range(gTextureHeight): if (y & 15) == 0: Blender.Window.DrawProgressBar( float(y) / gTextureHeight, "Loading texture") i = 0 j = 0 mi = gTextureWidth / num_palettes for x in range(gTextureWidth / 2): c = struct.unpack("B", file.read(1))[0] c1 = (c >> 4) & 0xf r = txPalettes[j][c1][0] g = txPalettes[j][c1][1] b = txPalettes[j][c1][2] gTextureImage.setPixelF(x * 2, y, (r, g, b, 1.0)) c2 = c & 0xf r = txPalettes[j][c2][0] g = txPalettes[j][c2][1] b = txPalettes[j][c2][2] gTextureImage.setPixelF(x * 2 + 1, y, (r, g, b, 1.0)) i += 2 if (i >= mi): i = 0 j += 1 elif gTextureBpp == 8: for y in range(gTextureHeight): if (y & 15) == 0: Blender.Window.DrawProgressBar( float(y) / gTextureHeight, "Loading texture") i = 0 j = 0 mi = gTextureWidth / num_palettes for x in range(gTextureWidth): c = struct.unpack("B", file.read(1))[0] r = txPalettes[j][c][0] g = txPalettes[j][c][1] b = txPalettes[j][c][2] gTextureImage.setPixelF(x, y, (r, g, b, 1.0)) i += 1 if (i >= mi): i = 0 j += 1 elif gTextureBpp == 16: for y in range(gTextureHeight): if (y & 15) == 0: Blender.Window.DrawProgressBar( float(y) / gTextureHeight, "Loading texture") for x in range(gTextureWidth): c = struct.unpack("<H", file.read(2))[0] r = (c << 3) & 0xf8 g = (c >> 2) & 0xf8 b = (c >> 7) & 0xf8 r |= r >> 5 g |= g >> 5 b |= b >> 5 r = float(r) / 255.0 g = float(g) / 255.0 b = float(b) / 255.0 gTextureImage.setPixelF(x, y, (r, g, b, 1.0)) # Create texture gTexObj = Texture.New("emd_tex") gTexObj.setType("Image") gTexObj.setImage(gTextureImage) # Create material gMat = Material.New("emd_mat") gMat.mode |= Material.Modes.TEXFACE gMat.mode |= Material.Modes.SHADELESS gMat.setTexture(0, gTexObj, Texture.TexCo.UV) except IOError, (errno, strerror): file.close() return 0