I try to display the light type options inside the toolbar in a shelf.

enter image description here

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"

    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)
  • $\begingroup$ Quick note. There is a context.lamp available if you are in the properties space 'DATA' panel, with a lamp object selected, just as there is a context.speaker if that is the type of object selected. As shown they are not available elsewhere, and is something to look out for when re-using properties panel code elsewhere, like in the 3D view toolbar. $\endgroup$ – batFINGER Feb 15 '16 at 14:02

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.