Advertisement
Guest User

Untitled

a guest
Jan 12th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.68 KB | None | 0 0
  1. import bpy
  2. from bpy.props import FloatProperty, StringProperty, EnumProperty
  3. import sys
  4. import traceback
  5.  
  6. bl_info = {
  7.     "name": "Exploded bake",
  8.     "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. ",
  9.     "author": "Paul Le Henaff",
  10.     "version": (2, 0),
  11.     "blender": (2, 72, 0),
  12.     "location": "Tool panel > Explode Bake > Exploded Bake V2",
  13.     "warning": "", # used for warning icon and text in addons panel
  14.     "category": "Rendering"}
  15.  
  16. #
  17. #   Explode ad bake action
  18. #
  19. class OBJECT_OT_Button(bpy.types.Operator):
  20.     bl_idname = "button.explodenbake"
  21.     bl_label = "Explode and bake"
  22.     bl_description = "Perform explode operation then bakes the objects to the texture."
  23.     bl_options = {'REGISTER', 'UNDO'}
  24.    
  25.     def execute(self, context):
  26.         try:
  27.             explodeFunc()
  28.             selectObjects()
  29.             renderNormal()
  30.         except:
  31.             error = traceback.format_exc()
  32.             self.report({'WARNING'}, "Make sure objects in object list actualy exist. Also make sure the texture in the texture box exists.")
  33.             print(error)
  34.         return{'FINISHED'}
  35.    
  36.    
  37. #
  38. #   Explode action
  39. #
  40. class OBJECT_OT_Button(bpy.types.Operator):
  41.     bl_idname = "button.explodev2"
  42.     bl_label = "Explode"
  43.     bl_description = "Performs the explosion operation on the objects"
  44.     bl_options = {'REGISTER', 'UNDO'}
  45.    
  46.     def execute(self, context):
  47.         explodeFunc()
  48.        
  49.         return{'FINISHED'}
  50.  
  51.  
  52.  
  53. #
  54. #   Unexplode
  55. #
  56. class OBJECT_OT_unexplode(bpy.types.Operator):
  57.     bl_idname = "button.unexplode"
  58.     bl_label = "Un-Explode"
  59.     bl_description = "Reverts the objects to their unexploded state"
  60.     bl_options = {'REGISTER', 'UNDO'}
  61.    
  62.     def execute(self, context):
  63.         unExplodeFunc()
  64.         return{'FINISHED'}
  65.  
  66. def selectObjects():
  67.     proxies= bpy.context.scene['ObjList']
  68.     objlist = proxies.split(",")
  69.    
  70.     for i in range(0, len(objlist)):
  71.         object = bpy.data.objects[objlist[i]]
  72.         object.select = True
  73.     print("Done selecting")
  74.        
  75. def unSelectObjects():
  76.     proxies= bpy.context.scene['ObjList']
  77.     objlist = proxies.split(",")
  78.    
  79.     for i in range(0, len(objlist)):
  80.         object = bpy.data.objects[objlist[i]]
  81.         object.select = False
  82.     print("Done unselecting")
  83.  
  84. def renderNormal():
  85.    
  86.     #need to add a checkbox to make this work
  87.     specifyTexture = False
  88.     if specifyTexture:
  89.         texture_name = bpy.context.scene['ImgTarget']   # image texture name property
  90.         temp_obj = bpy.context.selected_objects[0]  # object
  91.         for uv_face in temp_obj.data.uv_textures.active.data:
  92.             uv_face.image = bpy.data.images[texture_name]
  93.         print("Assigned ", texture_name," to uv_face")
  94.     print("Rendering...")
  95.     finished = bpy.ops.object.bake_image() # uses the render settings
  96.     if finished:
  97.         print("Done rendering")
  98.         reset()
  99.        
  100.     print("Done.")
  101.    
  102. def reset():
  103.     unSelectObjects()
  104.     unExplodeFunc()
  105.     print("Object reseted")
  106.     #aa
  107.    
  108. def explodeFunc():
  109.     proxies= bpy.context.scene['ObjList']
  110.     objlist = proxies.split(",")
  111.     vGroups = bpy.context.selected_objects[0]
  112.    
  113.     #control
  114.     distance = bpy.context.scene['ExplodeDistance'];
  115.  
  116.    
  117.     if((len(vGroups.vertex_groups) == len(objlist)) and bpy.context.selected_objects[0]):
  118.         for i in range (0,len(objlist)):
  119.             cObj = bpy.data.objects[objlist[i]]
  120.            
  121.             #get vertexgroup
  122.             groupName = bpy.context.selected_objects[0].vertex_groups.keys()[i]
  123.             gi = vGroups.vertex_groups[groupName].index # get group index
  124.             for v in vGroups.data.vertices:
  125.                 for g in v.groups:
  126.                     if g.group == gi: # compare with index in VertexGroupElement
  127.                         #print(objlist[gi])
  128.                         v.co.x += distance*i
  129.                        
  130.             #move hi res mesh
  131.             bpy.data.objects[objlist[i]].location.x += distance*i;  
  132.                        
  133.             i+=1
  134.         print("Done exploding")
  135.     else:
  136.         print("Number of vertex groups in selection must match the number of high poly objects")
  137.    
  138. def unExplodeFunc():
  139.     proxies= bpy.context.scene['ObjList']
  140.     objlist = proxies.split(",")
  141.     vGroups = bpy.context.selected_objects[0]
  142.    
  143.     #control
  144.     distance = bpy.context.scene['ExplodeDistance'];
  145.    
  146.     if(len(vGroups.vertex_groups) == len(objlist)):
  147.         for i in range (0,len(objlist)):
  148.                                    
  149.             cObj = bpy.data.objects[objlist[i]]
  150.            
  151.             #get vertexgroup
  152.             groupName = bpy.context.selected_objects[0].vertex_groups.keys()[i]
  153.             gi = vGroups.vertex_groups[groupName].index # get group index
  154.             for v in vGroups.data.vertices:
  155.                 for g in v.groups:
  156.                     if g.group == gi: # compare with index in VertexGroupElement
  157.                         #print(objlist[gi])
  158.                         v.co.x -= distance*i
  159.            
  160.             #move hi res mesh back
  161.             bpy.data.objects[objlist[i]].location.x -= distance*i;
  162.             i+=1
  163.         print("Done unexploding")
  164.     else:
  165.         print("Number of vertex groups in selection must match the number of high poly objects")   
  166.  
  167.  
  168. #
  169. #   Menu in UI region
  170. #
  171. class UIPanel(bpy.types.Panel):
  172.     bl_label = "Explode bake"
  173.     bl_space_type = "VIEW_3D"
  174.     bl_region_type = "TOOLS"
  175.     bl_description = "TOOLS"
  176.  
  177.     initSceneProperties()
  178.  
  179.     def draw(self, context):
  180.         layout = self.layout
  181.         scn = context.scene
  182.         obj = context.object
  183.        
  184.         proxies= bpy.context.scene['ObjList']
  185.         objlist = proxies.split(",")
  186.    
  187.    
  188.         #col = layout.column()
  189.         section2 = layout.column(align=True)
  190.         section2.label("Main settings")
  191.         box = section2.box()
  192.         box.label("High resolution objects:")
  193.         box.prop(scn, 'ObjList')
  194.         box.label("Vertex group assignments:")
  195.         vg_box = box.box()
  196.         if (len(obj.vertex_groups) > 0 and len(objlist) > 0) and (len(obj.vertex_groups) == len(objlist)):
  197.             for group in obj.vertex_groups:
  198.                 vg_box.label(group.name + ' ('+objlist[group.index]+')')
  199.         else:
  200.             vg_box.label("No vertex groups or high poly objects")
  201.         box.label("Target texture:")
  202.         box.prop(scn, 'ImgTarget')
  203.  
  204.         section1 = layout.column(align=True)       
  205.         section1.operator("button.explodenbake")
  206.         section1.prop(scn, 'ExplodeDistance')
  207.        
  208.        
  209.         #section2.label("Operators")
  210.         #section2.operator("button.explodev2")
  211.         #section2.operator("button.unexplode")  
  212.  
  213.    
  214. def register():
  215.     bpy.utils.register_module(__name__)
  216.  
  217.     bpy.types.Scene.ExplodeDistance = FloatProperty(
  218.         name = "Explode Distance",
  219.         description = "Enter an amount large enough to stop the mesh interference.",
  220.         default = 5,
  221.         min = -100,
  222.         max = 100)
  223.        
  224.     bpy.types.Scene.ObjList = StringProperty(
  225.         name = "",
  226.         description = "Enter objects names, comma separated, in the same order as the vertex groups (each object will be assigned to the corresponding group)")
  227.        
  228.     bpy.types.Scene.ImgTarget = StringProperty(
  229.         name = "",
  230.         description = "Enter the name of the texture you want to bake to.")
  231.    
  232.  
  233. def unregister():
  234.     bpy.utils.unregister_module(__name__)
  235.  
  236.     del bpy.types.Scene.ExplodeDistance
  237.     del bpy.types.Scene.ObjList
  238.     del bpy.types.Scene.ImgTarget
  239.  
  240.    
  241. if __name__ == "__main__":
  242.     register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement