Animate paths

Animate pathsΒΆ

For objects for which a path has been constructed using the path-API, the magpylib library enables, users to animate the object movements, in addition to displaying them statically.

Note

This feature is only available for the plotly backend at the moment.

While the orientation is static while the animation is running, it can be dynamically set when the the animation is on pause.

import numpy as np
import plotly.graph_objects as go
import magpylib as magpy

# Define sources and sensor
dim = 2
col = magpy.Collection(
    [
        magpy.magnet.Cuboid(magnetization=(0, 1, 0), dimension=[dim] * 3),
        magpy.magnet.Cylinder(magnetization=(0, 1, 0), dimension=[dim] * 2),
        magpy.magnet.Sphere(magnetization=(0, 1, 0), diameter=dim),
    ]
)

ts = np.arange(-0.6, 0.6, 0.2)
sens = magpy.Sensor(position=(0, 0, 2), pixel=[(x, y, 0) for x in ts for y in ts])

# Create paths
pathlen = 40
for displ, src in zip([(0.1414, 0, 0.1), (-0.1, -0.1, 0.1), (-0.1, 0.1, 0.1)], col):
    src.move([np.array(displ)] * pathlen, increment=True)
    src.rotate_from_angax(
        angle=[10] * pathlen, axis="z", anchor=0, start=0, increment=True
    )
sens.move([(0, 0, 0.1)] * 10, increment=True)

# display animation
fig = go.Figure()
magpy.display(
    col,
    sens,
    canvas=fig,
    path="animate",
    zoom=0,
    animate_time=2,
    animate_fps=20,
    animate_slider=True,
    backend="plotly",
)
fig.update_layout(height=800)