0
$\begingroup$

I would like to add the prop from Properties > Camera > Focal Length to a panel in the 3D View, but I can't seem to get it right.

class MyPanel(bpy.types.Panel) :
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_label = "My Panel"

    def draw(self, context) :
        TheLayout = self.layout
        TheLayout.prop(context.space_data, "lens")  #Like this, but for the Camera's focal length.

The console suggests:

bpy.context.object.data.lens

And this gives me the value of the function:

bpy.data.cameras[bpy.context.active_object.name].lens

But how do I make a widget with this function in a 3D View panel?

$\endgroup$

1 Answer 1

1
$\begingroup$

I would recommend starting with the templates available. You need to import the blender python module & register your panel.

import bpy
from bpy.types import Panel


class MyPanel(Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'  # properties section 'n'
#    bl_region_type = 'TOOLS'  # tool bar section 't'
#    bl_category = "My Tab"  # add to specific tab
    bl_label = "My Panel"

    def draw(self, context):
        TheLayout = self.layout
        col = TheLayout.column(align=True)
        col.label(text="viewport lens")
        col.prop(context.space_data, "lens")
        col.separator()
        col.prop(context.scene, "camera")
        col.separator()
        col.prop(context.scene.camera.data, "lens")


def register():
    bpy.utils.register_class(MyPanel)


def unregister():
    bpy.utils.unregister_class(MyPanel)


if __name__ == "__main__":
    register()

Edited

  • Removed Loop of all objects to find multiple cameras.
  • Added scene camera prop for local selection in the event of multiple cameras

.

$\endgroup$
2
  • 2
    $\begingroup$ for obj in context.scene.objects may get slow in a big scene. Probably context.scene.camera is enough to draw the property. $\endgroup$
    – dr. Sybren
    Dec 10, 2017 at 9:35
  • $\begingroup$ Thank you @Ratt & dr. Sybren. I wonder how you got to (bpy.context.scene.camera.data, "lens") would work? Which it does! Googling it, this came up(for further reading): docs.blender.org/api/blender_python_api_2_77_0/… $\endgroup$
    – tintwotin
    Dec 10, 2017 at 12:07

You must log in to answer this question.

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