### Install All Requirements from Source Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md If you are using Python Lottie from source, install all requirements (except pypotrace) by running `pip install -r requirements.txt`. ```bash pip install -r requirements.txt ``` -------------------------------- ### Install lottie from local source Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md Install the lottie package from a local source directory where the setup.py file is located. ```bash pip install /path/to/the/sources ``` -------------------------------- ### Install Optional Requirements with Pip Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md Use pip to install specific optional requirements for Python Lottie. For example, to install GIF export support, use `pip install lottie[GIF]`. ```bash pip install lottie[GIF] ``` ```bash pip install lottie[all] ``` -------------------------------- ### Install lottie from Git Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md Install the lottie package directly from its GitLab repository using pip. ```bash pip install git+https://gitlab.com/mattbas/python-lottie.git@master ``` -------------------------------- ### Install Python Lottie Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md Install the python-lottie package using pip. This is the first step to using the framework. ```bash pip install lottie ``` -------------------------------- ### Command Line Conversion Examples Source: https://context7.com/mattbas/python-lottie/llms.txt Illustrates various command-line operations for converting between Lottie, GIF, PNG, and SVG formats using the 'lottie_convert.py' script. Options for frame extraction, dimension control, vectorization modes, and optimization are shown. ```bash # Convert Lottie JSON to animated GIF lottie_convert.py input.json output.gif # Convert SVG to Lottie JSON lottie_convert.py input.svg output.json # Convert to Telegram sticker (auto-sanitizes to 512x512, 30/60fps) lottie_convert.py input.json output.tgs # Extract a single frame as PNG lottie_convert.py input.json frame.png --frame 30 # Convert with custom dimensions lottie_convert.py input.json output.gif --width 256 --height 256 # Convert GIF to Lottie using vectorization lottie_convert.py input.gif output.json --bmp-mode trace # Convert pixel art GIF to Lottie lottie_convert.py pixel_art.gif output.json --bmp-mode pixel # Optimize output (reduce file size) lottie_convert.py input.json output.json -O 2 ``` -------------------------------- ### Vectorize GIF to Lottie using Potrace Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md Convert a GIF file into a Lottie JSON file using the potrace vectorization algorithm. Install with `pip install "lottie[trace]"`. ```bash lottie_convert.py input_file.gif output_file.json --bmp-mode trace ``` -------------------------------- ### Organize Animations with Precompositions in Lottie Source: https://context7.com/mattbas/python-lottie/llms.txt Use precompositions to group and reuse parts of an animation. This example shows converting a layer to a precomp, scaling the animation, and sanitizing it for Telegram stickers. ```python from lottie import objects, Point, Color animation = objects.Animation(60) animation.width = 512 animation.height = 512 # Create main layer with shapes layer = objects.ShapeLayer() animation.add_layer(layer) circle = layer.add_shape(objects.Ellipse()) circle.size.value = Point(100, 100) circle.position.value = Point(256, 256) layer.add_shape(objects.Fill(Color(1, 0, 0))) # Convert current composition to a precomp # This wraps existing layers into a reusable asset precomp = animation.to_precomp() # Now animation.layers contains a single PreCompLayer # and the original content is in animation.assets # Scale the animation to fit different dimensions animation.scale(256, 256) # For Telegram stickers, sanitize dimensions and framerate animation.tgs_sanitize() # Sets to 512x512, 30 or 60 fps ``` -------------------------------- ### Create Basic Lottie Animation Source: https://context7.com/mattbas/python-lottie/llms.txt Programmatically create a Lottie animation by defining layers, shapes, and styles. This example creates a red circle with a black stroke and exports it to a JSON file. ```python from lottie import objects, Point, Color # Create a 60-frame animation at 60fps (1 second) animation = objects.Animation(n_frames=60, framerate=60) # Add a shape layer layer = objects.ShapeLayer() animation.add_layer(layer) # Add shapes to the layer circle = layer.add_shape(objects.Ellipse()) circle.size.value = Point(100, 100) circle.position.value = Point(256, 256) # Add fill and stroke styling fill = layer.add_shape(objects.Fill(Color(1, 0, 0))) # Red fill stroke = layer.add_shape(objects.Stroke(Color(0, 0, 0), 5)) # Black stroke, 5px width # Export to JSON from lottie.exporters.core import export_lottie export_lottie(animation, "output.json", pretty=True) ``` -------------------------------- ### Add Keyframe Animations in Lottie Source: https://context7.com/mattbas/python-lottie/llms.txt Animate properties of Lottie objects over time using keyframes. This example animates the position and opacity of a circle. ```python from lottie import objects, Point, Color animation = objects.Animation(60) layer = objects.ShapeLayer() animation.add_layer(layer) circle = layer.add_shape(objects.Ellipse()) circle.size.value = Point(100, 100) # Animate position with keyframes (time, value) circle.position.add_keyframe(0, Point(100, 256)) # Start position circle.position.add_keyframe(30, Point(400, 256)) # Middle position circle.position.add_keyframe(60, Point(100, 256)) # End position (loop back) # Animate opacity fill = layer.add_shape(objects.Fill(Color(1, 1, 0))) fill.opacity.add_keyframe(0, 100) # Fully opaque fill.opacity.add_keyframe(30, 20) # Nearly transparent fill.opacity.add_keyframe(60, 100) # Back to opaque ``` -------------------------------- ### Apply Easing Functions to Lottie Animations Source: https://context7.com/mattbas/python-lottie/llms.txt Control the timing and speed of animations using various easing functions. This example applies different easing functions to the position keyframes of a rectangle. ```python from lottie import objects, Point, Color from lottie.objects import easing animation = objects.Animation(90) layer = objects.ShapeLayer() animation.add_layer(layer) rect = layer.add_shape(objects.Rect()) rect.size.value = Point(50, 50) # Available easing functions: # - easing.Linear() - Constant speed # - easing.EaseIn(delay) - Slow start, fast end # - easing.EaseOut(delay) - Fast start, slow end # - easing.Sigmoid(delay) - Slow start and end # - easing.Hold() / easing.Jump() - Jump to value at end group = layer.add_shape(objects.Group()) group.add_shape(rect) # Apply easing to keyframes group.transform.position.add_keyframe(0, Point(50, 256), easing.EaseOut(1/3)) group.transform.position.add_keyframe(45, Point(460, 256), easing.Sigmoid(1/3)) group.transform.position.add_keyframe(90, Point(50, 256), easing.EaseIn(1/3)) layer.add_shape(objects.Fill(Color(0, 0.5, 1))) ``` -------------------------------- ### Apply Masks to Lottie Layers Source: https://context7.com/mattbas/python-lottie/llms.txt Use masks to clip shapes or layers within an animation. This example demonstrates creating a star mask from a bezier shape and applying it to a layer. ```python from lottie import objects, Point, Color from lottie.importers.svg import import_svg # Load an existing animation animation = import_svg("image.svg") # Create a star mask shape star = objects.Star() star.inner_radius.value = 100 star.outer_radius.value = 200 star.rotation.value = 180 star.position.value = Point(256, 256) # Convert star to bezier and create mask star_bezier = star.to_bezier() mask = objects.Mask(star_bezier.shape.value) # Apply mask to layer animation.layers[0].masks = [mask] # Optionally animate the mask mask.shape.add_keyframe(0, star_bezier.shape.value) star.outer_radius.value = 250 star2_bezier = star.to_bezier() mask.shape.add_keyframe(30, star2_bezier.shape.value) ``` -------------------------------- ### Create Bezier Paths for Lottie Animations Source: https://context7.com/mattbas/python-lottie/llms.txt Define custom shapes using bezier curves for use in Lottie animations. This example creates a heart shape using a series of bezier points and tangents. ```python from lottie import objects, Point, Color animation = objects.Animation(60) layer = objects.ShapeLayer() animation.add_layer(layer) # Create a heart shape using bezier points heart = objects.Bezier() heart.add_point(Point(50, 20), Point(50, -20), Point(-50, -20)) # (vertex, in_tangent, out_tangent) heart.add_smooth_point(Point(0, 50), Point(-5, -10)) heart.add_smooth_point(Point(100, 50), Point(-10, 0)) heart.add_smooth_point(Point(50, 100), Point(-5, 10)) heart.closed = True # Create path shape and add to group group = layer.add_shape(objects.Group()) group.transform.position.value = Point(200, 200) path = group.add_shape(objects.Path()) path.shape.value = heart ``` -------------------------------- ### Get Inkscape extension paths Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md This Python code snippet, intended for use within Inkscape, helps determine the user extensions directory. ```python import addon_utils; print(addon_utils.paths()) ``` -------------------------------- ### Apply Animation Utilities in Lottie Source: https://context7.com/mattbas/python-lottie/llms.txt Utilize built-in animation presets for common effects like shaking, following a path, or applying spring physics. Ensure necessary imports are included. ```python from lottie import objects, Point, Color, NVector from lottie.utils.animation import shake, follow_path, spring_pull animation = objects.Animation(120) layer = objects.ShapeLayer() animation.add_layer(layer) group = layer.add_shape(objects.Group()) rect = group.add_shape(objects.Rect()) rect.size.value = Point(50, 50) group.add_shape(objects.Fill(Color(1, 0, 0))) # Shake animation shake( position_prop=group.transform.position, x_radius=20, y_radius=20, start_time=0, end_time=60, n_frames=30 ) # Follow a bezier path path = objects.Bezier() path.add_point(Point(100, 400)) path.add_point(Point(200, 100)) path.add_point(Point(400, 300)) path.add_point(Point(400, 400)) group2 = layer.add_shape(objects.Group()) circle = group2.add_shape(objects.Ellipse()) circle.size.value = Point(30, 30) group2.add_shape(objects.Fill(Color(0, 1, 0))) follow_path( position_prop=group2.transform.position, bezier=path, start_time=0, end_time=120, n_keyframes=30, rotation_prop=group2.transform.rotation # Auto-orient along path ) # Spring effect group3 = layer.add_shape(objects.Group()) square = group3.add_shape(objects.Rect()) square.size.value = Point(40, 40) group3.add_shape(objects.Fill(Color(0, 0, 1))) spring_pull( position_prop=group3.transform.position, point=Point(400, 256), # Target position start_time=60, end_time=120, falloff=15, oscillations=7 ) ``` -------------------------------- ### Apply Gradient Fills and Strokes Source: https://context7.com/mattbas/python-lottie/llms.txt Demonstrates how to apply linear and radial gradients to shapes, and animate gradient colors for strokes. Requires 'objects', 'Point', and 'Color' from the 'lottie' library. ```python from lottie import objects, Point, Color, NVector animation = objects.Animation(60) layer = objects.ShapeLayer() animation.add_layer(layer) group = layer.add_shape(objects.Group()) # Create a circle circle = group.add_shape(objects.Ellipse()) circle.size.value = Point(200, 200) circle.position.value = Point(256, 256) # Linear gradient fill linear_fill = group.add_shape(objects.GradientFill()) linear_fill.start_point.value = Point(156, 256) linear_fill.end_point.value = Point(356, 256) linear_fill.colors.set_stops([ (0, Color(1, 0, 0)), # Red at start (0.5, Color(1, 1, 0)), # Yellow at middle (1, Color(0, 1, 0)) # Green at end ]) # Radial gradient (alternative example) # radial_fill = group.add_shape(objects.GradientFill()) # radial_fill.gradient_type = objects.GradientType.Radial # radial_fill.start_point.value = Point(256, 256) # Center # radial_fill.end_point.value = Point(356, 256) # Radius endpoint # radial_fill.colors.set_stops([(0, Color(1, 1, 1)), (1, Color(0, 0, 1))]) # Animated gradient stroke stroke = group.add_shape(objects.GradientStroke(5)) stroke.width.value = 8 stroke.start_point.value = Point(156, 256) stroke.end_point.value = Point(356, 256) stroke.colors.add_keyframe(0, [(0, Color(1, 0, 0)), (1, Color(1, 1, 0))]) stroke.colors.add_keyframe(30, [(0, Color(0, 1, 0)), (1, Color(0, 0, 1))]) stroke.colors.add_keyframe(60, [(0, Color(1, 0, 0)), (1, Color(1, 1, 0))]) stroke.colors.count = 2 ``` -------------------------------- ### Convert GIF to Lottie (Default Mode) Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md Convert a GIF file into a Lottie JSON file using the default algorithm, preserving raster images as they are. This is useful when vectorization is not desired. ```bash lottie_convert.py input_file.gif output_file.json ``` -------------------------------- ### Vectorize GIF to Lottie using Pixel Algorithm Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md Convert a GIF file into a Lottie JSON file using the pixel algorithm for pixel art. This method does not require potrace. ```bash lottie_convert.py input_file.gif output_file.json --bmp-mode pixel ``` -------------------------------- ### Import and Convert Lottie Files Source: https://context7.com/mattbas/python-lottie/llms.txt Shows how to import animations from SVG, Lottie JSON, and TGS formats, and export them to various formats including Lottie JSON, TGS, and embedded HTML. Requires specific importer/exporter functions from the 'lottie.importers' and 'lottie.exporters' modules. ```python from lottie.importers.svg import import_svg from lottie.importers.core import import_tgs, import_lottie from lottie.exporters.core import export_lottie, export_tgs # Import from SVG animation = import_svg("input.svg") # Import from Lottie JSON or TGS (same function handles both) animation = import_lottie("animation.json") animation = import_tgs("sticker.tgs") # Export to Lottie JSON export_lottie(animation, "output.json", pretty=True) # Export to Telegram animated sticker (with validation and sanitization) export_tgs(animation, "sticker.tgs", sanitize=True, validate=True) # Export to HTML with embedded animation from lottie.exporters.core import export_embedded_html export_embedded_html(animation, "preview.html") ``` -------------------------------- ### Apply Shape Modifiers (Trim Paths and Repeaters) Source: https://context7.com/mattbas/python-lottie/llms.txt Demonstrates the use of shape modifiers like Trim Paths for animating the drawing of a shape and Repeaters for creating duplicated and transformed instances of a shape. Requires 'objects', 'Point', and 'Color' from the 'lottie' library. ```python from lottie import objects, Point, Color animation = objects.Animation(60) layer = objects.ShapeLayer() animation.add_layer(layer) # Create a path circle = layer.add_shape(objects.Ellipse()) circle.size.value = Point(200, 200) circle.position.value = Point(256, 256) # Trim path modifier - animate drawing the shape trim = layer.add_shape(objects.Trim()) trim.start.value = 0 trim.end.add_keyframe(0, 0) trim.end.add_keyframe(60, 100) # Draw from 0% to 100% layer.add_shape(objects.Stroke(Color(0, 0, 0), 5)) # Repeater modifier example group = objects.Group() rect = group.add_shape(objects.Rect()) rect.size.value = Point(30, 30) rect.position.value = Point(50, 256) group.add_shape(objects.Fill(Color(0.2, 0.6, 1))) repeater = objects.Repeater(copies=10) repeater.transform.position.value = Point(45, 0) repeater.transform.rotation.value = 10 repeater.transform.scale.value = Point(95, 95) repeater.transform.start_opacity.value = 100 repeater.transform.end_opacity.value = 20 layer2 = objects.ShapeLayer() animation.add_layer(layer2) layer2.add_shape(group) layer2.add_shape(repeater) ``` -------------------------------- ### Convert Lottie to Still Image Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md Use the lottie_convert.py script to render a specific frame from a Lottie file into a still image format like PNG. Requires cairosvg and pillow. ```bash lottie_convert.py input_file.json output_file.png --frame 30 ``` -------------------------------- ### Create a Lottie Animation Object Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md Instantiate a Lottie Animation object and convert it to a dictionary for JSON output. This is a basic step for creating Lottie animations. ```python foo = lottie.Animation() # ... json.dump(foo.to_dict(), output_file) ``` -------------------------------- ### Load Lottie Animation with Bodymovin Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/devtools/bindings/js_example.html This function initializes and loads a Lottie animation into a specified container. Ensure the 'bodymovin' library is included and the container element exists. ```javascript import * as objects from './js/generated/all.js'; import { Vector } from './js/vector.js'; var bm_anim = null; function bm_reload(data) { if ( bm_anim ) bm_anim.destroy(); var animData = { container: document.getElementById('bodymovin'), renderer: 'svg', loop: true, autoplay: true, animationData: data, }; bm_anim = bodymovin.loadAnimation(animData); } var an = new objects.Animation(); var layer = new objects.ShapeLayer(); an.layers.push(layer); an.in_point = layer.in_point = 0; an.out_point = layer.out_point = 60; var circle = new objects.Ellipse() layer.shapes.push(circle); circle.size.value = new Vector(100, 100); circle.position.value = new Vector(220, 100); var fill = new objects.Fill(); fill.color.value = new Vector(1, 1, 0); layer.shapes.push(fill); // console.log(JSON.stringify(an.to_lottie())); bm_reload(an.to_lottie()); ``` -------------------------------- ### Create Animated Text Layers in Lottie Source: https://context7.com/mattbas/python-lottie/llms.txt Generate text animations with dynamic content, font, size, and color changes. Note that text layers are not supported by Telegram stickers. ```python from lottie import objects, Point, Color animation = objects.Animation(120) # Set up fonts animation.fonts = objects.text.FontList() animation.fonts.append(objects.text.Font("sans", name="sans")) # Create text layer text_layer = objects.TextLayer() animation.add_layer(text_layer) # Animate text content text_layer.data.add_keyframe( 0, objects.text.TextDocument("Hello", font_size=100, color=Color(1, 0, 0), font="sans") ) text_layer.data.add_keyframe( 60, objects.text.TextDocument("World", font_size=100, color=Color(0, 1, 0), font="sans") ) text_layer.transform.position.value = Point(50, 200) ``` -------------------------------- ### Convert Lottie to Animated Image Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md Convert a Lottie file into an animated image format such as GIF or WebP using the lottie_convert.py script. Requires cairosvg and pillow. ```bash lottie_convert.py input_file.json output_file.webp ``` -------------------------------- ### Convert TGS to WebP Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md Convert a Telegram animated sticker (TGS) file to a WebP animated image. TGS files are natively supported by python-lottie. ```bash lottie_convert.py AnimatedSticker.tgs output_file.webp ``` -------------------------------- ### Animate Between Two Bezier Shapes Source: https://context7.com/mattbas/python-lottie/llms.txt Defines two bezier shapes and animates a path between them over time. Requires 'objects', 'Point', and 'Color' from the 'lottie' library. ```python from lottie import objects, Point, Color # Assuming 'layer' and 'heart' are defined elsewhere # other_shape = ( # objects.Bezier() # .add_smooth_point(Point(50, 0), Point(10, 0)) # .add_smooth_point(Point(0, 50), Point(0, -20)) # .add_point(Point(50, 80), Point(-50, 20), Point(50, 20)) # .add_smooth_point(Point(100, 50), Point(0, 20)) # .close() # ) # animated_path = layer.add_shape(objects.Path()) # animated_path.shape.add_keyframe(0, heart) # animated_path.shape.add_keyframe(30, other_shape) # animated_path.shape.add_keyframe(60, heart) # layer.add_shape(objects.Fill(Color(1, 0, 0))) ``` -------------------------------- ### Convert Lottie to TGS Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md Convert a Lottie JSON file into a Telegram animated sticker (TGS) file. The script automatically scales the animation for Telegram and warns about unsupported features. ```bash lottie_convert.py input_file.json output_file.tgs ``` -------------------------------- ### Create and Transform Groups in Lottie Source: https://context7.com/mattbas/python-lottie/llms.txt Organize shapes within groups and apply transformations like position, anchor point, rotation, and scale. This is useful for creating complex hierarchical animations. ```python from lottie import objects, Point, Color animation = objects.Animation(60) layer = objects.ShapeLayer() animation.add_layer(layer) # Create a group with multiple shapes group = layer.add_shape(objects.Group()) # Shapes within the group rect = group.add_shape(objects.Rect()) rect.size.value = Point(80, 80) rect.position.value = Point(0, 0) circle = group.add_shape(objects.Ellipse()) circle.size.value = Point(40, 40) circle.position.value = Point(0, -60) group.add_shape(objects.Fill(Color(0.8, 0.2, 0.2))) # Transform the entire group group.transform.position.value = Point(256, 256) group.transform.anchor_point.value = Point(0, 0) # Animate group rotation group.transform.rotation.add_keyframe(0, 0) group.transform.rotation.add_keyframe(60, 360) # Animate group scale group.transform.scale.add_keyframe(0, Point(100, 100)) group.transform.scale.add_keyframe(30, Point(150, 150)) group.transform.scale.add_keyframe(60, Point(100, 100)) ``` -------------------------------- ### Apply Wave Effect to Shape Source: https://context7.com/mattbas/python-lottie/llms.txt Create a flag-like rectangle and apply a SineDisplacer to animate its shape, simulating a waving effect. This requires importing necessary objects and utilities for animation manipulation. ```python from lottie import objects, Point, Color, NVector from lottie.utils.animation import SineDisplacer, DepthRotationDisplacer animation = objects.Animation(60) layer = objects.ShapeLayer() animation.add_layer(layer) # Create a flag-like rectangle group = layer.add_shape(objects.Group()) rect = objects.Rect() rect.size.value = Point(300, 200) rect.position.value = Point(256, 256) bezier_path = rect.to_bezier() path = group.add_shape(objects.Path()) path.shape.value = bezier_path.shape.value group.add_shape(objects.Fill(Color(1, 0, 0))) # Apply wave displacement (flag waving effect) wave = SineDisplacer( wavelength=100, amplitude=20, time_start=0, time_end=60, n_frames=30, speed=2, axis=90 # Vertical displacement ) wave.animate_bezier(path.shape) ``` -------------------------------- ### Apply 3D Rotation Effect Source: https://context7.com/mattbas/python-lottie/llms.txt Create a square shape and apply a DepthRotationDisplacer to simulate a 3D rotation effect around the Y axis. This effect works best with bezier paths and requires defining the rotation center and angle. ```python # 3D rotation effect (pseudo-3D flip) group2 = layer.add_shape(objects.Group()) square = group2.add_shape(objects.Rect()) square.size.value = Point(100, 100) square.position.value = Point(0, 0) group2.add_shape(objects.Fill(Color(0, 0, 1))) group2.transform.position.value = Point(400, 256) rotation_3d = DepthRotationDisplacer( center=NVector(400, 256, 0), time_start=0, time_end=60, n_frames=30, axis=DepthRotationDisplacer.axis_y, # Rotate around Y axis angle=360 ) # Note: 3D rotation works best with bezier paths ``` -------------------------------- ### Check TGS File for Telegram Compatibility Source: https://gitlab.com/mattbas/python-lottie/-/blob/master/README.md Use the tgs_check.py script to identify potential compatibility issues with Telegram for an existing TGS file. ```bash tgs_check.py AnimatedSticker.tgs ``` -------------------------------- ### Programmatic Telegram Sticker Validation Source: https://context7.com/mattbas/python-lottie/llms.txt Import a Lottie animation, validate its properties using TgsValidator, export it as a TGS file, and then check the exported file's size. Any validation errors are stored in the validator's errors attribute. ```python from lottie.exporters.tgs_validator import TgsValidator from lottie.importers.core import import_lottie animation = import_lottie("animation.json") validator = TgsValidator() validator(animation) # Check animation properties # Check file after export from lottie.exporters.core import export_tgs export_tgs(animation, "sticker.tgs") validator.check_file_size("sticker.tgs") # Print any validation errors for error in validator.errors: print(error) ``` -------------------------------- ### Validate Telegram Sticker Animation Source: https://context7.com/mattbas/python-lottie/llms.txt Use the TgsValidator to check Lottie animations for Telegram sticker compatibility, including file size constraints. This can be done programmatically or via the command line. ```bash # Command line validation tgs_check.py sticker.tgs ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.