Advertisement
Guest User

Untitled

a guest
Oct 26th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.66 KB | None | 0 0
  1. import bpy
  2.  
  3. class gen():
  4.     def textures(i, c):
  5.         # Create the images for each object
  6.         bpy.context.scene.objects.active = i
  7.         stencil = bpy.data.images.new(name = 'Stencil'+str(c), width = 1024, height = 1024)
  8.         stencil2 = bpy.data.images.new(name = '2Stencil'+str(c), width = 1204, height = 1024)
  9.         displ = bpy.data.images.new(name = 'Displ'+str(c), width = 1024, height= 1024)
  10.         diffuse = bpy.data.images.new(name = 'Diffuse'+str(c), width= 1024, height=1024)
  11.         normal = bpy.data.images.new(name = 'Normal'+str(c), width=1024, height=1024)
  12.         bpy.data.materials['Normal'].node_tree.nodes['Normal'].outputs[0].default_value = (0,1,1)
  13.         bpy.data.materials['Normal'].node_tree.nodes['Mapping'].scale[2] = 20
  14.        
  15.         #assign the images and bake stencils from 'Normal'
  16.         for face in i.data.uv_textures.active.data:
  17.             face.image = stencil
  18.         bpy.data.scenes['Scene'].render.bake_type = 'TEXTURE'
  19.         bpy.ops.object.bake_image()
  20.         #needs 'Normal' for stencil
  21.         bpy.data.materials['Normal'].node_tree.nodes['Mapping'].scale[2] = 40
  22.  
  23.         for face in i.data.uv_textures.active.data:
  24.             face.image = stencil2
  25.         bpy.ops.object.bake_image()
  26.          
  27.         #Create and assign a new material ans textures for displacement
  28.         m = bpy.data.materials.new(name='Ground'+str(c))
  29.         m.texture_slots.add()
  30.         m.texture_slots.add()
  31.         m.texture_slots.add()
  32.         m.texture_slots.add()
  33.         m.texture_slots.add()
  34.         #get rid of original material
  35.         bpy.ops.object.material_slot_remove(0)    
  36.         bpy.ops.object.material_slot_add()
  37.         i.material_slots[''].material = m
  38.         m.diffuse_color = (0,0,0)
  39.            
  40.         #créer au préalable/needs une texture de 'Rochers', 'Herbe', 'Dirt'
  41.         m.active_texture_index = 4
  42.         m.texture_slots[4].texture = bpy.data.textures['Herbe']
  43.         m.texture_slots['Herbe'].texture_coords = 'UV'
  44.         m.texture_slots['Herbe'].use_map_color_spec = True
  45.         m.texture_slots['Herbe'].scale[0] = 5
  46.         m.texture_slots['Herbe'].scale[1] = 5
  47.                      
  48.         t = bpy.data.textures.new(name='Stencil'+str(c), type='IMAGE')
  49.         m.active_texture_index = 3
  50.         m.texture_slots[3].texture = bpy.data.textures['Stencil'+str(c)]
  51.         m.texture_slots['Stencil'+str(c)].texture_coords = 'UV'
  52.         m.active_texture.image = stencil
  53.         m.texture_slots['Stencil'+str(c)].use_rgb_to_intensity = True
  54.         m.texture_slots['Stencil'+str(c)].use_stencil = True
  55.         m.texture_slots['Stencil'+str(c)].use_map_color_diffuse = False
  56.            
  57.         m.active_texture_index = 2
  58.         m.texture_slots[2].texture = bpy.data.textures['Rochers']
  59.         m.texture_slots['Rochers'].texture_coords = 'UV'
  60.         m.texture_slots['Rochers'].use_map_color_spec = True
  61.        
  62.         t = bpy.data.textures.new(name='2Stencil'+str(c), type='IMAGE')
  63.         m.active_texture_index = 1
  64.         m.texture_slots[1].texture = bpy.data.textures['2Stencil'+str(c)]
  65.         m.active_texture.image = stencil2
  66.         m.texture_slots['2Stencil'+str(c)].blend_type = 'SUBTRACT'
  67.         m.texture_slots['2Stencil'+str(c)].texture_coords = 'UV'
  68.         m.texture_slots['2Stencil'+str(c)].use_rgb_to_intensity = True
  69.         m.texture_slots['2Stencil'+str(c)].use_stencil = True
  70.         m.texture_slots['2Stencil'+str(c)].use_map_color_diffuse = False
  71.              
  72.         m.active_texture_index = 0
  73.         m.texture_slots[0].texture = bpy.data.textures['Dirt']
  74.         m.texture_slots['Dirt'].texture_coords = 'UV'
  75.         m.texture_slots['Dirt'].use_map_color_spec = True
  76.         m.texture_slots['Dirt'].scale[0] = 5
  77.         m.texture_slots['Dirt'].scale[1] = 5
  78.         #deactivate the textures not needed for displace
  79.         m.use_textures[0] = False
  80.         m.use_textures[3] = False
  81.         m.use_textures[4] = False
  82.        
  83.         #Bake
  84.         for face in i.data.uv_textures.active.data:
  85.             face.image = displ
  86.         bpy.ops.object.bake_image()
  87.          
  88.         bpy.ops.object.modifier_add(type='DISPLACE')
  89.         i.modifiers['Displace'].direction = 'Z'
  90.         bpy.ops.texture.new()
  91.         t = bpy.data.textures['Texture']
  92.         t.name = 'Displ'+str(c)
  93.         bpy.data.textures['Displ'+str(c)].type = 'IMAGE'
  94.         bpy.data.textures['Displ'+str(c)].image = displ
  95.         i.modifiers['Displace'].texture = bpy.data.textures['Displ'+str(c)]
  96.         i.modifiers['Displace'].texture_coords = 'UV'
  97.         i.modifiers['Displace'].strength = 0.25
  98.         bpy.ops.object.modifier_apply(apply_as='DATA', modifier = 'Displace')
  99.        
  100.         #Reactivate the textures, now the mesh is displaced
  101.        
  102.         m.use_textures[0] = True
  103.         m.use_textures[3] = True
  104.         m.use_textures[4] = True
  105.         m.texture_slots['Stencil'+str(c)].invert = True
  106.         m.texture_slots['2Stencil'+str(c)].invert = True
  107.          
  108.         for face in i.data.uv_textures.active.data:
  109.             face.image = diffuse
  110.         bpy.data.scenes['Scene'].render.bake_type = 'TEXTURE'
  111.         bpy.ops.object.bake_image()
  112.         bpy.data.images['Diffuse'+str(c)].pack(as_png=True)
  113.         for face in i.data.uv_textures.active.data:
  114.             face.image = normal
  115.         bpy.data.scenes['Scene'].render.bake_type = 'NORMALS'
  116.         bpy.ops.object.bake_image()            
  117.         bpy.data.images['Normal'+str(c)].pack(as_png=True)
  118.            
  119.         #Textures are baked and packed, now (maybe useless) deactivate all the slots
  120.        
  121.         m.active_texture_index = 2
  122.         m.texture_slots[2].use = False
  123.         m.texture_slots.clear(2)
  124.         m.active_texture_index = 3
  125.         m.texture_slots[3].use = False
  126.         m.texture_slots.clear(3)
  127.         m.active_texture_index = 4
  128.         m.texture_slots[4].use = False
  129.         m.texture_slots.clear(4)
  130.  
  131.         #Next assign newly created textures.    
  132.  
  133.         m.active_texture_index = 0
  134.         t= bpy.data.textures.new(name='Diffuse'+str(c), type='IMAGE')
  135.         t.image = diffuse
  136.         m.texture_slots[0].texture = bpy.data.textures['Diffuse'+str(c)]
  137.         m.texture_slots[0].use_map_color_spec = True
  138.         m.texture_slots[0].texture_coords = 'UV'
  139.    
  140.         m.active_texture_index = 1
  141.         t = bpy.data.textures.new(name='Normal'+str(c),type='IMAGE')
  142.         t.image = normal
  143.         m.texture_slots[1].texture = bpy.data.textures['Normal'+str(c)]
  144.         m.texture_slots[1].texture_coords = 'UV'
  145.         m.texture_slots[1].use_map_normal = True
  146.         m.texture_slots[1].use_map_color_diffuse = False
  147.         t.use_normal_map = True
  148.         m.texture_slots[1].normal_factor = 0.4
  149.  
  150.     def lod():
  151.         d = 1
  152.         for i in bpy.context.selected_objects:
  153.             bpy.context.scene.objects.active = i  
  154.             bpy.ops.object.modifier_add(type='DECIMATE')
  155.             bpy.context.object.modifiers["Decimate"].ratio = 0.1
  156.             bpy.ops.object.modifier_apply(apply_as='DATA', modifier = 'Decimate')    
  157.             bpy.context.object.name = i.name + str(d)
  158.             bpy.ops.object.duplicate()
  159.  
  160.             bpy.ops.object.modifier_add(type='DECIMATE')
  161.             bpy.context.object.modifiers["Decimate"].ratio = 0.3
  162.             bpy.ops.object.modifier_apply(apply_as='DATA', modifier = 'Decimate')
  163.             bpy.context.object.name = i.name + str(d+1)
  164.             bpy.ops.object.duplicate()
  165.             bpy.ops.object.modifier_add(type='DECIMATE')
  166.             bpy.context.object.modifiers["Decimate"].ratio = 0.5
  167.             bpy.ops.object.modifier_apply(apply_as='DATA', modifier = 'Decimate')
  168.             bpy.context.object.name = i.name + str(d+2)
  169.             bpy.ops.object.duplicate()
  170.             bpy.ops.object.modifier_add(type='DECIMATE')
  171.             bpy.context.object.modifiers["Decimate"].ratio = 0.5
  172.             bpy.ops.object.modifier_apply(apply_as='DATA', modifier = 'Decimate')
  173.             bpy.context.object.name = i.name + str(d+3)
  174.            
  175.             bpy.context.scene.objects.active = i
  176.             bpy.ops.object.lod_add()
  177.             bpy.ops.object.lod_add()
  178.             bpy.ops.object.lod_add()
  179.             i.lod_levels[1].object = bpy.data.objects[i.name+str(d+1)]
  180.             i.lod_levels[2].object = bpy.data.objects[i.name+str(d+2)]
  181.             i.lod_levels[3].object = bpy.data.objects[i.name+str(d+3)]
  182.            
  183.             bpy.data.objects[i.name+str(d+1)].hide = True
  184.             bpy.data.objects[i.name+str(d+2)].hide = True
  185.             bpy.data.objects[i.name+str(d+3)].hide = True
  186.             d = d + 1
  187.            
  188. Gen = gen
  189. c = 1
  190. for i in bpy.context.selected_objects:
  191.     gen.textures(i, c)
  192.     c = c + 1
  193. #gen.lod()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement