Post #4782: Good morning @mcjamall07 and @Nanomanpro
π Metadata
- Author: Pluglug
- Date: 2023-07-09 04:36:33
- Type:
answer - Quality Score: 8/10
- Reply to: post_04781
- Replies (1): post_04783
π·οΈ Tags
macro python-scripting intermediate solved
βοΈ Related PME Features
- Macro Editor
- Python Scripting
π¬ Content
Good morning @mcjamall07 and @Nanomanpro
To perform operations on multiple objects at once, you can use bpy.context.selected_objects , which returns a list of all currently selected objects.
# The first three objects in the list of selected objects each have a value set.
bpy.context.selected_objects[0].color[3] = value
bpy.context.selected_objects[1].color[3] = value
bpy.context.selected_objects[2].color[3] = value
# A for loop sets a value for all selected objects.
for obj in bpy.context.selected_objects:
obj.color[3] = value
# This uses list comprehension notation to achieve the same result in a more concise manner.
[o.color.__setitem__(3, value) for o in bpy.context.selected_objects]
# In summary, all three scripts set the value of color[3] for the selected objects.
# However, while the first script targets only the first three objects, the latter two target all selected objects.
This method assumes that all objects have a color attribute. If any of the selected objects do not have a color attribute, an error may occur.
Furthermore, itβs difficult to retrieve values from multiple objects because when multiple objects are selected, their color[3] values may differ. Generally, we refer to the value of the active object.
To address potential issues, Iβve added a simple error handling measure that limits processing to Mesh objects.
After careful consideration, I propose two methods.
The first one involves using the Poll method to ensure that the active object is a MESH. In this case, you can use the following scripts:
# Getter
return bpy.context.active_object.color[3]
# Setter
[o.color.__setitem__(3, value) for o in C.selected_objects if isinstance(o.data, T.Mesh)]
Next, I propose a method that does not function at all if the active object is not a MESH. For this case, you can use these scripts:
# Getter
return C.active_object.color[3] if C.active_object and isinstance(C.active_object.data, T.Mesh) else 0.0
# Setter
[o.color.__setitem__(3, value) for o in C.selected_objects if isinstance(o.data, T.Mesh)] if C.active_object and isinstance(C.active_object.data, T.Mesh) else None
β€οΈ 4 likes
image1110Γ382 86.7 KB
image710Γ190 23.8 KB