Created
November 27, 2024 08:22
-
-
Save CGArtPython/26b59c22d08c36042c0462d95aa5439a to your computer and use it in GitHub Desktop.
[Blender Python] Here is a way to flip the two edge stops on a color ramp (video explination here https://www.skool.com/cgpython/how-to-flip-a-color-ramp?p=38e4ac8e)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
class OBJECT_OT_FlipColorRamp(bpy.types.Operator): | |
"""Flip Color Ramp Elements""" | |
bl_idname = "object.flip_color_ramp" | |
bl_label = "Flip Color Ramp" | |
bl_options = {'REGISTER', 'UNDO'} | |
def execute(self, context): | |
try: | |
geo_nodes_modifier = context.active_object.modifiers.get('GeometryNodes') | |
if not geo_nodes_modifier: | |
self.report({'WARNING'}, "No Geometry Nodes modifier found!") | |
return {'CANCELLED'} | |
color_ramp_node = geo_nodes_modifier.node_group.nodes.get('Color Ramp') | |
if not color_ramp_node: | |
self.report({'WARNING'}, "No Color Ramp node found!") | |
return {'CANCELLED'} | |
elements = color_ramp_node.color_ramp.elements | |
if len(elements) < 2: | |
self.report({'WARNING'}, "Not enough elements to flip!") | |
return {'CANCELLED'} | |
r, g, b, a = elements[-1].color | |
elements[-1].color = elements[0].color | |
elements[0].color = r, g, b, a | |
self.report({'INFO'}, "Color ramp flipped successfully!") | |
return {'FINISHED'} | |
except Exception as e: | |
self.report({'ERROR'}, f"Unexpected error: {e}") | |
return {'CANCELLED'} | |
class GEONODES_PT_CustomPanel(bpy.types.Panel): | |
"""Custom Panel in the Geometry Nodes Editor""" | |
bl_label = "Custom Tools" | |
bl_idname = "GEONODES_PT_custom_panel" | |
bl_space_type = 'NODE_EDITOR' | |
bl_region_type = 'UI' | |
bl_category = "Custom Tools" | |
def draw(self, context): | |
layout = self.layout | |
layout.operator("object.flip_color_ramp", text="Flip Color Ramp") | |
classes = [OBJECT_OT_FlipColorRamp, GEONODES_PT_CustomPanel] | |
def register(): | |
for cls in classes: | |
bpy.utils.register_class(cls) | |
def unregister(): | |
for cls in classes: | |
bpy.utils.unregister_class(cls) | |
if __name__ == "__main__": | |
register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment