10.5. Styling and exporting#

Every element of the Player can be stylized. Let’s load the tennis serve data again, then we will use the properties of the Player to style its elements.

Hide code cell content
import kineticstoolkit.lab as ktk

# Download and read markers from a sample C3D file
filename = ktk.doc.download("kinematics_tennis_serve_2players.c3d")
markers = ktk.read_c3d(filename)["Points"]
interconnections = dict()  # Will contain all segment definitions
interconnections["LLowerLimb"] = {
    "Color": (0, 0.5, 1),  # In RGB format (here, greenish blue)
    "Links": [  # List of lines that span lists of markers
        ["*LTOE", "*LHEE", "*LANK", "*LTOE"],
        ["*LANK", "*LKNE", "*LASI"],
        ["*LKNE", "*LPSI"],
    ],
}
interconnections["RLowerLimb"] = {
    "Color": (0, 0.5, 1),
    "Links": [
        ["*RTOE", "*RHEE", "*RANK", "*RTOE"],
        ["*RANK", "*RKNE", "*RASI"],
        ["*RKNE", "*RPSI"],
    ],
}
interconnections["LUpperLimb"] = {
    "Color": (0, 0.5, 1),
    "Links": [
        ["*LSHO", "*LELB", "*LWRA", "*LFIN"],
        ["*LELB", "*LWRB", "*LFIN"],
        ["*LWRA", "*LWRB"],
    ],
}
interconnections["RUpperLimb"] = {
    "Color": (1, 0.5, 0),
    "Links": [
        ["*RSHO", "*RELB", "*RWRA", "*RFIN"],
        ["*RELB", "*RWRB", "*RFIN"],
        ["*RWRA", "*RWRB"],
    ],
}
interconnections["Head"] = {
    "Color": (1, 0.5, 1),
    "Links": [
        ["*C7", "*LFHD", "*RFHD", "*C7"],
        ["*C7", "*LBHD", "*RBHD", "*C7"],
        ["*LBHD", "*LFHD"],
        ["*RBHD", "*RFHD"],
    ],
}
interconnections["TrunkPelvis"] = {
    "Color": (0.5, 1, 0.5),
    "Links": [
        ["*LASI", "*STRN", "*RASI"],
        ["*STRN", "*CLAV"],
        ["*LPSI", "*T10", "*RPSI"],
        ["*T10", "*C7"],
        ["*LASI", "*LSHO", "*LPSI"],
        ["*RASI", "*RSHO", "*RPSI"],
        ["*LPSI", "*LASI", "*RASI", "*RPSI", "*LPSI"],
        ["*LSHO", "*CLAV", "*RSHO", "*C7", "*LSHO"],
    ],
}
p = ktk.Player(
    markers,
    interconnections=interconnections,
    up="z",
    anterior="-y",
    target=(0, 0.5, 1),
    azimuth=0.1,
    zoom=1.5, 
)
_images/2f06d0de76eb810dd34c1bdee93e5914c37919ce155c9a0b9da04fa33a55ae56.png

10.5.1. Styling the background#

To set a white background:

p.background_color = 'w'

# Equivalent to p.background_color = (1.0, 1.0, 1.0)
_images/6e439dd333a37615f6adddf4eba4da0fa70b3be3115c169c844b8c7cc524e455.png

10.5.2. Styling the grid#

10.5.2.1. Grid size and subdivisions#

To have a smaller grid (4x4 metres) with a smaller subdivision size (50x50 cm):

p.grid_size = 4
p.grid_subdivision_size = 0.5
_images/badfbc8de7ef0b39572a08ef462c9f9bf0afe5d409d32bd6b415d131f5f78399.png

10.5.2.2. Grid origin#

The grid origin can be placed anywhere in the scene, using the grid_origin property. For example:

p.grid_origin = (0.0, 0.5, 0.0)
_images/9a82cde66d2321536ea00a1f3a210696724c3c8f3cd64a16c190f394c10e22d4.png

10.5.2.3. Grid width#

To get a thinner grid:

p.grid_width = 0.5
_images/4547753b245d7114133d10aad1713e9568df4d04d95a1704b87742d67955b220.png

10.5.2.4. Grid colour#

To get a light grey grid:

p.grid_color = (0.75, 0.75, 0.75)
_images/2fce3fc5390cc18fd0eca5cc6c2cab350e494259cf935935a2605c74a5b14d7b.png

10.5.3. Styling points#

10.5.3.1. Point size#

To get bigger points:

p.point_size = 10
_images/40359ccf8302ec4bd8f92b872b384ec37b0e8384ae627610a09a1c8a41ce0174.png

10.5.3.2. Point colour#

The default point colour can be set by the default_point_color property:

p.default_point_color = 'r'

# Equivalent to: p.default_point_color = (1.0, 0.0, 0.0)
_images/8ba480f403be781ddf734ad58ee6a1253273aaf1465b2fddec653f86ebccd5e7.png

To assign individual colours, we add a Color info to the TimeSeries’ data. For instance, to show Viktor’s markers in dark green:

markers = p.get_contents()

for key in markers.data:
    if key.startswith("Viktor:"):
        markers = markers.add_data_info(key, "Color", (0.0, 0.75, 0.0))

p.set_contents(markers)
_images/6fb7f1e7b524734d764a368a0aa814ca6c76e9614da0158316838d8618373e2e.png

10.5.4. Styling interconnections#

10.5.4.1. Interconnection width#

To get thicker interconnections:

p.interconnection_width = 4.0
_images/41d413dd916f3617e19cdec43bbbcf7d268d11ee2a2459a4cf88099b3a247849.png

10.5.4.2. Interconnection colour#

Colour is part of the interconnection dictionary. Therefore, we change interconnection colour using this dictionary. For instance, to set all interconnections to light grey:

interconnections = p.get_interconnections()

for segment in interconnections:
    interconnections[segment]["Color"] = (0.75, 0.75, 0.75)

p.set_interconnections(interconnections)
_images/55c3622190f091e2c99a473007b27632ac7e4508c80e34cc93764e4a288c81be.png

10.5.5. Styling frames#

10.5.5.1. Frame size#

To get half-metre long frames:

p.frame_size = 0.5
_images/7574be8d3065bc7e901abe61a271f6f09ac82a8d48a3fbffb0e5cf80d99a0ce0.png

10.5.5.2. Frame width#

To get thinner frames:

p.frame_width = 1.0
_images/c8a7037b43e5cf614d11d6a590a359a97b7404f863e34da1eeb3678236228d21.png

10.5.6. Exporting#

10.5.6.1. Exporting to an image#

The Player’s current view can be exported to any file format supported by Matplotlib such as PNG, JPEG, SVG, or PDF, using ktk.Player.to_image:

p.to_image("exported_image.png")

10.5.6.2. Exporting to a video#

The whole animation in the current viewpoint can be exported to an MP4 file using ktk.Player.to_video:

p.to_video("exported_video.mp4")

Note

To create videos, the package ffmpeg must be installed on your computer. If you installed Kinetics Toolkit using conda, then it is already installed. If you used pip, you must install it manually.