夜風のMixedReality

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

BlenderでパネルUIにEnum型のUIを実装する

本日はBlenderPython枠です。

〇環境

Windows 11PC

Blender 4.1

BlenderのパネルUIでEnam型のUIを選択する

今回はBlenderのUI実装に関してみていきます。

目的としては、自身のアドオン内で設定などを変更したい際に決められたリストから任意に変更できるようにしていきます。

Enum型の定義

 まずはEnum型を定義していきます。

Blenderの場合関数を定義するregisterに定義します。

今回は自身が開発を進めている音楽アドオンを例に見ていきます。

def register():
 ・・・
    bpy.utils.register_class(HarmonyKeysPanel)
    
    # 楽器の選択肢を追加
    bpy.types.Scene.selected_instrument = bpy.props.EnumProperty(
        name="Instrument",
        description="Choose the instrument for the animation",
        items=[
            ('PIANO', "Piano", ""),
            ('TRUMPET', "Trumpet", ""),
        ],
        default='PIANO'
    )
    

これをUIパネル内で使用します。

class HarmonyKeysPanel(bpy.types.Panel):
    bl_label = "HarmonyKeys Panel"
    bl_idname = "PT_HarmonyKeysPanel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'HarmonyKeys'

    def draw(self, context):
        layout = self.layout
   ・・・
        
        # 楽器選択のドロップダウンメニュー
        layout.prop(context.scene, "selected_instrument")
        print(bpy.context.scene.selected_instrument)
        # MusicXMLのパス
        layout.prop(context.scene, "load_musicxml_path")

layout.prop(context.scene, "selected_instrument")によってUIに描画されるようになります。

以上でEnum型の変数を定義できました。