Blender 3D:菜鳥到專業/高階教程/Blender 指令碼/為您的外掛新增使用者介面
|
|
適用的 Blender 版本:2.68。 |
因此您已經在外掛中定義了一個新的運算子。現在,為該外掛提供一個合適的介面會很不錯,這樣使用者就不必在空格選單中查詢該運算子了。
執行此操作的最佳方法是定義一個面板,該面板顯示在某個視窗中,其中包含使用者可以單擊以操作您的外掛的控制元件。您可以將面板放置在各種位置,但在這裡我們將其插入工具架(可以透過按 T 來隱藏或顯示在 3D 檢視的左側)。
面板是透過對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"
這三個屬性會使面板顯示在工具架中,但僅在 3D 檢視處於物件模式時才會顯示。bl_category 行確定外掛所在的工具欄選項卡,並且僅適用於帶有選項卡的工具欄(2.7 版本新增)。指定任何現有的選項卡,或定義一個新的選項卡。
您的類還需要定義一個draw方法,該方法定義面板中的專案。此示例為面板建立一個新的 UI 元素列,並插入一個 UI 元素,該元素是一個按鈕,單擊該按鈕將呼叫您之前在定義時定義的運算子。
def draw(self, context):
TheCol = self.layout.column(align=True)
TheCol.operator("mesh.make_tetrahedron", text="Add Tetrahedron")
#end draw
趁此機會,返回並向運算子定義新增以下行
bl_options = {"UNDO"}
這允許使用者以通常的方式使用 CTRL + Z 撤銷四面體的新增,並使用 CTRL + SHIFT + Z 重新執行該操作。
以下是外掛的完整指令碼,它現在處於執行狀態
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)
請注意為我們的自定義面板添加了另一個 register_class 呼叫。
如前所述,按 ALT + P 執行它。似乎什麼也沒發生;Blender 會處理您的子類定義,並根據請求將它們註冊到相應的位置。
但現在,返回到 3D 檢視。確保您處於物件模式。透過按 T 顯示工具架(如果它不可見)。在底部,您指定的選項卡中,您應該看到您的新面板出現

請注意標題旁邊的三角形,Blender 會自動為您提供它,以在不進行任何額外操作的情況下摺疊或展開面板。刪除上一頁教程中可能建立的任何四面體物件;現在單擊新的“新增四面體”按鈕,然後觀察物件再次被建立!
這些值bl_space_type, bl_region_type和bl_context在 此處 的 Blender API 文件中(部分)列出,但沒有完全解釋。以下是我透過檢視原始碼找到的每個屬性的允許值列表
| 屬性 | 允許的值 | 源引用 |
|---|---|---|
| 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在rna_space.c |
| bl_region_type | "WINDOW", "HEADER", "CHANNELS", "TEMPORARY", "UI", "TOOLS", "TOOL_PROPS", "PREVIEW" | region_type_items在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在context.c |
可以推測,並非所有組合都有意義。