Blender 2.80 introduces a new feature of collections, which (in my opinion) is a very important for various purposes like grouping objects, selectively enable / disable in view port, etc.
Accordingly, the codes also needs to be updated in 2.80 for adding an object to the active scene. From the documentation (https://wiki.blender.org/wiki/Reference/Release_Notes/2.80/Python_API/Scene_and_Object_API),
The top-level “root” collection is bpy.context.scene.collection
. You
can create children collections use:
# Create new collection
col = bpy.data.collections.new("Collection")
# The collection hasn't been added as children of the root collection
# Need to do this step to be visible in outline
root_col = bpy.context.scene.collection
root_col.children.link(col)
Objects are within the objects
instances of each collection. Add an
object to the collection by:
# data is some sort of mesh data
obj = bpy.data.objects.new("Obj", data)
# col is the collection we created before
col.objects.link(obj)
Use the new select_set
method of an object:
ob = bpy.context.object
if not ob.select_get():
ob.select_set(True)
Another interesting method is to select the whole collection and make operations like move, rotate and scale on all the children objects. This is way easier than the group methods in Blender 2.7x.
Select all objects in outliner can be called from python script:
bpy.ops.outliner.collection_objects_select()