Post #4797: Hi @Nanomanpro
📋 Metadata
- Author: Pluglug
- Date: 2023-07-15 07:19:51
- Type:
showcase - Quality Score: 9/10
- Replies (1): post_04798
🏷️ Tags
pie-menu hotkeys advanced solved
⚙️ Related PME Features
- Pie Menu Editor
- Property Editor
- Python Scripting
- Custom Icons
💬 Content
Hi @Nanomanpro
I am writing to you about a way to set keyframes for the alpha of an object. You may have already found a good way, but I thought I would share my approach.
I have set up a Boolean property next to the slider that allows us to check the keyframe status and set keyframes for the alpha value of an object.
Writing such logic directly in the Pie menu editor can be complex, and chances are, we might forget how it works a year from now. Therefore, I’ve created a class called PME_ObjPropAux and added its instances to pme.context.globals.
pme_obj_prop_aux.py (6.9 KB)
Please run this script in Blender or place it in ‘pie_menu_editor\scripts\autorun’ and restart Blender.
At the end of the script, you will see that I have created an instance called my_alpha by specifying the attribute color and its index. This instance is added to globals so that we can use the methods of the PME_ObjPropAux class for color[3] within Pie menu editor.
my_alpha = PME_ObjPropAux("color", 3)
pme.context.add_global("my_alpha", my_alpha)
In Pie menu editor, create a Boolean property called MyAlphaKeys. The key_value_match method checks if the current value of the property matches the value at the keyframe for the current frame. (One could say that I created the class for this feature.)
key_insert is equivalent to C.active_object.keyframe_insert(data_path='color', index=3). However, the information of color[3] is encapsulated in my_alpha, so dedicated methods are provided.
# Getter
return my_alpha.key_value_match()
# Setter
my_alpha.key_insert()

Next, let’s use MyAlphaKeys in your menu items. (Please use in the Custom tab.) get_key_icon returns an icon string based on the current keyframe status.
L.prop(props(), "MyAlphaKeys", text="", icon=my_alpha.get_key_icon())
With that, we have completed a keyframe button visually. It looks good!
By the way, I also modified the float property we created last time. set_values integrates the three patterns we proposed last time into one. uniform is the default, but you can also specify mode='same' or mode='ratio'. (I have not created a method for the getter.)
# Getter
return C.active_object.color[3] if C.active_object and hasattr(C.active_object, 'color') else 0.0
# Setter
my_alpha.set_values(value, min_value=0.0, max_value=1.0)
I left out some options in the explanation, so please read the comments in the script for more details. This script was created with versatility in mind, so it can be used for other object properties or custom properties, not just color[3].
There might be other good ways to do this, but I’m planning to adopt this method. I hope this proposal will be of help to you.
❤️ 4 likes
menu691×226 26 KB