スポンサーリンク

Blender Python Addonのチェックボックスの追加とプロパティへのアクセス

Blender PythonでUIにGUI部品を追加するときは、「チェックボックス」「エディットボックス」として追加するのではなく、

「String型のプロパティ」を登録するとエディットボックスとして表示され、

「Bool型のプロパティ」を登録するとチェックボックスとして表示される。

import bpy

#テキストボックスを定義
class myCInputs(bpy.types.PropertyGroup):
    myTextField: bpy.props.StringProperty(
        name="テキスト",
        description="説明文",
        default="",
        maxlen=1024, # 最大文字数
        subtype="NONE"
    )
    myCheckField: bpy.props.BoolProperty(
        name="チェック",
        default=True
    )
class myCTool_PT_panel(bpy.types.Panel):
    bl_label = "パネルタイトル"
    bl_category = "タブタイトル"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

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

        #横に並べるならrow.propを使う
        # row = layout.row()

        layout.prop(context.scene.myinputs, "myTextField")
        layout.prop(context.scene.myinputs, "myCheckField")

classes = (
    myCInputs,
    myCTool_PT_panel
)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)

    bpy.types.Scene.myinputs = bpy.props.PointerProperty(type=myCInputs)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    del bpy.types.Scene.myinputs

if __name__ == "__main__":
    register()

値の取得方法

import bpy

# ボタンを定義
class execButton(bpy.types.Operator):
  bl_idname = "szl.button"
  bl_label = "access to property"
  
  def execute(self, context):
    print( context.scene.myinputs.myTextField)# 他のGUI部品の値を取得
    return{'FINISHED'}
# ボタン以外のUIを定義
class myCInputs(bpy.types.PropertyGroup):
    myTextField: bpy.props.StringProperty(
        name="テキスト",
        description="説明文",
        default="",
        maxlen=1024, # 最大文字数
        subtype="NONE"
    )
    
    myCheckField: bpy.props.BoolProperty(
        name="チェック",
        default=True
    )
class myCTool_PT_panel(bpy.types.Panel):
    bl_label = "パネルタイトル"
    bl_category = "タブタイトル"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

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

        #横に並べるならrow.propを使う
        # row = layout.row()

        layout.prop(context.scene.myinputs, "myTextField")
        layout.prop(context.scene.myinputs, "myCheckField")
        layout.operator("szl.button")
classes = (
    myCInputs,
    execButton,
    myCTool_PT_panel,
)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)

    bpy.types.Scene.myinputs = bpy.props.PointerProperty(type=myCInputs)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    del bpy.types.Scene.myinputs

if __name__ == "__main__":
    register()

2 件のコメント

プロパティの使い方 (Property / bpy.props) [Blender] – Site-Builder.wiki へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)


この記事のトラックバックURL: