Take the 2-minute tour ×
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 7 down vote accepted

Short Version

  • 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

Longer Explanation

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 it's name beforehand (bpy.context.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 through the spacebar menu, 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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.