【问题标题】:def function : if nothing selected , return a list of all the objectsdef 函数:如果没有选择,则返回所有对象的列表
【发布时间】:2013-06-05 13:16:13
【问题描述】:

我正在尝试定义一个函数,当我指定一个对象时返回一个列表,当我没有指定任何内容时,它会返回带有 *_control 的场景中所有对象的列表。 这是我的功能,但它不起作用.... 那我正在和玛雅一起工作..

from maya import cmds

def correct_value(selection):

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

       if not selection : 
            objs = cmds.ls ('*_control')    
            return objs

当我没有指定任何内容时,它会返回错误:

错误:第 1 行:TypeError:文件第 1 行:正确值() 只接受 1 个参数(给定 0)

怎么了??

【问题讨论】:

  • 你能贴出这个函数是如何运行的代码示例吗?
  • 有什么困惑?您指定 correct_value 采用单个参数,然后在没有参数的情况下调用它。
  • 以后对这个问题感兴趣的人可能会感到困惑

标签: python function


【解决方案1】:
def correct_value(selection=None):
     if selection is None:  # note that You should check this before  
                            # You wil check whether it is list or not
        objs = cmds.ls ('*_control')    
        return objs

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

【讨论】:

    【解决方案2】:

    嗯,你用一个必需的参数编写了你的​​函数。因此,您必须通过参数。您可以编写它,这样参数是可选的,方法是指定什么都不传递时将使用的值:

    def correct_value(selection=None):
    

    等等

    【讨论】:

      【解决方案3】:

      如果你想让一个参数是可选的,你需要提供一个默认值:

      def correct_value(selection=None):
          # do something
      
          if selection is None: 
              #do something else
      

      【讨论】:

        【解决方案4】:

        处理一个默认参数,即使它可能是None

        def correct_value(*args):
            if not args:
                objs = cmds.ls ('*_control')    
                return objs
            elif len(args) == 1:
                selection = args
                objs = selection
                return objs
            else:
               raise TypeError # ...
        

        【讨论】:

        • 这是一种有趣的方法,适用于单个参数。在这种情况下,我通常使用专门定义的默认值(例如不用于其他任何事情的类)。
        【解决方案5】:

        对于这类东西,这是一对非常有用的模式:

        # always return a list from scene queries (maya will often return 'none' 
        def get_items_named_foo():
             return cmds.ls("foo") or []  # this makes sure that you've always got a list, even if it's empty
        
        
        # always use the variable *args method to pass in lists
        def do_something(*args):
            for item in args:
                do_something(item)  # args will always be a tuple, so you can iterate over it
        
        # this lets you do stuff like this without lots of boring argument checks:
        do_something (*get_items_named_foo())
        

        如果您始终使用这两种技巧,您可以透明地处理 Maya 查询返回 None 而不是列表的情况

        顺便说一句,您可以像这样模仿默认的 maya 行为(不传递参数使用当前选择):

        def work_on_list_or_selected(*args):
           args = args or cmds.ls(sl=True) or []
           for item in args:
               do_something (item)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多