-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflip_gradient.py
58 lines (44 loc) · 1.86 KB
/
flip_gradient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import bpy
class BRUSH_OT_flip_gradient(bpy.types.Operator):
bl_idname = "brush.flip_gradient"
bl_label = "Flip Brush Gradient"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
brush = self.get_active_brush(context)
if not brush:
self.report({'WARNING'}, "No active brush found.")
return {'CANCELLED'}
if not hasattr(brush, "gradient") or brush.gradient is None:
self.report({'WARNING'}, "The active brush does not use a gradient.")
return {'CANCELLED'}
self.flip_color_ramp(brush.gradient)
return {'FINISHED'}
def get_active_brush(self, context):
tool_settings = context.tool_settings
if context.sculpt_object:
return tool_settings.sculpt.brush
elif context.vertex_paint_object:
return tool_settings.vertex_paint.brush
elif context.weight_paint_object:
return tool_settings.weight_paint.brush
elif context.image_paint_object:
return tool_settings.image_paint.brush
else:
return None
def flip_color_ramp(self, color_ramp):
elements = color_ramp.elements
n = len(elements)
# Create lists to hold the positions and colors temporarily
positions = [e.position for e in elements]
colors = [e.color[:] for e in elements] # Use slicing to copy the color values
for i in range(n):
elements[i].position = 1.0 - positions[n-1-i]
elements[i].color = colors[n-1-i]
def register():
bpy.utils.register_class(BRUSH_OT_flip_gradient)
def unregister():
bpy.utils.unregister_class(BRUSH_OT_flip_gradient)
if __name__ == "__main__":
register()
# Test call
bpy.ops.brush.flip_gradient()