MRが楽しい

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

Blender3.0で利用可能なpythonスクリプトを作る その137(指定のタイプのモディファイアを適用する)

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

指定のタイプのモディファイアを適用する

モディファイアを適用するには 関数を利用します。
docs.blender.org
モディファイアが指定のタイプからは type 変数を確認することで確認できます。
docs.blender.org

サンプルスクリプト

以下のサンプルスクリプトを作成しました。
指定オブジェクトに含まれる指定タイプのモディファイアを全て適用します。
・Script_apply_modifier_targettype.py

# bpyインポート
import bpy

def apply_modifier_targettype(arg_object:bpy.types.Object, arg_modifier_type:str) -> bool:
    """指定オブジェクトの指定タイプのモディファイアを適用する

    Keyword Arguments:
        arg_object (bpy.types.Object): 指定オブジェクト
        arg_modifier_type {str} -- 指定のモディファイアタイプ

    Returns:
        bool -- 実行成否
    """
    
    # 対象のオブジェクトをアクティブオブジェクトにする
    bpy.context.view_layer.objects.active = arg_object

    # オブジェクト内の全てのモディファイアを走査する
    for check_modifier in arg_object.modifiers:
        # モディファイアの種類が指定のものであるか確認する
        if check_modifier.type == arg_modifier_type:
            # 指定モディファイアなら適用する
            # (https://docs.blender.org/api/current/bpy.ops.object.html#bpy.ops.object.modifier_add)
            bpy.ops.object.modifier_apply(modifier=check_modifier.name)

    # 実行成否を返却する
    return True

# 関数の実行例
# ミラーモディファイアを適用する
# モディファイアの種類のマニュアル
# (https://docs.blender.org/api/current/bpy_types_enum_items/object_modifier_type_items.html#rna-enum-object-modifier-type-items)
apply_modifier_targettype(bpy.context.scene.objects.get('Cube'), 'MIRROR')

・実行前

・実行後