Maya脚本edi中的Exec行范围

2024-10-01 15:32:41 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以在Maya脚本编辑器中的单个命令中执行某些行范围?(我用的是Python)

这个想法很清楚

用户通常需要在调试时选择部分代码。比如说,从脚本编辑器的当前选项卡的第20行到第45行(显然)。在

问题是,任何类似这样的命令:“execute(lines=[20,45]”或类似的命令。在

我一直在玩“runup”和“cmdScrollFieldExecuter”,但到现在还不走运。在


Tags: 代码用户命令脚本execute编辑器选项卡lines
3条回答

使用Maya的“脚本编辑器”(script editor)时,可以选择部分文本并单击Ctrl + Enter,因此仅执行脚本的这一部分。先前分配的变量也仍在内存中。在

基于此,您可以使用脚本模拟此行为:

import maya.cmds as cmds
from functools import partial

def getScriptEditor():
    for scriptEditor in cmds.lsUI( type=['cmdScrollFieldExecuter'] ):
        yield scriptEditor 

def executeLines(*args):  
    firstLine = cmds.intSliderGrp( "ExL_FirstLine", query=True, value=True)
    lastLine = cmds.intSliderGrp( "ExL_LastLine", query=True, value=True)
    scriptEditor = cmds.optionMenuGrp( "ExL_Editors", query=True, value=True)

    firstChar = None #First character to be selected as cmdScrollFieldExecuter select is made on char not lines
    lastChar = None #last character
    lineIncrement = 0 #Count line iteration

    for index, char in enumerate(cmds.cmdScrollFieldExecuter(scriptEditor, query=True, text=True)): #Iterate through all text, char by char
        if char == "\n" or index == 0: #If there is a carriage return
            lineIncrement+=1 #Increment the line number

            if lineIncrement == firstLine and not firstChar: #If this is the desired line and firstChar is still None
                firstChar = index+1 if char == "\n" else 0 #Special case for first line

            elif lineIncrement == lastLine+1 and not lastChar: #If this is the desired line and lastChar is still None
                lastChar = index

    cmds.cmdScrollFieldExecuter(scriptEditor, edit=True, select=[firstChar, lastChar]) #Select the desired range of character
    cmds.cmdScrollFieldExecuter(scriptEditor, edit=True, execute=True) #Execute them

def updateMaxValues(*args):
    scriptEditor = cmds.optionMenuGrp( "ExL_Editors", query=True, value=True)
    nbLines = cmds.cmdScrollFieldExecuter(scriptEditor, query=True, nl=True)
    cmds.intSliderGrp( "ExL_FirstLine", edit=True, maxValue = nbLines, fieldMaxValue=nbLines )
    cmds.intSliderGrp( "ExL_LastLine", edit=True, maxValue = nbLines, fieldMaxValue=nbLines )

def drawUI(*args):

    if cmds.window("Win_ExecuteLines", query=True, exists=True):
        cmds.deleteUI("Win_ExecuteLines")
    cmds.window( "Win_ExecuteLines", title='Execute lines of current script editor', w=390, h=50, rtf=True )
    cmds.columnLayout()

    cmds.button("ExL_RefreshButton", l="Refresh", c=drawUI, w=390)

    listEditors = getScriptEditor()
    cmds.optionMenuGrp( "ExL_Editors", l='Editors: ', cc=updateMaxValues )
    for edit in listEditors:
        cmds.menuItem( label=edit )
    cmds.intSliderGrp( "ExL_FirstLine", field=True, label='First Line: ', minValue=1, maxValue= 1000, fieldMinValue=1, fieldMaxValue=1000, value= 1 ) 
    cmds.intSliderGrp( "ExL_LastLine", field=True, label='Last Line: ', minValue=1, maxValue= 1000, fieldMinValue=1, fieldMaxValue=1000, value= 1 )

    cmds.button("ExL_ExecuteLines", l="Execute Lines", c=executeLines, w=390 )
    cmds.showWindow( "Win_ExecuteLines" )

drawUI()

这里最重要的方法是executeLines(),其他方法仅用于处理UI,但您可以复制并粘贴此代码,就像它在工具架按钮中一样。这是一个快速和肮脏的原型,你正在寻找,所以它可以大大改进。在

executeLines()逐字符解析所选cmdScrollFieldExecuter的文本。当踏上\n时,它会检查它是所需的第一行还是最后一行。然后,它选择与指定行范围相对应的正确字符范围并执行此代码。在

注意:

我无法从最后一个集中的cmdScrollFieldExecuter执行代码,因为当您更改所选窗口时(在本例中,当您单击execute按钮时),只有一个hasFocus标志,它是{}。在

您可以使用pythonexec语句尝试执行所选历史记录中的行。然而,这种方法的实用性将非常有限,因为这些行在exec中所做的事情与在函数内部做的事情不同:例如,它们不能访问在该选择之外定义的变量,或者访问原始代码可以看到的所有相同的名称空间。这只在非常有限的情况下有用。在

你知道你可以选择多个行并Ctrl+enter以这种方式执行它们,对吗?受上述相同限制。在

这在使用Python源代码的Python中是不可能的。在

相反,将代码拆分为函数,然后调用单个函数。E、 在第20-45行做一个函数。在

相关问题 更多 >

    热门问题