Blender批量导出所有的物体到单独的glTF文件

有时候为了使WebGL的三维引擎加载模型文件单个文件做到最小,所以需要把单个建筑或者物体单独导出成一个glTF文件,手动一个个导出太麻烦了,于是编写了一个脚本,能够批量的导出。

首先需要把项目另存为.blender工程文件。然后在菜单Scripting中,新建一个脚本,把下面的脚本粘贴,然后运行即可。

但是要注意的是,Scene Colletion中需要把默认的Camera,Cube删除,否则也会单独导出一个文件。

# 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!")

粘贴完成后,点击【Run Script】 按钮即可运行,在保存.blender项目文件的文件夹里,会有输出很多个gltf文件。为了方便web端加载,贴图文件是外置的,并非内嵌的。如有需要可以自行修改导出参数。

参数解释:

export_format="GLTF_SEPARATE"

导出分离的文件,目录中除了.gltf以外,还有bin(模型),图片相关文件,都不可以删除。

export_format="GLTF_EMBEDDED"

导出内嵌的文件,目录中只有一个.glft文件,体积较大,所有模型、贴图资源都在里面了。

注意:根据网友评论和反应:以上代码在2.9版本是可以运行,3.X以上的无法运行,现在做了修改如下。目前在3.3.1版本测试OK。

# 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
今日推荐