オペレーター

公式サイト:

Operator | Blender Python API [Official]

使い方

基底メソッド
  • execute
  • invoke
  • modal
  • report
派生メソッド
  • cancel
  • draw
クラスメソッド
  • description
  • poll
基本

最小限の動作サンプル

import bpy class EXAMPLE_OT_hello(bpy.types.Operator): bl_idname = 'example.hello' bl_label = "Hello Operator" def execute(self, context): print("Hello, Operator") return {'FINISHED'}
Code language: Python (python)

ドキュメント:

Operators (bpy.ops) | Blender Python API [Official]

参考:

What do operator methods do? (poll, invoke, execute, draw & modal) – Blender Stack Exchange

オプション (bl_options)

  • REGISTER処理の実行を「情報」 (Info) エディターに表示する。また、「最後の操作を調整」 (Adjust Last Operation) パネルを有効にする。
  • UNDOオペレーターの処理をアンドゥスタックに追加する。
  • UNDO_GROUPEDアンドゥ処理をグループ化する。
  • BLOCKINGオペレーターの処理においてカーソルの使用を禁止する。
  • MACROオペレーターがマクロ処理であるかを確認する。
  • GRAB_CURSORカーソルを使用する。カーソル移動を画面端で折り返す。
  • GRAB_CURSOR_Xカーソルを使用する。X軸方向のみ、カーソル移動を画面端で折り返す。
  • GRAB_CURSOR_Yカーソルを使用する。Y軸方向のみ、カーソル移動を画面端で折り返す。
  • DEPENDS_ON_CURSORカーソルの初期位置を使用する。メニューあるいはボタンから開始した場合、初期位置を指定するようユーザーに求める。
  • PRESETオペレーターの設定にプリセットを使用する。
  • INTERNAL内部処理用のオペレーターとして指定する。「メニュー検索」 (Menu Search) で表示しない。
デフォルト
bl_options = {'REGISTER'}
Code language: Python (python)

ドキュメント:

bpy.types.Operator.bl_options | Blender Python API [Official]

モーダル実行/返り値

処理が終了した場合は 'FINISHED' をセットに含めて返す。

return {'FINISHED'}
Code language: Python (python)

処理がキャンセルされた場合は 'CANCELLED' をセットに含めて返す。

return {'CANCELLED'}
Code language: Python (python)
こちらのページを参照

ドキュメント:

Modal Execution | Blender Python API [Official]

参考:

custom end a modal operator – Stack Overflow

アンドゥを有効化する

オペレータークラスの bl_options'UNDO' を含める。

class MYTOOL_OT_xxx(bpy.types.Oerator): # ... bl_options = {'REGISTER', 'UNDO'}
Code language: Python (python)

ドキュメント:

bl_options – Operator | Blender Python API [Official]

参考:

Undo while in modal mode – Blender Artists Community

Addon Operators and Undo support – Blender Developer Talk

as_keywords

ドキュメント:

Operator.as_keywords | Blender Python API [Official]

プロパティを渡す

オペレータークラスでプロパティを定義し、パネルでボタン生成時にプロパティの値を渡す。

import bpy from bpy.props import FloatProperty class MYTOOL_OT_hoge_xxx(bpy.types.Oerator): bl_idname = 'mytool.hoge_xxx' # ... length: FloatProperty( name="Length", description="Length for Hoge", default=1.0f) # ... class MYTOOL_PT_hoge_xxx_panel(): # ... def draw(self, context): layout = self.layout layout.operator("mytool.hoge_xxx", text="Hoge by 2").length = 2.0f layout.operator("mytool.hoge_xxx", text="Hoge by 3").length = 3.0f
Code language: Python (python)
こちらのページを参照

ドキュメント:

Operator Example – Property Definitions | Blender Python API [Official]

参考:

アドオン開発で複数ボタンを1つの Operator クラスで作成する | panda_nakami

How to access variable of other user class? – Stack Overflow

How to pass variable (tuple) to operator invoke – Blender Stack Exchange

モーダル内でキー入力を受け付ける

参考:

キーボードのイベントを扱う | はじめての Blender アドオン開発

Using a key modifier with modal – Stack Overflow

Modal Operator Fixes – Blender Stack Exchange

モーダル内でポップアップを表示する

参考:

Dialog Box (slider) with Modal Execution – Blender Stack Exchange

パネルにボタンを設置する

class EXAMPLE_PT_xxx(bpy.types.Panel): # ... def draw(self, context): layout = self.layout layout.operator('object.transform_apply', text="Apply All Transforms")
Code language: Python (python)
こちらのページを参照

ドキュメント:

bpy.types.UILayout.operator | Blender Python API [Official]

オペレーターを拡張する

参考:

Get current value of built-in modal operator – Blender Artists Community

How to extend an operator – Blender Stack Exchange

Prevent accidental deletion of object – Blender Stack Exchange

Object delete handler – Blender Stack Exchange

Save blender files with lowercase – Blender Stack Exchange

How to re-register a built-in operator? – Blender Stack Exchange

Access active operator during its execution – Blender Developer Talk

登録を判定する

参考:

How to know if an operator is registered? – Blender Artists Community

コンテキスト

※ 3.2 以降、コンテキストをオーバーライドする方法が変更された。

ドキュメント:

bpy.types.Context.temp_override | Blender Python API [Official]

参考:

RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect (281913) – Blender Stack Exchange

I got “RuntimeError: Operator bpy.ops.mesh.select_mode.poll() failed, context is incorrect” – Blender Stack Exchange

仕組み

参考:

オペレータ | COLORFUL PICO

サンプル

Example Operator | Blender Python API [Official]

context is incorrect エラー

現象:

オペレーターを実行する際に、コンテキストが不適切なため、poll メソッドが失敗した旨のラインタイムエラーが発生する。

RuntimeError: Operator bpy.ops.xxx.yyy.poll() failed, context is incorrect

ドキュメント:

Why does an operator’s poll fail? | Blender Python API [Official]

参考:

指定種別のエリアコンテキストの参照を取得する | MR が楽しい

poll() failed, context incorrect? – Blender Stack Exchange

RuntimeError: Operator bpy.ops.object.convert.poll() failed, context is incorrect (275149) – Blender Stack Exchange

RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect (281913) – Blender Stack Exchange

I got “RuntimeError: Operator bpy.ops.mesh.select_mode.poll() failed, context is incorrect” – Blender Stack Exchange

How do I fix bpy.ops.object.mode_set.poll() failed, context is incorrect? – Blender Stack Exchange

記事をシェアする:
タグ:

コメントを残す

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

Protected by reCAPTCHA