Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bpy
- import bmesh
- import math
- import mathutils
- from mathutils import Vector
- def getHeight(list, xPos, yPos):
- return list[round(yPos * math.sqrt(len(list)) + xPos)].center.z
- def getPoly(list, xPos, yPos):
- return list[round(yPos * math.sqrt(len(list)) + xPos)]
- class BlockyOcean:
- print("")
- for checkObj in bpy.data.objects:
- if "Blocky Ocean" in checkObj.name:
- bpy.data.objects.remove(checkObj, True)
- for mesh in bpy.data.meshes:
- if "OceanApplied" in mesh.name:
- bpy.data.meshes.remove(mesh, True)
- for mesh in bpy.data.meshes:
- if "BlockyOcean" in mesh.name:
- bpy.data.meshes.remove(mesh, True)
- for obj in bpy.data.objects:
- if len(obj.modifiers) > 0:
- for mod in obj.modifiers:
- if mod.type == "OCEAN":
- scene = bpy.context.scene
- time = scene.frame_start
- ocean = obj.to_mesh(bpy.context.scene, True, "PREVIEW")
- ocean.name = "OceanApplied"
- mesh = bpy.data.meshes.new("BlockyOcean")
- clone = bpy.data.objects.new("Blocky Ocean",mesh)
- scene.objects.link(clone)
- scene.objects.active = clone
- clone.select = True
- if len(clone.material_slots) == 0:
- clone.data.materials.append(obj.material_slots[0].material)
- else:
- clone.material_slots[0].material = obj.material_slots[0].material
- clone.shape_key_add("Basis",False)
- co = ocean.vertices[0].co
- bm = bmesh.new()
- row = round(math.sqrt(len(ocean.polygons)))
- i = 0
- x = 0
- mult = row / (-co.x * 2)
- row = -co.x * 2 - 1
- print("Preparing object...")
- while x < row:
- x = round(x)
- y = 0
- # print("Adding top faces: " + str((x + 1) * y) + "/" + str(round(-co.x * -co.x * 2)))
- while y < row:
- y = round(y)
- # Extras
- bm.verts.new((x + co.x, y + co.y, 2))
- bm.verts.new((x + co.x, y + co.y + 1, 2))
- bm.verts.new((x + co.x, y + co.y + 1, 4))
- bm.verts.new((x + co.x, y + co.y + 2, 4))
- bm.verts.new((x + co.x + 1, y + co.y + 2, 2))
- bm.verts.new((x + co.x + 2, y + co.y + 2, 2))
- bm.verts.new((x + co.x, y + co.y + 2, 1))
- bm.verts.new((x + co.x + 1, y + co.y + 2, 1))
- extras = 8
- # (0,0)
- bm.verts.new((x + co.x, y + co.y, 1))
- bm.verts.new((x + co.x + 1, y + co.y, 1))
- bm.verts.new((x + co.x + 1, y + co.y + 1, 1))
- bm.verts.new((x + co.x, y + co.y + 1, 1))
- # (1,0)
- bm.verts.new((x + co.x + 1, y + co.y, 2))
- bm.verts.new((x + co.x + 2, y + co.y, 2))
- bm.verts.new((x + co.x + 2, y + co.y + 1, 2))
- bm.verts.new((x + co.x + 1, y + co.y + 1, 2))
- # (0,1)
- bm.verts.new((x + co.x, y + co.y + 1, 3))
- bm.verts.new((x + co.x + 1, y + co.y + 1, 3))
- bm.verts.new((x + co.x + 1, y + co.y + 2, 3))
- bm.verts.new((x + co.x, y + co.y + 2, 3))
- # (1,1)
- bm.verts.new((x + co.x + 1, y + co.y + 1, 4))
- bm.verts.new((x + co.x + 2, y + co.y + 1, 4))
- bm.verts.new((x + co.x + 2, y + co.y + 2, 4))
- bm.verts.new((x + co.x + 1, y + co.y + 2, 4))
- bm.verts.ensure_lookup_table()
- off = i + extras
- # add top face
- bm.faces.new((bm.verts[off],bm.verts[off+1],bm.verts[off+2],bm.verts[off+3]))
- # connect to (1,0)
- bm.faces.new((bm.verts[off+2],bm.verts[off+1],bm.verts[off+4],bm.verts[off+7]))
- # connect to (0,1)
- bm.faces.new((bm.verts[off+3],bm.verts[off+2],bm.verts[off+9],bm.verts[off+8]))
- off += 4
- bm.faces.new((bm.verts[off],bm.verts[off+1],bm.verts[off+2],bm.verts[off+3]))
- bm.faces.new((bm.verts[off+3],bm.verts[off+2],bm.verts[off+9],bm.verts[off+8]))
- off += 4
- bm.faces.new((bm.verts[off],bm.verts[off+1],bm.verts[off+2],bm.verts[off+3]))
- bm.faces.new((bm.verts[off+2],bm.verts[off+1],bm.verts[off+4],bm.verts[off+7]))
- off += 4
- bm.faces.new((bm.verts[off],bm.verts[off+1],bm.verts[off+2],bm.verts[off+3]))
- # add extra faces
- off = i
- if x > 0:
- bm.faces.new((bm.verts[off+3],bm.verts[off+2],bm.verts[off+extras+8],bm.verts[off+extras+11]))
- bm.faces.new((bm.verts[off+1],bm.verts[off],bm.verts[off+extras],bm.verts[off+extras+3]))
- if y < row - 1:
- bm.faces.new((bm.verts[off+7],bm.verts[off+6],bm.verts[off+extras+11],bm.verts[off+extras+10]))
- bm.faces.new((bm.verts[off+5],bm.verts[off+4],bm.verts[off+extras+15],bm.verts[off+extras+14]))
- i += 4 * 4 + extras
- y += 2
- x += 2
- print("Removing doubles...")
- bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.01)
- bm.to_mesh(mesh)
- bm.free()
- mesh.validate()
- mesh.update()
- obj.select = False
- obj.hide = True
- obj.hide_render = True
- clone.parent = obj
- for poly in mesh.polygons:
- poly.use_smooth = False
- while time <= scene.frame_end:
- timeString = ("0000" + str(time))[-4:]
- clone.shape_key_add("Frame " + timeString,False)
- shapeKey = clone.data.shape_keys.key_blocks["Frame " + timeString]
- print("-----")
- print("Preparing frame " + str(timeString))
- scene.frame_set(time)
- for m in bpy.data.meshes:
- if "OceanApplied" in m.name:
- bpy.data.meshes.remove(m, True)
- ocean = obj.to_mesh(bpy.context.scene, True, "PREVIEW")
- ocean.name = "OceanApplied"
- row = -co.x * 2 - 1
- #print("mult=" + str(mult))
- #print("row=" + str(row))
- for poly in mesh.polygons:
- if poly.normal.z > 0:
- polyPos = poly.center - Vector((0.5, 0.5, 0))
- #print(polyPos)
- for vert in poly.vertices:
- pos = mesh.vertices[vert].co
- 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)))))
- keyName = "value"
- shapeKey.value = 0
- shapeKey.keyframe_insert(keyName, frame=time - 1)
- shapeKey.keyframe_insert(keyName, frame=time + 1)
- shapeKey.value = 1
- shapeKey.keyframe_insert(keyName, frame=time)
- time += 1
- #bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
- #bpy.context.scene.update()
- print("Finished frame " + timeString + " (" + str(round((time - scene.frame_start) / (scene.frame_end - scene.frame_start + 1) * 100)) + "%)")
- mesh.validate()
- mesh.update()
- #clone.modifiers.new("Array","ARRAY").count = repeatX
- #mod2 = clone.modifiers.new("Array","ARRAY")
- #mod2.count = repeatY
- #mod2.relative_offset_displace[0] = 0
- #mod2.relative_offset_displace[1] = 1
- scene.frame_set(scene.frame_start)
- print("DONE!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement