2023年9月27日水曜日

選択した複数のメッシュについてアーマチュアも含めて個別にFBXとしてエクスポートするBlender Pythonスクリプト

 import bpy


# 選択されたメッシュオブジェクトを取得

selected_meshes = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']


for mesh in selected_meshes:

    # すべてのオブジェクトの選択を解除

    bpy.ops.object.select_all(action='DESELECT')

    

    # メッシュを選択

    mesh.select_set(True)

    

    # メッシュに関連付けられているアーマチュアを探す

    armature = None

    if mesh.parent and mesh.parent.type == 'ARMATURE':

        armature = mesh.parent

        armature.select_set(True)

    

    # アクティブなオブジェクトを設定 (エクスポートの際に必要)

    bpy.context.view_layer.objects.active = mesh

    

    # FBXとしてエクスポート

    bpy.ops.export_scene.fbx(

        filepath=f"path_to_save/{mesh.name}.fbx",

        use_selection=True,

        mesh_smooth_type='FACE',

        bake_anim=False,

        add_leaf_bones=False,

        primary_bone_axis='X',

        secondary_bone_axis='Y',

        global_scale=1.0

    )


2023年9月26日火曜日

BlenderでFBXの一括Export

import bpy

import json


# テキストファイルからエクスポートリストを読み込む

with open("your_filepath/export_list.txt", "r") as file:

    export_list = json.load(file)


# 各オブジェクトグループをFBXとしてエクスポート

for group in export_list:

    # すべてのオブジェクトの選択を解除

    bpy.ops.object.select_all(action='DESELECT')

    

    # グループ内のオブジェクトを選択

    for obj_name in group:

        obj = bpy.data.objects.get(obj_name)

        if obj:

            obj.select_set(True)

    

    # アクティブなオブジェクトを設定 (エクスポートの際に必要)

    bpy.context.view_layer.objects.active = bpy.data.objects.get(group[0])    

    

    # FBXとしてエクスポート

    bpy.ops.export_scene.fbx(

        filepath=f"your_filepath/{group[0]}.fbx",
        #filepath=f"path_to_save/{'_'.join(group)}.fbx",

        use_selection=True,
         # 出力設定色々

        mesh_smooth_type='FACE',

        bake_anim=False,

        add_leaf_bones=False,

        primary_bone_axis='X',

        secondary_bone_axis='Y',

        global_scale=0.01 

    )





----------------------------------------------------------------

[

    ["Mesh1", "Armature1"],

    ["Mesh2", "Mesh3"],

    ["Armature1"]

]

みたいなテキストファイルを読み込んで使用する

2023年8月24日木曜日

Change the texture group of the selected textures.

 import unreal


# エディタ内で選択されたアセットを取得

selected_assets = unreal.EditorUtilityLibrary.get_selected_assets()


for asset in selected_assets:

    if isinstance(asset, unreal.Texture2D):

        # Texture GroupをCharacterに設定

        asset.set_editor_property('LODGroup', unreal.TextureGroup.TEXTUREGROUP_CHARACTER)

        # アセットの変更を保存

        unreal.EditorAssetLibrary.save_loaded_asset(asset)





 #他の値に変更する場合はTEXTUREGROUP_CHARACTERの代わりに以下のEnumを使用する

 #https://docs.unrealengine.com/4.26/en-US/PythonAPI/class/TextureGroup.html

2023年7月24日月曜日

異なるアーマチュアからボーンの名前をコピーする

import bpy

import math


def find_nearest_bone(target_bone, reference_armature):

    min_distance = float('inf')

    nearest_bone_name = None


    for bone in reference_armature.data.bones:

        distance = (bone.head - target_bone.head).length

        if distance < min_distance:

            min_distance = distance

            nearest_bone_name = bone.name


    return nearest_bone_name


# ArmatureAとArmatureBを取得

armature_a = bpy.data.objects.get("ArmatureA")

armature_b = bpy.data.objects.get("ArmatureB")


# ArmatureAがアーマチュアであることを確認

if armature_a and armature_a.type == 'ARMATURE':

    # ArmatureBがアーマチュアであることを確認

    if armature_b and armature_b.type == 'ARMATURE':


        # ArmatureAのボーンに対して処理を行う

        for bone_a in armature_a.data.bones:

            # ArmatureBの中で一番近いボーンを見つける

            nearest_bone_name = find_nearest_bone(bone_a, armature_b)


            # ボーンの名前を変更

            bone_a.name = nearest_bone_name


    else:

        print("ArmatureB is not an armature.")

else:

    print("ArmatureA is not an armature.")

2023年4月12日水曜日

Copies keyframes of specific frame number of selected bones in pose mode to specified frame numbers.

 import bpy


# キーフレームの番号リスト

keyframe_numbers = [1, 2]  # このリストにコピー先のキーフレーム番号を追加してください。


# アクティブなオブジェクトがアーマチュアであることを確認

if bpy.context.object.type == 'ARMATURE':

    armature = bpy.context.object

    action = armature.animation_data.action


    # 選択中のボーンに対して処理を行う

    for bone in armature.pose.bones:

        if bone.bone.select:

            bone_path = f'pose.bones["{bone.name}"].'


            # キーフレーム10の各F-Curveを探索

            for fcurve in action.fcurves:

                if fcurve.data_path.startswith(bone_path):

                    keyframe_10 = None


                    # キーフレーム10の値を取得

                    for keyframe in fcurve.keyframe_points:

                        if keyframe.co[0] == 10:

                            keyframe_10 = keyframe

                            break


                    # キーフレーム10が見つかった場合

                    if keyframe_10 is not None:

                        # 与えられた番号のリストのキーフレームにコピー

                        for kf_number in keyframe_numbers:

                            fcurve.keyframe_points.insert(kf_number, keyframe_10.co[1])


else:

    print("Error: Active object is not an armature.")