Advertisement
Guest User

myscript

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