Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # domino
- import bpy
- import math
- import time
- domino_num = 0
- def dom_duplicate():
- bpy.ops.object.select_all(action = 'DESELECT')
- dom.select = True
- bpy.context.scene.objects.active = dom
- bpy.ops.object.duplicate_move_linked()
- obj = bpy.context.object
- obj.rotation_euler.z = math.pi / 2
- obj.rotation_euler.y = math.pi / 2
- global domino_num
- domino_num += 1
- def cell_co(i):
- d = dom.dimensions
- return d.z / 2 + i * (d.z - d.y)
- # dirs = (0, 1, 2, 3)
- def create_block(i, j, lvl, dirs):
- d = dom.dimensions
- block_x = building_x0 + cell_co(j)
- block_y = building_y0 + cell_co(i)
- block_z = building_z0 + d.x * (lvl + 0.5)
- block_offset = (d.z - d.y) / 2
- for r in dirs:
- dom_duplicate()
- obj = bpy.context.object
- obj.location.x = block_x
- obj.location.y = block_y
- obj.location.z = block_z
- if r == 0:
- obj.location.x += block_offset
- elif r == 1:
- obj.location.y += block_offset
- obj.rotation_euler.x = math.pi / 2
- elif r == 2:
- obj.location.x -= block_offset
- elif r == 3:
- obj.location.y -= block_offset
- obj.rotation_euler.x = math.pi / 2
- def is_hole(i, j):
- if not hole:
- return False
- return (j >= hole_left and j < hole_right and
- i >= hole_bottom and i < hole_top)
- def create_level(lvl):
- lvl_type = lvl % 4
- if lvl_type < 2:
- for i in range(0, grid_h, 2):
- for j in range(0, grid_w, 2):
- if not is_hole(i, j):
- if lvl_type == 0:
- dirs = (0, 2)
- else:
- dirs = (1, 3)
- create_block(i, j, lvl, dirs)
- else:
- if lvl_type == 2:
- for i in range(0, grid_h, 2):
- create_block(i, 0, lvl, (2,))
- create_block(i, grid_w - 1, lvl, (0,))
- for j in range(1, grid_w - 1, 2):
- create_block(0, j, lvl, (3,))
- create_block(grid_h - 1, j, lvl, (1,))
- if hole:
- for i in range(hole_bottom + 1, hole_top - 1, 2):
- create_block(i, hole_left, lvl, (2,))
- create_block(i, hole_right - 1, lvl, (0,))
- for j in range(hole_left, hole_right, 2):
- create_block(hole_bottom, j, lvl, (3,))
- create_block(hole_top - 1, j, lvl, (1,))
- else:
- for i in range(1, grid_h - 1, 2):
- create_block(i, 0, lvl, (2,))
- create_block(i, grid_w - 1, lvl, (0,))
- for j in range(0, grid_w, 2):
- create_block(0, j, lvl, (3,))
- create_block(grid_h - 1, j, lvl, (1,))
- if hole:
- for i in range(hole_bottom, hole_top, 2):
- create_block(i, hole_left, lvl, (2,))
- create_block(i, hole_right - 1, lvl, (0,))
- for j in range(hole_left + 1, hole_right - 1, 2):
- create_block(hole_bottom, j, lvl, (3,))
- create_block(hole_top - 1, j, lvl, (1,))
- for i in range(1, grid_h - 1, 2):
- for j in range(1, grid_w - 1, 2):
- if not is_hole(i, j):
- if lvl_type == 2:
- dirs = (1, 3)
- else:
- dirs = (0, 2)
- create_block(i, j, lvl, dirs)
- def blocks_to_cells(n):
- return 2 * n - 1
- def building_size(m):
- d = dom.dimensions
- return m * (d.z - d.y) + d.y
- # nx, ny - размер строения
- # hx, hy - размер дырки
- def create_building(nx, ny, hx, hy, height):
- global dom
- dom = bpy.data.objects.get(domino_name)
- if dom == None:
- print("Can't find '{}' instance!".format(domino_name))
- return
- n_min = 2
- if nx < n_min: nx = n_min
- if ny < n_min: ny = n_min
- if nx % 2 != hx % 2: hx -= 1
- if ny % 2 != hy % 2: hy -= 1
- mnw = 4
- hx_max = nx - mnw if nx > mnw else 0
- hy_max = ny - mnw if ny > mnw else 0
- if hx > hx_max: hx = hx_max
- if hx < 0: hx = 0
- if hy > hy_max: hy = hy_max
- if hy < 0: hy = 0
- if height < 1: height = 1
- global grid_w
- global grid_h
- global hole
- global hole_w
- global hole_h
- grid_w = blocks_to_cells(nx)
- grid_h = blocks_to_cells(ny)
- hole = False
- if hx > 0 and hy > 0:
- hole = True
- hole_w = blocks_to_cells(hx) + 2
- hole_h = blocks_to_cells(hy) + 2
- global building_size_x
- global building_size_y
- global building_x0
- global building_y0
- global building_z0
- building_size_x = building_size(grid_w)
- building_size_y = building_size(grid_h)
- cursor = bpy.context.scene.cursor_location
- building_x0 = cursor.x - building_size_x / 2
- building_y0 = cursor.y - building_size_y / 2
- building_z0 = cursor.z
- if hole:
- global hole_left
- global hole_right
- global hole_top
- global hole_bottom
- hole_left = (grid_w - hole_w) // 2
- hole_right = hole_left + hole_w
- hole_bottom = (grid_h - hole_h) // 2
- hole_top = hole_bottom + hole_h
- for lvl in range(height):
- create_level(lvl)
- ################################################################
- domino_name = "Domino"
- t1 = time.time()
- create_building(6, 5, 2, 1, 4)
- t2 = time.time()
- dt = t2 - t1
- print("-- create_building --")
- print("{} dominos, {:.2f} sec".format(domino_num, dt))
Add Comment
Please, Sign In to add comment