diff options
Diffstat (limited to 'release')
-rw-r--r-- | release/scripts/bpymodules/BPyMessages.py | 10 | ||||
-rw-r--r-- | release/scripts/bpymodules/BPyRender.py | 228 | ||||
-rwxr-xr-x | release/scripts/bpymodules/BPyWindow.py | 12 | ||||
-rwxr-xr-x | release/scripts/image_bake_from_uvs.py | 251 | ||||
-rw-r--r-- | release/scripts/image_bake_image.py | 94 | ||||
-rw-r--r-- | release/scripts/image_bake_normals.py | 117 | ||||
-rw-r--r-- | release/scripts/image_bake_vcol.py | 94 | ||||
-rw-r--r-- | release/scripts/image_bake_wire.py | 96 |
8 files changed, 643 insertions, 259 deletions
diff --git a/release/scripts/bpymodules/BPyMessages.py b/release/scripts/bpymodules/BPyMessages.py new file mode 100644 index 00000000000..425bbc06d01 --- /dev/null +++ b/release/scripts/bpymodules/BPyMessages.py @@ -0,0 +1,10 @@ +from Blender import Draw +def Error_NoMeshSelected(): + Draw.PupMenu('ERROR%t|No mesh objects selected') +def Error_NoMeshActive(): + Draw.PupMenu('ERROR%t|Active object is not a mesh') +def Error_NoMeshUvSelected(): + Draw.PupMenu('ERROR%t|No mesh objects with texface selected') +def Error_NoMeshUvActive(): + Draw.PupMenu('ERROR%t|Active object is not a mesh with texface') + diff --git a/release/scripts/bpymodules/BPyRender.py b/release/scripts/bpymodules/BPyRender.py index ea87bfd73aa..7f5b56391e6 100644 --- a/release/scripts/bpymodules/BPyRender.py +++ b/release/scripts/bpymodules/BPyRender.py @@ -1,5 +1,7 @@ +import Blender from Blender import Scene, sys, Camera, Object, Image from Blender.Scene import Render +Vector= Blender.Mathutils.Vector def imageFromObjectsOrtho(objects, path, width, height, smooth, alpha= True): ''' @@ -92,3 +94,229 @@ def imageFromObjectsOrtho(objects, path, width, height, smooth, alpha= True): scn.makeCurrent() Scene.Unlink(render_scn) + + + + + + + +#-----------------------------------------------------------------------------# +# UV Baking functions, make a picture from mesh(es) uvs # +#-----------------------------------------------------------------------------# + +def mesh2uv(me_s, PREF_SEL_FACES_ONLY=False): + ''' + Converts a uv mapped mesh into a 2D Mesh from UV coords. + returns a triple - + (mesh2d, face_list, col_list) + "mesh" is the new mesh and... + "face_list" is the faces that were used to make the mesh, + "material_list" is a list of materials used by each face + These are in sync with the meshes faces, so you can easerly copy data between them + + ''' + render_me= Blender.Mesh.New() + render_me.verts.extend( [Vector(0,0,0),] ) # 0 vert uv bugm dummy vert + face_list= [] + material_list= [] + for me in me_s: + me_materials= me.materials + + if PREF_SEL_FACES_ONLY: + me_faces= [f for f in me.faces if f.flag & FACE_SEL] + else: + me_faces= me.faces + + # Keep in sync with render_me.faces + face_list.extend(me_faces) + + # Dittro + if me_materials: + material_list.extend([me_materials[f.mat] for f in me_faces]) + else: + material_list.extend([None]*len(me_faces)) + + # Now add the verts + render_me.verts.extend( [ Vector(uv.x, uv.y, 0) for f in face_list for uv in f.uv ] ) + + # Now add the faces + tmp_faces= [] + vert_offset= 1 + for f in face_list: + tmp_faces.append( [ii+vert_offset for ii in xrange(len(f))] ) + vert_offset+= len(f) + + render_me.faces.extend(tmp_faces) + render_me.faceUV=1 + return render_me, face_list, material_list + + +def uvmesh_apply_normals(render_me, face_list): + '''Worldspace normals to vertex colors''' + for i, f in enumerate(render_me.faces): + face_orig= face_list[i] + f_col= f.col + for j, v in enumerate(face_orig): + c= f_col[j] + nx, ny, nz= v.no + c.r= int((nx+1)*128)-1 + c.g= int((ny+1)*128)-1 + c.b= int((nz+1)*128)-1 + +def uvmesh_apply_image(render_me, face_list): + '''Copy the image and uvs from the original faces''' + for i, f in enumerate(render_me.faces): + f.uv= face_list[i].uv + f.image= face_list[i].image + + +def uvmesh_apply_vcol(render_me, face_list): + '''Copy the vertex colors from the original faces''' + for i, f in enumerate(render_me.faces): + face_orig= face_list[i] + f_col= f.col + for j, c_orig in enumerate(face_orig.col): + c= f_col[j] + c.r= c_orig.r + c.g= c_orig.g + c.b= c_orig.b + +def uvmesh_apply_matcol(render_me, material_list): + '''Get the vertex colors from the original materials''' + for i, f in enumerate(render_me.faces): + mat_orig= material_list[i] + f_col= f.col + if mat_orig: + for c in f_col: + c.r= int(mat_orig.R*255) + c.g= int(mat_orig.G*255) + c.b= int(mat_orig.B*255) + else: + for c in f_col: + c.r= 255 + c.g= 255 + c.b= 255 + +def uvmesh_apply_col(render_me, color): + '''Get the vertex colors from the original materials''' + r,g,b= color + for i, f in enumerate(render_me.faces): + f_col= f.col + for c in f_col: + c.r= r + c.g= g + c.b= b + + +def vcol2image(me_s,\ + PREF_IMAGE_PATH,\ + PREF_IMAGE_SIZE,\ + PREF_IMAGE_BLEED,\ + PREF_IMAGE_SMOOTH,\ + PREF_IMAGE_WIRE,\ + PREF_IMAGE_WIRE_INVERT,\ + PREF_IMAGE_WIRE_UNDERLAY,\ + PREF_USE_IMAGE,\ + PREF_USE_VCOL,\ + PREF_USE_MATCOL,\ + PREF_USE_NORMAL,\ + PREF_SEL_FACES_ONLY): + + + def rnd_mat(): + render_mat= Blender.Material.New() + mode= render_mat.mode + + # Dont use lights ever + mode |= Blender.Material.Modes.SHADELESS + + if PREF_IMAGE_WIRE: + # Set the wire color + if PREF_IMAGE_WIRE_INVERT: + render_mat.rgbCol= (1,1,1) + else: + render_mat.rgbCol= (0,0,0) + + mode |= Blender.Material.Modes.WIRE + if PREF_USE_VCOL or PREF_USE_MATCOL or PREF_USE_NORMAL: # both vcol and material color use vertex cols to avoid the 16 max limit in materials + mode |= Blender.Material.Modes.VCOL_PAINT + if PREF_USE_IMAGE: + mode |= Blender.Material.Modes.TEXFACE + + # Copy back the mode + render_mat.mode |= mode + return render_mat + + + render_me, face_list, material_list= mesh2uv(me_s, PREF_SEL_FACES_ONLY) + + # Normals exclude all others + if PREF_USE_NORMAL: + uvmesh_apply_normals(render_me, face_list) + else: + if PREF_USE_IMAGE: + uvmesh_apply_image(render_me, face_list) + uvmesh_apply_vcol(render_me, face_list) + + elif PREF_USE_VCOL: + uvmesh_apply_vcol(render_me, face_list) + + elif PREF_USE_MATCOL: + uvmesh_apply_matcol(render_me, material_list) + + # Handel adding objects + render_ob= Blender.Object.New('Mesh') + render_me.materials= [rnd_mat()] + + render_ob.link(render_me) + obs= [render_ob] + + if PREF_IMAGE_WIRE_UNDERLAY: + # Make another mesh with the material colors + render_me_under, face_list, material_list= mesh2uv(me_s, PREF_SEL_FACES_ONLY) + + uvmesh_apply_matcol(render_me_under, material_list) + + # Handel adding objects + render_ob= Blender.Object.New('Mesh') + render_ob.link(render_me_under) + render_ob.LocZ= -0.01 + + # Add material and disable wire + mat= rnd_mat() + mat.rgbCol= 1,1,1 + mat.alpha= 0.5 + mat.mode &= ~Blender.Material.Modes.WIRE + mat.mode |= Blender.Material.Modes.VCOL_PAINT + + render_me_under.materials= [mat] + + obs.append(render_ob) + + elif PREF_IMAGE_BLEED and not PREF_IMAGE_WIRE: + # EVIL BLEEDING CODE!! - Just do copys of the mesh and place behind. Crufty but better then many other methods I have seen. - Cam + BLEED_PIXEL= 1.0/PREF_IMAGE_SIZE + z_offset= 0.0 + for i in xrange(PREF_IMAGE_BLEED): + for diag1, diag2 in ((-1,-1),(-1,1),(1,-1),(1,1), (1,0), (0,1), (-1,0), (0, -1)): # This line extends the object in 8 different directions, top avoid bleeding. + + render_ob= Blender.Object.New('Mesh') + render_ob.link(render_me) + + render_ob.LocX= (i+1)*diag1*BLEED_PIXEL + render_ob.LocY= (i+1)*diag2*BLEED_PIXEL + render_ob.LocZ= -z_offset + + obs.append(render_ob) + z_offset += 0.01 + + + + im= imageFromObjectsOrtho(obs, PREF_IMAGE_PATH, PREF_IMAGE_SIZE, PREF_IMAGE_SIZE, PREF_IMAGE_SMOOTH) + + # Clear from memory as best as we can + render_me.verts= None + + if PREF_IMAGE_WIRE_UNDERLAY: + render_me_under.verts= None diff --git a/release/scripts/bpymodules/BPyWindow.py b/release/scripts/bpymodules/BPyWindow.py index 9b205042db8..b1b8fdc22cf 100755 --- a/release/scripts/bpymodules/BPyWindow.py +++ b/release/scripts/bpymodules/BPyWindow.py @@ -54,7 +54,7 @@ def mouseViewRay(screen_x, screen_y, localMatrix=None, useMid = False): # ortho mode: is a bit strange - actually there's no definite location of the camera ... # but the camera could be displaced anywhere along the viewing direction. - ortho_d.x, ortho_d.y, ortho_d.z = Window.GetViewVector() + ortho_d[:] = Window.GetViewVector() ortho_d.w = 0 # all rays are parallel in ortho mode - so the direction vector is simply the viewing direction @@ -73,7 +73,7 @@ def mouseViewRay(screen_x, screen_y, localMatrix=None, useMid = False): # is used in sculpt_mesh to initialize backface culling...) else: # PERSPECTIVE MODE: here everything is well defined - all rays converge at the camera's location - vmi = Matrix(Window.GetViewMatrix()); vmi.invert() # the inverse viewing matrix + vmi = Window.GetViewMatrix().inverted() # the inverse viewing matrix fp = mouseViewRay.fp dx = pm[3][3] * (((screen_x-win_min_x)/win_size_x)-1.0) - pm[3][0] @@ -104,13 +104,9 @@ def mouseViewRay(screen_x, screen_y, localMatrix=None, useMid = False): # Do we want to return a direction in object's localspace? if localMatrix: - localInvMatrix = Matrix(localMatrix) - localInvMatrix.invert() + localInvMatrix = localMatrix.inverted() p = p*localInvMatrix - d = d*localInvMatrix # normalize_v3 - p.x += localInvMatrix[3][0] - p.y += localInvMatrix[3][1] - p.z += localInvMatrix[3][2] + d = d*localInvMatrix.rotationPart() # normalize_v3 #else: # Worldspace, do nothing diff --git a/release/scripts/image_bake_from_uvs.py b/release/scripts/image_bake_from_uvs.py deleted file mode 100755 index c3d37dd8b81..00000000000 --- a/release/scripts/image_bake_from_uvs.py +++ /dev/null @@ -1,251 +0,0 @@ -#!BPY -""" -Name: 'Bake Image from UVs (vcol/img/nor)' -Blender: 241 -Group: 'Image' -Tooltip: 'Save the active or selected meshes meshes images, vertex colors or normals to an image.' -""" -__author__= ['Campbell Barton'] -__url__= ('blender', 'elysiun', 'http://www.gametutorials.com') -__version__= '0.95' -__bpydoc__= '''\ - -Bake from UVs to image - -This script makes an image from a meshes vertex colors, using the UV coordinates -to draw the faces into the image. - -This makes it possible to bake radiosity into a texture. -Make sure your UV Coordinates do not overlap. LSCM Unwrapper or archimap unwrapper work well -to automaticaly do this. -''' - - -import Blender -import BPyRender -import BPyMesh -Vector= Blender.Mathutils.Vector -Create= Blender.Draw.Create - - -def vcol2image(me_s,\ - PREF_IMAGE_PATH,\ - PREF_IMAGE_SIZE,\ - PREF_IMAGE_BLEED,\ - PREF_IMAGE_SMOOTH,\ - PREF_IMAGE_WIRE,\ - PREF_USE_IMAGE,\ - PREF_USE_VCOL,\ - PREF_USE_MATCOL,\ - PREF_USE_NORMAL,\ - PREF_SEL_FACES_ONLY): - - - def rnd_mat(): - render_mat= Blender.Material.New() - mode= render_mat.mode - - # Dont use lights ever - mode |= Blender.Material.Modes.SHADELESS - - if PREF_IMAGE_WIRE: - mode |= Blender.Material.Modes.WIRE - if PREF_USE_VCOL or PREF_USE_MATCOL: # both vcol and material color use vertex cols to avoid the 16 max limit in materials - mode |= Blender.Material.Modes.VCOL_PAINT - if PREF_USE_IMAGE: - mode |= Blender.Material.Modes.TEXFACE - - # Copy back the mode - render_mat.mode |= mode - return render_mat - - - BLEED_PIXEL= 1.0/PREF_IMAGE_SIZE - render_me= Blender.Mesh.New() - render_me.verts.extend( [Vector(0,0,0),] ) # 0 vert uv bugm dummy vert - - FACE_SEL= Blender.Mesh.FaceFlags.SELECT - - for me in me_s: - - if PREF_SEL_FACES_ONLY: - me_faces= [f for f in me.faces if f.flag & FACE_SEL] - else: - me_faces= me.faces - - # Multiple mesh support. - - if PREF_USE_NORMAL: - BPyMesh.meshCalcNormals(me) - - vert_offset= len(render_me.verts) - render_me.verts.extend( [ Vector(uv.x-BLEED_PIXEL, uv.y-BLEED_PIXEL/2, 0) for f in me_faces for uv in f.uv ] ) - - tmp_faces= [] - for f in me_faces: - tmp_faces.append( [ii+vert_offset for ii in xrange(len(f))] ) - vert_offset+= len(f) - - face_offset= len(render_me.faces) - render_me.faces.extend(tmp_faces) - - if PREF_USE_MATCOL: - materials= [] - for mat in me.materials: - if mat==None: - materials.append((1.0, 1.0, 1.0)) # white - else: - materials.append(mat.rgbCol) - - if not materials: # Well need a dummy material so the index works if we have no materials. - materials= [(1.0, 1.0, 1.0)] - - for i, f in enumerate(me_faces): - - if PREF_SEL_FACES_ONLY and not f.flag & FACE_SEL: - continue - - frnd= render_me.faces[face_offset+i] - if PREF_USE_IMAGE: - ima= f.image - if ima: - frnd.image= ima - - frnd.uv= f.uv - - # Use normals excludes other color operations - if PREF_USE_NORMAL: - for ii, v in enumerate(f.v): - nx, ny, nz= v.no - c= frnd.col[ii] - # Modified to adjust from the current color - c.r= int((nx+1)*128)-1 - c.g= int((ny+1)*128)-1 - c.b= int((nz+1)*128)-1 - else: - # Initialize color - if PREF_USE_VCOL: - frnd.col= f.col - - # Mix with vert color - if PREF_USE_MATCOL: - # Multiply with existing color - r,g,b= materials[f.mat] - for col in frnd.col: - col.r= int(col.r*r) - col.g= int(col.g*g) - col.b= int(col.b*b) - - elif PREF_USE_MATCOL: # Mat color only - # Multiply with existing color - r,g,b= materials[f.mat] - for col in frnd.col: - col.r= int(255*r) - col.g= int(255*g) - col.b= int(255*b) - - render_ob= Blender.Object.New('Mesh') - render_ob.link(render_me) - obs= [render_ob] - - # EVIL BLEEDING CODE!! - Just do copys of the mesh and place behind. Crufty but better then many other methods I have seen. - if PREF_IMAGE_BLEED and not PREF_IMAGE_WIRE: - z_offset= 0.0 - for i in xrange(PREF_IMAGE_BLEED): - for diag1, diag2 in ((-1,-1),(-1,1),(1,-1),(1,1), (1,0), (0,1), (-1,0), (0, -1)): # This line extends the object in 8 different directions, top avoid bleeding. - - render_ob= Blender.Object.New('Mesh') - render_ob.link(render_me) - - render_ob.LocX= (i+1)*diag1*BLEED_PIXEL - render_ob.LocY= (i+1)*diag2*BLEED_PIXEL - render_ob.LocZ= -z_offset - - obs.append(render_ob) - z_offset += 0.01 - - - render_me.materials= [rnd_mat()] - im= BPyRender.imageFromObjectsOrtho(obs, PREF_IMAGE_PATH, PREF_IMAGE_SIZE, PREF_IMAGE_SIZE, PREF_IMAGE_SMOOTH) - - # Clear from memory as best as we can - render_me.verts= None - - - - -def main(): - # Create the variables. - # Filename without path or extension. - scn= Blender.Scene.GetCurrent() - act_ob= scn.getActiveObject() - obsel= [ob for ob in Blender.Object.GetSelected() if ob.getType()=='Mesh'] - - if not act_ob or act_ob.getType() != 'Mesh' or not act_ob.getData(mesh=1).faceUV: - Blender.Draw.PupMenu('Error, no active mesh selected.') - return - - - newpath= Blender.Get('filename').split('/')[-1].split('\\')[-1].replace('.blend', '') - - PREF_IMAGE_PATH = Create('//%s_grp' % newpath) - PREF_IMAGE_SIZE = Create(1024) - PREF_IMAGE_BLEED = Create(6) - PREF_IMAGE_SMOOTH= Create(1) - PREF_IMAGE_WIRE= Create(0) - - PREF_USE_IMAGE = Create(1) - PREF_USE_VCOL = Create(1) - PREF_USE_MATCOL = Create(0) - PREF_USE_NORMAL = Create(0) - PREF_SEL_FACES_ONLY= Create(0) - PREF_USE_MULIOB = Create(0) - - pup_block = [\ - 'Image Path: (no ext)',\ - ('', 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.'),\ - ('Pixel Bleed:', PREF_IMAGE_BLEED, 0, 64, 'Extend pixels from boundry edges to avoid mipmapping errors on rendering.'),\ - ('Smooth lines', PREF_IMAGE_SMOOTH, 'Render smooth lines.'),\ - ('Wire Only', PREF_IMAGE_WIRE, 'Renders a wireframe from the mesh, implys bleed is zero.'),\ - 'Color Source',\ - ('Image Texface', PREF_USE_IMAGE, 'Render the faces image in the output.'),\ - ('Vertex Colors', PREF_USE_VCOL, 'Use Normals instead of VCols.'),\ - ('Material Color', PREF_USE_MATCOL, 'Use the materials color.'),\ - ('Normal Map', PREF_USE_NORMAL, 'Use Normals instead of VCols.'),\ - '',\ - ('Selected Faces only', PREF_SEL_FACES_ONLY, 'Only bake from selected faces.'),\ - ] - - if len(obsel)>1: - - pup_block.append(('All Selected Meshes', PREF_USE_MULIOB, 'Use faces from all selcted meshes, Make sure UV coords dont overlap between objects.')) - - - if not Blender.Draw.PupBlock('VCol to Image', pup_block): - return - - if not PREF_USE_MULIOB.val: - me_s= [act_ob.getData(mesh=1)] - else: - # Make double sure datas unique - me_s = dict([(ob.getData(name_only=1), ob.getData(mesh=1)) for ob in obsel]).values() - me_s = [me for me in me_s if me.faceUV] - - vcol2image(me_s,\ - PREF_IMAGE_PATH.val,\ - PREF_IMAGE_SIZE.val,\ - PREF_IMAGE_BLEED.val,\ - PREF_IMAGE_SMOOTH.val,\ - PREF_IMAGE_WIRE.val,\ - PREF_USE_IMAGE.val,\ - PREF_USE_VCOL.val,\ - PREF_USE_MATCOL.val,\ - PREF_USE_NORMAL.val,\ - PREF_SEL_FACES_ONLY.val) - - Blender.Window.RedrawAll() - -if __name__ == '__main__': - main() diff --git a/release/scripts/image_bake_image.py b/release/scripts/image_bake_image.py new file mode 100644 index 00000000000..038224f3a49 --- /dev/null +++ b/release/scripts/image_bake_image.py @@ -0,0 +1,94 @@ +#!BPY +""" +Name: 'Bake Texface Image from UVs' +Blender: 242 +Group: 'Image' +Tooltip: 'Make a new image from the texface of the selected mesh objects.' +""" +__author__= ['Campbell Barton'] +__url__= ('blender', 'elysiun', 'http://www.gametutorials.com') +__version__= '0.1' +__bpydoc__= '''\ +Bake Vertex Colors to an image + +This script makes an image from a meshes vertex colors, using the UV coordinates +to draw the faces into the image. + +This makes it possible to bake radiosity into a texture. +Make sure your UV Coordinates do not overlap. +LSCM Unwrapper or archimap unwrapper work well to automaticaly do this. +''' + +import Blender +import BPyRender +#reload(BPyRender) +import BPyMessages +Vector= Blender.Mathutils.Vector +Create= Blender.Draw.Create + + +def main(): + # Create the variables. + # Filename without path or extension. + # Big LC, gets all unique mesh objects from the selection that have UV coords. + me_s= dict([\ + (ob.getData(name_only=1), ob.getData(mesh=1))\ + for ob in Blender.Object.GetSelected()\ + if ob.getType()=='Mesh' if ob.getData(mesh=1).faceUV]).values() + + if not me_s: + BPyMessages.Error_NoMeshUvSelected() + return + + newpath= Blender.Get('filename').split('/')[-1].split('\\')[-1].replace('.blend', '') + PREF_IMAGE_PATH = Create('//%s_img' % newpath) + PREF_IMAGE_SIZE = Create(512) + PREF_IMAGE_BLEED = Create(4) + PREF_IMAGE_SMOOTH= Create(1) + + PREF_SEL_FACES_ONLY= Create(0) + + pup_block = [\ + 'Image Path: (no ext)',\ + ('', 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.'),\ + ('Pixel Bleed:', PREF_IMAGE_BLEED, 0, 64, 'Extend pixels from boundry edges to avoid mipmapping errors on rendering.'),\ + ('Smooth lines', PREF_IMAGE_SMOOTH, 'Render smooth lines.'),\ + '',\ + ('Selected Faces only', PREF_SEL_FACES_ONLY, 'Only bake from selected faces.'),\ + ] + + if not Blender.Draw.PupBlock('Texface Image Bake', pup_block): + return + + + # Defaults for VCol, user cant change + PREF_IMAGE_WIRE= False + PREF_IMAGE_WIRE_INVERT= False + PREF_IMAGE_WIRE_UNDERLAY= False + + PREF_USE_IMAGE= True # of course we need this one + + PREF_USE_VCOL= False + PREF_USE_MATCOL= False + PREF_USE_NORMAL= False + + BPyRender.vcol2image(me_s,\ + PREF_IMAGE_PATH.val,\ + PREF_IMAGE_SIZE.val,\ + PREF_IMAGE_BLEED.val,\ + PREF_IMAGE_SMOOTH.val,\ + PREF_IMAGE_WIRE,\ + PREF_IMAGE_WIRE_INVERT,\ + PREF_IMAGE_WIRE_UNDERLAY,\ + PREF_USE_IMAGE,\ + PREF_USE_VCOL,\ + PREF_USE_MATCOL,\ + PREF_USE_NORMAL,\ + PREF_SEL_FACES_ONLY.val) + + Blender.Window.RedrawAll() + +if __name__ == '__main__': + main() diff --git a/release/scripts/image_bake_normals.py b/release/scripts/image_bake_normals.py new file mode 100644 index 00000000000..a1d1022c82e --- /dev/null +++ b/release/scripts/image_bake_normals.py @@ -0,0 +1,117 @@ +#!BPY +""" +Name: 'Bake Normal Map Image from UVs' +Blender: 242 +Group: 'Image' +Tooltip: 'Generate a normal map image from selected mesh objects.' +""" +__author__= ['Campbell Barton'] +__url__= ('blender', 'elysiun', 'http://www.gametutorials.com') +__version__= '0.1' +__bpydoc__= '''\ +Bake Vertex Colors to an image + +This script makes an image from a meshes vertex colors, using the UV coordinates +to draw the faces into the image. + +This makes it possible to bake radiosity into a texture. +Make sure your UV Coordinates do not overlap. +LSCM Unwrapper or archimap unwrapper work well to automaticaly do this. +''' + +import Blender +import BPyRender +#reload(BPyRender) +import BPyMessages +Vector= Blender.Mathutils.Vector +Create= Blender.Draw.Create + + +def main(): + # Create the variables. + # Filename without path or extension. + # Big LC, gets all unique mesh objects from the selection that have UV coords. + + def worldnormals(ob): + nor_mtx= ob.matrixWorld.rotationPart() + me= ob.getData(mesh=1) + for v in me.verts: + v.no= v.no*nor_mtx + return me + + ob_s= dict([\ + (ob.getData(name_only=1), ob)\ + for ob in Blender.Object.GetSelected()\ + if ob.getType()=='Mesh' if ob.getData(mesh=1).faceUV]).values() + + me_s= [worldnormals(ob) for ob in ob_s] + del ob_s + + if not me_s: + BPyMessages.Error_NoMeshUvSelected() + return + + newpath= Blender.Get('filename').split('/')[-1].split('\\')[-1].replace('.blend', '') + PREF_IMAGE_PATH = Create('//%s_nor' % newpath) + PREF_IMAGE_SIZE = Create(512) + PREF_IMAGE_BLEED = Create(4) + PREF_IMAGE_SMOOTH= Create(1) + + PREF_SEL_FACES_ONLY= Create(0) + + pup_block = [\ + 'Image Path: (no ext)',\ + ('', 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.'),\ + ('Pixel Bleed:', PREF_IMAGE_BLEED, 0, 64, 'Extend pixels from boundry edges to avoid mipmapping errors on rendering.'),\ + ('Smooth lines', PREF_IMAGE_SMOOTH, 'Render smooth lines.'),\ + '',\ + ('Selected Faces only', PREF_SEL_FACES_ONLY, 'Only bake from selected faces.'),\ + ] + + if not Blender.Draw.PupBlock('Texface Image Bake', pup_block): + # Update the normals before we exit + for me in me_s: + me.update() + return + + + + + + + + # Defaults for VCol, user cant change + PREF_IMAGE_WIRE= False + PREF_IMAGE_WIRE_INVERT= False + PREF_IMAGE_WIRE_UNDERLAY= False + + PREF_USE_IMAGE= False + PREF_USE_VCOL= False + PREF_USE_MATCOL= False + + PREF_USE_NORMAL= True # of course we need this one + + BPyRender.vcol2image(me_s,\ + PREF_IMAGE_PATH.val,\ + PREF_IMAGE_SIZE.val,\ + PREF_IMAGE_BLEED.val,\ + PREF_IMAGE_SMOOTH.val,\ + PREF_IMAGE_WIRE,\ + PREF_IMAGE_WIRE_INVERT,\ + PREF_IMAGE_WIRE_UNDERLAY,\ + PREF_USE_IMAGE,\ + PREF_USE_VCOL,\ + PREF_USE_MATCOL,\ + PREF_USE_NORMAL,\ + PREF_SEL_FACES_ONLY.val) + + # Restore normals + for me in me_s: + me.update() + + Blender.Window.RedrawAll() + +if __name__ == '__main__': + main() diff --git a/release/scripts/image_bake_vcol.py b/release/scripts/image_bake_vcol.py new file mode 100644 index 00000000000..a0daf12c1af --- /dev/null +++ b/release/scripts/image_bake_vcol.py @@ -0,0 +1,94 @@ +#!BPY +""" +Name: 'Bake Vertex Color Image from UVs' +Blender: 242 +Group: 'Image' +Tooltip: 'Generate an image from the selected mesh objects vertex colors.' +""" +__author__= ['Campbell Barton'] +__url__= ('blender', 'elysiun', 'http://www.gametutorials.com') +__version__= '0.1' +__bpydoc__= '''\ +Bake Vertex Colors to an image + +This script makes an image from a meshes vertex colors, using the UV coordinates +to draw the faces into the image. + +This makes it possible to bake radiosity into a texture. +Make sure your UV Coordinates do not overlap. +LSCM Unwrapper or archimap unwrapper work well to automaticaly do this. +''' + +import Blender +import BPyRender +# reload(BPyRender) +import BPyMessages +Vector= Blender.Mathutils.Vector +Create= Blender.Draw.Create + + +def main(): + # Create the variables. + # Filename without path or extension. + # Big LC, gets all unique mesh objects from the selection that have UV coords. + me_s= dict([\ + (ob.getData(name_only=1), ob.getData(mesh=1))\ + for ob in Blender.Object.GetSelected()\ + if ob.getType()=='Mesh' if ob.getData(mesh=1).faceUV]).values() + + if not me_s: + BPyMessages.Error_NoMeshUvSelected() + return + + newpath= Blender.Get('filename').split('/')[-1].split('\\')[-1].replace('.blend', '') + PREF_IMAGE_PATH = Create('//%s_vcol' % newpath) + PREF_IMAGE_SIZE = Create(512) + PREF_IMAGE_BLEED = Create(4) + PREF_IMAGE_SMOOTH= Create(1) + + PREF_SEL_FACES_ONLY= Create(0) + + pup_block = [\ + 'Image Path: (no ext)',\ + ('', 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.'),\ + ('Pixel Bleed:', PREF_IMAGE_BLEED, 0, 64, 'Extend pixels from boundry edges to avoid mipmapping errors on rendering.'),\ + ('Smooth lines', PREF_IMAGE_SMOOTH, 'Render smooth lines.'),\ + '',\ + ('Selected Faces only', PREF_SEL_FACES_ONLY, 'Only bake from selected faces.'),\ + ] + + if not Blender.Draw.PupBlock('VCol to Image', pup_block): + return + + + # Defaults for VCol, user cant change + PREF_IMAGE_WIRE= False + PREF_IMAGE_WIRE_INVERT= False + PREF_IMAGE_WIRE_UNDERLAY= False + PREF_USE_IMAGE= False + + PREF_USE_VCOL= True # of course we need this one + + PREF_USE_MATCOL= False + PREF_USE_NORMAL= False + + BPyRender.vcol2image(me_s,\ + PREF_IMAGE_PATH.val,\ + PREF_IMAGE_SIZE.val,\ + PREF_IMAGE_BLEED.val,\ + PREF_IMAGE_SMOOTH.val,\ + PREF_IMAGE_WIRE,\ + PREF_IMAGE_WIRE_INVERT,\ + PREF_IMAGE_WIRE_UNDERLAY,\ + PREF_USE_IMAGE,\ + PREF_USE_VCOL,\ + PREF_USE_MATCOL,\ + PREF_USE_NORMAL,\ + PREF_SEL_FACES_ONLY.val) + + Blender.Window.RedrawAll() + +if __name__ == '__main__': + main() diff --git a/release/scripts/image_bake_wire.py b/release/scripts/image_bake_wire.py new file mode 100644 index 00000000000..99276eca88e --- /dev/null +++ b/release/scripts/image_bake_wire.py @@ -0,0 +1,96 @@ +#!BPY +""" +Name: 'Bake Wire Image from UVs' +Blender: 242 +Group: 'Image' +Tooltip: 'Generate an image from the selected mesh objects UV coords and materials' +""" +__author__= ['Campbell Barton'] +__url__= ('blender', 'elysiun', 'http://www.gametutorials.com') +__version__= '0.1' +__bpydoc__= '''\ +Bake Vertex Colors to an image + +This script makes an image from a meshes vertex colors, using the UV coordinates +to draw the faces into the image. + +This makes it possible to bake radiosity into a texture. +Make sure your UV Coordinates do not overlap. +LSCM Unwrapper or archimap unwrapper work well to automaticaly do this. +''' + +import Blender +import BPyRender +#reload(BPyRender) +import BPyMessages +Vector= Blender.Mathutils.Vector +Create= Blender.Draw.Create + + +def main(): + # Create the variables. + # Filename without path or extension. + # Big LC, gets all unique mesh objects from the selection that have UV coords. + me_s= dict([\ + (ob.getData(name_only=1), ob.getData(mesh=1))\ + for ob in Blender.Object.GetSelected()\ + if ob.getType()=='Mesh' if ob.getData(mesh=1).faceUV]).values() + + if not me_s: + BPyMessages.Error_NoMeshUvSelected() + return + + newpath= Blender.Get('filename').split('/')[-1].split('\\')[-1].replace('.blend', '') + PREF_IMAGE_PATH = Create('//%s_wire' % newpath) + PREF_IMAGE_SIZE = Create(512) + PREF_IMAGE_WIRE_INVERT = Create(0) + PREF_IMAGE_WIRE_UNDERLAY = Create(1) + PREF_IMAGE_SMOOTH= Create(1) + + PREF_SEL_FACES_ONLY= Create(0) + + pup_block = [\ + 'Image Path: (no ext)',\ + ('', 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.'),\ + ('White Wire', PREF_IMAGE_WIRE_INVERT, 'Sets the wire to white (otherwise its black).'),\ + ('Fill Faces', PREF_IMAGE_WIRE_UNDERLAY, 'Fill in faces with material color.'),\ + ('Smooth lines', PREF_IMAGE_SMOOTH, 'Render smooth lines.'),\ + '',\ + ('Selected Faces only', PREF_SEL_FACES_ONLY, 'Only bake from selected faces.'),\ + ] + + if not Blender.Draw.PupBlock('Wire Bake', pup_block): + return + + + # Defaults for VCol, user cant change + PREF_IMAGE_WIRE= True # of course we need this one + + PREF_USE_IMAGE= False + + PREF_IMAGE_BLEED= 0 + + PREF_USE_VCOL= False + PREF_USE_MATCOL= False + PREF_USE_NORMAL= False + + BPyRender.vcol2image(me_s,\ + PREF_IMAGE_PATH.val,\ + PREF_IMAGE_SIZE.val,\ + PREF_IMAGE_BLEED,\ + PREF_IMAGE_SMOOTH.val,\ + PREF_IMAGE_WIRE,\ + PREF_IMAGE_WIRE_INVERT.val,\ + PREF_IMAGE_WIRE_UNDERLAY.val,\ + PREF_USE_IMAGE,\ + PREF_USE_VCOL,\ + PREF_USE_MATCOL,\ + PREF_USE_NORMAL,\ + PREF_SEL_FACES_ONLY.val) + + Blender.Window.RedrawAll() + +if __name__ == '__main__': + main() |