Jul-12-2021, 05:13 PM
(This post was last modified: Jul-12-2021, 07:52 PM by Yoriz.
Edit Reason: Added code tags
)
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