Blender バッチはすべてのオブジェクトを単一の glTF ファイルにエクスポートします

WebGL 3D エンジンが読み込む単一ファイルを最小化するために、単一の建物やオブジェクトを glTF ファイルにエクスポートする必要がある場合がありますが、手動で 1 つずつエクスポートするのは面倒なので、バッチでエクスポートできるスクリプトを書きました。

 

まず、プロジェクトを .blender プロジェクト ファイルとして保存する必要があります。次に、[スクリプト] メニューで新しいスクリプトを作成し、次のスクリプトを貼り付けて実行します。

ただし、デフォルトのカメラとキューブはシーン コレクションで削除する必要があることに注意してください。削除しないと、ファイルが個別にエクスポートされます。

# exports single object to single glTF file.
# Copyright https://blog.csdn.net/wangmy1988

import bpy
import os

# export to blend file location
basedir = os.path.dirname(bpy.data.filepath)

print("START Output path = "+basedir)

if not basedir:
    raise Exception("Blend file is not saved")

objects = bpy.context.collection.objects

def exportfunc(objName):
    name = bpy.path.clean_name(objName)
    fn = os.path.join(basedir, name)

    # Export gltf
    bpy.ops.export_scene.gltf(filepath=fn + ".gltf", export_format="GLTF_SEPARATE", export_lights=False, export_selected=True)

    print("written:", fn)
    return

#Deselect all objects
for obj in objects:
    obj.select_set(False)

print("===Start save files===")

for obj in objects:
    if obj.parent == None:
        if obj.type == 'EMPTY' and len(obj.children) > 0:
            obj.select_set(True)
            for subObj in obj.children:
                subObj.select_set(True)

            # some exporters only use the active object
            #view_layer.objects.active = obj
            exportfunc(obj.name)
            
            #Deselect all objects
            for obj in objects:
                obj.select_set(False)
        else:
            obj.select_set(True)
            exportfunc(obj.name)
            obj.select_set(False)
    
print("All save completed!")

貼り付け完了後、[スクリプトの実行]ボタンをクリックして実行すると、.blenderプロジェクトファイルが保存されているフォルダーに、gltfファイルが多数出力されます。Web 側での読み込みを容易にするために、テクスチャ ファイルは埋め込みではなく外部にあります。必要に応じて、エクスポート パラメータを自分で変更できます。

パラメータの説明:

エクスポート形式="GLTF_SEPARATE"

分離したファイルをエクスポートします。ディレクトリ内には .gltf の他に、bin (モデル) や画像関連のファイルもありますが、これらは削除できません。

エクスポート_フォーマット = " GLTF_EMBEDDED "

埋め込みファイルをエクスポートします。ディレクトリ内には比較的大きな .glft ファイルが 1 つだけあり、すべてのモデルとテクスチャのリソースがその中にあります。

注: ネットユーザーからのコメントと回答によると、上記のコードはバージョン 2.9 では実行できますが、3.X 以降のバージョンでは実行できません。現在、次のように変更されています。現在、バージョン 3.3.1 でテスト中です。

# exports single object to single glTF file.
# Copyright https://blog.csdn.net/wangmy1988
# Compatible with Blender 3.3.1
 
import bpy
import os
 
# export to blend file location
basedir = os.path.dirname(bpy.data.filepath)
 
print("START Output path = "+basedir)
 
if not basedir:
    raise Exception("Blend file is not saved")
 
objects = bpy.context.collection.objects
 
def exportfunc(objName):
    name = bpy.path.clean_name(objName)
    fn = os.path.join(basedir, name)
 
    # Export gltf
    bpy.ops.export_scene.gltf(filepath=fn + ".gltf", export_format="GLTF_SEPARATE", export_lights=False, use_selection=True)
 
    print("written:", fn)
    return
 
#Deselect all objects
for obj in objects:
    obj.select_set(False)
 
print("===Start save files===")
 
for obj in objects:
    if obj.parent == None:
        if obj.type == 'EMPTY' and len(obj.children) > 0:
            obj.select_set(True)
            for subObj in obj.children:
                subObj.select_set(True)
 
            # some exporters only use the active object
            #view_layer.objects.active = obj
            exportfunc(obj.name)
            
            #Deselect all objects
            for obj in objects:
                obj.select_set(False)
        else:
            obj.select_set(True)
            exportfunc(obj.name)
            obj.select_set(False)
    
print("All save completed!")

おすすめ

転載: blog.csdn.net/wangmy1988/article/details/122309219