Uv_on_emitter for child particles

What is the proper way to get the uv’s for child particles (hair)? I looked at the Cycles source code and tried doing something similar with our exporter, but it seems like it only works for a quarter of the hair; for the other three quarters the UV’s look all random. The test scene I have is a UV sphere, with a hair particle system, and Children set to Interpolated, Display Amount set to 10, and Number set to 5000. Our code looks something like:

    # psys is the particle system
    num_parents = len(psys.particles)
    num_children = len(psys.child_particles)
    total_hair_count = num_parents + num_children
    start_idx = 0
    if psys.settings.child_type != 'NONE' and num_children > 0:
        start_idx = num_parents
    
    uv_s = []
    for pindex in range(start_idx, total_hair_count):
           .
           .
           .

                if pindex >= num_parents:
                    # This is a child particle
                    particle = psys.particles[0]
                else:
                    particle = psys.particles[pindex]
                uv = psys.uv_on_emitter(psys_modifier, particle=particle, particle_no=pindex)
                uv_s.append([uv[0], uv[1]])

psys.particles[0] can’t work since you’ll always get the same particle.

Thanks for the reply, @brecht! From the Cycles source (blender_curves.cpp) it looked like it never incremented the iterator when it’s exporting the children, but I’m probably reading it wrong. What is the recommended way to get the correct particle then? I can’t use pindex as psys.particles seems to only contain the parents.

Here’s how we do it:

Thanks @bsavery! I’ll take a look.

@bsavery Cool, that worked. Thanks for the tip!