When accessing items in a python script somethings are prefixed with bpy.data
some with bpy.context
and others with bpy.ops
What is the difference between these prefixes?
bpy.data
- all data in your blend file.bpy.context
- data in the current active view.bpy.ops
- tools which typically operate on bpy.context
bpy.data
gives you access to all data in the blend file, every object, material, compositing node etc. in every scene (also the scenes themselves) can be accessed through bpy.data
.
Generally, you would either loop over all datablocks, or get a specific item by name.
bpy.context
is context sensitive, it depends on the area your mouse is over, the selections you make and so on. It lists only a subset of data
such as the current scene, the active object etc. This is an easy way to have a script work on whatever object is selected rather than having to know its name beforehand (bpy.context.active_object
refers to the active object, or is None
if there is no active object, bpy.context.selected_objects
is a list of the selected objects).
bpy.ops
is where you can access tools (operators) organized in categories, e.g. bpy.ops.object.select_all()
(object
is the category for object-related operations, together with select_all
it is the operator name and ()
calls it). These perform the actions typically accessed in the user interface, referenced by menu items and key shortcuts. When you search using F3, you are seeing a list of all operators, which are available in the current context.
These three prefixes are the most common but there are others. bpy.types
contains all the types or "classes" used within Blender. You will also use bpy.utils
if you write an addon (mainly to register and unregister classes derived from bpy.types, e.g., to create own operators, menus, panels...). There are also some other modules available that aren't prefixed with bpy
such as mathutils
and bmesh
.
For a list of all items available in Python you should look at Blenders Python API documentation.