The error you're encountering, TypeError: type mismatch, can arise when passing incorrect types to functions or attributes in the GIMP-Python API. Specifically, the opacity level should be a float between 0.0 (completely transparent) and 1.0 (completely opaque), rather than an integer between 0 and 255.
Here's the corrected code that adheres to the proper data type for the opacity attribute:
Corrected Plugin Script for GIMP
from gimpfu import *
def auto_keyframe(image, layer, num_frames, transparency_step):
gimp.message("Auto Keyframing: Creating {} frames...".format(num_frames))
# Start by duplicating the original layer
original_layer = layer
for i in range(num_frames):
# Create a duplicate of the original layer
new_layer = original_layer.copy()
# Change the layer's name to denote it as a keyframe
new_layer.name = "Frame {}".format(i + 1)
# Set the layer's opacity
new_alpha = max(0, 1.0 - (transparency_step / 255.0) * i)
new_layer.opacity = new_alpha # Opacity as a float between 0.0 and 1.0
# Add the new layer to the image
image.add_layer(new_layer)
# Update the image display
gimp.DisplaysFlush()
register(
"python_fu_auto_keyframe",
"Auto Keyframe",
"Automatically create keyframes with varying transparency",
"Your Name",
"Your Name",
"2023",
"<Image>/Filters/Animation/Auto Keyframe...",
"*",
[
(PF_INT, "num_frames", "Number of Frames", 5),
(PF_INT, "transparency_step", "Transparency Step (0-255)", 51),
],
[],
auto_keyframe)
main()
Key Changes
- Opacity Calculation: The formula for calculating
new_alpha now divides transparency_step by 255.0 to convert the value from a range of 0-255 to a float range of 0.0-1.0.
- Setting Opacity: The value assigned to
new_layer.opacity remains a float, complying with the expected data type.
Summary of Functionality
- The plugin creates a specified number of frames (layers) and adjusts their opacity levels based on the
transparency_step provided by the user.
- The resulting layers will have a gradient of opacity, simulating basic keyframing behavior for animations.
Instructions for Testing
- Save the updated script in your GIMP plug-ins directory.
- Restart GIMP to ensure it recognizes the new script.
- Test the plugin from
Filters > Animation > Auto Keyframe... to generate frames with varying transparency.
Let me know if this resolves your issue, and feel free to reach out if you have further questions!