Problem with Python + Paricle System

Heya there :slight_smile:

I just started yesterday with python scripts in blender but im using blender for 2 years now.

After I finished my Sound Visualizer yesterday, i thought it does look kinda boring with just moving bars.
Therefore I decided to try to create a code for a sound visualizer, at which each bar which would previously scale along the z axis would be replaced by a plane which does emit particles. My goal is it to make the particles lifetime change according to the way in which the bars got sclaed along the z axis previously.

Here’s my code in its current state:


import bpy


bars = 50
startframe = 1
endframe = 4250 
c = 1
  
for i in range(0, bars):
    
    bpy.ops.mesh.primitive_plane_add(location = (c, 1, 0))
    bpy.context.active_object.scale.x = 0.5
    bpy.context.active_object.scale.y = 0.5
    bpy.ops.object.particle_system_add() 
    ParticleSettings.count = 100000
    ParticleSettings.frame_start = startframe
    ParticleSettings.frame_end = endframe
    ParticleSettings.normal_factor = 100
    ParticleSettings.random_factor = 5
    ParticleSettings.material = 1  
    bpy.ops.anim.keyframe_insert_menu(type=)




   
    c += 1



My problems are:

  1. neither ParticleSettings.count = 100000 (and the following lines) nor bpy.data.particles(“ParticleSetting.001”).count = 100000 does work.

  2. I got no Idea how to create a keyframe for the lifetime of particles via python. In my first Visualizer I could just use bpy.ops.anim.keyframe_insert_menu(type=‘Scaling’), but i guess thats not working now ;). Therefore i tought bpy.ops.anim.keyframe_insert_button() might work, but i dont find/know any syntax on how to use it …

I hope my very bad english didnt kept you from understanding what im talking about :wink: every help would be really apreciated :slight_smile:

Greetings Japoc


import bpy

bars = 1
startframe = 1
endframe = 4250 
c = 1
  
for i in range(0, bars):
    
    bpy.ops.mesh.primitive_plane_add(location = (c, 1, 0)) # c == i+1 
    ob = bpy.context.active_object
    
    ob.scale.x = 0.5
    ob.scale.y = 0.5
    
    bpy.ops.object.particle_system_add() 
    
    pss = ob.particle_systems.active.settings
    
    pss.count = 100000
    pss.frame_start = startframe
    pss.frame_end = endframe
    pss.normal_factor = 100
    pss.factor_random = 5
    pss.material = 1  
    
    pss.keyframe_insert("lifetime", frame=1)
    pss.lifetime = 10
    pss.keyframe_insert("lifetime", frame=100)
   
    # Changes the first key interpolation from bezier to linear.
    pss_fcurves = pss.animation_data.action.fcurves
    
    for curve in pss_fcurves:
        if curve.data_path == "lifetime":
            break;
                                
    curve.keyframe_points[0].interpolation = "LINEAR"
    
    c += 1

Hope it helps you a bit.

woow, thanks! Thats by far more then I could expect :slight_smile:

i kept working on it and thats its current state:

import bpy

bars = 3
startframe = 1
endframe = 4250 
c = 1


steps = 8000/bars
  
for i in range(0, bars):
      
    bpy.ops.mesh.primitive_plane_add(location = (c, 1, 0)) # c == i+1 
    ob = bpy.context.active_object
    
    ob.scale.x = 0.5
    ob.scale.y = 0.5
    
    bpy.ops.object.particle_system_add() 
    
    pss = ob.particle_systems.active.settings
    
    pss.count = 100000
    pss.frame_start = startframe
    pss.frame_end = endframe
    pss.normal_factor = 100
    pss.factor_random = 5
    pss.material = 1 
    pss.lifetime=0 
    
    pss.keyframe_insert("lifetime", frame=1)
    #pss.lifetime = 10
    #pss.keyframe_insert("lifetime", frame=100)
    bpy.context.area.type = 'GRAPH_EDITOR'
    bpy.ops.graph.sound_bake(filepath="/Users/Till/Desktop/ShakeDrizzle 4 President/ShakeDrizzle's Music Playlist/The Two Friends ft. Jeff Sontag - Sedated (Culture Code Remix).mp3", low=steps*i, high=steps*i+steps, attack=0.01, release=0.2)
    
    
   
    #Changes the first key interpolation from bezier to linear.
    #pss_fcurves = pss.animation_data.action.fcurves
    
    #for curve in pss_fcurves:
    #    if curve.data_path == "lifetime":
    #        break;
                                
    #curve.keyframe_points[0].interpolation = "LINEAR"
    
    c += 1
    
bpy.context.area.type = 'TEXT_EDITOR'

My current problem is that there is a correct graph which gets assigned to the lifetime of each single particle system but the lifetime isnt changing at all if I run the animation :confused: Probably you can try it yourself and just change the path of the .mp3 used.
Would cool if you/anyone got an idea how to fix this/ can tell me what I did wrong.

If you got some more spare time it would be cool if you could answer two small other questions of mine:

  1. I’ve looked for lines of code at google and blender.org which would do what i needed (in my first post) but only found ones which didnt worked. How can I find the correct lines of code for my purpose more precisly? Always using the forum to ask people cant be the real way :confused:

  2. What exactly are you trying to achieve with “Changes the first key interpolation from bezier to linear.”? I just commented it since I had no idea what it did and it made seemingly to me no difference if its in the code or not :open_mouth:

Greeting,
Japoc

woow, thanks! Thats by far more then I could expect :slight_smile:

i kept working on it and thats its current state:

import bpy

bars = 3
startframe = 1
endframe = 4250 
c = 1


steps = 8000/bars
  
for i in range(0, bars):
      
    bpy.ops.mesh.primitive_plane_add(location = (c, 1, 0)) # c == i+1 
    ob = bpy.context.active_object
    
    ob.scale.x = 0.5
    ob.scale.y = 0.5
    
    bpy.ops.object.particle_system_add() 
    
    pss = ob.particle_systems.active.settings
    
    pss.count = 100000
    pss.frame_start = startframe
    pss.frame_end = endframe
    pss.normal_factor = 100
    pss.factor_random = 5
    pss.material = 1 
    pss.lifetime=0 
    
    pss.keyframe_insert("lifetime", frame=1)
    #pss.lifetime = 10
    #pss.keyframe_insert("lifetime", frame=100)
    bpy.context.area.type = 'GRAPH_EDITOR'
    bpy.ops.graph.sound_bake(filepath="/Users/Till/Desktop/ShakeDrizzle 4 President/ShakeDrizzle's Music Playlist/The Two Friends ft. Jeff Sontag - Sedated (Culture Code Remix).mp3", low=steps*i, high=steps*i+steps, attack=0.01, release=0.2)
    
    
   
    #Changes the first key interpolation from bezier to linear.
    #pss_fcurves = pss.animation_data.action.fcurves
    
    #for curve in pss_fcurves:
    #    if curve.data_path == "lifetime":
    #        break;
                                
    #curve.keyframe_points[0].interpolation = "LINEAR"
    
    c += 1
    
bpy.context.area.type = 'TEXT_EDITOR'

My current problem is that there is a correct graph which gets assigned to the lifetime of each single particle system but the lifetime isnt changing at all if I run the animation :confused: Probably you can try it yourself and just change the path of the .mp3 used.
Would cool if you/anyone got an idea how to fix this/ can tell me what I did wrong.

If you got some more spare time it would be cool if you could answer two small otehr questions of mine:

  1. I’ve looked for lines of code at google and blender.org which would do what i needed (in my first post) but only found ones which didnt worked. How can I find the correct lines of code for my purpose more precisly? Always using the forum to ask people cant be the real way :confused:

  2. What exactly are you trying to achieve with “Changes the first key interpolation from bezier to linear.”? I just commented it since I had no idea what it did and it made seemingly to me no difference if its in the code or not :open_mouth:

Greeting,
Japoc

anyone got an idea? :slight_smile: