Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import bpy, bmesh
  2. import numpy as np
  3. from mathutils import Vector
  4.  
  5. def generate_quad( original, quads, level, max_level ):
  6. ''' Recursive function generating 4 tetrahedra off of the the input tetrahedron '''
  7.  
  8. # The 1st new tetrahedron is simply half the scale of the original starting from the original tet's bottom left corner
  9. origin = original.min(axis=0) / 2
  10. half = original / 2
  11. half += origin
  12.  
  13. # Calculate the dimensions of the new tetrahedra
  14. newdims = Vector( half.max(axis=0) - half.min(axis=0) )
  15.  
  16. # The 2nd moves by the new tet's x dimension to the right
  17. right = half.copy()
  18. right[:,0] += newdims.x
  19.  
  20. # The 3rd moves by half the x dim and all the y dim
  21. forward = half.copy()
  22. forward[:,0] += newdims.x / 2
  23. forward[:,1] += newdims.y
  24.  
  25. # The 4th moves by half the x, a third of the y and all the z
  26. up = half.copy()
  27. up[:,0] += newdims.x / 2
  28. up[:,1] += newdims.y / 3
  29. up[:,2] += newdims.z
  30.  
  31. quad = [ half, right, forward, up ]
  32. quads[ level ].extend( quad )
  33.  
  34. if level < max_level:
  35. for q in quad:
  36. quads = generate_quad( q, quads, level + 1, max_level )
  37.  
  38. return quads
  39.  
  40. ## Main Code
  41. C = bpy.context
  42. D = bpy.data
  43. S = C.scene
  44.  
  45. n = 2 # Number of steps of the fractal
  46. scale = 10 # Scale of the original tetrahedron
  47.  
  48. # Generate regular tetrahedron at the desired scale
  49. orig = np.array([
  50. [0, 0, 0],
  51. [1, 0, 0],
  52. [0.5, 3**0.5/2, 0],
  53. [0.5, 1/3*3**0.5/2, ((3**0.5/2)**2 - (1/3*3**0.5/2)**2)**0.5]
  54. ]) * scale
  55.  
  56. regular_tet_faces = np.array([[3, 0, 1], [1, 0, 2], [0, 3, 2], [3, 1, 2]])
  57.  
  58. # Initialize data structure for all tetrahedra in all steps
  59. quads = { i : [] for i in range( n+1 ) }
  60.  
  61. # Generate tetrix via recursive function
  62. quads = generate_quad( orig, quads, 0, n )
  63.  
  64. # Generate mesh data and initialize vertex and face arrays
  65. m = D.meshes.new('tetrix')
  66. verts = []
  67. faces = regular_tet_faces.tolist()
  68.  
  69. # Generate mesh data
  70. for q in quads[n]:
  71. # Find last vert index and add new verts
  72. last_vert_index = max([ len( verts ) - 1, 0 ])
  73. verts.extend( q.tolist() )
  74.  
  75. # Use base face arrangement of original regular tetrahedron and offset indices
  76. faces_indices = regular_tet_faces + last_vert_index + 1
  77. faces.extend( faces_indices.tolist() )
  78.  
  79. # Generate mesh object and add to scene
  80. m.from_pydata( verts, [], faces )
  81. print({ k : len( quads[k] ) for k in quads })
  82. o = D.objects.new('base',m)
  83. S.collection.objects.link( o )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement