blender4.2 ChatGPTに書かせたスクリプトまとめ

自分用アドオンにまとめたいけど時間ないので


ボーンの見た目関連(カスタムシェイプ)

選択中のカスタムシェイプの形状を一括で変える

「XXXX」のところに任意のオブジェクト名を入れる


import bpy

# アーマチュアオブジェクトを取得
armature = bpy.context.object

# カスタムシェイプに使用するオブジェクト名
custom_shape_name = "XXXXXXXXXXXX"  # カスタムシェイプのオブジェクト名

# カスタムシェイプのオブジェクトを取得
custom_shape = bpy.data.objects.get(custom_shape_name)

if armature and armature.type == 'ARMATURE' and custom_shape:
    # 選択中のボーンにカスタムシェイプを設定
    for bone in armature.pose.bones:
        if bone.bone.select:  # 選択されているボーンのみ対象
            bone.custom_shape = custom_shape
            print(f"Custom shape set for bone: {bone.name}")
else:
    print("Error: Armature or Custom Shape Object not found")

選択中のカスタムシェイプのスケールを一括で変える

import bpy

# アーマチュアオブジェクトを取得
armature = bpy.context.object

# 設定したいスケール値 (XYZ 各軸)
custom_shape_scale_xyz = (0.8, 1.0, 2.0)  # 必要に応じて変更

if armature and armature.type == 'ARMATURE':
    # ポーズボーンにアクセス
    for bone in armature.pose.bones:
        if bone.bone.select:  # 選択されているボーンのみ対象
            bone.custom_shape_scale_xyz = custom_shape_scale_xyz
            print(f"Custom shape scale XYZ set for bone: {bone.name}")
else:
    print("Error: No armature selected or active")

選択中のカスタムシェイプ位置を一括で変える

下のどっちか。多分2つめの方だと思う

import bpy

# アーマチュアオブジェクトを取得
armature = bpy.context.object

# 設定したいカスタムシェイプの位置 (XYZ 各軸)
custom_shape_location = (0.0, 0.0, 0.005)  # 必要に応じて変更

if armature and armature.type == 'ARMATURE':
    # ポーズボーンにアクセス
    for bone in armature.pose.bones:
        if bone.bone.select:  # 選択されているボーンのみ対象
            # カスタムシェイプが設定されている場合のみ移動を適用
            if bone.custom_shape:
                if not bone.custom_shape_transform:
                    # トランスフォーム用のボーンを作成
                    bone.custom_shape_transform = bone.bone
                bone.custom_shape_transform.location = custom_shape_location
                print(f"Custom shape location set for bone: {bone.name}")
else:
    print("Error: No armature selected or active")
import bpy

# アーマチュアオブジェクトを取得
armature = bpy.context.object

# 設定したいカスタムシェイプの位置 (XYZ 各軸)
custom_shape_translation = (0.0, 0.0, 0.005)  # 必要に応じて変更

if armature and armature.type == 'ARMATURE':
    # ポーズボーンにアクセス
    for bone in armature.pose.bones:
        if bone.bone.select:  # 選択されているボーンのみ対象
            if bone.custom_shape:  # カスタムシェイプが設定されている場合のみ
                bone.custom_shape_translation[0] = custom_shape_translation[0]  # X軸
                bone.custom_shape_translation[1] = custom_shape_translation[1]  # Y軸
                bone.custom_shape_translation[2] = custom_shape_translation[2]  # Z軸
                print(f"Custom shape translation set for bone: {bone.name}")
else:
    print("Error: No armature selected or active")

mayaから持ってきたデータで、グループノードがエンプティになってるのをエンプティのペアレント一括で外す奴

import bpy

# すべてのメッシュオブジェクトを取得
mesh_objects = [obj for obj in bpy.context.scene.objects if obj.type == 'MESH']

# 選択をクリア
bpy.ops.object.select_all(action='DESELECT')

