0
$\begingroup$

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.

enter image description here

$\endgroup$
2
  • $\begingroup$ Actually "cam = context.scene.camera" does not work correctly. But "cam = context.active_object.data" does. $\endgroup$ Commented Feb 15, 2016 at 16:01
  • $\begingroup$ Yep, noticed too late to edit comment, made it an answer instead. $\endgroup$
    – batFINGER
    Commented Feb 15, 2016 at 16:04

1 Answer 1

2
$\begingroup$

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'
$\endgroup$
6
  • $\begingroup$ Right now I try to write the proper Poll function to only render this tab visible when a camera is selected but I fail to understand how that has to be written correctly. $\endgroup$ Commented Feb 15, 2016 at 16:24
  • $\begingroup$ Also I noticed with "cam = context.active_object.data" I can edit each camera via the toolshelf camera UI, but if I use "cam = context.active_object.data" I can only edit the active camera not matter what camera I have selected in 3d view. $\endgroup$ Commented Feb 15, 2016 at 16:30
  • $\begingroup$ Do you mean this way? "return context.object and context.active_object.type == 'CAMERA'" $\endgroup$ Commented Feb 15, 2016 at 16:34
  • $\begingroup$ Yep, added similar code to answer. $\endgroup$
    – batFINGER
    Commented Feb 15, 2016 at 16:35
  • $\begingroup$ ah interesting! this helped me excluding showing UI when camera is selected that cannot have a material or render visibility: if context.object.type == 'CAMERA': return False $\endgroup$ Commented Feb 15, 2016 at 16:46

You must log in to answer this question.

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