Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. import bpy
  2. from random import randint, random
  3. from math import sqrt, pi
  4.  
  5. # Function to calculate the distance between two points
  6. def distance(a, b) :
  7. '''
  8. list , list --> float
  9. a = list with three element
  10. b = list with three element
  11.  
  12. return the distance between two points in float
  13. '''
  14. return sqrt((a[0]-b[0])**2+(a[1]-b[1])**2+(a[2]-b[2])**2)
  15.  
  16. # How many cylinder you want to add
  17. count = 50
  18.  
  19. # Cylinder properties
  20. radius = 0.15
  21. depth = 0.5
  22.  
  23. # The cylinder will be created between -domain <--> domain
  24. domain = 1
  25.  
  26. # Max time to try finding a new location before break the loop
  27. maxTry = 500
  28.  
  29. # Variable to count how many try has been done
  30. Try = 0
  31.  
  32. # list with three element to generate locations
  33. location = [0, 0, 0]
  34.  
  35. # Variable to hold the distant between balls
  36. dist = 0
  37.  
  38. # True == Won't collide , False == Will collide
  39. State = True
  40.  
  41. # This tuple will hold centers of created cylinders
  42. locList = ()
  43.  
  44. while count > 0 :
  45.  
  46. # Calculate x, y, z position
  47. location[0] = randint(-domain*10, domain*10)/10.0
  48. location[1] = randint(-domain*10, domain*10)/10.0
  49. location[2] = randint(-domain*10, domain*10)/10.0
  50.  
  51.  
  52. # Start check if it will collide with other cylinders
  53. for x in locList :
  54. # Calculate the distant
  55. dist = distance(x, location)
  56.  
  57. # If it's too close make State = False
  58. if dist < size*2 + 0.1 :
  59. State = False
  60. break
  61. # If it's in a good position State = True
  62. else :
  63. State = True
  64.  
  65. # The distant is too close , recalculate the location
  66. if State == False :
  67. Try += 1
  68. continue
  69.  
  70. # After Try reach maxTry break the loop
  71. if Try > maxTry :
  72. break
  73.  
  74. # Successfully found a New location
  75. # Add this point
  76. locList += (location[:],)
  77.  
  78. # Smooth the faces
  79. bpy.ops.object.shade_smooth()
  80.  
  81. #Create a new cylinder at the same location, and resize it to an cylinder
  82. bpy.ops.mesh.primitive_cylinder_add(radius = radius, depth = depth, location = location)
  83. #bpy.ops.transform.resize(value=cylinderSize)
  84.  
  85. #Give the cylinder a random orientation
  86. bpy.ops.transform.rotate(value=(2*pi*random()-1), axis=((2*pi*random()-1), (2*pi*random()-1), (2*pi*random()-1)))
  87.  
  88. #Smooth the faces
  89. bpy.ops.object.shade_smooth()
  90.  
  91. # reset Try
  92. Try = 0
  93.  
  94. # Decrease the counter
  95. count -= 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement