我试图在Maya中编写用户界面,并且它与多个级别的父级和无缩进的混乱令人难以置信。目前,基本代码(无任何功能)大约有400行,需要一段时间才能找到我需要的位。
例如,使用下面的代码而不加注释:
#Earlier user interface
py.rowColumnLayout( numberOfColumns = 5 )
py.text( label="", width = 1 )
py.text( label="Column 1", enable = False, width = 250 )
py.text( label="", width = 1 )
py.text( label="Column 2" enable = False, width = 250 )
py.text( label="", width = 1 )
py.text( label="" )
py.rowColumnLayout( numberOfColumns = 4 )
py.text( label=" Input data:", align="left" )
py.text( label="" )
py.text( label="" )
py.text( label="" )
py.textField( text = "Text here" )
py.text( label="" )
py.text( label="" )
py.text( label="" )
py.setParent( ".." )
py.text( label="" )
py.rowColumnLayout( numberOfColumns = 4 )
py.rowColumnLayout( numberOfColumns = 5 )
py.radioButton( label = "Read file from path", width = 100 )
py.text( label="" )
py.button( label = "Browse" )
py.text( label="" )
py.button( label = "Validate" )
py.setParent( ".." )
py.text( label="" )
py.text( label="" )
py.text( label="" )
py.setParent( ".." )
py.setParent( ".." )
但是,这就是缩进的样子
py.rowColumnLayout( numberOfColumns = 5 )
py.text( label="", width = 1 )
py.text( label="Column 1", enable = False, width = 250 )
py.text( label="", width = 1 )
py.text( label="Column 2" enable = False, width = 250 )
py.text( label="", width = 1 )
py.text( label="" )
py.rowColumnLayout( numberOfColumns = 4 )
py.text( label=" Input data:", align="left" )
py.text( label="" )
py.text( label="" )
py.text( label="" )
py.textField( text = "Text here" )
py.text( label="" )
py.text( label="" )
py.text( label="" )
py.setParent( ".." )
py.text( label="" )
py.rowColumnLayout( numberOfColumns = 4 )
py.rowColumnLayout( numberOfColumns = 5 )
py.radioButton( label = "Read file from path", width = 100 )
py.text( label="" )
py.button( label = "Browse" )
py.text( label="" )
py.button( label = "Validate" )
py.setParent( ".." )
py.text( label="" )
py.text( label="" )
py.text( label="" )
py.setParent( ".." )
py.setParent( ".." )
有什么办法我可以使用缩进来编写它,但在执行时使其完全忽略它们?我看到了一个问题,问是否可以编写不带缩进的python,但我有点相反。
注意:某些py.*
功能的输出值也需要分配给变量,只是尚未分配,因为需要首先对布局进行排序。
这是一个很好的用例,像我们这样的技术艺术家在Maya中构建UI时每天都要面对。
这是内置在PyMEL中的。您不必创建上下文管理器。布局命令本身是上下文管理器。您只需with
在每个布局命令调用之前添加一个关键字,如下所示:
# Do this when using PyMEL for your UI code
import pymel.core as pm
# ...
with pm.rowColumnLayout( numberOfColumns = 5 ):
pm.text( label="", width = 1 )
pm.text( label="Column 1", enable = False, width = 250 )
pm.text( label="", width = 1 )
pm.text( label="Column 2", enable = False, width = 250 )
pm.text( label="", width = 1 )
pm.text( label="" )
with pm.rowColumnLayout( numberOfColumns = 4 ):
pm.text( label=" Input data:", align="left" )
pm.text( label="" )
pm.text( label="" )
pm.text( label="" )
pm.textField( text = "Text here" )
pm.text( label="" )
pm.text( label="" )
pm.text( label="" )
pm.text( label="" )
with pm.rowColumnLayout( numberOfColumns = 4 ):
with pm.rowColumnLayout( numberOfColumns = 5 ):
pm.radioButton( label = "Read file from path", width = 100 )
pm.text( label="" )
pm.button( label = "Browse" )
pm.text( label="" )
pm.button( label = "Validate" )
pm.text( label="" )
pm.text( label="" )
pm.text( label="" )
一种快速的解决方案是制作一个虚拟的上下文管理器。你可以做这样的事情
# Do this when using Maya's cmds for your UI code
import maya.cmds as cmds
# ...
from contextlib import contextmanager
@contextmanager
def neat_indent():
# OPTIONAL: This is also an opportunity to do something before the block of code runs!
try:
# During this is where your indented block will execute
# Leave it empty
yield
finally:
# OPTIONAL: This is where you can write code that executes AFTER your indented block executes.
pass
这样,您的代码不必更改太多。只需with
在每个预期缩进的开头添加您的上下文管理器功能和关键字!
cmds.rowColumnLayout( numberOfColumns = 5 )
with neat_indent():
cmds.text( label="", width = 1 )
cmds.text( label="Column 1", enable = False, width = 250 )
cmds.text( label="", width = 1 )
cmds.text( label="Column 2", enable = False, width = 250 )
cmds.text( label="", width = 1 )
cmds.text( label="" )
cmds.rowColumnLayout( numberOfColumns = 4 )
with neat_indent():
cmds.text( label=" Input data:", align="left" )
cmds.text( label="" )
cmds.text( label="" )
cmds.text( label="" )
cmds.textField( text = "Text here" )
cmds.text( label="" )
cmds.text( label="" )
cmds.text( label="" )
cmds.setParent( ".." )
cmds.text( label="" )
cmds.rowColumnLayout( numberOfColumns = 4 )
with neat_indent():
cmds.rowColumnLayout( numberOfColumns = 5 )
with neat_indent():
cmds.radioButton( label = "Read file from path", width = 100 )
cmds.text( label="" )
cmds.button( label = "Browse" )
cmds.text( label="" )
cmds.button( label = "Validate" )
cmds.setParent( ".." )
cmds.text( label="" )
cmds.text( label="" )
cmds.text( label="" )
cmds.setParent( ".." )
cmds.setParent( ".." )
我们创建的上下文管理器neat_indent()
,还使您有机会编写包装缩进块的代码。这里的一个实际例子是,在每个缩进的结尾处,您都会发现自己在写作py.setParent("..")
。您可以将其放入finally
上下文管理器的部分:
from contextlib import contextmanager
@contextmanager
def neat_indent(parent=None):
# OPTIONAL: This is also an opportunity to do something before the block of code runs!
try:
# During this is where your indented block will execute
# Leave it empty
yield
finally:
# OPTIONAL: This is where you can write code that executes AFTER your indented block executes.
if parent:
cmds.setParent(parent)
您的代码现在更有意义了:
cmds.rowColumnLayout( numberOfColumns = 5 )
with neat_indent(".."):
cmds.text( label="", width = 1 )
cmds.text( label="Column 1", enable = False, width = 250 )
cmds.text( label="", width = 1 )
cmds.text( label="Column 2", enable = False, width = 250 )
cmds.text( label="", width = 1 )
cmds.text( label="" )
cmds.rowColumnLayout( numberOfColumns = 4 )
with neat_indent(".."):
cmds.text( label=" Input data:", align="left" )
cmds.text( label="" )
cmds.text( label="" )
cmds.text( label="" )
cmds.textField( text = "Text here" )
cmds.text( label="" )
cmds.text( label="" )
cmds.text( label="" )
cmds.text( label="" )
cmds.rowColumnLayout( numberOfColumns = 4 )
with neat_indent(".."):
cmds.rowColumnLayout( numberOfColumns = 5 )
with neat_indent(".."):
cmds.radioButton( label = "Read file from path", width = 100 )
cmds.text( label="" )
cmds.button( label = "Browse" )
cmds.text( label="" )
cmds.button( label = "Validate" )
cmds.text( label="" )
cmds.text( label="" )
cmds.text( label="" )
上下文管理器功能强大。在这篇文章中,我使用了标准库模块中的contextmanager
装饰器contextlib
。您可以在此处阅读有关此技术的信息。关于这里with
一般。
同样,出于这个目的(目的之一),使Maya中的UI开发更加清洁,并且使用Pythonic @theodox创建了mGui模块。一探究竟。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句