Python Forum
Random Particle Simulation in Blender
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random Particle Simulation in Blender
#1
I am new to Blender/Python and am trying to create a random particle simulation inside of Blender. I used my code from my python script that worked and am trying to implement it to blender to assign the objects. I have been searching the internet and don't exactly know what to search for and have been making little headway. I was hoping someone could help me to figure this out. I am hoping to learn this in order to eventually implement a smooth particle hydrodynamic simulation of a bubble/froth zinc flotation.

import bpy
import bmesh
import time
import mathutils
import numpy as np

ob = bpy.data.objects("Sphere")
frame_number = 0
sphere = 1

#get initial coordinates for sphere
def get_initial_coordinates():
  x_coord = [np.random.random()*box_width for i in range(sphere)]
  y_coord = [np.random.random()*box_width for i in range(sphere)]
  z_coord = [np.random.random()*box_width for i in range(sphere)]
  
  return x_coord, y_coord

def get_initial_velocities():
  x_vel = [2*(np.random.random()-0.5)*box_width for i in range(sphere)]
  y_vel = [2*(np.random.random()-0.5)*box_width for i in range(sphere)]
  z_vel = [2*(np.random.random()-0.5)*box_width for i in range(sphere)]
  
  return x_vel, y_vel

def take_step(x_coord, y_coord, x_vel, y_vel):
  for i in range(n_particles):
    x_coord[i] += x_vel[i]*dt
    y_coord[i] += y_vel[i]*dt
    
    if abs(x_coord[i]) > box_width:
      x_vel[i] = -x_vel[i]
      x_coord[i] += x_vel[i]*dt
    
    if abs(y_coord[i]) > box_width:
      y_vel[i] = -y_vel[i]
      y_coord[i] += y_vel[i]*dt
    
  return x_coord, y_coord, x_vel, y_vel

sphere = 1
box_width = 10
n_steps = 5000
dt = 0.001
global trajectory 

x_coord, y_coord = get_initial_coordinates()

x_vel, y_vel = get_initial_velocities()

for i in range(n_steps):
    x_coord, y_coord, z_coord, x_vel, y_vel, z_vel = take_step(x_coord, y_coord, z_coord, x_vel, y_vel, z_vel)
   
   if i%10 == 0:
       add_frame(x_coord, y_coord,i)
       
 bpy.context.scene.frame_set(frame_number)
    ob.location = (x_coord,y_coord,z_coord)
    ob.keyframe_insert(data_path="location",index=-1)
    frame_number +=5
Yoriz write Jul-12-2021, 07:52 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
Random Particle Simulation in Blender - by willm168 - Jul-12-2021, 05:13 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020