本日は Blender の技術調査枠です。
Blender2.8で利用可能なpythonスクリプトを作ります。
コレクション内のオブジェクトの取得と移動
指定のオブジェクトを指定のコレクションにリンクします。
オブジェクトが他のコレクションにリンクされていた場合は解除されます。
・move_object_collections.py
import bpy
def move_object_collections(arg_objectname="Default", arg_destinationname="Collection") -> bool:
"""指定のオブジェクトを指定のコレクションにリンクする
他のコレクションへのリンクは解除される
Args:
arg_objectname (str, optional): 指定オブジェクト名. Defaults to "Default".
arg_destinationname (str, optional): 移動先コレクション名. Defaults to "Collection".
Returns:
bool: 実行正否
"""
selectob = bpy.data.objects.get(arg_objectname)
if selectob == None:
return False
destinationcollection = bpy.data.collections.get(arg_destinationname)
if destinationcollection == None:
return False
for collection in bpy.data.collections:
checklink = collection.objects.get(selectob.name)
if checklink != None:
collection.objects.unlink(selectob)
destinationcollection.objects.link(selectob)
return True
move_object_collections(arg_objectname="Sphere", arg_destinationname="FirstCollection")
アドオンバージョン
上記スクリプトをアドオン化してみました。
追加されるパネルから対象のオブジェクトと移動先のコレクションを選択し、実行ボタンをクリックします。
すると、オブジェクトが指定のコレクションに移動します。
・Addon_move_object_collections.py
bl_info = {
"name": "HoloMon Move Collection Addon",
"author": "HoloMon",
"version": (1, 0),
"blender": (2, 80, 0),
"support": "TESTING",
"category": "3D View",
"location": "View3D > Sidebar > HoloMon",
"description": "Addon Curve Check",
"location": "",
"warning": "",
"doc_url": "",
}
import bpy
from bpy.types import Operator, Panel, PropertyGroup
from bpy.props import PointerProperty
class HOLOMON_PT_addon_move_collection(Panel):
bl_label = "Move Collection Addon Panel"
bl_idname = "HOLOMON_PT_addon_move_collection"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_options = {'DEFAULT_CLOSED'}
bl_order = 0
bl_category = "HoloMon"
def draw(self, context):
draw_layout = self.layout
select_row = draw_layout.row()
select_row.prop(context.scene.holomon_movecollection, "prop_objectslect", text='')
length_row = draw_layout.row()
length_row.prop(context.scene.holomon_movecollection, "prop_collectionslect", text='')
button_row = draw_layout.row()
button_row.operator("holomon.movecollection")
class HOLOMON_OT_addon_move_collection(Operator):
bl_idname = "holomon.movecollection"
bl_label = "MOVE COLLECTION"
dl_description = "Move Collection Addon Description"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
target_obj = context.scene.holomon_movecollection.prop_objectslect
if target_obj == None:
self.report({'ERROR'}, "No objects selected.")
return {'CANCELLED'}
target_collection = context.scene.holomon_movecollection.prop_collectionslect
if target_collection == None:
self.report({'ERROR'}, "No collection selected.")
return {'CANCELLED'}
move_object_collections(arg_selectobj=target_obj, arg_destinationcollection=target_collection)
return {'FINISHED'}
class HOLOMON_addon_move_collection_properties(PropertyGroup):
def prop_object_select_poll(self, context, ):
if(context and context.type in ('MESH', )):
return True
return False
prop_objectslect: PointerProperty(
name = "Select Object",
type = bpy.types.Object,
description = "",
poll = prop_object_select_poll,
)
prop_collectionslect: PointerProperty(
name = "Select Object",
type = bpy.types.Collection,
description = "",
)
regist_classes = (
HOLOMON_PT_addon_move_collection,
HOLOMON_OT_addon_move_collection,
HOLOMON_addon_move_collection_properties,
)
def register():
for regist_cls in regist_classes:
bpy.utils.register_class(regist_cls)
bpy.types.Scene.holomon_movecollection = PointerProperty(type=HOLOMON_addon_move_collection_properties)
def unregister():
del bpy.types.Scene.holomon_movecollection
for regist_cls in regist_classes:
bpy.utils.unregister_class(regist_cls)
def move_object_collections(arg_selectobj: bpy.types.Object, arg_destinationcollection: bpy.types.Collection) -> bool:
"""指定のオブジェクトを指定のコレクションにリンクする
他のコレクションへのリンクは解除される
Args:
arg_selectobj (bpy.types.Object): 指定オブジェクト
arg_destinationcollection (bpy.types.Collection): 移動先コレクション
Returns:
bool: 実行正否
"""
if arg_selectobj == None:
return False
if arg_destinationcollection == None:
return False
for collection in bpy.data.collections:
checklink = collection.objects.get(arg_selectobj.name)
if checklink != None:
collection.objects.unlink(arg_selectobj)
arg_destinationcollection.objects.link(arg_selectobj)
return True
if __name__ == "__main__":
register()