0

I'm writing a script for Maya where I try to save a shot from a sequence with only its own camera.

import maya.cmds as mc

    list_of_shots_to_delete = mc.sequenceManager( listShots=True )
    list_of_shots_to_delete.remove( my_shot )

    for k in list_of_shots_to_delete:

        cam = getShotsCamera( k )

        if cam != None:

            if cam == getShotsCamera( my_shot ):
                print cam + " is needed!"

            else:  
                mc.delete( getShotsCamera( k ) )

            mc.lockNode( k, lock=False )
            print "Shot "+ k +" deleted!"
            mc.delete( k )

Basically what this loop does is, for each unneeded shot in the sequence, deletes its camera (unless it is the same camera from the shot I want to save) and then deletes the shot itself. For some reason, if a shot shares the same camera as another, the last one of then in the list_of_shots_to_delete list won't be deleted (it will skip the last 3 lines of this loop).

Can anybody help?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
PSlayer
  • 61
  • 1
  • 2
  • 11
  • Sounds like a case of changing list size during iteration but there is no definition of `mc`, `mc.delete`, `list_of_shots_to_delete`, etc, so it's hard to say. – Two-Bit Alchemist Jul 07 '16 at 19:26
  • I'm sorry i forgot to put the "import maya.cmds as mc". – PSlayer Jul 07 '16 at 19:28
  • Well what is `list_of_shots_to_delete` -- that's probably the most important bit of info left out – Two-Bit Alchemist Jul 07 '16 at 19:34
  • Just added in the script above.It is the list with the names of each shot in the shot manager.Then i remove the shot that i want to save from that list,resulting in a list with all the unneeded shots. – PSlayer Jul 07 '16 at 19:39
  • I don't know anything about maya but I'm almost certain it's the same thing as this: http://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list – Two-Bit Alchemist Jul 07 '16 at 19:43

1 Answers1

1

I don't have any problem with your code, it is deleting everything but the shot specified at the beginning, and the camera associated with it. Just to be sure, here is the final code I used with some improvements. Maybe there is a problem in your scene.

import maya.cmds as mc

def getShotsCamera(shot):
    for camera in mc.listConnections(shot + '.currentCamera') or ['']:
        return camera


my_shot = 'shot1'
my_shot_camera = getShotsCamera(my_shot)
list_of_shots_to_delete = mc.sequenceManager(listShots=True)
list_of_shots_to_delete.remove(my_shot)

for k in list_of_shots_to_delete:
    cam = getShotsCamera(k)

    if cam and cam != my_shot_camera:
        mc.delete(cam)

    mc.lockNode(k, lock=False)
    print "Shot " + k + " deleted!"
    mc.delete(k)
Regnareb
  • 134
  • 6
  • -->for camera in mc.listConnections(my_shot + '.currentCamera') or [''] should be -->for camera in mc.listConnections(shot + '.currentCamera') or [''] Otherwise you're referencing the variable from the outer scope instead of the passed attribute. – Klaudikus May 29 '18 at 12:46
  • Exactly, my bad – Regnareb Jun 11 '18 at 21:46