I was trying to make a custom panel for the settings(which includes the viewport Material colour settings) in the materials tab in the properties windows and I faced this problem. The problem is that I am only able to use the panel for the specific material name that I define. For example in the code I added a specific name "MyMat", and it only work for that specific material. How do I make it work for the current material that is currently assigned to the object regardless of the name. Here's the code and the gif.

Import Bpy
Class MaterialPanel(bpy.types.Panel):
    bl_idname = "mat.p_operator"
    bl_label = "Material panel"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_category = "Mats"

def draw(self, context):
    layout = self.layout
    row = layout.row()
    mat = bpy.data.materials["MyMat"]
    row.prop(mat, "diffuse_color", text="Diffuse")
    row = layout.row()
    row.prop(mat, "specular_color", text="Specular")

def register():

def unregister():

if__name__ == "__main__":

GIF here, somehow it's not appearing in the post


You can get the active material of the active object like this:

mat = bpy.context.active_object.active_material

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.