Unable to change the legend color

Hello everyone,
I’m trying to use the OVITO python API to visualise my simulations. In the process I’m trying to color particles by type and print a legend with their names. However, the legend is printed in the default colors, and I can’t change the colors. I’ve tried assigning the pipeline (it is the only pipeline in the script) and using the ColorCodingModifier and color_mapping_source. Both of them only give me gradients to work with. I’d appreciate any insight.

pipeline1 = ov.io.import_file(dumpfilename)
pipeline1.add_to_scene()

pipeline1.modifiers.append(ov.modifiers.SelectTypeModifier(types={1}))
pipeline1.modifiers.append(ov.modifiers.AssignColorModifier(color=(0.490, 0.737, 0)))
pipeline1.modifiers.append(ov.modifiers.SelectTypeModifier(types={2}))
pipeline1.modifiers.append(ov.modifiers.AssignColorModifier(color=(1, 0, 0.5)))

vp = ov.vis.Viewport()
legend = ov.vis.ColorLegendOverlay(
    title = 'Particle type',
    alignment = QtCore.Qt.AlignmentFlag.AlignRight | QtCore.Qt.AlignmentFlag.AlignTop,
    orientation = QtCore.Qt.Orientation.Horizontal,
    offset_y = -0.04,
    font_size = 0.08,
    pipeline = pipeline1,
    property = 'particles/Particle Type'

)
vp.overlays.append(legend)

Your Color Legend Overlay is set up in a way that it uses the colors specified at the particle type level, distinguishing it from the broader Color particle property. Instead of applying the Select Type and Assign Color Modifiers, you can use this approach:

def modify_type_colors(frame: int, data: DataCollection):
    data.particles_.particle_types_.type_by_id_(1).color = [0.490, 0.737, 0]
    data.particles_.particle_types_.type_by_id_(2).color = [1, 0, 0.5]
pipeline.modifiers.append(modify_type_colors)

https://www.ovito.org/docs/current/python/modules/ovito_data.html#ovito.data.ParticleType
https://www.ovito.org/docs/current/python/modules/ovito_data.html#ovito.data.ElementType

Thank you, it worked