<aside> <img src="/icons/list-indent_gray.svg" alt="/icons/list-indent_gray.svg" width="40px" />
目次:
</aside>
Blender内で実行したPySideアプリからBlendrの bpyのコマンドを実行した際に、以下のようなエラーメッセージが出る場合がある。
bpy.ops.wm.usd_import.poll() failed, context is incorrect
Blender内のPySideアプリから bpy.ops を呼び出した際に context の中身が変わってしまうのが原因のようなので、contextをオーバーライドすることで問題を解決できるようだ。
PySideアプリから abc や usd の ファイルをIOする際に、contextをオーバーライドする。
USDのインポート
def context_window(func):
"""
Reference from : <https://blender.stackexchange.com/questions/269960/bpy-context-object-changes-within-pyside2-button-callback>
Support running operators from QT (ex. on button click).
Decorator to override the context window for a function,
"""
def wrapper(*args, **kwargs):
with bpy.context.temp_override(window=bpy.context.window_manager.windows[0]):
return func(*args, **kwargs)
return wrapper
@context_window
def import_usd(filepath: str, scale: float = 0.01):
""" USDファイルをインポート
Reference from:
* <https://devtalk.blender.org/t/issue-with-importing-usd-files-via-bpy-ops-wm-usd-import-and-python/26152>
"""
bpy.ops.wm.usd_import(filepath=filepath, scale=scale)
bpy.context object changes within pyside2 button callback
from pprint import pprint
pprint(dir(bpy.ops.wm))
というようなコードをPySideアプリから実行したり、BlenderのScriptEditorで実行したりしてみると中身が異なっているのが確認できる。同じコマンドでも、コードを実行する場所によってモジュールの中身の構造が変わる仕様らしい。