Random Particle Simulation in Blender - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Random Particle Simulation in Blender (/thread-34263.html) |
Random Particle Simulation in Blender - willm168 - Jul-12-2021 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 RE: Random Particle Simulation in Blender - Larz60+ - Jul-12-2021 There are several python packages that deal with the simulation portion: PySPH - This is a framework for smooth hydrodynamic particle simulation, see: https://pypi.org/project/PySPH/ Also Spheral++ which is a combination of C++ and python, it's written in c++, but has a Scriptable user interface for python. see: https://github.com/LLNL/spheral and SPlisHSPlasH, which is a fluid simulator, see: https://github.com/InteractiveComputerGraphics/SPlisHSPlasH and (python interface) https://pypi.org/search/?q=pysplishsplash Hope there's something useful here, no blender that I can see though. I guess you'd be familiar with: https://docs.blender.org/api/current/bpy.types.ParticleSystem.html RE: Random Particle Simulation in Blender - willm168 - Jul-13-2021 (Jul-12-2021, 09:29 PM)Larz60+ Wrote: There are several python packages that deal with the simulation portion: Thank you, very much. I will let you know how those pieces of information are. I have done an extensive amount of research in PySPH, can't seem to get the packages to work though. keep getting 'cannot import name profile2csv from compyle.profile' RE: Random Particle Simulation in Blender - Larz60+ - Jul-13-2021 The problem is that profile2csv was written 10 years ago, only for python 2, so needs a python 3 version however if compyle is installed for python 3, there is a fix for that. Make sure you use python 3 for all (i used 3.9.6) Here's how I tested (see notes at end for fix) Note all of this done on Linux-mint 20 using python 3.9.6 All steps are included for clarity - please excuse.
I didn't expand e, but didn't need to, the exercise is to make sure all works So, my guess is that after you install PySPH, reinstall complye and then try your code. Hope this works for you |