Advertisement
Guest User

BlockyOcean.py

a guest
Aug 6th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.97 KB | None | 0 0
  1. import bpy
  2. import bmesh
  3. import math
  4. import mathutils
  5. from mathutils import Vector
  6.  
  7.  
  8. def getHeight(list, xPos, yPos):
  9.     return list[round(yPos * math.sqrt(len(list)) + xPos)].center.z
  10.  
  11. def getPoly(list, xPos, yPos):
  12.     return list[round(yPos * math.sqrt(len(list)) + xPos)]
  13.  
  14. class BlockyOcean:
  15.    
  16.     print("")
  17.    
  18.     for checkObj in bpy.data.objects:
  19.         if "Blocky Ocean" in checkObj.name:
  20.             bpy.data.objects.remove(checkObj, True)
  21.     for mesh in bpy.data.meshes:
  22.         if "OceanApplied" in mesh.name:
  23.             bpy.data.meshes.remove(mesh, True)
  24.     for mesh in bpy.data.meshes:
  25.         if "BlockyOcean" in mesh.name:
  26.             bpy.data.meshes.remove(mesh, True)
  27.    
  28.     for obj in bpy.data.objects:
  29.         if len(obj.modifiers) > 0:
  30.             for mod in obj.modifiers:
  31.                 if mod.type == "OCEAN":
  32.                     scene = bpy.context.scene
  33.                     time = scene.frame_start
  34.                    
  35.                     ocean = obj.to_mesh(bpy.context.scene, True, "PREVIEW")
  36.                     ocean.name = "OceanApplied"
  37.                     mesh = bpy.data.meshes.new("BlockyOcean")
  38.                     clone = bpy.data.objects.new("Blocky Ocean",mesh)
  39.                     scene.objects.link(clone)
  40.                     scene.objects.active = clone
  41.                     clone.select = True
  42.                    
  43.                     if len(clone.material_slots) == 0:
  44.                         clone.data.materials.append(obj.material_slots[0].material)
  45.                     else:
  46.                         clone.material_slots[0].material = obj.material_slots[0].material
  47.                    
  48.                     clone.shape_key_add("Basis",False)
  49.                    
  50.                     co = ocean.vertices[0].co
  51.                     bm = bmesh.new()
  52.                     row = round(math.sqrt(len(ocean.polygons)))
  53.                     i = 0
  54.                     x = 0
  55.                     mult = row / (-co.x * 2)
  56.                     row = -co.x * 2 - 1
  57.                     print("Preparing object...")
  58.                     while x < row:
  59.                         x = round(x)
  60.                         y = 0
  61.                         # print("Adding top faces: " + str((x + 1) * y) + "/" + str(round(-co.x * -co.x * 2)))
  62.                         while y < row:
  63.                             y = round(y)
  64.                            
  65.                             # Extras
  66.                             bm.verts.new((x + co.x, y + co.y, 2))
  67.                             bm.verts.new((x + co.x, y + co.y + 1, 2))
  68.                            
  69.                             bm.verts.new((x + co.x, y + co.y + 1, 4))
  70.                             bm.verts.new((x + co.x, y + co.y + 2, 4))
  71.                        
  72.                             bm.verts.new((x + co.x + 1, y + co.y + 2, 2))
  73.                             bm.verts.new((x + co.x + 2, y + co.y + 2, 2))
  74.                            
  75.                             bm.verts.new((x + co.x, y + co.y + 2, 1))
  76.                             bm.verts.new((x + co.x + 1, y + co.y + 2, 1))
  77.                             extras = 8
  78.                            
  79.                             # (0,0)
  80.                             bm.verts.new((x + co.x, y + co.y, 1))
  81.                             bm.verts.new((x + co.x + 1, y + co.y, 1))
  82.                             bm.verts.new((x + co.x + 1, y + co.y + 1, 1))
  83.                             bm.verts.new((x + co.x, y + co.y + 1, 1))
  84.                        
  85.                             # (1,0)
  86.                             bm.verts.new((x + co.x + 1, y + co.y, 2))
  87.                             bm.verts.new((x + co.x + 2, y + co.y, 2))
  88.                             bm.verts.new((x + co.x + 2, y + co.y + 1, 2))
  89.                             bm.verts.new((x + co.x + 1, y + co.y + 1, 2))
  90.                            
  91.                             # (0,1)
  92.                             bm.verts.new((x + co.x, y + co.y + 1, 3))
  93.                             bm.verts.new((x + co.x + 1, y + co.y + 1, 3))
  94.                             bm.verts.new((x + co.x + 1, y + co.y + 2, 3))
  95.                             bm.verts.new((x + co.x, y + co.y + 2, 3))
  96.                        
  97.                             # (1,1)
  98.                             bm.verts.new((x + co.x + 1, y + co.y + 1, 4))
  99.                             bm.verts.new((x + co.x + 2, y + co.y + 1, 4))
  100.                             bm.verts.new((x + co.x + 2, y + co.y + 2, 4))
  101.                             bm.verts.new((x + co.x + 1, y + co.y + 2, 4))
  102.                            
  103.                             bm.verts.ensure_lookup_table()
  104.                             off = i + extras
  105.                             # add top face
  106.                             bm.faces.new((bm.verts[off],bm.verts[off+1],bm.verts[off+2],bm.verts[off+3]))
  107.                             # connect to (1,0)
  108.                             bm.faces.new((bm.verts[off+2],bm.verts[off+1],bm.verts[off+4],bm.verts[off+7]))
  109.                             # connect to (0,1)
  110.                             bm.faces.new((bm.verts[off+3],bm.verts[off+2],bm.verts[off+9],bm.verts[off+8]))
  111.                            
  112.                             off += 4
  113.                             bm.faces.new((bm.verts[off],bm.verts[off+1],bm.verts[off+2],bm.verts[off+3]))
  114.                             bm.faces.new((bm.verts[off+3],bm.verts[off+2],bm.verts[off+9],bm.verts[off+8]))
  115.                            
  116.                             off += 4
  117.                             bm.faces.new((bm.verts[off],bm.verts[off+1],bm.verts[off+2],bm.verts[off+3]))
  118.                             bm.faces.new((bm.verts[off+2],bm.verts[off+1],bm.verts[off+4],bm.verts[off+7]))
  119.                            
  120.                             off += 4
  121.                             bm.faces.new((bm.verts[off],bm.verts[off+1],bm.verts[off+2],bm.verts[off+3]))
  122.                            
  123.                             # add extra faces
  124.                             off = i
  125.                             if x > 0:
  126.                                 bm.faces.new((bm.verts[off+3],bm.verts[off+2],bm.verts[off+extras+8],bm.verts[off+extras+11]))
  127.                                 bm.faces.new((bm.verts[off+1],bm.verts[off],bm.verts[off+extras],bm.verts[off+extras+3]))
  128.                             if y < row - 1:
  129.                                 bm.faces.new((bm.verts[off+7],bm.verts[off+6],bm.verts[off+extras+11],bm.verts[off+extras+10]))
  130.                                 bm.faces.new((bm.verts[off+5],bm.verts[off+4],bm.verts[off+extras+15],bm.verts[off+extras+14]))
  131.                             i += 4 * 4 + extras
  132.                             y += 2
  133.                         x += 2
  134.                     print("Removing doubles...")
  135.                     bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.01)
  136.                    
  137.                     bm.to_mesh(mesh)
  138.                     bm.free()
  139.                    
  140.                     mesh.validate()    
  141.                     mesh.update()
  142.                    
  143.                     obj.select = False
  144.                     obj.hide = True
  145.                     obj.hide_render = True
  146.                     clone.parent = obj
  147.                    
  148.                     for poly in mesh.polygons:
  149.                         poly.use_smooth = False
  150.                    
  151.                     while time <= scene.frame_end:
  152.                         timeString = ("0000" + str(time))[-4:]
  153.                         clone.shape_key_add("Frame " + timeString,False)
  154.                         shapeKey = clone.data.shape_keys.key_blocks["Frame " + timeString]
  155.                         print("-----")
  156.                         print("Preparing frame " + str(timeString))
  157.                         scene.frame_set(time)
  158.                        
  159.                         for m in bpy.data.meshes:
  160.                             if "OceanApplied" in m.name:
  161.                                 bpy.data.meshes.remove(m, True)
  162.                         ocean = obj.to_mesh(bpy.context.scene, True, "PREVIEW")
  163.                         ocean.name = "OceanApplied"
  164.                        
  165.                         row = -co.x * 2 - 1
  166.                         #print("mult=" + str(mult))
  167.                         #print("row=" + str(row))
  168.                        
  169.                         for poly in mesh.polygons:
  170.                             if poly.normal.z > 0:
  171.                                 polyPos = poly.center - Vector((0.5, 0.5, 0))
  172.                                 #print(polyPos)
  173.                                 for vert in poly.vertices:
  174.                                     pos = mesh.vertices[vert].co
  175.                                     shapeKey.data[vert].co = Vector((pos.x, pos.y, round(getHeight(ocean.polygons, round((polyPos.x + -co.x) * mult), round((polyPos.y + -co.x) * mult)))))
  176.                        
  177.                         keyName = "value"
  178.                         shapeKey.value = 0
  179.                         shapeKey.keyframe_insert(keyName, frame=time - 1)
  180.                         shapeKey.keyframe_insert(keyName, frame=time + 1)
  181.                         shapeKey.value = 1
  182.                         shapeKey.keyframe_insert(keyName, frame=time)
  183.                        
  184.                         time += 1
  185.                         #bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
  186.                         #bpy.context.scene.update()
  187.                         print("Finished frame " + timeString + " (" + str(round((time - scene.frame_start) / (scene.frame_end - scene.frame_start + 1) * 100)) + "%)")
  188.                    
  189.                     mesh.validate()    
  190.                     mesh.update()
  191.                
  192.                     #clone.modifiers.new("Array","ARRAY").count = repeatX
  193.                     #mod2 = clone.modifiers.new("Array","ARRAY")
  194.                     #mod2.count = repeatY
  195.                     #mod2.relative_offset_displace[0] = 0
  196.                     #mod2.relative_offset_displace[1] = 1
  197.                
  198.                     scene.frame_set(scene.frame_start)
  199.                     print("DONE!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement