Advertisement
mifthbeat

MakeShadow.py

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