Guest User

Untitled

a guest
Oct 7th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.68 KB | None | 0 0
  1. # domino
  2.  
  3. import bpy
  4. import math
  5. import time
  6.  
  7. domino_num = 0
  8.  
  9. def dom_duplicate():
  10.     bpy.ops.object.select_all(action = 'DESELECT')
  11.     dom.select = True
  12.     bpy.context.scene.objects.active = dom
  13.     bpy.ops.object.duplicate_move_linked()
  14.     obj = bpy.context.object
  15.     obj.rotation_euler.z = math.pi / 2
  16.     obj.rotation_euler.y = math.pi / 2
  17.     global domino_num
  18.     domino_num += 1
  19.  
  20. def cell_co(i):
  21.     d = dom.dimensions
  22.     return d.z / 2 + i * (d.z - d.y)
  23.  
  24. # dirs = (0, 1, 2, 3)
  25. def create_block(i, j, lvl, dirs):
  26.     d = dom.dimensions
  27.     block_x = building_x0 + cell_co(j)
  28.     block_y = building_y0 + cell_co(i)
  29.     block_z = building_z0 + d.x * (lvl + 0.5)
  30.     block_offset = (d.z - d.y) / 2
  31.     for r in dirs:
  32.         dom_duplicate()
  33.         obj = bpy.context.object
  34.         obj.location.x = block_x
  35.         obj.location.y = block_y
  36.         obj.location.z = block_z
  37.         if r == 0:
  38.             obj.location.x += block_offset
  39.         elif r == 1:
  40.             obj.location.y += block_offset
  41.             obj.rotation_euler.x = math.pi / 2
  42.         elif r == 2:
  43.             obj.location.x -= block_offset
  44.         elif r == 3:
  45.             obj.location.y -= block_offset
  46.             obj.rotation_euler.x = math.pi / 2
  47.            
  48. def is_hole(i, j):
  49.     if not hole:
  50.         return False
  51.    
  52.     return (j >= hole_left and j < hole_right and
  53.             i >= hole_bottom and i < hole_top)
  54.  
  55. def create_level(lvl):
  56.     lvl_type = lvl % 4
  57.     if lvl_type < 2:
  58.         for i in range(0, grid_h, 2):
  59.             for j in range(0, grid_w, 2):
  60.                 if not is_hole(i, j):
  61.                     if lvl_type == 0:
  62.                         dirs = (0, 2)
  63.                     else:
  64.                         dirs = (1, 3)
  65.                     create_block(i, j, lvl, dirs)
  66.     else:
  67.         if lvl_type == 2:
  68.             for i in range(0, grid_h, 2):
  69.                 create_block(i, 0, lvl, (2,))
  70.                 create_block(i, grid_w - 1, lvl, (0,))
  71.             for j in range(1, grid_w - 1, 2):
  72.                 create_block(0, j, lvl, (3,))
  73.                 create_block(grid_h - 1, j, lvl, (1,))
  74.             if hole:
  75.                 for i in range(hole_bottom + 1, hole_top - 1, 2):
  76.                     create_block(i, hole_left, lvl, (2,))
  77.                     create_block(i, hole_right - 1, lvl, (0,))
  78.                 for j in range(hole_left, hole_right, 2):
  79.                     create_block(hole_bottom, j, lvl, (3,))
  80.                     create_block(hole_top - 1, j, lvl, (1,))
  81.                
  82.         else:
  83.             for i in range(1, grid_h - 1, 2):
  84.                 create_block(i, 0, lvl, (2,))
  85.                 create_block(i, grid_w - 1, lvl, (0,))
  86.             for j in range(0, grid_w, 2):
  87.                 create_block(0, j, lvl, (3,))
  88.                 create_block(grid_h - 1, j, lvl, (1,))
  89.             if hole:
  90.                 for i in range(hole_bottom, hole_top, 2):
  91.                     create_block(i, hole_left, lvl, (2,))
  92.                     create_block(i, hole_right - 1, lvl, (0,))
  93.                 for j in range(hole_left + 1, hole_right - 1, 2):
  94.                     create_block(hole_bottom, j, lvl, (3,))
  95.                     create_block(hole_top - 1, j, lvl, (1,))
  96.            
  97.         for i in range(1, grid_h - 1, 2):
  98.             for j in range(1, grid_w - 1, 2):
  99.                 if not is_hole(i, j):
  100.                     if lvl_type == 2:
  101.                         dirs = (1, 3)
  102.                     else:
  103.                         dirs = (0, 2)
  104.                     create_block(i, j, lvl, dirs)
  105.  
  106. def blocks_to_cells(n):
  107.     return 2 * n - 1
  108.  
  109. def building_size(m):
  110.     d = dom.dimensions
  111.     return m * (d.z - d.y) + d.y
  112.  
  113. # nx, ny - размер строения
  114. # hx, hy - размер дырки
  115. def create_building(nx, ny, hx, hy, height):
  116.    
  117.     global dom
  118.     dom = bpy.data.objects.get(domino_name)
  119.     if dom == None:
  120.         print("Can't find '{}' instance!".format(domino_name))
  121.         return
  122.    
  123.     n_min = 2
  124.     if nx < n_min: nx = n_min
  125.     if ny < n_min: ny = n_min
  126.    
  127.     if nx % 2 != hx % 2: hx -= 1
  128.     if ny % 2 != hy % 2: hy -= 1
  129.     mnw = 4
  130.     hx_max = nx - mnw if nx > mnw else 0
  131.     hy_max = ny - mnw if ny > mnw else 0
  132.     if hx > hx_max: hx = hx_max
  133.     if hx < 0: hx = 0
  134.     if hy > hy_max: hy = hy_max
  135.     if hy < 0: hy = 0
  136.    
  137.     if height < 1:  height = 1
  138.    
  139.     global grid_w
  140.     global grid_h
  141.     global hole
  142.     global hole_w
  143.     global hole_h
  144.    
  145.     grid_w = blocks_to_cells(nx)
  146.     grid_h = blocks_to_cells(ny)
  147.     hole = False
  148.     if hx > 0 and hy > 0:
  149.         hole = True
  150.         hole_w = blocks_to_cells(hx) + 2
  151.         hole_h = blocks_to_cells(hy) + 2
  152.    
  153.     global building_size_x
  154.     global building_size_y
  155.     global building_x0
  156.     global building_y0
  157.     global building_z0
  158.    
  159.     building_size_x = building_size(grid_w)
  160.     building_size_y = building_size(grid_h)
  161.     cursor = bpy.context.scene.cursor_location
  162.     building_x0 = cursor.x - building_size_x / 2
  163.     building_y0 = cursor.y - building_size_y / 2
  164.     building_z0 = cursor.z
  165.    
  166.     if hole:
  167.         global hole_left
  168.         global hole_right
  169.         global hole_top
  170.         global hole_bottom
  171.         hole_left = (grid_w - hole_w) // 2
  172.         hole_right = hole_left + hole_w
  173.         hole_bottom = (grid_h - hole_h) // 2
  174.         hole_top = hole_bottom + hole_h
  175.  
  176.     for lvl in range(height):
  177.         create_level(lvl)
  178.  
  179. ################################################################
  180.  
  181. domino_name = "Domino"
  182.  
  183. t1 = time.time()
  184. create_building(6, 5, 2, 1, 4)
  185. t2 = time.time()
  186. dt = t2 - t1
  187. print("-- create_building --")
  188. print("{} dominos, {:.2f} sec".format(domino_num, dt))
Add Comment
Please, Sign In to add comment