3
$\begingroup$

I'm trying, with python, to get the color of a given particle in a simple particle system. So far, when I create a particle system, the particle get the color from where it's emitted, right? I can ask for it's position, orientation, and a lot of cool stuff. But, do you know any way to get the color information? An average RGB will be great.

I guess one way could be through the UV of the emitter?

$\endgroup$
4
$\begingroup$

As far as I know there is no simple way to get the color of a particle. In the case you have an object with UV's and 1 image texture defining the color you should be able to get the color.

The following example is for BI and assumes you only have 1 UV map, the texture is in slot 1 and there is only 1 particle system and 1 modifier (the modifier for the particle system) on the object.

import bpy
from mathutils import Color

def get_pixel_color(img, x, y):
    # Get the 'index' of the pixel. Pixels are stored as one big list of
    # numbers. The R, G, B and A value for every pixel, one after the other.
    pixel_i = y * img.size[0] * 4 + x * 4
    pixel_col = img.pixels[pixel_i:pixel_i + 4]
    # Return the color as Color object, which doesn't have alpha info.
    return Color((pixel_col[:-1]))

def get_uv_color(img, uv):
    # Get the x and y coordinates of the pixel for the given uv coordinate.
    pixel_x = int(uv.x * img.size[0])
    pixel_y = int(uv.y * img.size[1])
    uv_col = get_pixel_color(img, pixel_x, pixel_y)
    return uv_col

def get_particle_color(p, ps, ps_mod):
    p_uv = ps.uv_on_emitter(ps_mod, p)
    p_mat = bpy.data.materials[ps.settings.material_slot]
    p_mat_tex = p_mat.texture_slots[0].texture
    p_mat_img = p_mat_tex.image
    p_col = get_uv_color(p_mat_img, p_uv)
    return p_col

obj = bpy.context.object       # The object with the particle system
ps = obj.particle_systems[0]   # The particle system you need to query
ps_mod = obj.modifiers[0]      # The modifier for the particle system
p = ps.particles[0]            # The first particle

p_col = get_particle_color(p, ps, ps_mod)

For Cycles (assuming that there is only 1 Image Texture node in the Shading Node Tree) you should change the get_particle_color function. The statement for p_mat_tex should be changed to:

p_mat_tex = p_mat.node_tree.nodes['Image Texture']
$\endgroup$
  • $\begingroup$ It works great in my early tests ! Thanks Jasper ! :D $\endgroup$ – Flavio Nov 4 '14 at 17:02
-1
$\begingroup$

If you using Blender Render

  • Particle Halos allows to can set a material on the emitter and select it. Particles textures can modify color and size among other properties.
  • Object and Group Particles maintain their own color

enter image description here

If you are using Cycles Render

  • The [particles info] in the materials node editor can provide more info and can be programmed with the nodes for precisely colored particles.
  • Textures can change size and other properties

enter image description here

$\endgroup$
  • 1
    $\begingroup$ The question specifically asks for a way of getting the colour via python not how to colour the particles themselves. $\endgroup$ – Ray Mairlot Oct 3 '14 at 20:10
  • 1
    $\begingroup$ These "worse-than-Word-art" arrows are dreadful, you are atomising the reader's sight at the expense of clarity. $\endgroup$ – nantille Feb 9 '16 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.