先日Pythonを使用した任意のショートカットキーで変更可能な処理と面の向きを可視化する実装を行いました。
今回はこの二つを組み合わせてオリジナルのアドオンを創っていきます。
〇環境
・Windows11PC
・Blender4.1
〇面の向きをショートカットで有効化、無効化する
前回の任意のショートカットキーで実行可能な処理の実装ではショートカット器を押すときにオペレータが実行され、コンソールログに出力されるようになっていました。

またその前に面の向きを有効化、無効かするにはbpy.context.space_data.overlay.show_face_orientationを使用することで可能でした。
この二つを統合します。
以下のようなコードになります。
import bpy
# オペレーターを定義
class VIEW3D_OT_ToggleFaceOrientation(bpy.types.Operator):
bl_idname = "view3d.toggle_face_orientation"
bl_label = "Toggle Face Orientation"
bl_description = "Toggle face orientation visibility in the 3D Viewport"
def execute(self, context):
# 3Dビューエリアを見つける
area = next((area for area in bpy.context.window.screen.areas if area.type == 'VIEW_3D'), None)
if area:
for space in area.spaces:
if space.type == 'VIEW_3D':
# 面の向きの現在の状態を取得し、切り替える
space.overlay.show_face_orientation = not space.overlay.show_face_orientation
status = "enabled" if space.overlay.show_face_orientation else "disabled"
self.report({'INFO'}, f"Face Orientation visualization {status}.")
return {'FINISHED'}
# 3Dビューポートのメニューに登録するクラス
class VIEW3D_PT_FaceOrientationPanel(bpy.types.Panel):
bl_label = "Face Orientation Tools"
bl_idname = "VIEW3D_PT_face_orientation_panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Face Orientation"
def draw(self, context):
layout = self.layout
layout.label(text="Face Orientation Options:")
layout.operator("view3d.toggle_face_orientation", text="Toggle Face Orientation")
def register():
bpy.utils.register_class(VIEW3D_OT_ToggleFaceOrientation)
bpy.utils.register_class(VIEW3D_PT_FaceOrientationPanel)
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
if kc:
km = kc.keymaps.new(name="3D View", space_type='VIEW_3D')
km.keymap_items.new(
idname=VIEW3D_OT_ToggleFaceOrientation.bl_idname,
type='O',
value='PRESS',
ctrl=True
)
def unregister():
bpy.utils.unregister_class(VIEW3D_OT_ToggleFaceOrientation)
bpy.utils.unregister_class(VIEW3D_PT_FaceOrientationPanel)
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
if kc:
km = kc.keymaps.get("3D View")
if km:
for kmi in km.keymap_items:
if kmi.idname == VIEW3D_OT_ToggleFaceOrientation.bl_idname:
km.keymap_items.remove(kmi)
if __name__ == "__main__":
register()
このコードを実行することでCtrl+Oキーで面の向きを可視化することができます。
本日は以上です。