Skip to content

Instantly share code, notes, and snippets.

@blender8r
Created September 18, 2023 13:00
Show Gist options
  • Select an option

  • Save blender8r/87d49f5a0ce8673104c2fbe9cdc949a4 to your computer and use it in GitHub Desktop.

Select an option

Save blender8r/87d49f5a0ce8673104c2fbe9cdc949a4 to your computer and use it in GitHub Desktop.
Blender & Python: Getting and Selecting Objects
###########################################
# SNIPPETS: GETTING AND SELECTING OBJECTS #
# [email protected] #
###########################################
# Get a list of objects in the scene
objects = bpy.data.objects
for object in objects:
print(object.name)
# Get a specific object by name
# (replace text in brackets with the name of the object you're looking for)
cube = bpy.data.objects['Cube']
# Get a list of selected objects
sel = bpy.context.selected_objects
for item in sel:
print(item.name)
# Get camera from list of selected objects
for item in sel:
if item.type == 'CAMERA':
camera = item
# Get a value from the camera's object attributes (in this case, location)
loc = camera.location
# Get a value from the camera's data attributes (in this case, lens)
focal_length = camera.data.lens
# Get a list of all mesh data in the scene
meshes = bpy.data.meshes
for m in meshes:
print(m.name)
print(m.polygons)
# Get a list of non-transform items in the scene
cols = bpy.data.collections
mats = bpy.data.materials
# Select specific objects in the scene (in this case all meshes)
objects = bpy.data.objects
for o in objects:
if o.type == 'MESH':
o.select_set(True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment