見出し画像

[Blender][script]リンクで読み込まれたオブジェクトとデータの名前を変更する

標準の仕様では、同じアセットを複数リンクした時に、自動で「.001」「.002」みたいな名前がついて変更不可(追記:標準の「名前を一括変更」から普通に出来ました。普通のリネームだと出来ません)。名前で識別できず不便極まりないのでリネーム出来るスクリプト。をAIに書いてもらいましたが既存のアドオンでも出来ました。
実行前にライブラリオーバーライドが必要です。

https://weisl.gumroad.com/l/simple_renaming


上記でも出来ますが、作ったスクリプトの方が手数が少なくて済むので一応ここに残しておきます。実行するとオブジェクトとデータが同じ名前で置き換わります。

テキストエディタから実行するタイプ

対象を選択して、スクリプト内で名前を設定して実行

import bpy

def rename_selected_objects(new_name="NewObject"):
    for obj in bpy.context.selected_objects:
        # オブジェクト名を変更
        obj.name = new_name
        
        # オブジェクトタイプごとにデータ名を変更
        if obj.type == 'ARMATURE' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'MESH' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'CAMERA' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'LIGHT' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'SPEAKER' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'TEXT' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'CURVE' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'SURFACE' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'FONT' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'EMPTY' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'LIGHT_PROBE' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'VOLUME' and obj.data:
            obj.data.name = new_name

# 実行例
rename_selected_objects(new_name="MyObject")


サイドバーから実行するタイプ

対象を選択してサイドバーの「カスタム」タブから実行実行

import bpy

# オブジェクトの名前を変更する関数
def rename_selected_objects(new_name="NewObject"):
    for obj in bpy.context.selected_objects:
        # オブジェクト名を変更
        obj.name = new_name
        
        # オブジェクトタイプごとにデータ名を変更
        if obj.type == 'ARMATURE' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'MESH' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'CAMERA' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'LIGHT' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'SPEAKER' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'TEXT' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'CURVE' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'SURFACE' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'FONT' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'EMPTY' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'LIGHT_PROBE' and obj.data:
            obj.data.name = new_name
        elif obj.type == 'VOLUME' and obj.data:
            obj.data.name = new_name

# パネルの作成
class RenameObjectsPanel(bpy.types.Panel):
    bl_label = "Rename Selected Objects"
    bl_idname = "OBJECT_PT_rename_objects"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Custom'  # タブ名をCustomに設定

    def draw(self, context):
        layout = self.layout

        # 名前入力用のテキストフィールド
        layout.prop(context.scene, "new_object_name", text="New Name")

        # 実行ボタン
        layout.operator("object.rename_objects_operator", text="Rename Objects")

# 演算子の作成
class RenameObjectsOperator(bpy.types.Operator):
    bl_idname = "object.rename_objects_operator"
    bl_label = "Rename Objects Operator"

    def execute(self, context):
        new_name = context.scene.new_object_name  # 入力された名前を取得
        rename_selected_objects(new_name)
        return {'FINISHED'}

# プロパティの登録
def register():
    bpy.utils.register_class(RenameObjectsPanel)
    bpy.utils.register_class(RenameObjectsOperator)
    bpy.types.Scene.new_object_name = bpy.props.StringProperty(name="New Object Name", default="New Name")

# プロパティの解除
def unregister():
    bpy.utils.unregister_class(RenameObjectsPanel)
    bpy.utils.unregister_class(RenameObjectsOperator)
    del bpy.types.Scene.new_object_name

if __name__ == "__main__":
    register()

いいなと思ったら応援しよう!