0
$\begingroup$

Sofar I replicated all the needed shading tools for an object inside the tool shelve.

enter image description here

The transparency function however only makes sense when the UI also allows you to change the alpha value which is part of the material menu

enter image description here

This is the code I have sofar:

class ObjectShading(bpy.types.Panel):
bl_label = "Shading"
bl_idname = "ObjectShading"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_category = "Claas"

def draw(self, context):
    layout = self.layout

    obj = context.object
    mesh = obj.data

    row = layout.row()
    row.label(text="AutoEdgeBreak:")
    row = layout.row()
    row.prop(mesh, "use_auto_smooth", text="BreakEdge")
    row.active = mesh.use_auto_smooth and not mesh.has_custom_normals
    row.prop(mesh, "auto_smooth_angle", text="Angle")
    row = layout.row()
    row.label(text="ManualSmooth:")
    row = layout.row()
    row.operator("object.shade_smooth", text="Smooth", icon="SMOOTHCURVE")
    row.operator("object.shade_flat", text="Sharp", icon="LINCURVE")
    row = layout.row()
    row.prop(obj, "draw_type", text="Draw Type")
    row = layout.row()
    row.prop(obj, "show_wire", text="Wire")
    row.prop(obj, "show_transparent", text="Transparency")
    row.prop(obj, "show_x_ray", text="X-Ray")

Looking into UI.py I discovered this but I fail to figure out how I could extract the proper code to add to my script so it only renders the viewport color and alpha slider.

class CyclesMaterial_PT_settings(CyclesButtonsPanel, Panel):
bl_label = "Settings"
bl_context = "material"
bl_options = {'DEFAULT_CLOSED'}

@classmethod
def poll(cls, context):
    return context.material and CyclesButtonsPanel.poll(context)

def draw(self, context):
    layout = self.layout

    mat = context.material
    cmat = mat.cycles

    split = layout.split()
    col = split.column()
    col.label(text="Surface:")
    col.prop(cmat, "sample_as_light", text="Multiple Importance")
    col.prop(cmat, "use_transparent_shadow")

    col = split.column()
    col.label(text="Volume:")
    sub = col.column()
    sub.active = use_cpu(context)
    sub.prop(cmat, "volume_sampling", text="")
    sub.prop(cmat, "volume_interpolation", text="")
    col.prop(cmat, "homogeneous_volume", text="Homogeneous")

    layout.separator()
    split = layout.split()

    col = split.column(align=True)
    col.label("Viewport Color:")
    col.prop(mat, "diffuse_color", text="")
    col.prop(mat, "alpha")

    col.separator()
    col.label("Viewport Alpha:")
    col.prop(mat.game_settings, "alpha_blend", text="")

    col = split.column(align=True)
    col.label("Viewport Specular:")
    col.prop(mat, "specular_color", text="")
    col.prop(mat, "specular_hardness", text="Hardness")

    col.separator()
    col.prop(mat, "pass_index")

Adding the code

row.prop(mat, "alpha")

results into an error code stating that "mat" is not defined which makes sense.

So when I then add add the mat definition after the def command

def draw(self, context):
    layout = self.layout

    obj = context.object
    mesh = obj.data
    mat = context.material
    cmat = mat.cycles

I get this error and I dont understand this and what cases this. I just copied the code.

location: :-1 Traceback (most recent call last): File "C:\Users\info\Desktop\untitled.blend\Claas_UI_2.py", line 98, in draw AttributeError: 'Context' object has no attribute 'material'

$\endgroup$

1 Answer 1

0
$\begingroup$

The context contains the current state within blender, eg bpy.context.selected_objects is a list of the selected objects while bpy.context.active_object is the active object.

While you can have an active material selected, every object can have their own active material, so it is an object property not a global context property. So in your example you want to use -

obj = context.object
mesh = obj.data
mat = obj.active_material
cmat = mat.cycles
$\endgroup$
1
  • $\begingroup$ This worked perfect! $\endgroup$ Jan 5, 2016 at 17:12

You must log in to answer this question.

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