Skip to content

Instantly share code, notes, and snippets.

@alchem0x2A
Created September 15, 2019 20:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alchem0x2A/bcf6b8c2204c3c1e8624f88b113c4696 to your computer and use it in GitHub Desktop.
Save alchem0x2A/bcf6b8c2204c3c1e8624f88b113c4696 to your computer and use it in GitHub Desktop.
New python snippets Blender 2.8x

Add objects to scene in Blender 2.80

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

Linking collections

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)

Linking objects

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)

Selecting objects

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment