見出し画像

[Blender][script]1フレーム毎のパーティクル放出数を設定する(250204更新)

Blenderのパーティクル放出数の設定は「指定のフレーム範囲で〇個放出する」ですが、「1フレーム毎に〇個放出する」で設定するスクリプト。
スクリプト内の数字を書き換えて、エミッターオブジェクトを選択して実行。複数可。

import bpy

# 現在選択中のオブジェクトを取得
selected_object = bpy.context.object

# 選択中のオブジェクトが有効か確認
if selected_object is None:
    print("オブジェクトが選択されていません。")
elif not selected_object.particle_systems:
    print(f"選択中のオブジェクト '{selected_object.name}' にパーティクルシステムがありません。")
else:
    # 対象のパーティクルシステムを取得
    particle_system = selected_object.particle_systems[0]

    # 1フレームあたりのパーティクル数
    particles_per_frame = 1

    # 総フレーム数(開始~終了)
    frame_start = 1
    frame_end = 10

    # 放出数を計算
    total_particles = particles_per_frame * (frame_end - frame_start + 1)
    particle_system.settings.count = total_particles
    particle_system.settings.frame_start = frame_start
    particle_system.settings.frame_end = frame_end

    print(f"パーティクル設定が完了しました。\n"
          f"対象オブジェクト: {selected_object.name}\n"
          f"1フレームあたりのパーティクル数: {particles_per_frame}\n"
          f"フレーム範囲: {frame_start}{frame_end}\n"
          f"総パーティクル数: {total_particles}")


(250204追加)サイドバーから実行出来るバージョン

3Dビューのサイドバーから実行
import bpy

class PARTICLE_PT_CustomPanel(bpy.types.Panel):
    bl_label = "Particle Settings"
    bl_idname = "PARTICLE_PT_custom_panel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Particles"

    def draw(self, context):
        layout = self.layout
        scene = context.scene

        layout.prop(scene, "particles_per_frame")
        layout.prop(scene, "custom_frame_start")
        layout.prop(scene, "custom_frame_end")
        layout.operator("particle.apply_settings")

class PARTICLE_OT_ApplySettings(bpy.types.Operator):
    bl_label = "適用"
    bl_idname = "particle.apply_settings"

    def execute(self, context):
        obj = context.object
        scene = context.scene

        if obj is None:
            self.report({'WARNING'}, "オブジェクトが選択されていません。")
            return {'CANCELLED'}

        if not obj.particle_systems:
            self.report({'WARNING'}, f"選択中のオブジェクト '{obj.name}' にパーティクルシステムがありません。")
            return {'CANCELLED'}

        particle_system = obj.particle_systems[0]
        total_particles = scene.particles_per_frame * (scene.custom_frame_end - scene.custom_frame_start + 1)
        
        particle_system.settings.count = total_particles
        particle_system.settings.frame_start = scene.custom_frame_start
        particle_system.settings.frame_end = scene.custom_frame_end

        self.report({'INFO'}, "パーティクル設定を適用しました。")
        return {'FINISHED'}

# プロパティの追加
def register():
    bpy.utils.register_class(PARTICLE_PT_CustomPanel)
    bpy.utils.register_class(PARTICLE_OT_ApplySettings)
    bpy.types.Scene.particles_per_frame = bpy.props.IntProperty(name="Particles Per Frame", default=1, min=1)
    bpy.types.Scene.custom_frame_start = bpy.props.IntProperty(name="Start Frame", default=1, min=1)
    bpy.types.Scene.custom_frame_end = bpy.props.IntProperty(name="End Frame", default=10, min=1)

def unregister():
    bpy.utils.unregister_class(PARTICLE_PT_CustomPanel)
    bpy.utils.unregister_class(PARTICLE_OT_ApplySettings)
    del bpy.types.Scene.particles_per_frame
    del bpy.types.Scene.custom_frame_start
    del bpy.types.Scene.custom_frame_end

if __name__ == "__main__":
    register()

いいなと思ったら応援しよう!