thorvg_python.animation¶
Submodules¶
Classes¶
A structure representing an animation controller object. |
|
A structure representing a graphical element. |
|
Enumeration specifying the result from the APIs. |
|
Engine API |
|
Picture API |
|
Animation API |
Package Contents¶
- class thorvg_python.animation.AnimationPointer¶
Bases:
ctypes.c_void_pA structure representing an animation controller object.
Note
You should use
Animationclass instead.
- class thorvg_python.animation.PaintPointer¶
Bases:
ctypes.c_void_pA structure representing a graphical element.
Warning
The PaintPointer objects cannot be shared between Canvases.
Note
You should use
Paintclass instead.
- class thorvg_python.animation.Result¶
Bases:
enum.IntEnumEnumeration specifying the result from the APIs.
All ThorVG APIs could potentially return one of the values in the list. Please note that some APIs may additionally specify the reasons that trigger their return values.
-
SUCCESS =
0¶
-
INVALID_ARGUMENT =
1¶
-
INSUFFICIENT_CONDITION =
2¶
-
FAILED_ALLOCATION =
3¶
-
MEMORY_CORRUPTION =
4¶
-
NOT_SUPPORTED =
5¶
-
UNKNOWN =
255¶
-
SUCCESS =
-
class thorvg_python.animation.Engine(thorvg_lib_path: str | None =
None, threads: int =0)¶ Engine API
A module enabling initialization and termination of the TVG engines.
-
threads =
0¶
- init_result¶
-
_load_lib(thorvg_lib_path: str | None =
None) None¶
- __del__() None¶
- __exit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: types.TracebackType | None) None¶
- init(threads: int) thorvg_python.base.Result¶
Initializes TVG engines.
ThorVG requires an active runtime environment to operate. Internally, it utilizes a task scheduler to efficiently parallelize rendering operations. You can specify the number of worker threads using the
threadsparameter. During initialization, ThorVG will spawn the specified number of threads.- Parameters:¶
- threads : int¶
The number of additional threads used to perform rendering. Zero indicates only the main thread is to be used.
- Return type:¶
Note
The initializer uses internal reference counting to track multiple calls. The number of threads is fixed on the first call to Engine.init() and cannot be changed in subsequent calls.
See also
Engine.term()
- term() thorvg_python.base.Result¶
Terminates TVG engines.
Cleans up resources and stops any internal threads initialized by Engine.init().
- Returns:¶
Result.INSUFFICIENT_CONDITION Returned if there is nothing to terminate (e.g., Engine.init() was not called).
- Return type:¶
See also
Engine.init()
- version() tuple[thorvg_python.base.Result, int, int, int, str | None]¶
Retrieves the version of the TVG engine.
- Returns:¶
Result.SUCCESS
- Return type:¶
- Returns:¶
A major version number.
- Return type:¶
int
- Returns:¶
A minor version number.
- Return type:¶
int
- Returns:¶
A micro version number.
- Return type:¶
int
- Returns:¶
The version of the engine in the format major.minor.micro, or a
Nonein case of an internal error.- Return type:¶
Optional[str]
Added in version 0.15.
-
threads =
-
class thorvg_python.animation.Picture(engine: thorvg_python.engine.Engine, paint: thorvg_python.base.PaintPointer | None =
None)¶ Bases:
thorvg_python.paint.PaintPicture API
A module enabling to create and to load an image in one of the supported formats: svg, png, jpg, lottie and raw.
- engine¶
- thorvg_lib¶
- _new() thorvg_python.base.PaintPointer¶
Creates a new picture object.
Note that you need not call this method as it is auto called when initializing
Picture().
- load(path: str) thorvg_python.base.Result¶
Loads a picture data directly from a file.
ThorVG efficiently caches the loaded data using the specified
pathas a key. This means that loading the same file again will not result in duplicate operations; instead, ThorVG will reuse the previously loaded picture data.
- load_raw(data: bytes, w: int, h: int, cs: thorvg_python.base.Colorspace, copy: bool) thorvg_python.base.Result¶
Loads a picture data from a memory block of a given size.
ThorVG efficiently caches the loaded data using the specified
dataaddress as a key when thecopyhasfalse. This means that loading the same data again will not result in duplicate operations for the sharabledata. Instead, ThorVG will reuse the previously loaded picture data.- Parameters:¶
- data : bytes¶
A pointer to a memory location where the content of the picture raw data is stored.
- w : int¶
The width of the image
datain pixels.- h : int¶
The height of the image
datain pixels.- cs : thorvg.base.Colorspace¶
Specifies how the 32-bit color values should be interpreted (read/write).
- copy : bool¶
If
truethe data are copied into the engine local buffer, otherwise they are not.
- Returns:¶
Result.INVALID_ARGUMENT An invalid PaintPointer or no data are provided or the
worhvalue is zero or less.- Return type:¶
Added in version 0.9.
- load_data(data: bytes, mimetype: str, rpath: str | None, copy: bool) thorvg_python.base.Result¶
Loads a picture data from a memory block of a given size.
ThorVG efficiently caches the loaded data using the specified
dataaddress as a key when thecopyhasfalse. This means that loading the same data again will not result in duplicate operations for the sharabledata. Instead, ThorVG will reuse the previously loaded picture data.- Parameters:¶
- data : bytes¶
A pointer to a memory location where the content of the picture file is stored. A null-terminated string is expected for non-binary data if
copyisfalse- mimetype : str¶
Mimetype or extension of data such as “jpg”, “jpeg”, “svg”, “svg+xml”, “lottie”, “png”, etc. In case an empty string or an unknown type is provided, the loaders will be tried one by one.
- rpath : Optional[str]¶
A resource directory path, if the
dataneeds to access any external resources.- copy : bool¶
If
truethe data are copied into the engine local buffer, otherwise they are not.
- Returns:¶
Result.INVALID_ARGUMENT In case a
Noneis passed as the argument or thesizeis zero or less.Result.NOT_SUPPORTED A file with an unknown extension.
- Return type:¶
Warning
: It’s the user responsibility to release the
datamemory if thecopyistrue.
- set_asset_resolver(resolver: collections.abc.Callable[[thorvg_python.base.PaintPointer, ctypes.c_char_p, ctypes.c_void_p], bool], data: bytes) thorvg_python.base.Result¶
Sets the asset resolver callback for handling external resources (e.g., images and fonts).
This callback is invoked when an external asset reference (such as an image source or file path) is encountered in a Picture object. It allows the user to provide a custom mechanism for loading or substituting assets, such as loading from an external source or a virtual filesystem.
- Parameters:¶
- resolver : Callable[[thorvg_python.base.PaintPointer, ctypes.c_char_p, ctypes.c_void_p], bool]¶
A user-defined function that handles the resolution of asset paths. The function should return
trueif the asset was successfully resolved by the user, orfalseif it was not.- data : bytes¶
A pointer to user-defined data that will be passed to the callback each time it is invoked. This can be used to maintain context or access external resources.
- Returns:¶
Result.INVALID_ARGUMENT A
Nonepassed as thepictureargument.Result.INSUFFICIENT_CONDITION If the
pictureis already loaded.
- Return type:¶
Note
This function must be called before
Picture.load()Setting the resolver after loading will have no effect on asset resolution for that asset.Note
If
falseis returned byresolver, ThorVG will attempt to resolve the resource using its internal resolution mechanism as a fallback.Note
To unset the resolver, pass
Noneas theresolverparameter.Note
Experimental API
See also
PictureAssetResolverType
- set_size(w: float, h: float) thorvg_python.base.Result¶
Resizes the picture content to the given width and height.
The picture content is resized while keeping the default size aspect ratio. The scaling factor is established for each of dimensions and the smaller value is applied to both of them.
- get_size() tuple[thorvg_python.base.Result, float, float]¶
Gets the size of the loaded picture.
- set_origin(x: float, y: float) thorvg_python.base.Result¶
Sets the normalized origin point of the Picture object.
This method defines the origin point of the Picture using normalized coordinates. Unlike a typical pivot point used only for transformations, this origin affects both the transformation behavior and the actual rendering position of the Picture.
The specified origin becomes the reference point for positioning the Picture on the canvas. For example, setting the origin to (0.5f, 0.5f) moves the visual center of the picture to the position specified by Paint.translate().
The coordinates are given in a normalized range relative to the picture’s bounds: - (0.0f, 0.0f): top-left corner - (0.5f, 0.5f): center - (1.0f, 1.0f): bottom-right corner
Note
This origin directly affects how the Picture is placed on the canvas when using transformations such as translate(), rotate(), or scale().
See also
Paint.translate()
See also
Paint.rotate()
See also
Paint.scale()
See also
Paint.set_transform()
See also
Picture.get_origin()
Added in version 1.0.
- get_origin() tuple[thorvg_python.base.Result, float, float]¶
Gets the normalized origin point of the Picture object.
This method retrieves the current origin point of the Picture, expressed in normalized coordinates relative to the picture’s bounds.
See also
Picture.set_origin()
Added in version 1.0.
- get_paint(_id: int) thorvg_python.paint.Paint | None¶
Retrieve a paint object from the Picture scene by its Unique ID.
This function searches for a paint object within the Picture scene that matches the provided
id.- Parameters:¶
- _id : int¶
The Unique ID of the paint object.
- Returns:¶
A pointer to the paint object that matches the given identifier, or
Noneif no matching paint object is found.- Return type:¶
See also
Engine.accessor_generate_id()
-
class thorvg_python.animation.Animation(engine: thorvg_python.engine.Engine, animation: thorvg_python.base.AnimationPointer | None =
None)¶ Animation API
A module for manipulation of animatable images.
The module supports the display and control of animation frames.
- engine¶
- thorvg_lib¶
- _new() thorvg_python.base.AnimationPointer¶
Creates a new Animation object.
Note
You need not call this method as it is auto called when initializing
Animation().Added in version 0.13.
- set_frame(no: float) thorvg_python.base.Result¶
Specifies the current frame in the animation.
- Parameters:¶
- no : float¶
The index of the animation frame to be displayed. The index should be less than the Animation.get_total_frame().
- Returns:¶
Result.INVALID_ARGUMENT An invalid AnimationPointer.
Result.INSUFFICIENT_CONDITION if the given
nois the same as the current frame value.Result.NOT_SUPPORTED The picture data does not support animations.
- Return type:¶
Note
For efficiency, ThorVG ignores updates to the new frame value if the difference from the current frame value is less than 0.001. In such cases, it returns
Result.InsufficientCondition. Values less than 0.001 may be disregarded and may not be accurately retained by the Animation.See also
Animation.get_total_frame()
Added in version 0.13.
- get_picture() thorvg_python.paint.picture.Picture¶
Retrieves a picture instance associated with this animation instance.
This function provides access to the picture instance that can be used to load animation formats, such as Lottie(json). After setting up the picture, it can be pushed to the designated canvas, enabling control over animation frames with this Animation instance.
Warning
The picture instance is owned by Animation. It should not be deleted manually.
Added in version 0.13.
- get_frame() tuple[thorvg_python.base.Result, float]¶
Retrieves the current frame number of the animation.
- Returns:¶
Result.INVALID_ARGUMENT An invalid AnimationPointer or
no- Return type:¶
- Returns:¶
The current frame number of the animation, between 0 and totalFrame() - 1.
- Return type:¶
float
See also
Animation.get_total_frame()
See also
Animation.set_frame()
Added in version 0.13.
- get_total_frame() tuple[thorvg_python.base.Result, float]¶
Retrieves the total number of frames in the animation.
- Returns:¶
Result.INVALID_ARGUMENT An invalid AnimationPointer or
cnt.- Return type:¶
- Returns:¶
The total number of frames in the animation.
- Return type:¶
float
Note
Frame numbering starts from 0.
Note
If the Picture is not properly configured, this function will return 0.
Added in version 0.13.
- get_duration() tuple[thorvg_python.base.Result, float]¶
Retrieves the duration of the animation in seconds.
- Returns:¶
Result.INVALID_ARGUMENT An invalid AnimationPointer or
duration.- Return type:¶
- Returns:¶
The duration of the animation in seconds.
- Return type:¶
float
Note
If the Picture is not properly configured, this function will return 0.
Added in version 0.13.
- set_segment(begin: float, end: float) thorvg_python.base.Result¶
Specifies the playback segment of the animation.
The set segment is designated as the play area of the animation. This is useful for playing a specific segment within the entire animation. After setting, the number of animation frames and the playback time are calculated by mapping the playback segment as the entire range.
- Parameters:¶
- Returns:¶
Result.INSUFFICIENT_CONDITION In case the animation is not loaded.
Result.INVALID_ARGUMENT If the
beginis higher thanend.
Note
Animation allows a range from 0.0 to the total frame.
endshould not be higher thanbegin.Note
If a marker has been specified, its range will be disregarded.
See also
Animation.set_marker()
See also
Animation.get_total_frame()
Added in version 1.0.
- get_segment() tuple[thorvg_python.base.Result, float, float]¶
Gets the current segment.
- Returns:¶
Result.INSUFFICIENT_CONDITION In case the animation is not loaded.
Result.INVALID_ARGUMENT An invalid AnimationPointer.
- Return type:¶
- Returns:¶
segment begin frame.
- Return type:¶
float
- Returns:¶
segment end frame.
- Return type:¶
float
Added in version 1.0.
- _del() thorvg_python.base.Result¶
Deletes the given AnimationPointer object.
Added in version 0.13.