Advertisement
rosaliarenda

Scena con Sfere in Blender

Mar 4th, 2023 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | Gaming | 0 0
  1. import random
  2. import bpy
  3. from math import pi, sqrt
  4.  
  5. radius_size = 1.0
  6.  
  7. # Imposta il numero di sfere
  8. num_spheres = 200
  9.  
  10. # Lista delle posizioni delle sfere
  11. positions = []
  12.  
  13. # Crea le sfere in modo casuale
  14. for i in range(num_spheres):
  15.     # Genera le coordinate casuali per la posizione
  16.     x = random.uniform(-10, 10)
  17.     y = random.uniform(-10, 10)
  18.     z = random.uniform(-10, 10)
  19.  
  20.     # Verifica se la nuova sfera si sovrappone con quelle precedenti
  21.     overlap = True
  22.     while overlap:
  23.         overlap = False
  24.         for pos in positions:
  25.             distance = sqrt((pos[0] - x)**2 + (pos[1] - y)**2 + (pos[2] - z)**2)
  26.             if distance < (radius_size * 2):
  27.                 # Le sfere si sovrappongono, sposta la nuova sfera in una nuova posizione
  28.                 x = random.uniform(-10, 10)
  29.                 y = random.uniform(-10, 10)
  30.                 z = random.uniform(-10, 10)
  31.                 overlap = True
  32.                 break
  33.  
  34.     # Aggiungi la posizione alla lista delle posizioni delle sfere
  35.     positions.append((x, y, z))
  36.  
  37.     # Genera la dimensione casuale
  38.     scale = random.uniform(0.2, 1.5)
  39.  
  40.     # Genera il colore casuale
  41.     r = random.uniform(0.0, 1.0)
  42.     g = random.uniform(0.0, 1.0)
  43.     b = random.uniform(0.0, 1.0)
  44.     color = (r, g, b)
  45.  
  46.      # Crea una nuova sfera
  47.     obj =bpy.ops.mesh.primitive_uv_sphere_add(
  48.         radius=radius_size,
  49.         enter_editmode=False,
  50.         align='WORLD',
  51.         location=(x,y,z),
  52.         scale=(scale,scale,scale)
  53.  )
  54.     # Applica lo smooth alla sfera
  55.     bpy.ops.object.shade_smooth()
  56.    
  57.     # Crea un materiale
  58.     mat = bpy.data.materials.new("Material")
  59.     # Crea un colore Diffuse casuale :
  60.     mat.diffuse_color = (random.random(), random.random(), random.random(), 1)  
  61.      
  62.     # Assegna il materiale all'oggetto
  63.     bpy.context.object.data.materials.append(mat)
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement