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
#2
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/InteractiveComputerGr...lisHSPlasH
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...ystem.html
willm168 likes this post
Reply
#3
(Jul-12-2021, 09:29 PM)Larz60+ Wrote: 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/InteractiveComputerGr...lisHSPlasH
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...ystem.html

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'
Reply
#4
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.

  1. Created a test project:
    1. Start a terminal session.
    2. Create directory: mkdir TestCompyle
    3. change dir: cd TestCompyle
    4. create a virtual environment: python -m venv venv
    5. Activate virtual environment: . ./venv/bin/activate
    6. install pycompyle: pip install compyle
    7. Create 'src' directory: mkdir src
    8. add __init__.py to TestCompyle and src directory: touch __init__.py
      touch ./src/__init__.py
  2. add this script to src directory name it SimpleExample.py:
    from compyle.api import Elementwise, annotate, wrap, get_config
    import numpy as np
    
    @annotate
    def axpb(i, x, y, a, b):
        y[i] = a*sin(x[i]) + b
    
    x = np.linspace(0, 1, 10000)
    print(f"x unmodified: {x}")
    y = np.zeros_like(x)
    print(f"y unmodified: {y}")
    
    a, b = 2.0, 3.0
    print(f"a unmodified: {a}, b unmodified: {b}")
    
    backend = 'cython'
    get_config().use_openmp = True
    x, y = wrap(x, y, backend=backend)
    print(f"x after wrap: {x}, y after wrap: {y}")
    e = Elementwise(axpb, backend=backend)
    e(x, y, a, b)
    print(f"e: {e}")
  3. run script: python src/SimpleExample.py
  4. results are:
    Output:
    x unmodified: [0.00000000e+00 1.00010001e-04 2.00020002e-04 ... 9.99799980e-01 9.99899990e-01 1.00000000e+00] y unmodified: [0. 0. 0. ... 0. 0. 0.] a unmodified: 2.0, b unmodified: 3.0 x after wrap: [0.00000000e+00 1.00010001e-04 2.00020002e-04 ... 9.99799980e-01 9.99899990e-01 1.00000000e+00], y after wrap: [0. 0. 0. ... 0. 0. 0.] e: <compyle.parallel.Elementwise object at 0x7f6c9ef85eb0>

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
Reply


Forum Jump:

User Panel Messages

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