PyR3.shortcut

All files used as examples are contained in this repository, under corresponding relative paths.

This subpackage contains useful abstractions and shortcuts over blender API, which are used multiple times in other parts of PyR3, but it is mend also for public access and use.

Manipulating selection and active object

examples/shortcut/context/selection.py
# -*- coding: utf-8 -*-
from PyR3.shortcut.context import Objects
from PyR3.shortcut.mesh import addCube

# Deselects contents of current scene: default cube, light source and camera
Objects.deselect_all()
# Create new cube, uses shortcut from mesh module
cube = addCube()
# Select single object - our newly created cube
Objects.select(cube)
# prints Objects[bpy.data.objects['Cube.001']]
print(Objects.selected)

# selects everything in scene
Objects.select_all()
# Now Objects.selected is a longer sequence:
# Objects[bpy.data.objects['Cube'], bpy.data.objects['Light'],...]
print(Objects.selected)

# Lets now see whats currently active:
print(Objects.active)  # <bpy_struct, Object("Cube.001") at 0x4b39578>
# We can change it to light source existing in scene:
light_source = Objects.selected[1]
Objects.active = light_source
print(Objects.active)  # <bpy_struct, Object("Light") at 0x43a19b8>

# We can also use contents of Object.selected to alter selection:
selected = Objects.selected
selected.pop()
selected.pop(0)
selected.select_only_contained()
# Now it will print only Objects[bpy.data.objects['Light'], bpy.data.objects['Camera']]
print(Objects.selected)

Using temporary selection

examples/shortcut/context/temporary_selection.py
# -*- coding: utf-8 -*-
from PyR3.shortcut.context import Objects, temporarily_selected

print(Objects.selected)  # Objects[bpy.data.objects['Cube']]
cube = Objects.selected[0]

with temporarily_selected(*Objects.all()):
    print(Objects.selected)
    # Objects[bpy.data.objects['Cube'], bpy.data.objects['Light'], bpy.data.objects['Camera']]

print(Objects.selected)  # Objects[bpy.data.objects['Cube']]

Managing scenes

examples/shortcut/context/scene.py
# -*- coding: utf-8 -*-
from PyR3.shortcut.context import (
    delScene,
    getScene,
    listScenes,
    newScene,
    setScene,
    wipeScenes,
)

# First lets see our current scene:
print(getScene())  # <bpy_struct, Scene("Scene") at 0x41a0828>
# Now lets create new one:
newScene()
# And see list of all existing scenes
print(listScenes())  # [bpy.data.scenes['Scene'], bpy.data.scenes['Scene.001']]
# Then we can destroy _all_ of them and use new empty scene
wipeScenes()
print(listScenes())  # [bpy.data.scenes['Scene.001']]
# You can also manually set current scene:
old_scene = getScene()
newScene()
new_scene = getScene()
print(getScene() == new_scene)  # True
setScene(old_scene)
print(getScene() == old_scene)  # True
# deletes current scene
delScene()
print(getScene() == new_scene)  # True