Paste Code
Paste Blends
Paste Images
## by Matthaus Woolard
## add custom property "sim" = 1 to objects you wish to include in the simulation.
## add "mass" to set mass, default to 1
## add "vel.x" "vel.y" "vel.z" to set starting velocity default to (0,0,0)


import bpy
from mathutils import *
from math import *
import curve_simplify as ctools

def get_objects():
simobj = []
for obj in bpy.data.objects:
if obj.get("sim",0)==1:
simobj.append(obj)
return simobj





def run(sim):
def intergrate(vec,dx,time_step):
newvec = dx*(time_step) + vec
return newvec
def get_a(obj,simobj):
acc = Vector((0,0,0))
for o in simobj:
if o != obj:
acc += (o.get("mass",1)/(pow(((o.location-obj.location).magnitude),3))*(o.location-obj.location))
return(acc)
for obj in sim:
a = get_a(obj,sim)
v = Vector((obj.get("veld.x",0),obj.get("veld.y",0),obj.get("veld.z",0)))
v = intergrate(v,a,0.03)
obj["veld.x"] = v.x
obj["veld.y"] = v.y
obj["veld.z"] = v.z
obj.location = intergrate(obj.location,v,0.03)
obj.keyframe_insert(data_path="location",frame = bpy.data.scenes[0].frame_current +1 , index = 0)
obj.keyframe_insert(data_path="location",frame = bpy.data.scenes[0].frame_current +1 , index = 1)
obj.keyframe_insert(data_path="location",frame = bpy.data.scenes[0].frame_current +1 , index = 2)


sim = get_objects()

bpy.data.scenes[0].frame_current = 0
for obj in sim:
v = Vector((obj.get("vel.x",0),obj.get("vel.y",0),obj.get("vel.z",0)))
obj["veld.x"] = v.x
obj["veld.y"] = v.y
obj["veld.z"] = v.z
obj.keyframe_insert(data_path="location",frame = 0, index = 2)
obj.keyframe_insert(data_path="location",frame = 0, index = 1)
obj.keyframe_insert(data_path="location",frame = 0, index = 0)
for frame in range(1000):
run(sim)
bpy.data.scenes[0].frame_current +=1

def simp_curve(obj,error):
options = [
'distance', #0
'distance', #1
0, #2
5, #3
error, #4
5, #6
0] #7
fcurves = ctools.getFcurveData(obj)
ctools.fcurves_simplify(bpy.context, obj, options, fcurves)

for obj in sim:
simp_curve(obj,0.05)
  1. ## by Matthaus Woolard
  2. ## add custom property "sim" = 1 to objects you wish to include in the simulation.
  3. ## add "mass" to set mass, default to 1
  4. ## add "vel.x" "vel.y" "vel.z" to set starting velocity default to (0,0,0)
  5.  
  6.  
  7. import bpy
  8. from mathutils import *
  9. from math import *
  10. import curve_simplify as ctools
  11.  
  12. def get_objects():
  13.     simobj = []
  14.     for obj in bpy.data.objects:
  15.         if obj.get("sim",0)==1:
  16.             simobj.append(obj)
  17.     return simobj
  18.  
  19.  
  20.  
  21.  
  22.  
  23. def run(sim):
  24.     def intergrate(vec,dx,time_step):
  25.         newvec = dx*(time_step) + vec
  26.         return newvec
  27.     def get_a(obj,simobj):
  28.         acc = Vector((0,0,0))
  29.         for o in simobj:
  30.             if o != obj:
  31.                 acc += (o.get("mass",1)/(pow(((o.location-obj.location).magnitude),3))*(o.location-obj.location))
  32.         return(acc)
  33.     for obj in sim:
  34.         a = get_a(obj,sim)
  35.         v = Vector((obj.get("veld.x",0),obj.get("veld.y",0),obj.get("veld.z",0)))
  36.         v = intergrate(v,a,0.03)
  37.         obj["veld.x"] = v.x
  38.         obj["veld.y"] = v.y
  39.         obj["veld.z"] = v.z
  40.         obj.location = intergrate(obj.location,v,0.03)
  41.         obj.keyframe_insert(data_path="location",frame = bpy.data.scenes[0].frame_current +1 , index = 0)
  42.         obj.keyframe_insert(data_path="location",frame = bpy.data.scenes[0].frame_current +1 , index = 1)
  43.         obj.keyframe_insert(data_path="location",frame = bpy.data.scenes[0].frame_current +1 , index = 2)
  44.  
  45.  
  46. sim = get_objects()
  47.  
  48. bpy.data.scenes[0].frame_current = 0
  49. for obj in sim:
  50.     v = Vector((obj.get("vel.x",0),obj.get("vel.y",0),obj.get("vel.z",0)))
  51.     obj["veld.x"] = v.x
  52.     obj["veld.y"] = v.y
  53.     obj["veld.z"] = v.z
  54.     obj.keyframe_insert(data_path="location",frame = 0, index = 2)
  55.     obj.keyframe_insert(data_path="location",frame = 0, index = 1)
  56.     obj.keyframe_insert(data_path="location",frame = 0, index = 0)
  57. for frame in range(1000):
  58.     run(sim)
  59.     bpy.data.scenes[0].frame_current +=1
  60.  
  61. def simp_curve(obj,error):
  62.     options = [
  63.                 'distance',       #0
  64.                 'distance',       #1
  65.                 0,   #2
  66.                 5,   #3
  67.                 error,      #4
  68.                 5,  #6
  69.                 0]  #7
  70.     fcurves = ctools.getFcurveData(obj)
  71.     ctools.fcurves_simplify(bpy.context, obj, options, fcurves)
  72.  
  73. for obj in sim:    
  74.     simp_curve(obj,0.05)
go to heaven