Advertisement
gregwa

FCM 156 - Test2.py

Mar 28th, 2020
1,969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.40 KB | None | 0 0
  1. # -----------------------------------------
  2. # test2.py
  3. # -----------------------------------------
  4. # Modified by G.D. Walters from original source at https://medium.com/@behreajj/creative-coding-in-blender-a-primer-53e79ff71e
  5. # Written for Full Circle Magazine #156 April 2020
  6. # Code for use with Blender v2.8.2a
  7. # -----------------------------------------
  8. # NOTE: This program takes a few minutes to run, since it creates 1,000 cubes plus 1 light and 1 camera
  9. # -----------------------------------------
  10. import bpy
  11.  
  12. ###########################################
  13. # Same as test1.py
  14. # This function will remove all objects from the scene before the script runs, just to be safe
  15. def clear_scene():
  16.     objs = bpy.data.objects
  17.     for o in objs:
  18.         objs.remove(o, do_unlink=True)
  19.    
  20. clear_scene()
  21. ###########################################
  22. # Size of grid
  23. extents = 8.0
  24. # Number of cubes
  25. count = 10
  26. # Spacing between cubes
  27. padding = 0.005
  28. # Size of each cube
  29. sz = (extents / count) - padding
  30.  
  31. # To convert abstract grid position within loop to real-world coordinate.
  32. iprc = 0.0
  33. jprc = 0.0
  34. kprc = 0.0
  35. countf = 1.0 / (count - 1)
  36. # countf = 1.0 / (count + 1)
  37. diff = extents * 2
  38.  
  39. # Position of each cube.
  40. z = 0.0
  41. y = 0.0
  42. x = 0.0
  43.  
  44. # Center of grid.
  45. centerz = 0.0
  46. centery = 0.0
  47. centerx = 0.0
  48.  
  49. # Loop through grid z axis.
  50. for i in range(0, count, 1):
  51.     # Convert from index to percent in range 0 .. 1,
  52.     # then convert from prc to real world coordinate.
  53.     # Equivalent to map(val, lb0, ub0, lb1, ub1).
  54.     print('count = {0}'.format(i))
  55.     iprc = i * countf
  56.     z = -extents + iprc * diff
  57.  
  58.     # Loop through grid y axis.
  59.     for j in range(0, count, 1):
  60.         jprc = j * countf
  61.         y = -extents + jprc * diff
  62.  
  63.         # Loop through grid x axis.
  64.         for k in range(0, count):
  65.             kprc = k * countf
  66.             x = -extents + kprc * diff
  67.  
  68.             # Add grid world position to cube local position.
  69.             # bpy.ops.mesh.primitive_cube_add(location=(centerx + x, centery + y, centerz + z), radius=sz)
  70.             bpy.ops.mesh.primitive_cube_add(size = sz,location=(centerx + x, centery + y, centerz + z))
  71.             # Cache the current object being worked on.
  72.             current = bpy.context.object
  73.  
  74.             # Equivalent to Java's String.format. Placeholders
  75.             # between curly braces will be replaced by value of k, j, i.
  76.             current.name = 'Cube ({0}, {1}, {2})'.format(k, j, i)
  77.             current.data.name = 'Mesh ({0}, {1}, {2})'.format(k, j, i)
  78.  
  79.             # Create a material.
  80.             mat = bpy.data.materials.new(name='Material ({0}, {1}, {2})'.format(k, j, i))
  81.  
  82.             # Assign a diffuse color to the material. (R, G, B, Alpha)
  83.             mat.diffuse_color = (kprc, jprc, iprc, 1.0)
  84.             current.data.materials.append(mat)
  85.    
  86. ###########################################
  87. # same as in test1.py
  88. # =========================================
  89. # Add a sun lamp directly above cube on the grid.
  90. bpy.ops.object.light_add(type='SUN', radius=1.0, location=(0.0, 0.0, extents * 0.667))
  91.  
  92. # Add an isometric camera above the grid.
  93. # Rotate 45 degrees on the x-axis, 180 - 45 (135) degrees on the z-axis.
  94.  
  95. bpy.ops.object.camera_add(location=(extents * 1.414, extents * 1.414, extents * 2.121), rotation=(0.785398, 0.0, 2.35619))
  96. bpy.context.object.data.type = 'ORTHO'
  97. bpy.context.object.data.ortho_scale = extents * 7.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement