Skip to content

Commit 6b768ad

Browse files
committed
add animation looping option
1 parent fb118ce commit 6b768ad

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

ext/Render.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ function render(model, sol,
131131
timescale = 1.0,
132132
traces = nothing,
133133
display = false,
134+
loop = 1,
134135
kwargs...
135136
)
136137
scene, fig = default_scene(x,y,z; lookat,up,show_axis)
@@ -150,7 +151,9 @@ function render(model, sol,
150151
Makie.lines!(scene, points)
151152
end
152153
end
153-
154+
if loop > 1
155+
timevec = repeat(timevec, loop)
156+
end
154157
if display
155158
Base.display(fig)
156159
sleep(2)
@@ -160,7 +163,7 @@ function render(model, sol,
160163
Base.display(fig)
161164
end
162165
t[] = time/timescale
163-
sleep(1/framerate)
166+
sleep(max(0, 1/framerate))
164167
end
165168
end
166169
fn = fetch(fnt)
@@ -203,16 +206,16 @@ function render(model, sol, time::Real;
203206
fig, t
204207
end
205208

206-
function Multibody.loop_render(model, sol; timescale = 1.0, framerate = 30, kwargs...)
209+
function Multibody.loop_render(model, sol; timescale = 1.0, framerate = 30, max_loop = 5, kwargs...)
207210
fig, t = render(model, sol, sol.t[1]; kwargs...)
208211
sleeptime = 1/framerate
209212
timevec = range(sol.t[1], sol.t[end]*timescale, step=sleeptime)
210213
display(fig)
211214
@async begin
212-
for i = 1:5
215+
for i = 1:max_loop
213216
for ti in timevec
214217
execution_time = @elapsed t[] = ti
215-
sleep(sleeptime - execution_time)
218+
sleep(max(0, sleeptime - execution_time))
216219
end
217220
end
218221
end

src/Multibody.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export render, render!
1212

1313
"""
1414
scene, time = render(model, sol, t::Real; framerate = 30, traces = [])
15-
path = render(model, sol, timevec = range(sol.t[1], sol.t[end], step = 1 / framerate); framerate = 30, timescale=1, display=false)
15+
path = render(model, sol, timevec = range(sol.t[1], sol.t[end], step = 1 / framerate); framerate = 30, timescale=1, display=false, loop=1)
1616
1717
Create a 3D animation of a multibody system
1818
@@ -23,6 +23,7 @@ Create a 3D animation of a multibody system
2323
- `timevec`: If a vector of times is provided, an animation is created and the path to the file on disk is returned.
2424
- `framerate`: Number of frames per second.
2525
- `timescale`: Scaling of the time vector. This argument can be made to speed up animations (`timescale < 1`) or slow them down (`timescale > 1`). A value of `timescale = 2` will be 2x slower than real time.
26+
- `loop`: The animation will be looped this many times. Please note: looping the animation using this argument is only recommended when `display = true` for camera manipulation purposes. When the camera is not manipulated, looping the animation by other means is recommended to avoid an increase in the file size.
2627
- `filename` controls the name and the file type of the resulting animation
2728
- `traces`: An optional array of frames to show the trace of.
2829
@@ -40,7 +41,7 @@ See also [`loop_render`](@ref)
4041
function render end
4142

4243
"""
43-
loop_render(model, sol; framerate = 30, timescale = 1, kwargs...)
44+
loop_render(model, sol; framerate = 30, timescale = 1, max_loop = 5, kwargs...)
4445
4546
Similar to the method of [`render`](@ref) that produces an animation, but instead opens an interactive window where the time is automatically advanced in real time. This allows the user to manually manipulate the camera using the mouse is a live animation.
4647
"""

0 commit comments

Comments
 (0)