I'm new and I'm trying to learn how to make a toggle for the alpha channel of of the viewport display material so that I can set a shortcut on it. I can't do that by normal means it seems. So I'm trying to make a script for it. I already know how to make an addon. I know how to do something like this in Maya but I can't figure it out here in Blender!

What would be the line of code I should use to make this happen? I'd like to toggle between a value of 0.2 and 1 with the said command.

This is the attribute in question:



enter image description here


1 Answer 1


You would have to implement an Operator for a shortcut. I'd suggest ask for the alpha value and set it based on a certain condition. Following example is based on our famous operator_simple.py template that comes with Blender (Text Editor > Templates > Python > Operator Simple):

class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    def poll(cls, context):
        return context.active_object.active_material is not None

    def execute(self, context):
        # Get viewport display color
        diff_clr = context.object.active_material.diffuse_color
        # Get the alpha component and set it
        if diff_clr[3] == 1.0:
            diff_clr[3] = 0.2
            diff_clr[3] = 1.0
        return {'FINISHED'}

How to implement the shortcut: Create keyboard shortcut for an operator using python?


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .