## 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)
## 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)
|