2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Blenderで単純なオペレーターを実行させるパネルを作るスクリプト

Posted at

Blenderのスクリプトを作っている時に
UIのパネルからオペレータを実行した場合に得られるcontextが単純にスクリプトを使った場合と異なるので
それを調べるための 単純なオペレータを実行させるスクリプト
(実行しているコンテキストということでAriaやその下のSpace関連の情報が少し違ってくる)

さほど難しくないだけに 何度か書き捨ててしまっているので
試しにGrockに生成させてみた

import bpy

# カスタムオペレーターの定義
class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"  # 一意のID
    bl_label = "Simple Operator"          # ボタンに表示される名前
    bl_description = "A simple operator example"  # ツールチップ

    def execute(self, context):
        # ここでオペレーターの処理を記述
        self.report({'INFO'}, str(context.space_data.camera))
        return {'FINISHED'}

# 3Dビューポートにパネルを追加
class View3DPanel(bpy.types.Panel):
    bl_label = "Custom Panel"            # パネルのタイトル
    bl_idname = "VIEW3D_PT_custom_panel" # 一意のID
    bl_space_type = 'VIEW_3D'            # 3Dビューポートに表示
    bl_region_type = 'UI'                # UI領域(Nパネル)に表示
    bl_category = 'My Panel'             # パネルのタブ名

    def draw(self, context):
        layout = self.layout
        # ボタンを追加
        layout.operator("object.simple_operator", text="Run Simple Operator")

# 登録関数
def register():
    bpy.utils.register_class(SimpleOperator)
    bpy.utils.register_class(View3DPanel)

# 解除関数
def unregister():
    bpy.utils.unregister_class(View3DPanel)
    bpy.utils.unregister_class(SimpleOperator)

# スクリプトを直接実行する場合
if __name__ == "__main__":
    register()

この長さなので何が書いても変わりようがないけれど
適切にコメントが付くのは楽ですね
メモ程度に。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?