2
$\begingroup$

I want to pick the active object in the scene to tweak some settings at this object. A camera.

In Blender 2.79 and lower this could be done by bpy.context.scene.objects.active And it worked fine. But in the 2.8 API they have removed the active. And i get an error now.

enter image description here

So how am i supposed to pick the active object now?

The 2.8 api simply says active is removed. But not how to replace it. The api changes wiki states that it is replaced by context.render_layer.objects.active. But this just throws another error.

https://en.blender.org/index.php/Dev:2.8/Source/LayersCollections/API-Changes

Code that works in Blender 2.79, but not longer in 2.8:

bl_info = {
    "name": "Create My Cam",
    "description": "Creates a camera",
    "author": "",
    "version": (1, 0, 0),
    "blender": (2, 80, 0),
    "location": "add menu",
    "warning": "", # used for warning icon and text in addons panel
    "wiki_url": "",
    "category": "Create"}

import bpy
from bpy.types import Menu

# ----------------------------------------- Adding camera
class CIC_OT_mycam(bpy.types.Operator):
    """Creates a camera"""
    bl_idname = "scene.cic_create_cam"
    bl_label = "My cam"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):

        # ----------------------------Create Camera with correct position and rotation
        bpy.ops.object.camera_add(location=(30.60861, -30.60861, 30.60861), rotation = (0.955324, 0, 0.785398)) # Create Camera. 

        object = bpy.context.scene.objects.active # Pick the active object, our new created camera

        # ------------------------------Here we adjust some settings ---------------------------------
        object.data.type = 'ORTHO' # We want to set the type of the camera to orthographic
        object.data.ortho_scale = 14.123 # Let's fit the camera to a basetile in size of 10
        object.name = "My Cam" # let's rename the cam so that it cannot be confused with other cameras.
        bpy.ops.view3d.object_as_camera() # Set the current camera as the active one to look through

        return {'FINISHED'}

# -------------------------------------------------------------------------------------------

class CIC_MT_cammenu(bpy.types.Menu):
    bl_idname = "CIC_MT_cammenu"
    bl_label = "Create camera"

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

        layout.operator("scene.cic_create_cam", text="My Camera")

classes = (
    CIC_OT_mycam, 
    CIC_MT_cammenu, 
)

def menu_func(self, context):
    self.layout.menu("CIC_MT_cammenu")


def register():
    from bpy.utils import register_class
    for cls in classes:
       register_class(cls)

    bpy.types.VIEW3D_MT_add.append(menu_func)

def unregister():
    from bpy.utils import unregister_class
    for cls in classes:
       unregister_class(cls)

    bpy.types.VIEW3D_MT_add.remove(menu_func)
$\endgroup$
  • 1
    $\begingroup$ bpy.context.active_object should work. $\endgroup$ – Ratt Dec 1 '18 at 11:27
  • $\begingroup$ Cool, this one works too. So two solutions available. Thanks :) $\endgroup$ – Tiles Dec 1 '18 at 11:29
  • $\begingroup$ The problem is, both these solutions wouldn't work if you're trying to make an object active, because both of them are read only! $\endgroup$ – Georges Dec 19 '18 at 14:38
  • $\begingroup$ Yes, but that was not the goal. The goal here is to get the active object to perform the needed code at it and to adjust its settings. $\endgroup$ – Tiles Dec 20 '18 at 7:13
2
$\begingroup$

bpy.context.object seems to still work just fine with an extra bonus of less typing.

You can use the autocomplete function(Ctrl+Space) in the Python Console to find out how things are in 2.80 as well as in previous versions.

$\endgroup$
  • $\begingroup$ Works like charm. Thanks. The python console was unfortunately of no help here since i had no idea which route to go. And doesn't state changes. I wouldn't have thought to look under bpy.context.object here as the replacement. $\endgroup$ – Tiles Dec 1 '18 at 11:26
  • $\begingroup$ The console was how I checked it - I just looked through all the items after bpy.context. , bpy.context.scene., bpy.context.scene.collection. . $\endgroup$ – Martin Z Dec 1 '18 at 12:19
  • $\begingroup$ Will keep this in mind for the next time :) $\endgroup$ – Tiles Dec 1 '18 at 16:45

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.