```import bpy import math class VertexAccumulator: # use this class when you can't come up with a good deterministic numbering scheme for your vertices. def __init__(self): self.verts_ = [] self.vertIdxs = {} def keyFor(v): return "%f,%f,%f"%(v[0], v[1], v[2]) def idxFor(self, v): key = VertexAccumulator.keyFor(v) rval = self.vertIdxs.get(key) if None==rval: rval = len(self.verts_) self.vertIdxs[key] = rval self.verts_.append(v) return rval def verts(self): return self.verts_ def image_to_mesh(img): pix = img.pixels[:] w = img.size[0] h = img.size[1] def is_solid(pix, off): return pix[off] < 0.5 va = VertexAccumulator() faces = [] for y in range(h): for x in range(w): off= 4* ( y*w + x ) solid = is_solid(pix, off) x1 = x x2 = x + 1 if True: y1 = y y2 = y+1 else: y2 = h-y y1 = y2-1 if solid: i0 = va.idxFor([x1, y1, 0]) i1 = va.idxFor([x2, y1, 0]) i2 = va.idxFor([x1, y2, 0]) i3 = va.idxFor([x2, y2, 0]) i4 = va.idxFor([x1, y1, -1]) i5 = va.idxFor([x2, y1, -1]) i6 = va.idxFor([x1, y2, -1]) i7 = va.idxFor([x2, y2, -1]) # top faces.append( [i0, i1, i3, i2,] ) # bottom faces.append([i4, i6, i7, i5,]) if x <1 or not is_solid(pix, off-4): # x-- faces.append( [ i0, i2, i6, i4]) if x+1 >w or not is_solid(pix, off+4): faces.append( [i1, i5, i7, i3]) if y<1 or not is_solid(pix, off-4*w): faces.append( [i0, i4, i5, i1]) if y+1>h or not is_solid(pix, off+4*w): faces.append( [ i2, i3, i7, i6]) mesh = bpy.data.meshes.new('stencil') mesh.from_pydata(va.verts(), [], faces) mesh.show_normal_face = True return mesh def mission1(scn): img = bpy.data.images['frog-trace-post.png'] #img = bpy.data.images['test.png'] mesh = image_to_mesh(img) obj = bpy.data.objects.new("stencil", mesh) scn.objects.link(obj) s = 1/max(img.size[0], img.size[1]) print(s) obj.scale = (s,s,1) print(obj.scale) mission1(bpy.context.scene) ```

Syntax highlighting by Pygments.