blender 中常用的3个接口context, ops,data

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.

发表评论

邮箱地址不会被公开。 必填项已用*标注