Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bpy
- from bpy.props import FloatProperty, StringProperty, EnumProperty
- import sys
- import traceback
- bl_info = {
- "name": "Exploded bake",
- "description": "A script to make baking clean normal maps much easier by automatically exploding both the high poly and the low poly meshes before baking, then reverting the operation. ",
- "author": "Paul Le Henaff",
- "version": (2, 0),
- "blender": (2, 72, 0),
- "location": "Tool panel > Explode Bake > Exploded Bake V2",
- "warning": "", # used for warning icon and text in addons panel
- "category": "Rendering"}
- def initSceneProperties():
- bpy.types.Scene.ExplodeDistance = FloatProperty(
- name = "Explode Distance",
- description = "Enter an amount large enough to stop the mesh interference.",
- default = 5,
- min = -100,
- max = 100)
- bpy.types.Scene.ObjList = StringProperty(
- name = "",
- description = "Enter objects names, comma separated, in the same order as the vertex groups (each object will be assigned to the corresponding group)")
- bpy.types.Scene.ImgTarget = StringProperty(
- name = "",
- description = "Enter the name of the texture you want to bake to.")
- #specifyTexture = bpy.props.BoolProperty(name="Toggle Option")
- return
- initSceneProperties()
- #
- # Explode ad bake action
- #
- class OBJECT_OT_Button(bpy.types.Operator):
- bl_idname = "button.explodenbake"
- bl_label = "Explode and bake"
- bl_description = "Perform explode operation then bakes the objects to the texture."
- bl_options = {'REGISTER', 'UNDO'}
- def execute(self, context):
- try:
- explodeFunc()
- selectObjects()
- renderNormal()
- except:
- error = traceback.format_exc()
- self.report({'WARNING'}, "Make sure objects in object list actualy exist. Also make sure the texture in the texture box exists.")
- print(error)
- return{'FINISHED'}
- #
- # Explode action
- #
- class OBJECT_OT_Button(bpy.types.Operator):
- bl_idname = "button.explodev2"
- bl_label = "Explode"
- bl_description = "Performs the explosion operation on the objects"
- bl_options = {'REGISTER', 'UNDO'}
- def execute(self, context):
- explodeFunc()
- return{'FINISHED'}
- #
- # Unexplode
- #
- class OBJECT_OT_unexplode(bpy.types.Operator):
- bl_idname = "button.unexplode"
- bl_label = "Un-Explode"
- bl_description = "Reverts the objects to their unexploded state"
- bl_options = {'REGISTER', 'UNDO'}
- def execute(self, context):
- unExplodeFunc()
- return{'FINISHED'}
- def selectObjects():
- proxies= bpy.context.scene['ObjList']
- objlist = proxies.split(",")
- for i in range(0, len(objlist)):
- object = bpy.data.objects[objlist[i]]
- object.select = True
- print("Done selecting")
- def unSelectObjects():
- proxies= bpy.context.scene['ObjList']
- objlist = proxies.split(",")
- for i in range(0, len(objlist)):
- object = bpy.data.objects[objlist[i]]
- object.select = False
- print("Done unselecting")
- def renderNormal():
- #need to add a checkbox to make this work
- specifyTexture = False
- if specifyTexture:
- texture_name = bpy.context.scene['ImgTarget'] # image texture name property
- temp_obj = bpy.context.selected_objects[0] # object
- for uv_face in temp_obj.data.uv_textures.active.data:
- uv_face.image = bpy.data.images[texture_name]
- print("Assigned ", texture_name," to uv_face")
- print("Rendering...")
- finished = bpy.ops.object.bake_image() # uses the render settings
- if finished:
- print("Done rendering")
- reset()
- print("Done.")
- def reset():
- unSelectObjects()
- unExplodeFunc()
- print("Object reseted")
- #aa
- def explodeFunc():
- proxies= bpy.context.scene['ObjList']
- objlist = proxies.split(",")
- vGroups = bpy.context.selected_objects[0]
- #control
- distance = bpy.context.scene['ExplodeDistance'];
- if((len(vGroups.vertex_groups) == len(objlist)) and bpy.context.selected_objects[0]):
- for i in range (0,len(objlist)):
- cObj = bpy.data.objects[objlist[i]]
- #get vertexgroup
- groupName = bpy.context.selected_objects[0].vertex_groups.keys()[i]
- gi = vGroups.vertex_groups[groupName].index # get group index
- for v in vGroups.data.vertices:
- for g in v.groups:
- if g.group == gi: # compare with index in VertexGroupElement
- #print(objlist[gi])
- v.co.x += distance*i
- #move hi res mesh
- bpy.data.objects[objlist[i]].location.x += distance*i;
- i+=1
- print("Done exploding")
- else:
- print("Number of vertex groups in selection must match the number of high poly objects")
- def unExplodeFunc():
- proxies= bpy.context.scene['ObjList']
- objlist = proxies.split(",")
- vGroups = bpy.context.selected_objects[0]
- #control
- distance = bpy.context.scene['ExplodeDistance'];
- if(len(vGroups.vertex_groups) == len(objlist)):
- for i in range (0,len(objlist)):
- cObj = bpy.data.objects[objlist[i]]
- #get vertexgroup
- groupName = bpy.context.selected_objects[0].vertex_groups.keys()[i]
- gi = vGroups.vertex_groups[groupName].index # get group index
- for v in vGroups.data.vertices:
- for g in v.groups:
- if g.group == gi: # compare with index in VertexGroupElement
- #print(objlist[gi])
- v.co.x -= distance*i
- #move hi res mesh back
- bpy.data.objects[objlist[i]].location.x -= distance*i;
- i+=1
- print("Done unexploding")
- else:
- print("Number of vertex groups in selection must match the number of high poly objects")
- #
- # Menu in UI region
- #
- class UIPanel(bpy.types.Panel):
- bl_label = "Explode bake"
- bl_space_type = "VIEW_3D"
- bl_region_type = "TOOLS"
- bl_description = "TOOLS"
- initSceneProperties()
- def draw(self, context):
- layout = self.layout
- scn = context.scene
- obj = context.object
- proxies= bpy.context.scene['ObjList']
- objlist = proxies.split(",")
- #col = layout.column()
- section2 = layout.column(align=True)
- section2.label("Main settings")
- box = section2.box()
- box.label("High resolution objects:")
- box.prop(scn, 'ObjList')
- box.label("Vertex group assignments:")
- vg_box = box.box()
- if (len(obj.vertex_groups) > 0 and len(objlist) > 0) and (len(obj.vertex_groups) == len(objlist)):
- for group in obj.vertex_groups:
- vg_box.label(group.name + ' ('+objlist[group.index]+')')
- else:
- vg_box.label("No vertex groups or high poly objects")
- box.label("Target texture:")
- box.prop(scn, 'ImgTarget')
- section1 = layout.column(align=True)
- section1.operator("button.explodenbake")
- section1.prop(scn, 'ExplodeDistance')
- #section2.label("Operators")
- #section2.operator("button.explodev2")
- #section2.operator("button.unexplode")
- def register():
- bpy.utils.register_module(__name__)
- def unregister():
- bpy.utils.unregister_module(__name__)
- if __name__ == "__main__":
- register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement