Advertisement
mifthbeat

TextureAtlas.py

Sep 4th, 2012
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 33.58 KB | None | 0 0
  1. bl_info = {
  2.     "name": "Texture Atlas",
  3.     "author": "Andreas Esau, Paul Geraskin",
  4.     "version": (0, 1),
  5.     "blender": (2, 6, 3),
  6.     "api": 50000,
  7.     "location": "Properties > Render",
  8.     "description": "A simple Texture Atlas for baking of many objects. It creates additional UV",
  9.     "warning": "This is just a testversion and may contain several bugs!",
  10.     "wiki_url": "",
  11.     "tracker_url": "",
  12.     "category": "Game Engine"}
  13.  
  14. import bpy
  15. from bpy.props import StringProperty, BoolProperty, EnumProperty, IntProperty, FloatProperty
  16. import mathutils
  17.  
  18. class TextureAtlas(bpy.types.Panel):
  19.     bl_label = "TextureAtlas - Lightbaker"
  20.     bl_space_type = 'PROPERTIES'
  21.     bl_region_type = 'WINDOW'
  22.     bl_context = "render"
  23.     COMPAT_ENGINES = {'BLENDER_RENDER'}
  24.    
  25.     def draw(self, context):
  26.         col = self.layout.column()
  27.         row = self.layout.row()
  28.         split = self.layout.split()
  29.         ob = context.object
  30.         scene = context.scene
  31.         row.template_list(scene, "ms_lightmap_groups", scene, "ms_lightmap_groups_index",prop_list="template_list_controls", rows=2)
  32.         col = row.column(align=True)
  33.         col.operator("scene.ms_add_lightmap_group", icon='ZOOMIN', text="")
  34.         col.operator("scene.ms_del_lightmap_group", icon='ZOOMOUT', text="")
  35.        
  36.         row = self.layout.row(align=True)
  37.        
  38.         try:
  39.           #  row.prop(context.scene.ms_lightmap_groups[context.scene.ms_lightmap_groups_index], 'bake_type', text='Shadow',expand=True)
  40.           #  row = self.layout.row()
  41.             row.prop(context.scene.ms_lightmap_groups[context.scene.ms_lightmap_groups_index], 'resolution', text='Resolution',expand=True)
  42.             row = self.layout.row()
  43.             row.prop(context.scene.ms_lightmap_groups[context.scene.ms_lightmap_groups_index], 'unwrap_type', text='Lightmap',expand=True)
  44.             row = self.layout.row()            
  45.         #    row.prop(context.scene.ms_lightmap_groups[context.scene.ms_lightmap_groups_index], 'sh_intens', text='Shadow Intensity', slider = True)  
  46.         #    row = self.layout.row()
  47.          #   row.prop(context.scene.ms_lightmap_groups[context.scene.ms_lightmap_groups_index], 'spec_intens', text='Specular Intensity', slider = True)        
  48.         except:
  49.             pass    
  50.        
  51.       #  row = self.layout.row()
  52.       #  row = self.layout.row()
  53.       #  row = self.layout.row()
  54.       #  row = self.layout.row()
  55.       #  row = self.layout.row()
  56.       #  row = self.layout.row()
  57.         row = self.layout.row()
  58.         row = self.layout.row()
  59.         row.operator("scene.ms_add_selected_to_group", text="Add Selection To Current Group",icon="GROUP")
  60.         row.operator("scene.ms_select_group", text="Select Current Group",icon="GROUP")        
  61.        
  62.         row = self.layout.row()
  63.         row.operator("object.ms_run",text="Create Texture Atlas",icon="LAMP_SPOT")
  64.  
  65. class runTextureAtlas(bpy.types.Operator):
  66.     bl_idname = "object.ms_run"
  67.     bl_label = "TextureAtlas - Run"
  68.     bl_description = "Creates a Shadowmap from selected Objects"
  69.    
  70.    
  71.    
  72.     def execute(self, context):
  73.         old_context = bpy.context.area.type
  74.        
  75.         try:
  76.             OBJECTLIST = []
  77.             active_object = bpy.context.scene.objects.active.name
  78.             for object in bpy.context.selectable_objects:
  79.                 OBJECTLIST.append(object.name)
  80.            
  81.            
  82.            
  83.             bpy.context.area.type = 'VIEW_3D'
  84.            # i = 0    
  85.             for group in bpy.context.scene.ms_lightmap_groups:
  86.    
  87.                 if group.bake == True:
  88.                 #    if i == 0:
  89.                     bpy.ops.object.ms_merge_objects(group_name=group.name)
  90.                     bpy.ops.object.ms_create_lightmap(group_name=group.name)
  91.                    
  92.                     res = int(bpy.context.scene.ms_lightmap_groups[group.name].resolution)
  93.                     bpy.ops.object.ms_make_texture(group_name=group.name,resolution=res)
  94.                    
  95.                    
  96.                  #   i += 1  
  97.                  #   if i >= len(bpy.context.scene.ms_lightmap_groups):
  98.                     bpy.ops.object.ms_separate_objects(group_name=group.name)
  99.                      
  100.             bpy.context.area.type = old_context
  101.            
  102.             bpy.ops.object.select_all(action='DESELECT')
  103.             for object in OBJECTLIST:
  104.                 bpy.context.scene.objects[object].select = True
  105.                 bpy.context.scene.objects.active = bpy.context.scene.objects[active_object]
  106.             bpy.ops.object.select_all(action='DESELECT')    
  107.         except:
  108.              self.report({'INFO'}, "Something went wrong!")
  109.              bpy.context.area.type = old_context  
  110.         return{'FINISHED'}
  111.    
  112. class uv_layers(bpy.types.PropertyGroup):
  113.     name = bpy.props.StringProperty(default="")
  114.  
  115. class materials(bpy.types.PropertyGroup):
  116.     name = bpy.props.StringProperty(default="")
  117.  
  118. class children(bpy.types.PropertyGroup):
  119.     name = bpy.props.StringProperty(default="")
  120.    
  121. class vertex_groups(bpy.types.PropertyGroup):
  122.     name = bpy.props.StringProperty(default="")
  123.    
  124. class groups(bpy.types.PropertyGroup):
  125.     name = bpy.props.StringProperty(default="")
  126.  
  127. class ms_lightmap_groups(bpy.types.PropertyGroup):
  128.    
  129.   #  def update_color(self, context):
  130.   #      for object in bpy.data.groups[self.name].objects:
  131.   #          for material in object.data.materials:
  132.   #              material.texture_slots[self.name].diffuse_color_factor = self.sh_intens
  133.                
  134.   #  def update_spec(self, context):
  135.   #      for object in bpy.data.groups[self.name].objects:
  136.   #          for material in object.data.materials:
  137.   #              material.texture_slots[self.name].specular_factor = self.spec_intens
  138.                
  139.     def update(self,context):
  140.         for object in bpy.data.groups[self.name].objects:
  141.             for material in object.data.materials:
  142.                 material.texture_slots[self.name].use = self.bake
  143.    
  144.     name = bpy.props.StringProperty(default="")
  145.     bake = bpy.props.BoolProperty(default=True, update=update)
  146.   #  show = bpy.props.BoolProperty(default=True,update=update)
  147.   #  sh_intens = FloatProperty(default=1.0,min=0.0,max=1.0,update=update_color)
  148.   #  spec_intens = FloatProperty(default=5.0,min=0.0,max=10.0,update=update_spec)
  149.   #  bake_type = EnumProperty(name="bake_type",items=(('0','Shadow','SHADOW'),('1','Full','FULL'),('2','Ambient Occlusion','AMBIENT_OCCLUSION')))
  150.     unwrap_type = EnumProperty(name="unwrap_type",items=(('0','Smart_Unwrap', 'Smart_Unwrap'),('1','Lightmap', 'Lightmap'), ('2','No_Unwrap', 'No_Unwrap')))
  151.     resolution = EnumProperty(name="resolution",items=(('256','256','256'),('512','512','512'),('1024','1024','1024'),('2048','2048','2048'),('4096','4096','4096')))
  152.     template_list_controls = StringProperty(default="bake", options={"HIDDEN"})
  153.    
  154.    
  155.  
  156. class mergedObjects(bpy.types.PropertyGroup):
  157.     name = bpy.props.StringProperty(default="")
  158.     position = bpy.props.FloatVectorProperty(default=(0.0,0.0,0.0))
  159.     scale = bpy.props.FloatVectorProperty(default=(0.0,0.0,0.0))
  160.     rotation = bpy.props.FloatVectorProperty(default=(0.0,0.0,0.0))
  161.     material = bpy.props.CollectionProperty(type=materials)
  162.     parent = bpy.props.StringProperty(default="")
  163.     children = bpy.props.CollectionProperty(type=children)
  164.     vertex_groups = bpy.props.CollectionProperty(type=vertex_groups)
  165.     groups = bpy.props.CollectionProperty(type=groups)
  166.     uv_layers = bpy.props.CollectionProperty(type=uv_layers)
  167.    
  168.  
  169. class addSelectedToGroup(bpy.types.Operator):
  170.     bl_idname = "scene.ms_add_selected_to_group"
  171.     bl_label = ""
  172.     bl_description = "Adds selected Objects to current Group"
  173.    
  174.    
  175.     def execute(self, context):
  176.         try:
  177.             group_name = bpy.context.scene.ms_lightmap_groups[bpy.context.scene.ms_lightmap_groups_index].name
  178.         except:
  179.             self.report({'INFO'}, "No Groups Exists!")
  180.         for object in bpy.context.selected_objects:
  181.             if object.type == 'MESH':
  182.                 try:
  183.                     bpy.data.groups[group_name].objects.link(object)
  184.                 except:
  185.                     pass
  186.                    
  187.         return {'FINISHED'}
  188.  
  189. class selectGroup(bpy.types.Operator):
  190.     bl_idname = "scene.ms_select_group"
  191.     bl_label = ""
  192.     bl_description = "Selected Objects of current Group"
  193.    
  194.    
  195.     def execute(self, context):
  196.         try:
  197.             group_name = bpy.context.scene.ms_lightmap_groups[bpy.context.scene.ms_lightmap_groups_index].name
  198.         except:
  199.             self.report({'INFO'}, "No Groups Exists!")
  200.         for object in bpy.context.selectable_objects:
  201.                 try:
  202.                     bpy.ops.object.select_all(action='DESELECT')
  203.                     for object in bpy.data.groups[group_name].objects:
  204.                       object.select = True
  205.                 except:
  206.                     pass
  207.                    
  208.         return {'FINISHED'}        
  209.        
  210. class addLightmapGroup(bpy.types.Operator):
  211.     bl_idname = "scene.ms_add_lightmap_group"
  212.     bl_label = ""
  213.     bl_description = "Adds a new Lightmap Group"
  214.    
  215.     name = StringProperty(name="Group Name",default='lightmap')
  216.  
  217.     def execute(self, context):
  218.         group = bpy.data.groups.new(self.name)
  219.        
  220.         item = bpy.context.scene.ms_lightmap_groups.add()
  221.         item.name = group.name
  222.         item.resolution = '1024'
  223.         bpy.context.scene.ms_lightmap_groups_index = len(bpy.context.scene.ms_lightmap_groups)-1
  224.        
  225.        
  226.         for object in bpy.context.selected_objects:
  227.             bpy.context.scene.objects.active = object
  228.             if bpy.context.active_object.type == 'MESH':
  229.                 bpy.data.groups[group.name].objects.link(object)
  230.  
  231.        
  232.         return {'FINISHED'}
  233.    
  234.     def invoke(self, context, event):
  235.         wm = context.window_manager
  236.         return wm.invoke_props_dialog(self)
  237.    
  238. class delLightmapGroup(bpy.types.Operator):
  239.     bl_idname = "scene.ms_del_lightmap_group"
  240.     bl_label = ""
  241.     bl_description = "Deletes active Lightmap Group"
  242.    
  243.  
  244.     def execute(self, context):
  245.         idx = bpy.context.scene.ms_lightmap_groups_index
  246.         group_name = bpy.context.scene.ms_lightmap_groups[idx].name
  247.         for object in bpy.data.groups[group_name].objects:
  248.             for material in object.data.materials:
  249.                 for i in range(len(material.texture_slots)):
  250.                     if material.texture_slots[i] != None:
  251.                         if material.texture_slots[i].name == group_name:
  252.                             print (material.texture_slots[i].name)
  253.                             material.texture_slots[i].use = False
  254.                             material.texture_slots.clear(i)
  255.        
  256.        
  257.         bpy.data.groups.remove(bpy.data.groups[bpy.context.scene.ms_lightmap_groups[idx].name])
  258.         bpy.context.scene.ms_lightmap_groups.remove(bpy.context.scene.ms_lightmap_groups_index)
  259.         bpy.context.scene.ms_lightmap_groups_index -= 1
  260.         if bpy.context.scene.ms_lightmap_groups_index < 0:
  261.             bpy.context.scene.ms_lightmap_groups_index = 0
  262.            
  263.        
  264.         return {'FINISHED'}
  265.  
  266. class mergeObjects(bpy.types.Operator):
  267.     bl_idname = "object.ms_merge_objects"
  268.     bl_label = "TextureAtlas - MergeObjects"
  269.     bl_description = "Merges Objects and stores Origins"
  270.    
  271.     group_name = StringProperty(default='lightmap')
  272.  
  273.     def execute(self, context):
  274.         active_object = bpy.data.groups[self.group_name].objects[0]
  275.        
  276.         bpy.ops.object.select_all(action='DESELECT')
  277.         OBJECTLIST = []
  278.         for object in bpy.data.groups[self.group_name].objects:
  279.             OBJECTLIST.append(object)  
  280.             object.select = True  
  281.         bpy.context.scene.objects.active = active_object      
  282.  
  283.         #for object in OBJECTLIST:
  284.         #    object.select = True
  285.        
  286.         ### Make Object Single User
  287.         bpy.ops.object.make_single_user(type='SELECTED_OBJECTS', object=True, obdata=True, material=False, texture=False, animation=False)
  288.            
  289.         for object in bpy.context.selected_objects:
  290.             ### delete lightmap uv if existant
  291.             for uv in object.data.uv_textures:
  292.                 if uv.name == self.group_name:
  293.                     uv.active = True
  294.                     bpy.context.scene.objects.active = object
  295. #                    bpy.ops.mesh.uv_texture_remove()
  296.  
  297.                    
  298.            
  299.             ### create Material if none exists
  300.             #if len(object.material_slots) == 0:
  301.                 #mat = bpy.data.materials.new(object.name)
  302.                 #object.data.materials.append(bpy.data.materials[object.name])
  303.             #elif object.material_slots[0].name == '':
  304.                # mat = bpy.data.materials.new(object.name)
  305.                 #object.data.materials.append(bpy.data.materials[object.name])
  306.                
  307.             ### create vertex groups for each selected object
  308.             bpy.context.scene.objects.active = bpy.data.objects[object.name]
  309.             bpy.ops.object.mode_set(mode = 'EDIT')
  310.             bpy.ops.mesh.select_all(action='SELECT')
  311.             bpy.ops.object.vertex_group_add()
  312.             bpy.ops.object.vertex_group_assign()
  313.             id = len(bpy.context.object.vertex_groups)-1
  314.             bpy.context.object.vertex_groups[id].name = object.name
  315.             bpy.ops.mesh.select_all(action='DESELECT')
  316.             bpy.ops.object.mode_set(mode = 'OBJECT')
  317.            
  318.            
  319.             ### save object name and object location in merged object
  320.             item = active_object.ms_merged_objects.add()
  321.             item.name = object.name
  322.             item.scale = mathutils.Vector(object.scale)
  323.             item.rotation = mathutils.Vector(object.rotation_euler)
  324.            
  325.             ### save vertex group name in merged object
  326.             for vertex_group in object.vertex_groups:
  327.                 item4 = active_object.ms_merged_objects[item.name].vertex_groups.add()
  328.                 item4.name = vertex_group.name
  329.            
  330.             ### save uv layer name
  331.             for layer in object.data.uv_layers:
  332.                 item6 = active_object.ms_merged_objects[item.name].uv_layers.add()
  333.                 item6.name = layer.name
  334.                
  335.             ### save vertex group name in merged object
  336.             for group in bpy.data.groups:
  337.                 if object.name in group.objects:
  338.                     item5 = active_object.ms_merged_objects[item.name].groups.add()
  339.                     item5.name = group.name    
  340.            
  341.             ### save material name in merged object
  342.             for material in object.material_slots:
  343.                 item2 = active_object.ms_merged_objects[item.name].material.add()
  344.                 item2.name = material.name
  345.            
  346.             for child in object.children:
  347.                 if child not in bpy.context.selected_objects:
  348.                     item3 = active_object.ms_merged_objects[item.name].children.add()
  349.                     item3.name = child.name
  350.                    
  351.             if object.parent != None:
  352.                 item.parent = object.parent.name
  353.                 bpy.ops.object.select_all(action='DESELECT')
  354.                 bpy.context.scene.objects[object.name].select = True
  355.                 bpy.context.scene.objects.active = bpy.context.scene.objects[object.name]
  356.                 bpy.ops.object.parent_clear(type='CLEAR_KEEP_TRANSFORM')
  357.             else:
  358.                 item.parent = ''
  359.            
  360.             ### generate temp Duplicate Objects with copied modifier,properties and logic bricks
  361.             bpy.ops.object.select_all(action='DESELECT')
  362.            
  363.             bpy.context.scene.objects.active = object
  364.             me = bpy.data.meshes.new(object.name+'_t')
  365.             ob = bpy.data.objects.new(object.name+'_t',me)
  366.             bpy.context.scene.objects.link(ob)
  367.             ob.select = True
  368.             ob.location = object.location
  369.            
  370.             bpy.ops.object.make_links_data(type='MODIFIERS')
  371.             bpy.ops.object.game_property_copy(operation='MERGE')
  372.             bpy.ops.object.logic_bricks_copy()
  373.            
  374.             bpy.ops.object.select_all(action='DESELECT')
  375.             for object in OBJECTLIST:
  376.                 object.select = True
  377.             #####
  378.                
  379.             item.position = mathutils.Vector(object.location)
  380.            
  381.            
  382.                
  383.  
  384.         ### merge objects together
  385.         bpy.ops.object.transform_apply(location=False, rotation=True, scale=True)
  386.         bpy.context.scene.objects.active = active_object
  387.         bpy.ops.object.join()
  388.         bpy.context.object.name = 'mergedObject'
  389.        
  390.         return{'FINISHED'}
  391.  
  392. class createLightmap(bpy.types.Operator):
  393.     bl_idname = "object.ms_create_lightmap"
  394.     bl_label = "TextureAtlas - Generate Lightmap"
  395.     bl_description = "Generates a Lightmap"
  396.    
  397.     group_name = StringProperty(default='')
  398.  
  399.     def execute(self, context):  
  400.        
  401.         ### create lightmap uv layout
  402.         bpy.ops.object.mode_set(mode = 'EDIT')
  403.        
  404.         if bpy.context.object.data.uv_textures.active == None:
  405.             bpy.ops.mesh.uv_texture_add()
  406.             bpy.context.object.data.uv_textures.active.name = self.group_name
  407.         else:    
  408.             if self.group_name not in bpy.context.object.data.uv_textures:
  409.                 bpy.ops.mesh.uv_texture_add()
  410.                 bpy.context.object.data.uv_textures.active.name = self.group_name
  411.                 bpy.context.object.data.uv_textures[self.group_name].active = True
  412.                 bpy.context.object.data.uv_textures[self.group_name].active_render = True
  413.             else:
  414.                 bpy.context.object.data.uv_textures[self.group_name].active = True
  415.                 bpy.context.object.data.uv_textures[self.group_name].active_render = True
  416.        
  417.         bpy.ops.mesh.select_all(action='SELECT')
  418.        
  419.         if bpy.context.scene.ms_lightmap_groups[self.group_name].unwrap_type == '0':
  420.             bpy.ops.uv.smart_project(angle_limit=72.0, island_margin=0.2, user_area_weight=0.0)
  421.         elif bpy.context.scene.ms_lightmap_groups[self.group_name].unwrap_type == '1':
  422.             bpy.ops.uv.lightmap_pack(PREF_CONTEXT='ALL_FACES', PREF_PACK_IN_ONE=True, PREF_NEW_UVLAYER=False, PREF_APPLY_IMAGE=False, PREF_IMG_PX_SIZE=1024, PREF_BOX_DIV=48, PREF_MARGIN_DIV=0.2)
  423.          
  424.            
  425.         bpy.ops.object.mode_set(mode = 'OBJECT')
  426.         return{'FINISHED'}
  427.  
  428.  
  429. class makeTexture(bpy.types.Operator):
  430.     bl_idname = "object.ms_make_texture"
  431.     bl_label = "TextureAtlas - Make Texture"
  432.     bl_description = "Makes a Texture"
  433.    
  434.     group_name = StringProperty(default='')
  435.     resolution = IntProperty(default=1024)
  436.  
  437.     def execute(self, context):
  438.         bpy.ops.object.mode_set(mode = 'EDIT')
  439.         bpy.ops.mesh.select_all(action='SELECT')
  440.         if self.group_name not in bpy.data.images:
  441.             bpy.ops.image.new(name=self.group_name,width=self.resolution,height=self.resolution)
  442.             bpy.ops.object.mode_set(mode = 'EDIT')
  443.             bpy.data.screens['UV Editing'].areas[1].spaces[0].image = bpy.data.images[self.group_name]
  444.         else:
  445.             bpy.ops.object.mode_set(mode = 'EDIT')
  446.             bpy.data.screens['UV Editing'].areas[1].spaces[0].image = bpy.data.images[self.group_name]
  447.             bpy.data.images[self.group_name].generated_type = 'BLANK'
  448.             bpy.data.images[self.group_name].generated_width = self.resolution
  449.             bpy.data.images[self.group_name].generated_height = self.resolution
  450.            
  451.        
  452.         bpy.ops.object.mode_set(mode = 'OBJECT')
  453.        
  454.       #  current_bake_type = bpy.context.scene.render.bake_type
  455.       #  bake_margin = bpy.context.scene.render.bake_margin
  456.       #  bpy.context.scene.render.bake_margin = 4
  457.        
  458.       #  if bpy.context.scene.ms_lightmap_groups[self.group_name].bake_type == '0':
  459.       #       bpy.context.scene.render.bake_type = 'SHADOW'
  460.       #  elif bpy.context.scene.ms_lightmap_groups[self.group_name].bake_type == '1':
  461.       #       bpy.context.scene.render.bake_type = 'FULL'            
  462.        # elif bpy.context.scene.ms_lightmap_groups[self.group_name].bake_type == '2':
  463.       #       bpy.context.scene.render.bake_type = 'AO'
  464.        #      normalized = bpy.context.scene.render.use_bake_normalize
  465.        #      bpy.context.scene.render.use_bake_normalize = True
  466.            
  467.            
  468.      #   bpy.ops.object.bake_image()
  469.        
  470.      #   try:
  471.       #      bpy.context.scene.render.use_bake_normalize = normalized
  472.      #   except:
  473.      #       pass
  474.       #  bpy.context.scene.render.bake_type = current_bake_type
  475.       #  bpy.context.scene.render.bake_margin = bake_margin
  476.        
  477.         return{'FINISHED'}
  478.    
  479. class separateObjects(bpy.types.Operator):
  480.     bl_idname = "object.ms_separate_objects"
  481.     bl_label = "TextureAtlas - Separate Objects"
  482.     bl_description = "Separates Objects and restores Origin"
  483.    
  484.     group_name = StringProperty(default='')
  485.  
  486.     def execute(self, context):
  487.         active_object = bpy.context.active_object.name
  488.         for object in bpy.context.scene.objects:
  489.             if object.mode != 'OBJECT':
  490.                 bpy.context.scene.objects.active = object
  491.                 bpy.ops.object.mode_set(mode = 'OBJECT')
  492.            
  493.         bpy.context.scene.objects.active = bpy.context.scene.objects[active_object]
  494.        
  495.         OBJECTLIST = []
  496.         for object in bpy.context.active_object.ms_merged_objects:
  497.             OBJECTLIST.append(object.name)
  498.             ### select vertex groups and separate group from merged object
  499.             bpy.ops.object.mode_set(mode = 'EDIT')
  500.             bpy.ops.mesh.select_all(action='DESELECT')
  501.             bpy.context.active_object.vertex_groups.active_index = bpy.context.active_object.vertex_groups[object.name].index            
  502.             bpy.ops.object.vertex_group_select()
  503.             bpy.ops.mesh.separate(type='SELECTED')
  504.            
  505.             ### rename separated object to old name
  506.             bpy.ops.object.mode_set(mode = 'OBJECT')
  507.             bpy.context.scene.objects.active = bpy.context.scene.objects[active_object+'.001']
  508.             bpy.ops.object.select_all(action='TOGGLE')
  509.             bpy.context.scene.objects.active.select = True
  510.             bpy.context.active_object.name = object.name
  511.             bpy.context.active_object.data.name = object.name
  512.            
  513.             ### restore objects origin
  514.             bpy.context.scene.cursor_location = bpy.context.scene.objects[object.name+'_t'].location#object.position
  515.             bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
  516.            
  517.             ### restore objects rotation
  518.             bpy.context.active_object.rotation_euler = mathutils.Vector(object.rotation)
  519.             for vertex in bpy.context.active_object.data.vertices:
  520.                 vertex.co = vertex.co * bpy.context.active_object.rotation_euler.to_matrix()
  521.            
  522.             ### restore objects scale
  523.             old_pivot_point = bpy.context.space_data.pivot_point
  524.             bpy.context.active_object.scale = object.scale
  525.             bpy.context.space_data.pivot_point = 'CURSOR'
  526.             for vertex in bpy.context.active_object.data.vertices:
  527.                 vertex.co[0] = vertex.co[0] * (1/bpy.context.active_object.scale[0])
  528.                 vertex.co[1] = vertex.co[1] * (1/bpy.context.active_object.scale[1])
  529.                 vertex.co[2] = vertex.co[2] * (1/bpy.context.active_object.scale[2])            
  530.             bpy.context.space_data.pivot_point = old_pivot_point
  531.            
  532.                    
  533.             ### restore parent
  534.             if object.parent != '':
  535.                 bpy.context.scene.objects[object.parent].select=True
  536.                 bpy.context.scene.objects.active = bpy.context.scene.objects[object.parent]  
  537.                 bpy.ops.object.parent_set(type='OBJECT', xmirror=False)
  538.                 bpy.ops.object.select_all(action='TOGGLE')
  539.                 bpy.context.scene.objects.active = bpy.context.scene.objects[object.name]  
  540.                
  541.            
  542.                
  543.             ### delete not used materials from separeted objects
  544.             slot_length = len(bpy.context.scene.objects[object.name].material_slots)
  545.             for i in range(0,len(bpy.context.scene.objects[object.name].material_slots)):
  546.                 idx = slot_length-1-i
  547.                 bpy.context.active_object.active_material_index = idx
  548.                 try:
  549.                     if bpy.context.scene.objects[object.name].active_material.name not in object.material:
  550.                         bpy.ops.object.material_slot_remove()
  551.                 except:
  552.                     bpy.ops.object.material_slot_remove()
  553.                    
  554.             bpy.context.active_object.active_material_index = 0
  555.            
  556.             ### delete vertex groups and object properties
  557.             for group in bpy.context.active_object.ms_merged_objects:
  558.                 id = bpy.context.active_object.vertex_groups[group.name]
  559.                 bpy.context.active_object.vertex_groups.remove(id)
  560.             #bpy.ops.wm.properties_remove(data_path="object",property="ms_merged_objects")
  561.            
  562.             ### delete not used vertex groups from separeted objects
  563.             for vertex_group in bpy.context.scene.objects[object.name].vertex_groups:
  564.                 if vertex_group.name not in object.vertex_groups:
  565.                     bpy.context.scene.objects[object.name].vertex_groups.remove(vertex_group)
  566.                
  567.             ### delete not used groups from separeted objects
  568.             for group in bpy.data.groups:
  569.                 if group.name not in object.groups:
  570.                     try:
  571.                         group.objects.unlink(bpy.context.scene.objects[object.name])
  572.                     except:
  573.                         pass
  574.                 if group.name in object.groups:
  575.                     try:
  576.                         group.objects.link(bpy.context.scene.objects[object.name])
  577.                     except:
  578.                         pass
  579.                        
  580.            
  581.             ### generate overlay Texture
  582. #            for material in bpy.context.active_object.material_slots:  
  583. #                for image in bpy.data.images:
  584. #                    
  585. #                    if image.name in bpy.context.scene.ms_lightmap_groups:
  586. #                        print(image.name)
  587. #                        if image.name not in bpy.data.textures:
  588. #                            texture = bpy.data.textures.new(image.name, type = 'IMAGE')
  589. #                            texture.image = bpy.data.images[image.name]
  590. #                        else:
  591. #                            texture = bpy.data.textures[image.name]
  592. #                            texture.image = bpy.data.images[image.name]
  593. #
  594. #                        print(texture.name)
  595. #                        if image.name not in material.material.texture_slots:
  596. #                            mtex = material.material.texture_slots.add()
  597. #                        else:
  598. #                            mtex = material.material.texture_slots[image.name]
  599. #                        mtex.texture = texture
  600. #                        mtex.texture_coords = 'UV'
  601. #                        mtex.uv_layer = 'lightmap'
  602. #                        mtex.blend_type = 'MULTIPLY'
  603. #                        mtex.use_map_specular = True
  604. #                        mtex.use = True
  605. #                        
  606. #                        bpy.data.textures[image.name].use_alpha = False
  607. #                        mtex.specular_factor = 5.0
  608.            
  609.            
  610. #            for material in bpy.context.active_object.material_slots:  
  611. #                if self.group_name not in bpy.data.textures:
  612. #                    image = bpy.data.textures.new(self.group_name, type = 'IMAGE')
  613. #                    image.image = bpy.data.images[self.group_name]
  614. #                else:
  615. #                    image = bpy.data.textures[self.group_name]
  616. #                    image.image = bpy.data.images[self.group_name]
  617. #                
  618. #                if self.group_name not in material.material.texture_slots:
  619. #                    mtex = material.material.texture_slots.add()
  620. #                else:
  621. #                    mtex = material.material.texture_slots[self.group_name]
  622. #                mtex.texture = image
  623. #                mtex.texture_coords = 'UV'
  624. #                mtex.uv_layer = 'lightmap'
  625. #                mtex.blend_type = 'MULTIPLY'
  626. #                mtex.use_map_specular = True
  627. #                mtex.use = True
  628. #                
  629. #                bpy.data.textures[self.group_name].use_alpha = False
  630. #                mtex.specular_factor = 5.0
  631.                
  632.                
  633.                
  634.             bpy.context.scene.objects.active = bpy.context.scene.objects[active_object]
  635.             bpy.context.scene.objects.active.select = True
  636.             bpy.context.active_object.name = active_object
  637.            
  638.        
  639.  
  640.         ### restore children of object
  641.         for object in bpy.context.active_object.ms_merged_objects:            
  642.             if 'children' in object:
  643.                 for child in object.children:
  644.                     bpy.ops.object.select_all(action='DESELECT')
  645.                     bpy.context.scene.objects[child.name].select = True
  646.                     bpy.context.scene.objects[object.name].select = True
  647.                     bpy.context.scene.objects.active = bpy.context.scene.objects[object.name]
  648.                    
  649.                     bpy.ops.object.parent_set(type='OBJECT', xmirror=False)
  650.                
  651.        
  652.         ### restore UV Name
  653.            
  654.         for name in OBJECTLIST:
  655.             bpy.ops.object.select_all(action='DESELECT')
  656.            
  657.             bpy.context.scene.objects[name].select = True
  658.             bpy.context.scene.objects.active = bpy.context.scene.objects[name]
  659.            
  660.             for i in range(len(bpy.context.scene.objects[name].ms_merged_objects[name].uv_layers)):
  661.                 uv = bpy.context.scene.objects[name].data.uv_textures[i]
  662.                 uv.name = bpy.context.scene.objects[name].ms_merged_objects[name].uv_layers[i].name
  663.          
  664.             object = bpy.context.active_object
  665.            
  666.             uv = object.data.uv_textures
  667.             uv_len = len(uv)
  668.             for i in range(uv_len):
  669.                 idx = uv_len - i-1
  670.                 if uv[idx].name not in object.ms_merged_objects[object.name].uv_layers and uv[idx].name != self.group_name:
  671.                     uv[idx].active = True
  672.                     bpy.ops.mesh.uv_texture_remove()
  673.                    
  674.             ### delete ms_merged_objects property
  675.             bpy.ops.wm.properties_remove(data_path="object",property="ms_merged_objects")
  676.            
  677.        
  678.            
  679.         ### restore Logic Bricks, Properties, Modifier
  680.         for name in OBJECTLIST:
  681.             bpy.ops.object.select_all(action='DESELECT')
  682.             bpy.context.scene.objects[name].select = True
  683.             bpy.context.scene.objects[name+'_t'].select = True
  684.             bpy.context.scene.objects.active = bpy.context.scene.objects[name+'_t']
  685.            
  686.             bpy.ops.object.make_links_data(type='MODIFIERS')
  687.             bpy.ops.object.game_property_copy(operation='MERGE')
  688.             bpy.ops.object.logic_bricks_copy()
  689.        
  690.         ### delete empty merged object
  691.        
  692.         bpy.ops.object.select_all(action='DESELECT')
  693.         for name in OBJECTLIST:
  694.             bpy.context.scene.objects[name+'_t'].select = True
  695.         bpy.context.scene.objects[active_object].select = True
  696.         bpy.ops.object.delete(use_global=False)
  697.            
  698.         return{'FINISHED'}
  699.  
  700.  
  701.  
  702. def register():
  703.     bpy.utils.register_class(TextureAtlas)
  704.    
  705.     bpy.utils.register_class(addLightmapGroup)
  706.     bpy.utils.register_class(delLightmapGroup)
  707.     bpy.utils.register_class(addSelectedToGroup)
  708.     bpy.utils.register_class(selectGroup)
  709.    
  710.     bpy.utils.register_class(runTextureAtlas)
  711.     bpy.utils.register_class(mergeObjects)
  712.     bpy.utils.register_class(separateObjects)
  713.     bpy.utils.register_class(createLightmap)
  714.     bpy.utils.register_class(makeTexture)
  715.    
  716.     bpy.utils.register_class(materials)
  717.     bpy.utils.register_class(uv_layers)
  718.     bpy.utils.register_class(children)
  719.     bpy.utils.register_class(vertex_groups)
  720.     bpy.utils.register_class(groups)
  721.    
  722.     bpy.utils.register_class(mergedObjects)
  723.     bpy.types.Object.ms_merged_objects = bpy.props.CollectionProperty(type=mergedObjects)
  724.    
  725.     bpy.utils.register_class(ms_lightmap_groups)
  726.     bpy.types.Scene.ms_lightmap_groups = bpy.props.CollectionProperty(type=ms_lightmap_groups)
  727.     bpy.types.Scene.ms_lightmap_groups_index = bpy.props.IntProperty()
  728.    
  729.  
  730.  
  731. def unregister():
  732.     bpy.utils.unregister_class(TextureAtlas)
  733.    
  734.     bpy.utils.unregister_class(addLightmapGroup)
  735.     bpy.utils.unregister_class(delLightmapGroup)
  736.     bpy.utils.unregister_class(addSelectedToGroup)
  737.     bpy.utils.unregister_class(selectGroup)
  738.    
  739.     bpy.utils.unregister_class(runTextureAtlas)
  740.     bpy.utils.unregister_class(mergeObjects)
  741.     bpy.utils.unregister_class(separateObjects)
  742.     bpy.utils.unregister_class(createLightmap)
  743.     bpy.utils.unregister_class(makeTexture)
  744.    
  745.     bpy.utils.unregister_class(materials)
  746.     bpy.utils.unregister_class(uv_layers)
  747.     bpy.utils.unregister_class(children)
  748.     bpy.utils.unregister_class(vertex_groups)
  749.     bpy.utils.unregister_class(groups)
  750.    
  751.     bpy.utils.unregister_class(mergedObjects)
  752.    
  753.     bpy.utils.unregister_class(ms_lightmap_groups)
  754.    
  755.    
  756. if __name__ == "__main__":
  757.     register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement