if statement in list assignment

Hi I am trying to populate a list with a conditional for one of the elements.
I am still new to python.
I’ve seen stuff similar but I can’t get the syntax right.
Any ideas?
Thanks
Yardie


direct_maps = (
    ( "LATTICE_OT" , "Lattice", 0, 0),
    ( "PARTICLE_OT" , "Particle", 0, 0),
    ( "FONT_OT" , "Font", 0, 0),
    ( "PAINT_OT_face_select" , "Face Mask", 0, 0),
    ( "PAINT_OT",
        if bpy.context.mode == 'PAINT_WEIGHT':
            "Weight Paint"
        elif  bpy.context.mode == 'PAINT_VERTEX':
            "Vertex Paint"
        elif  bpy.context.mode == 'PAINT_TEXTURE': 
            "Image Paint"
        else: 
            None
        , 0 , 0)
    ),
)

Something like this should work…right?


if bpy.context.mode == 'PAINT_WEIGHT':
    direct_maps = (
            ( "LATTICE_OT" , "Lattice", 0, 0),
            ( "PARTICLE_OT" , "Particle", 0, 0),
            ( "FONT_OT" , "Font", 0, 0),
            ( "PAINT_OT_face_select" , "Face Mask", 0, 0),
            ( "PAINT_OT", "Weight Paint", 0 , 0)
            ),
        )

if bpy.context.mode == 'PAINT_VERTEX':
    direct_maps = (
            ( "LATTICE_OT" , "Lattice", 0, 0),
            ( "PARTICLE_OT" , "Particle", 0, 0),
            ( "FONT_OT" , "Font", 0, 0),
            ( "PAINT_OT_face_select" , "Face Mask", 0, 0),
            ( "PAINT_OT", "Vertex Paint", 0 , 0)
            ),
        )

if bpy.context.mode == 'PAINT_TEXTURE':
    direct_maps = (
            ( "LATTICE_OT" , "Lattice", 0, 0),
            ( "PARTICLE_OT" , "Particle", 0, 0),
            ( "FONT_OT" , "Font", 0, 0),
            ( "PAINT_OT_face_select" , "Face Mask", 0, 0),
            ( "PAINT_OT", "Texture Paint", 0 , 0)
            ),
        )

or what about something like this…


import bpy

s = None
if bpy.context.mode == 'PAINT_WEIGHT':
    s = "Weight Paint"
if bpy.context.mode == 'PAINT_VERTEX':
    s = "Vertex Paint"
if bpy.context.mode == 'PAINT_TEXTURE':
    s = "Texture Paint" 
if s != None:  
    direct_maps = (( "LATTICE_OT" , "Lattice", 0, 0),( "PARTICLE_OT" , "Particle", 0, 0),( "FONT_OT" , "Font", 0, 0),( "PAINT_OT_face_select" , "Face Mask", 0, 0),( "PAINT_OT", s, 0 , 0))

Yeah thanks Atom.
There is more than one way to skin a cat.
I did this then went for a ride.


 map += (
        ( "LATTICE_OT" , "Lattice", 0, 0),
        ( "PARTICLE_OT" , "Particle", 0, 0),
        ( "FONT_OT" , "Font", 0, 0),
        #/* Paint Face Mask */
        ( "PAINT_OT_face_select" , "Face Mask", 0, 0),
    )
    if bpy.context.mode == 'PAINT_WEIGHT':
        map += (( "PAINT_OT","Weight Paint",0,0),)
    elif  bpy.context.mode == 'PAINT_VERTEX':
        map += (( "PAINT_OT","Vertex Paint",0,0),)
    elif  bpy.context.mode == 'PAINT_TEXTURE':
        map += (( "PAINT_OT","Image Paint",0,0),)


but I was trying to do this assignment directly in the definition

a bit like this does


    keymap = [(k, v, s, r) for k, v, s, r in map if opname.startswith(k) ]

I am just learning python and am trying to see what I can do with it.

Well just for a laugh. :smiley:
This works but it breaks all pythons rules and sort of sucks as well. :eek::no::no:
Especially six months down the track when your trying to work out what does what.


direct_maps = (
    ( "LATTICE_OT" , "Lattice", 0, 0),
    ( "PARTICLE_OT" , "Particle", 0, 0),
    ( "FONT_OT" , "Font", 0, 0),
    ( "PAINT_OT_face_select" , "Face Mask", 0, 0),
    ( "PAINT_OT", "Weight Paint" if bpy.context.mode == 'PAINT_WEIGHT' else "Vertex Paint" if bpy.context.mode == 'PAINT_VERTEX' else "Image Paint" if bpy.context.mode == 'PAINT_TEXTURE' else None , 0 , 0 ),
)    


Just for completeness this works and it is actually quite readable.


direct_maps = (
    ( "LATTICE_OT" , "Lattice", 0, 0),
    ( "PARTICLE_OT" , "Particle", 0, 0),
    ( "FONT_OT" , "Font", 0, 0),
    ( "PAINT_OT_face_select" , "Face Mask", 0, 0),
    ( "PAINT_OT",
        "Weight Paint" if bpy.context.mode == 'PAINT_WEIGHT' else
        "Vertex Paint" if bpy.context.mode == 'PAINT_VERTEX' else
        "Image Paint" if bpy.context.mode == 'PAINT_TEXTURE' else None
        , 0 , 0),
    ( "ARMATURE_OT" , "Armature", 0, 0),
    ( "SKETCH_OT" , "Armature", 0, 0),
    ( "POSE_OT" , "Pose", 0, 0),
    ( "POSELIB_OT" , "Pose", 0, 0),
)