2013-06-08 4 views
0

keywords_aguments를 정의하는 데 문제가 있습니다. 아무 것도 지정되지 않은 경우 * _control을 사용하여 모든 개체를 반환하는 함수를 정의하려고합니다. 그러나 반환해야하는 '왼쪽'또는 '오른쪽'에 대한 것을 선택하고 싶습니다. 아래에서 내 기능을 찾을 수 있습니다. 나는 어디서 오류인지 이해하지 못한다.python의 ** keywords_arguments에 약간의 문제가 있습니다.

from maya import cmds 

def correct_value(selection=None, **keywords_arguments): 
    if selection is None: 
     selection = cmds.ls ('*_control')  

    if not isinstance(selection, list): 
     selection = [selection] 

    for each in keywords_arguments: 
     keywords_list = [] 
     if each.startswith('right','left'): 
      selection.append(each) 



    return selection 

correct_value() 
+0

'keywords_list = [ ]'? – chepner

답변

3

키워드 인수는 사전입니다. 인쇄하거나 type() 기능으로 유형을 확인할 수 있습니다. 이렇게하면 스스로 고립 된 상황에서 사전을 사용하고 문제를 스스로 해결하는 방법을 찾을 수 있습니다.

이제 x = {1:2} 사전이있는 경우에 for를 반복하면 하나만 표시됩니다. 예를 들어 일치 값이 아닌 키 (!) 만 반복합니다. 이를 위해서는 for key, value in dictionary.items()을 사용하고 if key in ('right', 'left') 값을 사용하십시오.

0

코드는 '오른쪽'또는 '왼쪽'을 목록의 끝에 추가합니다.

난 당신이 뭔가 싶은 생각 :

def find_controls(*selection, **kwargs): # with *args you can pass one item, several items, or a list 
    selection = selection or cmds.ls("*_control") or [] # supplied objects, or the ls command, or an empty list 

    if not kwargs: 
     return list(selection) # no flags? reutrn the whole list 

    lefty = lambda ctrl: ctrl.lower().startswith("left") # this will filter for items with left 
    righty = lambda ctrl: ctrl.lower().startswith("right") # this will filter for items with left 

    filters = [] 
    if kwargs.get('left'): # safe way to ask 'is this key here and does it have a true value?' 
     filters.append(lefty) 

    if kwargs.get('right'): 
     filters.append(righty) 

    result = [] 
    for each_filter in filters: 
     result += filter (each_filter, selection) 

    return result 


find_controls (left=True, right=True) 
# Result: [u'left_control', u'right_control'] # 

find_controls (left=True, right =False) # or just left=True 
# Result: [u'left_control'] # 

find_controls() 
# Result: [u'left_control', u'middle_control', u'right_control'] # 

여기에 트릭을 사용하는 것입니다을하고이에 기능을 적용하는 (필터 기능 내장 (기본적으로 그냥 짧은 형식의 함수이다)를 lambdas 목록에있는 모든 것을 반환하고, 함수가 0이 아닌 잘못된 답변을 반환합니다. 키워드와 해당 람다를 더 추가하면 어떻게 확장 할 수 있는지 쉽게 알 수 있습니다.

관련 문제