夜風のMixedReality

xRと出会って変わった人生と出会った技術を書き残すためのGeekなHoloRangerの居場所

Blenderでビューポートに自作のタブを追加する  チェックボックス

本日は昨日に引き続きBlenderの調査を行います。

昨日はアドオンなどでよくみられるUIからスクリプトの関数を実行できるウィンドウを作りました。

これは

〇チェックボックス

前回は以下のようなコードでタブを実行しました。

import bpy

# メニューを定義するクラス
class CustomPanel(bpy.types.Panel):
    bl_idname = "OBJECT_PT_custom_panel"
    bl_label = "Custom Panel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Custom'

    def draw(self, context):
        layout = self.layout

        # パネル内にボタンを追加する例
        layout.operator("mesh.primitive_cube_add", text="Add Cube")

# アドオンを有効化したときに呼び出される処理
def register():
    bpy.utils.register_class(CustomPanel)

# アドオンを無効化したときに呼び出される処理
def unregister():
    bpy.utils.unregister_class(CustomPanel)

# スクリプトを実行するときに呼び出される処理
if __name__ == "__main__":
    register()

これはlayout.operator()によってボタンを表示させています。

ここをチェックボックスにするためにはlayout.prop(context.scene, "my_checkbox_property", text="My Checkbox")を使用します。

layout.prop()は、BlenderのUIにBool型プロパティを表示するための関数です。

これを使用することでチェックボックスが表示できました。

〇コード

import bpy

class CustomPanel(bpy.types.Panel):
    bl_idname = "OBJECT_PT_custom_panel"
    bl_label = "Custom Panel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Custom'

    def draw(self, context):
        layout = self.layout

        # チェックボックスを追加
        layout.prop(context.scene, "my_checkbox_property", text="My Checkbox")

# アドオンを有効化したときに呼び出される処理
def register():
    bpy.types.Scene.my_checkbox_property = bpy.props.BoolProperty(name="My Checkbox Property")
    bpy.utils.register_class(CustomPanel)

# アドオンを無効化したときに呼び出される処理
def unregister():
    bpy.utils.unregister_class(CustomPanel)
    del bpy.types.Scene.my_checkbox_property

# スクリプトを実行するときに呼び出される処理
if __name__ == "__main__":
    register()