1

it has been a while for me doing python scripting after about 8 months of hiatus. Really need some insights. I have been trying to set or modify the camera filmback/film gate attribute via coding and after much trial and errors, I finally found the command for it but now I am meeting with some other issues..

The first item in list, in Python terms - the index usually starts from 0, however in Maya term's for this filmbackMenu, it starts from 1.

cmds.optionMenu('filmbackMenu', edit = True, select = 0) # Errors-out, stating that it is out of range
cmds.optionMenu('filmbackMenu', edit = True, select = 1) # No errors

Suppose if my filmback Menu is as follows:

list01 = ['itemA-01', 'itemB-02', 'itemC-02', 'itemD-01', 'itemE-01', 'itemF-03']
list01.insert(0,"")
for index, item in enumerate(list01):
    print index, item

while I manage to find a way to have my items start from index '1' by inserting a dummy/blank.. As I am trying to filter out anything that contains '-01' and append both the name and the index into list02, however I only know how to do for either just the name or the index, but I believe I will need both. This is my code section:

list02 = []
for i in list01:
    if '-01' in str(i):
        list02.append(i)
# Result: ['itemA-01', 'itemD-01', 'itemE-01']  # The index here should be 1, 4, 5 

Is it possible to append both the name and the index together? For example, itemA-01 1, 'itemD-01' 4 , 'itemE-01' 5? The index need not be shown in the UI, the name will do, still I will need to grab the index value eventually...

Kindly see below for my code, perhaps that may give a better insight and please do feel free to criticize if need be.

import maya.cmds as cmds

list01 = ['itemA-01', 'itemB-02', 'itemC-02', 'itemD-01', 'itemE-01', 'itemF-03']
list01.insert(0,"")
for index, item in enumerate(list01):
    print index, item

list02 = []
for i in list01:
    if '01' in str(i):
        list02.append(i)

window = cmds.window()
cmds.columnLayout()
cmds.optionMenu (label = 'Select a format')
for x in list02:
    cmds.menuItem(label=x)
cmds.button( label = 'Ok')
cmds.button( label = 'Cancel')

cmds.showWindow( window )

What happens in my UI is that, upon the user selection, eg. if I selected itemD-01 which is of index 4, the following code should run:

cmds.optionMenu('filmbackMenu', edit = True, select = 4)

Notice that the flag for select will be changed..

1 Answer 1

1

You don't need to add a dummy element to your list enumerate has a second argument that indicates the position it should start from. using this you can store the index and the item side by side in a separate list

list02 = []

list01 = ['itemA-01', 'itemB-02', 'itemC-02', 'itemD-01', 'itemE-01', 'itemF-03']
for index, item in enumerate(list01, 1):
    if '-01' in item:
        list02.append([index, item])

print(list02)

result

[[1, 'itemA-01'], [4, 'itemD-01'], [5, 'itemE-01']]

You can even turn in into an elegant list comprehension

result = [[index, item] for index, item in enumerate(list01, 1) if '-01' in item]
Sign up to request clarification or add additional context in comments.

3 Comments

cool, didn't know that I can enumerate it in this way... Was wondering if you may have any ideas as to how I can return the value in the command flag in the cmds.button? When I tried something like print item, it keeps printing the last item in the list02..
something like this cmds.optionMenu('filmbackMenu', edit = True, select = list02[0])?...i'm not familiar with maya or you mean return the value when the user clicks on a button
I am doing something simple like cmds.button( label = 'Ok', command = ('print list02[0]')) but in actual fact, I am trying to capture/return the index value based on the selection. The UI will be in a function - eg. def ui() in which the whole execution will takes places in another function..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.