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.
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)
