Attribute error while declaring correct variables I try to display the light type options inside the toolbar in a shelf.



So I copied the code from the Blender "ui.py" but I remain getting the error that for the light object I have not defined the attribute lamp right.


  AttributeError: 'Context' object has no attribute 'lamp'


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

    @classmethod
    def poll(cls, context):
        return context.lamp

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

        lamp = context.lamp
        clamp = lamp.cycles
        cscene = context.scene.cycles

        layout.prop(lamp, "type", expand=True)

        row = layout.row()
        row.prop(clamp, "cast_shadow")


What is it that I am missing or doing wrong here?

If I exchange lamp=context.lamp with lamp = context.active_object.data it works.
As the error suggests, there is no lamp property in bpy.context and as you found context.active_object.data gives you the properties you are after when a lamp is selected.

Unlike a camera, there is no "active" lamp and there could be hundreds of lamps in a scene. You will most likely want to test if the active object is a lamp and display it's properties if it is.

if context.active_object.type == 'LAMP':
    lamp = context.active_object.data
    layout.prop(lamp, "type", expand=True)