Blender 3D:第四单元:与高级教程起飞
Start
::开始
So you’ve defined a new operator in your addon. Now it would be nice to give that addon a proper interface, so the user doesn’t have to hunt through the spacebar menu to find that operator.
::现在给这个附加程序一个适当的界面是很好的,这样用户就不必在空格菜单中搜索这个操作符了.
The nicest way to do this is to define a
panel
that shows up in a window somewhere, with controls in it that the user can click on to operate your addon. You can put the panel in all kinds of places, but here we will insert it in the Tool Shelf (which can be hidden or shown on the left side of the 3D View by pressing T .
::您可以将面板放在各种各样的位置,但在这里我们将它插入到工具架 (可以通过按T键隐藏或显示在3D视图的左侧).
Your First Panel
::您的第一个面板
A panel is defined by subclassing the
bpy.types.Panel
class. You set the value of various attributes (
bl_space_type
,
bl_region_type
,
bl_category
and
bl_context
) to determine the context in which the panel will appear, and also give a title to the panel (
bl_label
):
::通过 bpy.types.Panel 类的子类定义一个面板.您设置各种属性的值 (bl_space_type,bl_region_type,bl_category 和 bl_context) 来确定面板将出现的上下文,并给面板一个标题 (bl_label):
class TetrahedronMakerPanel(bpy.types.Panel): bl_space_type = "VIEW_3D" bl_region_type = "TOOLS" bl_context = "objectmode" bl_category = "Create" bl_label = "Add Tetrahedron"
Those first three attributes cause the panel to appear in the Tool Shelf, but only while the 3D View is in Object mode.The bl_category line determines the toolbar tab the addon is placed in, and only applies to the toolbars with tabs (added 2.7). Specify any existing tab, or define a new one.
::这三种属性使面板在工具架中显示,但只有在3D视图处于对象模式时.bl_category行确定了插件放置的工具选项卡,并且仅适用于带有选项卡的工具 (添加2.7).指定任何现有的选项卡,或定义一个新的选项卡.
Your class also needs to define a
draw
method, which defines the items that go into the panel. This example creates a new column of UI elements for the panel, and inserts a single UI element which is a button that will invoke the operator with the name you previously defined when clicked:
::您的类还需要定义一个draw方法,它定义了进入面板的项目. 这个例子创建了一个面板的UI元素的新列,并插入一个单一的UI元素,它是一个按,它会在点击时调用操作符,使用您之前定义的名称:
def draw(self, context): TheCol = self.layout.column(align=True) TheCol.operator("mesh.make_tetrahedron", text="Add Tetrahedron") #end draw
Adding Undo Support
::添加撤销支持
While we’re at it, go back and add the following line to the operator definition:
::现在我们可以回到操作符定义中,
bl_options = {"UNDO"}
This allows the user to undo the addition of the tetrahedron in the usual way with CTRL + Z , and redo it with CTRL + SHIFT + Z .
::这允许用户以CTRL+Z的方式撤销四面体的加法 ,并用CTRL+SHIFT+Z重新加上它.
Put It All Together
::让所有这些东西都结合起来
Here is the complete script for the addon as it stands now:
::以下是目前的附加程序的完整脚本:
import math import bpy import mathutils class TetrahedronMakerPanel(bpy.types.Panel): bl_space_type = "VIEW_3D" bl_region_type = "TOOLS" bl_context = "objectmode" bl_category = "Create" bl_label = "Add Tetrahedron" def draw(self, context): TheCol = self.layout.column(align=True) TheCol.operator("mesh.make_tetrahedron", text="Add Tetrahedron") #end draw #end TetrahedronMakerPanel class MakeTetrahedron(bpy.types.Operator): bl_idname = "mesh.make_tetrahedron" bl_label = "Add Tetrahedron" bl_options = {"UNDO"} def invoke(self, context, event): Vertices = \ [ mathutils.Vector((0, -1 / math.sqrt(3),0)), mathutils.Vector((0.5, 1 / (2 * math.sqrt(3)), 0)), mathutils.Vector((-0.5, 1 / (2 * math.sqrt(3)), 0)), mathutils.Vector((0, 0, math.sqrt(2 / 3))), ] NewMesh = bpy.data.meshes.new("Tetrahedron") NewMesh.from_pydata \ ( Vertices, [], [[0, 2, 1], [0, 1, 3], [1, 2, 3], [2, 0, 3]] ) NewMesh.update() NewObj = bpy.data.objects.new("Tetrahedron", NewMesh) context.scene.objects.link(NewObj) return {"FINISHED"} #end invoke #end MakeTetrahedron bpy.utils.register_class(MakeTetrahedron) bpy.utils.register_class(TetrahedronMakerPanel)
Note the addition of another
register_class
call for our custom panel.
::另一个 register_class 调用为我们的自定义面板.
As before, hit ALT + P to execute it. Nothing should appear to happen; Blender processes your subclass definitions, and registers them for use, as requested, in the appropriate places.
::像之前一样,按ALT+P执行它. 似乎什么都不会发生; 混合器处理你的子类定义,并将它们注册用于适当的地方,如要求.
But now, go back to the 3D View. Make sure you are in Object mode. Bring up the Tool Shelf if it isn’t visible, by pressing T . At the bottom; in the tab you have specified, you should see your new panel appear:
::现在,回到3D视图. 确保您处于对象模式. 如果没有可见的工具架,请按T. 在底部; 在您指定的选项卡中,您应该看到您的新面板出现:

Note the triangle next to the title that Blender automatically gives you, to collapse or expand the panel without any extra work on your part. Get rid of any tetrahedron object that might have been created by following the tutorial on the previous page; now click your new “Add Tetrahedron” button, and watch the object be created again!
::标题旁边的三角形是 Blender 自动给你的,可以在没有任何额外工作的情况下缩小或扩展面板. 通过上一页的教程来删除任何可能被创建的四面体对象; 现在点击您的新"添加四面体"按,并观看该对象再次被创建!
Space Types, Region Types, Contexts, Oh My!
::空间类型,区域类型,背景,!
Those values for
bl_space_type
,
bl_region_type
and
bl_context
are (partially) listed
here
in the Blender API documentation, but not fully explained. Here’s a list of permissible values for each that I found from looking at source code:
::这些bl_space_type,bl_region_type和bl_context的值在Blender API文档中 (部分) 列出,但没有完全解释. 以下是我从源代码中找到的每个允许值的列表:
Attribute | Permissible Values | Source Reference |
---|---|---|
bl_space_type | "EMPTY", "VIEW_3D", "TIMELINE", "GRAPH_EDITOR", "DOPESHEET_EDITOR", "NLA_EDITOR", "IMAGE_EDITOR", "SEQUENCE_EDITOR", "CLIP_EDITOR", "TEXT_EDITOR", "NODE_EDITOR", "LOGIC_EDITOR", "PROPERTIES", "OUTLINER", "USER_PREFERENCES", "INFO", "FILE_BROWSER", "CONSOLE" | space_type_items in rna_space.c |
bl_region_type | "WINDOW", "HEADER", "CHANNELS", "TEMPORARY", "UI", "TOOLS", "TOOL_PROPS", "PREVIEW" | region_type_items in rna_screen.c |
bl_context | "mesh_edit", "curve_edit", "surface_edit", "text_edit", "armature_edit", "mball_edit", "lattice_edit", "posemode", "sculpt_mode", "weightpaint", "vertexpaint", "imagepaint", "particlemode", "objectmode" | data_mode_strings in context.c |
Presumably, not all combinations will make sense.
::假设,并非所有组合都会有意义.