loopspymol

how to create pymol rename loop


I would like to create a loop for changing interactions name in PyMol. But after one selection loop it crashes and doesn't work.

def get_dists(interactions): # interactions=([1,2], [3,4])

    for i in interactions:
        a = "////" + str(i[0]) + "/C2'"
        b = "////" + str(i[1]) + "/C2'"
        cmd.distance("(" + a + ")", "(" + b + ")")

        for j in range(1, 599):
            x = "dist" + "0" + str(j)
            y = str(i[0]) + " " + str(i[1])
            cmd.set_name(str(x), str(y))

In Pymol the default name of interactions is dist01, 02 , 03.

I want to change these to 1_3, 5_59, 4_8, (interaction between residue).


Solution

  • Your code is totally fine except for one thing: If PyMol doesn't succeed with set_name the whole script is aborted. When you change it to, it should work:

    try:
        cmd.set_name(str(x), str(y))
    except:
        print('failed to rename')
    

    Some additional comments: