MRが楽しい

MRやVRについて学習したことを書き残す

Blender2.8で利用可能なpythonスクリプトを作る その15(オブジェクト指定のエクスポート、異なる形式のエクスポート)

本日は Blender の技術調査枠です。
Blender2.8で利用可能なpythonスクリプトを作ります。

オブジェクト指定のエクスポート

出力するメッシュオブジェクトを指定してFBXファイルをエクスポートします。
・targetob_fbx_export.py

# bpyインポート
import bpy

# FBXモデルをエクスポートする(メッシュオブジェクト指定)
def export_targets_fbx(arg_filepath="", arg_targetnames=[]):
    """FBXモデルをエクスポートする(メッシュオブジェクト指定)
    
    Keyword Arguments:
        arg_filepath {str} -- 書き出しファイルパス (default: {""})
        arg_targetnames {list} -- 指定オブジェクト名 (default: {[]})
    """

    # 他のオブジェクトに操作を適用しないよう全てのオブジェクトを走査する
    for ob in bpy.context.scene.objects:
        # 非選択状態に設定する
        ob.select_set(False)
    
    # 他のオブジェクトに操作を適用しないよう全てのオブジェクトを走査する
    for obname in arg_targetnames:
        # 指定オブジェクトを取得する
        # (get関数は対象が存在しない場合 None が返る)
        selectob = bpy.data.objects.get(obname)

        # 指定オブジェクトが存在するか確認する
        if selectob == None:
            # 指定オブジェクトが存在しない場合は処理しない
            continue

        # 指定オブジェクトを選択状態に設定する
        selectob.select_set(True)

    # FBXインポート
    # bpy.ops.import_scene.fbx()
    # https://docs.blender.org/api/2.82a/bpy.ops.export_scene.html#bpy.ops.export_scene.fbx
    bpy.ops.export_scene.fbx(
        filepath=arg_filepath,
        check_existing=True,
        filter_glob="*.fbx",
        use_selection=True,
        use_active_collection=False,
        global_scale=1.0,
        apply_unit_scale=True,
        apply_scale_options='FBX_SCALE_NONE',
        bake_space_transform=False,
        object_types={'MESH'},
        use_mesh_modifiers=True,
        use_mesh_modifiers_render=True,
        mesh_smooth_type='OFF',
        use_subsurf=False,
        use_mesh_edges=False,
        use_tspace=False,
        use_custom_props=False,
        add_leaf_bones=True,
        primary_bone_axis='Y',
        secondary_bone_axis='X',
        use_armature_deform_only=False,
        armature_nodetype='NULL',
        bake_anim=True,
        bake_anim_use_all_bones=True,
        bake_anim_use_nla_strips=True,
        bake_anim_use_all_actions=True,
        bake_anim_force_startend_keying=True,
        bake_anim_step=1.0,
        bake_anim_simplify_factor=1.0,
        path_mode='AUTO',
        embed_textures=False,
        batch_mode='OFF',
        use_batch_own_dir=True,
        use_metadata=True,
        axis_forward='-Z',
        axis_up='Y'
    )
    return


# 関数の実行例
export_targets_fbx(
    arg_filepath="C:\\WORK\\target_export_sample.fbx",
    arg_targetnames=["Cone"]
)

f:id:bluebirdofoz:20200407025754j:plain

異なる形式のエクスポート

指定のGLBファイルをデフォルト設定の指定でエクスポートします。
・glb_export.py

# bpyインポート
import bpy

# GLB形式のモデルをエクスポートする(デフォルト設定)
def export_file_glb(arg_filepath=""):
    """GLB形式のモデルをエクスポートする(デフォルト設定)
    
    Keyword Arguments:
        arg_filepath {str} -- 書き出しファイルパス (default: {""})
    """

    # GLB形式のエクスポート
    # bpy.ops.export_scene.gltf()
    # https://docs.blender.org/api/2.82a/bpy.ops.export_scene.html#bpy.ops.export_scene.gltf
    bpy.ops.export_scene.gltf(
        filepath=arg_filepath, 
        export_format='GLB',
        ui_tab='GENERAL',
        export_copyright="",
        export_image_format='NAME',
        export_texture_dir="",
        export_texcoords=True,
        export_normals=True,
        export_draco_mesh_compression_enable=False,
        export_draco_mesh_compression_level=6,
        export_draco_position_quantization=14,
        export_draco_normal_quantization=10,
        export_draco_texcoord_quantization=12,
        export_draco_generic_quantization=12,
        export_tangents=False,
        export_materials=True,
        export_colors=True,
        export_cameras=False,
        export_selected=False,
        export_extras=False,
        export_yup=True,
        export_apply=False,
        export_animations=True,
        export_frame_range=True,
        export_frame_step=1,
        export_force_sampling=True,
        export_nla_strips=True,
        export_def_bones=False,
        export_current_frame=False,
        export_skins=True,
        export_all_influences=False,
        export_morph=True,
        export_morph_normal=True,
        export_morph_tangent=False,
        export_lights=False,
        export_displacement=False,
        will_save_settings=False,
        check_existing=True,
        filter_glob="*.glb;*.gltf"
    )

    return


# 関数の実行例
export_file_glb(arg_filepath="C:\\WORK\\export_sample.glb")

f:id:bluebirdofoz:20200407025736j:plain