# 親子関係を解除し、ワールド座標を維持
for mesh in mesh_objects:
    if mesh.parent and mesh.parent.type == 'EMPTY':
        mesh.select_set(True)  # オブジェクトを選択
        bpy.context.view_layer.objects.active = mesh  # アクティブに設定
        bpy.ops.object.parent_clear(type='CLEAR_KEEP_TRANSFORM')  # 親子関係を解除
        mesh.select_set(False)  # 選択を解除

# すべてのエンプティオブジェクトを取得
empty_objects = [obj for obj in bpy.context.scene.objects if obj.type == 'EMPTY']

# エンプティごとにコレクションを作成し、エンプティを削除
for empty in empty_objects:
    collection_name = empty.name  # コレクション名をエンプティ名にする

    # 既存のコレクションがあるか確認し、なければ作成
    if collection_name in bpy.data.collections:
        target_collection = bpy.data.collections[collection_name]
    else:
        target_collection = bpy.data.collections.new(collection_name)
        bpy.context.scene.collection.children.link(target_collection)

    # メッシュを対応するエンプティのコレクションに移動
    for mesh in mesh_objects:
        if mesh.parent == empty:  # もともとエンプティの子だったメッシュを対象
            target_collection.objects.link(mesh)
            bpy.context.scene.collection.objects.unlink(mesh)

    # エンプティを削除
    bpy.data.objects.remove(empty, do_unlink=True)

print(f"処理完了: {len(empty_objects)} 個のエンプティをコレクションに置き換え、メッシュを整理しました。")

ホントは各コレクションにオブジェクトを格納するようにしたいけどそれはまた今度やる


すべてのオブジェクトの細分化モディファイヤの表示非表示を切り替える

#Toggle Subsurf Mods
import bpy

# ONにする場合はTrue、OFFにする場合はFalse
toggle_state = False

# すべてのオブジェクトを選択(デフォルトで選択状態にするわけではなく、全オブジェクトを対象)
for obj in bpy.data.objects:
    # モディファイヤが存在するか確認
    if obj.modifiers:
        for mod in obj.modifiers:
            # 細分化モディファイヤかどうか確認
            if mod.type == 'SUBSURF':
                # ビューポートでの表示切替
                mod.show_viewport = toggle_state
                # レンダリングでの表示切替
                mod.show_render = toggle_state

print(f"細分化モディファイヤを {'ON' if toggle_state else 'OFF'} にしました。")

トグル機能はついてないのでON/OFFは手動で書き替えてください↓
# ONにする場合はTrue、OFFにする場合はFalse
toggle_state =ここを変更

選択中のボーンのボーンコンストレイントを一括で無効にする

import bpy

# 選択中のオブジェクトを取得
obj = bpy.context.object

# 選択中のボーンを取得(ポーズモードで実行)
if obj and obj.type == 'ARMATURE' and obj.mode == 'POSE':
    for bone in obj.pose.bones:
        if bone.bone.select:  # 選択中のボーンのみ処理
            for constraint in bone.constraints:
                constraint.mute = True  # コンストレイントを無効化
    print("選択中のボーンのボーンコンストレイントを無効化しました。")
else:
    print("ポーズモードのアーマチュアを選択してください。")

選択中のコレクション内にあるエンプティをすべて削除する

import bpy

# アクティブなコレクションを取得
view_layer = bpy.context.view_layer
active_collection = view_layer.active_layer_collection.collection  # アクティブなコレクションを取得

if active_collection:
    # コレクション内のエンプティオブジェクトを取得
    objects_to_remove = [obj for obj in active_collection.objects if obj.type == 'EMPTY']
    
    # エンプティを削除
    for obj in objects_to_remove:
        bpy.data.objects.remove(obj, do_unlink=True)

    print(f"コレクション '{active_collection.name}' 内のエンプティを削除しました。")
else:
    print("アクティブなコレクションが見つかりません。")

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