- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnimatingFallObjects.py
More file actions
Latest commit
155 lines (137 loc) · 4.57 KB
/
Copy pathAnimatingFallObjects.py
File metadata and controls
155 lines (137 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
importbpy
frombpyimportcontext
importmath
importrandom
## run SelectionVertices.py script prior to running this in console
## all modifiers (e.g., deform, smooth or anything else) must be applied to the mesh computed
## before using script. Height data read from mesh otherwise is done so on pre modified mesh.
##
defgravity(z,vi,t):
## t is time since tinitial
return.5*-9.8*t*t+vi*t+z
defsolvet(z, vi):
a=-9.8*.5
b=vi
c=z
if (b*b-4.0*a*c) <0:
t1=None
t2=None
else:
t1=b+math.sqrt((b*b-4.0*a*c))
t2=b-math.sqrt((b*b-4.0*a*c))
ift1>0:
returnt1
ift2>0:
returnt2
returnNone
defsolvet2(vi,vf):
return (vf-vi)/(-9.8)
defsolveh(vi,t):
return.5*9.8*t*t-vi*t
defsolvehf(vi,t):
return-.5*9.8*t*t+vi*t
defsolvevf(vi,t):
return-9.8*t+vi
numFallingObjs=50
fobj_list= []
vi=0.0
tf=7.0## in frames 30 sec per frame is the standard so 1 frame equals 1/30.0 seconds
tfs=7.0/30.0
dampingfactor=.07
bounces=1
## read vertex data on terrain
vdat= {}
objname="Land"
objname2="Dk2"
frameStart=87
obj=bpy.data.objects[objname]
## populate object names
foriinrange(0,numFallingObjs):
fobj_list.append("D"+str(i))
forvinobj.data.vertices:
x,y,z=obj.matrix_world*v.co
if (x,y) invdat:
vdat[(x,y)] =max(vdat[(x,y)],z)
else:
vdat[(x,y)] =z
selvertices2=selvertices[0:len(selvertices)]
print("length of selection vertices: "+str(len(selvertices)))
framedat= []
coorddat= []
bouncedat= [] ## set as (initial_height,final_height,timetofinalheightinframes) tuple
##bounce2dat = []
foriinrange(0,numFallingObjs):
vpick=random.randint(0,len(selvertices2))
x,y,z=selvertices2[vpick]
tf1=float(tf) +float(random.randint(0,5))
tfs1=tf1/30.0
h=solveh(vi,tfs1)
vf=solvevf(vi,tfs1)
t2=solvet2(-vf*dampingfactor,0.0)
print("t2: "+str(t2))
print("vf: "+str(-vf))
hf=solvehf(-vf*dampingfactor,t2) ## bounce
print("Solve Height: "+str(h))
print("original height: "+str(vdat[(x,y)]))
print("time: "+str(tfs1))
print("bounce final height: "+str(hf))
newz=vdat[(x,y)]+h
framedat.append(tf1)
coorddat.append((x,y,newz))
bouncedat.append((vdat[(x,y)],hf+vdat[(x,y)],int(t2*30.0)))
delselvertices2[vpick]
Scene_Name=bpy.context.scene.name
bpy.ops.object.mode_set(mode='OBJECT')
bpy.data.objects['Land'].select=False
bpy.context.scene.update()
fori, coinenumerate(coorddat):
bpy.data.objects[objname2].select=True##obj
bpy.context.scene.update()
##bpy.ops.object.duplicate(linked=False)
bpy.ops.object.duplicate()
bpy.ops.object.make_single_user(type='SELECTED_OBJECTS',
object=True,animation=True)
bpy.data.objects[objname2].select=False
bpy.context.scene.update()
## bpy.ops.object.make_single_user(type='SELECTED_OBJECTS',
## object=True, obdata=True,animation=True)
bpy.context.scene.update()
objname=objname2+"."+str(i+1).zfill(3)
bpy.data.scenes[Scene_Name].frame_set(0)
obj=bpy.data.objects[objname]
obj.location=co[0:len(co)]
print(obj.location)
## print(bpy.ops.anim.keying_set_add())
## print(bpy.ops.anim.keying_set_active_set(i))
## print(bpy.ops.anim.keying_set_path_add())
print(bpy.context.scene.update())
print(obj.keyframe_insert(data_path="location"))
## if there are existing key frames use this
## for num in range(0, 30):
## bpy.context.active_object.keyframe_delete('location', frame=num)
forframeinrange(frameStart, frameStart+int(framedat[i])+1):
tstep=float(frame-frameStart)/30.0
h=gravity(co[2],vi,tstep)
bpy.data.scenes[Scene_Name].frame_set(frame)
print(h)
##th = co[2]-h
##print(bpy.ops.transform.translate(value=(0, 0, -th)))
obj.location= (obj.location.x, obj.location.y, h)
bpy.context.scene.update()
# create keyframe
print(obj.keyframe_insert(data_path="location"))
bpy.context.scene.update()
## apply bounceposv
hi,hf,tf=bouncedat[i]
bpy.data.scenes[Scene_Name].frame_set(frame+tf)
obj.location= (obj.location.x, obj.location.y, hf)
# create keyframe
print(obj.keyframe_insert(data_path="location"))
##apply bouncenegv
bpy.data.scenes[Scene_Name].frame_set(frame+2*tf)
obj.location= (obj.location.x, obj.location.y, hi)
# create keyframe
print(obj.keyframe_insert(data_path="location"))
obj.select=False
bpy.context.scene.update()
bpy.ops.object.select_all(action='DESELECT')