Attribute problem with defining Camera for context object I struggle to understand why this does not work in my script, giving me the error that the context object has not attribute camera, while I use the original code from the properties_data_camera.py file.


  File "C:\Users\info\Desktop\UI Testing.blend\studioTools_Shelf_Beta
  v2.py", line 709, in draw AttributeError: 'Context' object has no
  attribute 'camera'


I also added the following line at the beginning

from bl_ui.properties_data_camera import CameraButtonsPanel
from rna_prop_ui import PropertyPanel


# CAMERA ---------------------------------------------------------------------------
class CAMERA(bpy.types.Panel):
    bl_label = "Camera"
    bl_idname = "Camera"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = "Shading Toolbox"
    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME'}

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

        cam = context.camera

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


Everything I would like is to show few buttons from the Camera UI in the toolshelf. Currently it is rendered empty because of the error I get.
The properties panel has some of its own context members that only work in the property panel space. The "context" attribute of the space is shown in the icons across the top which change for what type of object is active, eg a camera icon for the data part of a camera object, in which case context.camera resolves to the data part of the active object, similarly for context.lamp and context.speaker for those types of object.  

This is something to look out for when refactoring code from space_properties to, for instance, the 3D toolshelf. Anyhoo a good workaround here is 

cam = context.scene.camera.data


the "active camera", ie the one that renders the scene, or for the context object

cam = context.active_object.data


and in the poll method make sure 'context.scene` and or context.scene.camera are not None, and if using the latter that context.active_object is not None and  context.active_object.type is 'CAMERA' 

example poll

    @classmethod
    def poll(cls, context):
        if context.active_object is None:
            return False
        return context.active_object.type == 'CAMERA'