先日はシーン内のすべてのコレクション内のカメラを取得して1つ除いて
〇環境
・Blender4.1
・Windows11PC
〇GUIの実装
GUIとして汎用的に使用するために昨日のコードをまずはアドオン化します。

コード冒頭部にbl_infoを追加します。これはBlenderのアドオンとしての情報を格納するブロックです。
bl_info = {
"name": "HoloMotoutility",
"blender": (3, 0, 0),
"category": "Object",
"version": (1, 0, 0),
"author": "HoloMoto",
"description": "An addon to ensure music21 library and its dependencies are loaded",
"location": "View3D > Tool Shelf",
"warning": "",
"doc_url": "",
}
今回はBlender3.x以上での動作を想定しています。
次にアドオンの登録及び解除のためにregisterを定義します。 ここで定義されるクラスがBlenderで登録されます。
# アドオンの登録
def register():
# アドオンの登録解除
def unregister():
if __name__ == "__main__":
register()
次にオペレータクラスとパネルクラスを定義します。
オペレータークラスとパネルクラスとは、GUIやその操作をカスタマイズするための重要なクラスです。
class OBJECT_OT_delete_non_selected_cameras(bpy.types.Operator):
bl_idname = "object.delete_non_selected_cameras"
bl_label = "Delete Non-Selected Cameras"
bl_description = "Deletes all cameras in the scene except the selected one"
def execute(self, context):
delete_non_selected_cameras()
return {'FINISHED'}
class VIEW3D_PT_holomoto_utility(bpy.types.Panel):
bl_label = "HoloMoto Utility"
bl_idname = "VIEW3D_PT_holomoto_utility"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'HoloMoto'
def draw(self, context):
layout = self.layout
layout.operator("object.delete_non_selected_cameras", text="Delete Non-Selected Cameras")
ここではGUIの定義とボタンを押されたら実行するための関数を定義しています。
これをZipファイルにしてアドオンとして読み込むことでGUIでカメラ削除制御ができるようになります。

本日は以上です。
〇コード全文
bl_info = {
"name": "HoloMotoutility",
"blender": (3, 0, 0),
"category": "Object",
"version": (1, 0, 0),
"author": "HoloMoto",
"description": "An addon to ensure music21 library and its dependencies are loaded",
"location": "View3D > Tool Shelf",
"warning": "",
"doc_url": "",
}
import bpy
# 非選択カメラを削除する関数
def delete_non_selected_cameras():
selected_camera = bpy.context.scene.camera
cameras_to_delete = []
for collection in bpy.data.collections:
for obj in collection.objects:
if obj.type == 'CAMERA' and obj != selected_camera:
cameras_to_delete.append(obj)
for sub_collection in collection.children:
for obj in sub_collection.objects:
if obj.type == 'CAMERA' and obj != selected_camera:
cameras_to_delete.append(obj)
for camera in cameras_to_delete:
bpy.data.objects.remove(camera, do_unlink=True)
print(f"削除したカメラ: {camera.name}")
class OBJECT_OT_delete_non_selected_cameras(bpy.types.Operator):
bl_idname = "object.delete_non_selected_cameras"
bl_label = "Delete Non-Selected Cameras"
bl_description = "Deletes all cameras in the scene except the selected one"
def execute(self, context):
delete_non_selected_cameras()
return {'FINISHED'}
class VIEW3D_PT_holomoto_utility(bpy.types.Panel):
bl_label = "HoloMoto Utility"
bl_idname = "VIEW3D_PT_holomoto_utility"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'HoloMoto'
def draw(self, context):
layout = self.layout
layout.operator("object.delete_non_selected_cameras", text="Delete Non-Selected Cameras")
# アドオンの登録
def register():
bpy.utils.register_class(OBJECT_OT_delete_non_selected_cameras)
bpy.utils.register_class(VIEW3D_PT_holomoto_utility)
# アドオンの登録解除
def unregister():
bpy.utils.register_class(OBJECT_OT_delete_non_selected_cameras)
bpy.utils.register_class(VIEW3D_PT_holomoto_utility)
if __name__ == "__main__":
register()