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"]

]

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