将外部 Python-脚本从磁盘导入 Maya
Importing external Python-Scripts from Disk into Maya
我正在尝试设置一个接口来调用特定文件夹中当前版本的 scipts。
我附加了我的个人脚本文件夹,并尝试按照提示导入它。
每当我按下按钮时,都会出现以下错误消息:
错误:名称错误:文件第 1 行:名称 'setVars' 未定义
我尝试了多种可能的导入技术。但是错误不断出现。
我将附上界面脚本和要调用的脚本之一。
接口:
import maya.cmds as mc
import sys
import os
import importlib
FolderContent = []
SysPath = 'C:/Users/[Username]/Documents/maya/2019/scripts/Personal'
sys.path.append(SysPath)
print sys.path
for file in os.listdir(SysPath):
if file.endswith(".py"):
FolderContent.append(file)
def UI(FolderContent):
x = 0
if mc.window("Run_Script", ex = True):
mc.deleteUI("Run_Script")
popUp = mc.window("Run_Script")
mc.gridLayout(nc = 1, ch = 30, cw = 300)
for i in FolderContent:
ScriptName = FolderContent[x]
mc.button(label = FolderContent[x], command = 'callFunc(\'%s\')' % (ScriptName), bgc = (0.25, 0.25, 0.25))
x += 1
mc.button(label = 'Cancel', command = 'close()', bgc = (0.92, 0.4, 0.4))
mc.window(popUp, e = True, s = False, h = 50, w = 100)
mc.showWindow()
UI(FolderContent)
def callFunc(ScriptName):
ScriptName = ScriptName[0:-3]
mymodule = importlib.import_module(ScriptName)
def close():
mc.deleteUI("Run_Script")
要调用的示例脚本:
import maya.cmds as mc
mc.select(hi = True)
BaseChain = mc.ls(sl = True)
ChainLength = len(BaseChain) - 1
###################################################################################
if mc.window("SetVariables", ex = True):
mc.deleteUI("SetVariables")
popUp = mc.window("SetVariables")
mc.gridLayout(nc = 2, ch = 30, cw = 200)
mc.text(label = "Side")
mc.textField("side")
mc.text(label = "Limb")
mc.textField("limb")
mc.text(label = "FK-Amount")
mc.textField("amount", fi = ChainLength)
cmd = 'setVars()'
close = 'closeWindow()'
mc.button(label = "OK", command = cmd, bgc = (0.2, 0.92, 0.75))
mc.button(label = "Cancel", command = close, bgc = (0.92, 0.4, 0.4))
mc.window(popUp, e = True, s = False, h = 50, w = 100)
mc.showWindow()
###################################################################################
def createChains(side, limb, amount):
mc.select(hi = True)
BndChain = mc.ls(sl = True)
x = 0
for i in BndChain:
mc.rename(BndChain[x], side + '_' + limb + '_BND_Chain_0' + str(x + 1))
x += 1
BndChain = mc.ls(sl = True)
ChainLength = len(BndChain)
mc.duplicate( n = side + '_' + limb + '_FK_Chain_01')
FkChain = mc.ls(sl = True)
mc.duplicate( n = side + '_' + limb + '_IK_Chain_01')
IkChain = mc.ls(sl = True)
parentChains(ChainLength, FkChain, IkChain, BndChain, side, limb, amount)
###################################################################################
def parentChains(ChainLength, FkChain, IkChain, BndChain, side, limb, amount):
x = 0
while x < ChainLength:
mc.select(FkChain[x])
mc.select(IkChain[x], add = True)
mc.select(BndChain[x], add = True)
mc.parentConstraint(mo = False)
x += 1
FKChainCtrl(ChainLength, FkChain, IkChain, BndChain, side, limb, amount)
###################################################################################
def FKChainCtrl(ChainLength, FkChain, IkChain, BndChain, side, limb, amount):
listLength = len(FkChain)
invertedNumber = listLength - int(amount)
doFK = listLength - invertedNumber
x = 0
Fk_ctrls = []
while x < doFK:
queryObj = FkChain[x]
currCtrl = mc.circle(n = side + '_' + limb + '_' + 'FK' + '_' + 'CTRL' + '_01', nr = [1, 0, 0], sw = 180)[0]
mc.curveRGBColor('translate.X', 1, 0, 1)
Fk_ctrls.append(currCtrl)
mc.parentConstraint(queryObj, currCtrl, mo = False)
mc.select(cl = True)
mc.select(queryObj)
mc.select(currCtrl, add = True)
mc.RemoveConstraintTarget()
mc.select(cl = True)
mc.select(currCtrl)
sel = mc.ls(sl = True)[0]
mc.FreezeTransformations(sel)
mc.parentConstraint(currCtrl, queryObj, mo = True)
if(x > 0):
mc.parent(Fk_ctrls[-1], Fk_ctrls[-2])
x += 1
mc.hide(FkChain)
IKChainCtrl(IkChain, BndChain, side, limb)
###################################################################################
def IKChainCtrl(IkChain, BndChain, side, limb):
currHandle = mc.ikHandle(n = side + '_' + limb + '_IkHandle', sj = IkChain[0], ee = IkChain[-1])[0]
mc.hide()
currHandlePos = mc.xform(currHandle, q = True, t = True, ws = True)
currCtrl = mc.circle(n = currHandle + '_CTRL', nr = [1, 0, 0], r = 1.5)
# currCtrl = mc.curve(n = currHandle + '_CTRL', d = 1, p = [(-0.75, 0, 0), (-0.75, 0, 2.5), (-2.5, 0, 2.5), (0, 0, 6.25), (2.5, 0, 2.5), (0.75, 0, 2.5), (0.75, 0, 0), (0.75, 0, -2.5), (2.5, 0, -2.5), (0, 0, -6.25), (-2.5, 0, -2.5), (-0.75, 0, -2.5), (-0.75, 0, 0)])
# mc.setAttr(currCtrl + '.overrideEnabled', 1)
# mc.setAttr(currCtrl + '.overrideColor', 21)
mc.select(IkChain[-2])
mc.select(currCtrl, add = True)
mc.parentConstraint(mo = False)
mc.select(IkChain[-2])
mc.select(currCtrl, add = True)
sel = mc.ls(sl = True)
mc.select(sel[0:-1], r = True)
mc.RemoveConstraintTarget()
mc.xform(currCtrl, t = [currHandlePos[0], currHandlePos[1], currHandlePos[2]])
mc.select(currCtrl, r = True)
mc.FreezeTransformations()
# mc.select('makeNurbCircle3', d = True)
mc.duplicate()
mc.parentConstraint(currCtrl, currHandle, mo = True)
mc.rename(side + '_' + limb + '_IkHandle_CTRL1', side + '_' + limb + '_Switch')
mc.xform(s = [1.5, 1.5, 1.5])
SwitchCtrl = mc.ls(sl = True)
mc.pointConstraint(BndChain[-1], SwitchCtrl)
currCtrl = mc.ls(sl = True)[0]
mc.hide(IkChain)
AddSwitchAttributes(currCtrl, side, limb)
###################################################################################
def AddSwitchAttributes(currCtrl, side, limb):
mc.addAttr(currCtrl, sn = 'FK', nn = 'FK', min = 0, max = 1, dv = 0, k = True, h = True)
mc.addAttr(currCtrl, sn = 'IK', nn = 'IK', min = 0, max = 1, dv = 1, k = True, h = True)
mc.addAttr(currCtrl, sn = 'Switch', nn = 'FK|IK Switch', min = 0, max = 1, dv = 1, k = True)
mc.connectAttr(side + '_' + limb + '_Switch.FK', side + '_' + limb + '_FK_CTRL_01.visibility')
mc.connectAttr(side + '_' + limb + '_Switch.IK', side + '_' + limb + '_IkHandle_CTRL.visibility')
i = 1
while i < ChainLength + 2:
mc.setDrivenKeyframe(side + '_' + limb + '_BND_Chain_0' + str(i) + '_parentConstraint1.' + side + '_' + limb + '_IK_Chain_0' + str(i) + 'W1', cd = side + '_' + limb + '_Switch.Switch', v = 1.0)
mc.setDrivenKeyframe(side + '_' + limb + '_BND_Chain_0' + str(i) + '_parentConstraint1.' + side + '_' + limb + '_FK_Chain_0' + str(i) + 'W0', cd = side + '_' + limb + '_Switch.Switch', v = 0.0)
i += 1
mc.setDrivenKeyframe(side + '_' + limb + '_Switch.IK', cd = side + '_' + limb + '_Switch.Switch', v = 1.0)
mc.setDrivenKeyframe(side + '_' + limb + '_Switch.FK', cd = side + '_' + limb + '_Switch.Switch', v = 0.0)
mc.setAttr(side + '_' + limb + '_Switch.Switch', 0)
y = 1
while y < ChainLength + 2:
mc.setDrivenKeyframe(side + '_' + limb + '_BND_Chain_0' + str(y) + '_parentConstraint1.' + side + '_' + limb + '_IK_Chain_0' + str(y) + 'W1', cd = side + '_' + limb + '_Switch.Switch', v = 0.0)
mc.setDrivenKeyframe(side + '_' + limb + '_BND_Chain_0' + str(y) + '_parentConstraint1.' + side + '_' + limb + '_FK_Chain_0' + str(y) + 'W0', cd = side + '_' + limb + '_Switch.Switch', v = 1.0)
y += 1
mc.setDrivenKeyframe(side + '_' + limb + '_Switch.IK', cd = side + '_' + limb + '_Switch.Switch', v = 0.0)
mc.setDrivenKeyframe(side + '_' + limb + '_Switch.FK', cd = side + '_' + limb + '_Switch.Switch', v = 1.0)
mc.setAttr(side + '_' + limb + '_Switch.Switch', 1)
mc.select(cl = True)
###################################################################################
def setVars():
side = mc.textField("side", q = True, tx = True)
limb = mc.textField("limb", q = True, tx = True)
amount = mc.textField("amount", q = True, tx = True)
createChains(side, limb, amount)
closeWindow()
###################################################################################
def closeWindow():
mc.deleteUI("SetVariables")
感谢您的帮助!
没有尝试...我想原因是您使用字符串“setVars()”作为命令名称。如果您从主模块调用它,则没有 setVars(),而是一个名为 WhatEverYourModuelNameIs.setVars().
的函数
您可以尝试像这样全部导入:
from WhatEverYourModuelNameIs import *
什么不是很好的解决方案,或者你可以尝试直接使用函数对象作为按钮的参数:
mc.button(label = "OK", command = setVars, bgc = (0.2, 0.92, 0.75))
好的,我试了一下,还是有一些问题。我想你直接从 Maya 中执行了你的主脚本?因为如果我把它放入一个模块并加载它,我会得到一个类似的错误,即找不到“close()”以及找不到“callFunc()”。两者的原因相同,不使用字符串即可轻松解决。
在您的主脚本中,您可以使用与上述相同的步骤替换按钮命令中的“close()”:
mc.button(label = 'Cancel', command = close, bgc = (0.92, 0.4, 0.4))
由于按钮命令会自动添加一个参数,因此您将为 close() 命令添加一个默认参数,例如 close(dummyArg)。
callFunc() 有点复杂,因为您使用了参数。这可以通过部分解决例如:
from functools import partial
for value in FolderContent:
mc.button(label = value, command = partial(callFunc, value), bgc = (0.25, 0.25, 0.25))
按钮调用将向调用添加另一个布尔值,以便 callFunc 将接收另一个参数,可以通过向 callFunc 定义添加另一个参数来捕获该参数,例如 callFunc(scriptName, value)。
这样你的函数对象就被使用了,而不是字符串。但是现在你有另一个问题,因为如果你第一次加载模块,你会得到一个错误
NameError: global name 'close' is not defined #
那是因为函数在我们使用的时候还没有定义,所以必须把函数定义放在UI定义函数之前。
现在,如果您的脚本被导入,您将遇到与字符串定义的函数类似的问题,但它们可以通过与主模块中相同的方式解决。
抱歉耽搁了大家,但我很高兴地报告现在正在运行!
结果确实是问题,我在执行 UI.
之前没有导入脚本
这就是此时的动态 UI:
import maya.cmds as mc
import sys
import os
FolderContent = []
SysPath = 'C:/Users/mkzie/Documents/maya/2019/scripts/Personal'
sys.path.append(SysPath)
for file in os.listdir(SysPath):
if file.endswith(".py"):
FolderContent.append(file[0:-3])
print FolderContent
x = 0
for i in FolderContent:
exec('import %s') % (FolderContent[x])
x += 1
def UI(FolderContent):
x = 0
if mc.window("CXL_Interface", ex = True):
mc.deleteUI("CXL_Interface")
Interface = mc.window("CXL_Interface", s = True, rtf = True, h = 1)
# mc.scrollLayout()
for i in FolderContent:
ScriptName = FolderContent[x]
col = mc.columnLayout(cat = ('left', -1))
mc.frameLayout(l = ScriptName, cll = True, cl = True, w = 300, mh = 5, mw = 0)
exec('%s.init()') % (ScriptName)
mc.setParent( '..' )
mc.setParent( '..' )
x += 1
mc.setParent( '..' )
mc.setParent( '..' )
mc.separator(h = 1, st = 'none')
mc.columnLayout(h = 45, adj = True)
mc.separator(h = 5, st = 'shelf')
mc.button(l = 'Close Interface', c = 'close()', h = 40, w = 300, bgc = (.9, 0.2, 0.3))
mc.window(Interface, e = True, h = 1, w = 1)
mc.showWindow()
UI(FolderContent)
def callFunc(ScriptName):
exec('%s.init()') % (ScriptName)
def close():
mc.deleteUI("CXL_Interface")
通过在调用 UI 之前这样做,在各自的脚本中使用字符串甚至不是问题:
mc.button(l = "OK", c = 'BuildKinematics.setVars()', bgc = (.4, .75, .4))
我觉得还是有点粗糙
我才刚刚开始做类似的事情(可能需要 4 周),所以非常感谢改进。
非常感谢您的大力帮助!
干杯。
我正在尝试设置一个接口来调用特定文件夹中当前版本的 scipts。
我附加了我的个人脚本文件夹,并尝试按照提示导入它。 每当我按下按钮时,都会出现以下错误消息:
错误:名称错误:文件第 1 行:名称 'setVars' 未定义
我尝试了多种可能的导入技术。但是错误不断出现。
我将附上界面脚本和要调用的脚本之一。
接口:
import maya.cmds as mc
import sys
import os
import importlib
FolderContent = []
SysPath = 'C:/Users/[Username]/Documents/maya/2019/scripts/Personal'
sys.path.append(SysPath)
print sys.path
for file in os.listdir(SysPath):
if file.endswith(".py"):
FolderContent.append(file)
def UI(FolderContent):
x = 0
if mc.window("Run_Script", ex = True):
mc.deleteUI("Run_Script")
popUp = mc.window("Run_Script")
mc.gridLayout(nc = 1, ch = 30, cw = 300)
for i in FolderContent:
ScriptName = FolderContent[x]
mc.button(label = FolderContent[x], command = 'callFunc(\'%s\')' % (ScriptName), bgc = (0.25, 0.25, 0.25))
x += 1
mc.button(label = 'Cancel', command = 'close()', bgc = (0.92, 0.4, 0.4))
mc.window(popUp, e = True, s = False, h = 50, w = 100)
mc.showWindow()
UI(FolderContent)
def callFunc(ScriptName):
ScriptName = ScriptName[0:-3]
mymodule = importlib.import_module(ScriptName)
def close():
mc.deleteUI("Run_Script")
要调用的示例脚本:
import maya.cmds as mc
mc.select(hi = True)
BaseChain = mc.ls(sl = True)
ChainLength = len(BaseChain) - 1
###################################################################################
if mc.window("SetVariables", ex = True):
mc.deleteUI("SetVariables")
popUp = mc.window("SetVariables")
mc.gridLayout(nc = 2, ch = 30, cw = 200)
mc.text(label = "Side")
mc.textField("side")
mc.text(label = "Limb")
mc.textField("limb")
mc.text(label = "FK-Amount")
mc.textField("amount", fi = ChainLength)
cmd = 'setVars()'
close = 'closeWindow()'
mc.button(label = "OK", command = cmd, bgc = (0.2, 0.92, 0.75))
mc.button(label = "Cancel", command = close, bgc = (0.92, 0.4, 0.4))
mc.window(popUp, e = True, s = False, h = 50, w = 100)
mc.showWindow()
###################################################################################
def createChains(side, limb, amount):
mc.select(hi = True)
BndChain = mc.ls(sl = True)
x = 0
for i in BndChain:
mc.rename(BndChain[x], side + '_' + limb + '_BND_Chain_0' + str(x + 1))
x += 1
BndChain = mc.ls(sl = True)
ChainLength = len(BndChain)
mc.duplicate( n = side + '_' + limb + '_FK_Chain_01')
FkChain = mc.ls(sl = True)
mc.duplicate( n = side + '_' + limb + '_IK_Chain_01')
IkChain = mc.ls(sl = True)
parentChains(ChainLength, FkChain, IkChain, BndChain, side, limb, amount)
###################################################################################
def parentChains(ChainLength, FkChain, IkChain, BndChain, side, limb, amount):
x = 0
while x < ChainLength:
mc.select(FkChain[x])
mc.select(IkChain[x], add = True)
mc.select(BndChain[x], add = True)
mc.parentConstraint(mo = False)
x += 1
FKChainCtrl(ChainLength, FkChain, IkChain, BndChain, side, limb, amount)
###################################################################################
def FKChainCtrl(ChainLength, FkChain, IkChain, BndChain, side, limb, amount):
listLength = len(FkChain)
invertedNumber = listLength - int(amount)
doFK = listLength - invertedNumber
x = 0
Fk_ctrls = []
while x < doFK:
queryObj = FkChain[x]
currCtrl = mc.circle(n = side + '_' + limb + '_' + 'FK' + '_' + 'CTRL' + '_01', nr = [1, 0, 0], sw = 180)[0]
mc.curveRGBColor('translate.X', 1, 0, 1)
Fk_ctrls.append(currCtrl)
mc.parentConstraint(queryObj, currCtrl, mo = False)
mc.select(cl = True)
mc.select(queryObj)
mc.select(currCtrl, add = True)
mc.RemoveConstraintTarget()
mc.select(cl = True)
mc.select(currCtrl)
sel = mc.ls(sl = True)[0]
mc.FreezeTransformations(sel)
mc.parentConstraint(currCtrl, queryObj, mo = True)
if(x > 0):
mc.parent(Fk_ctrls[-1], Fk_ctrls[-2])
x += 1
mc.hide(FkChain)
IKChainCtrl(IkChain, BndChain, side, limb)
###################################################################################
def IKChainCtrl(IkChain, BndChain, side, limb):
currHandle = mc.ikHandle(n = side + '_' + limb + '_IkHandle', sj = IkChain[0], ee = IkChain[-1])[0]
mc.hide()
currHandlePos = mc.xform(currHandle, q = True, t = True, ws = True)
currCtrl = mc.circle(n = currHandle + '_CTRL', nr = [1, 0, 0], r = 1.5)
# currCtrl = mc.curve(n = currHandle + '_CTRL', d = 1, p = [(-0.75, 0, 0), (-0.75, 0, 2.5), (-2.5, 0, 2.5), (0, 0, 6.25), (2.5, 0, 2.5), (0.75, 0, 2.5), (0.75, 0, 0), (0.75, 0, -2.5), (2.5, 0, -2.5), (0, 0, -6.25), (-2.5, 0, -2.5), (-0.75, 0, -2.5), (-0.75, 0, 0)])
# mc.setAttr(currCtrl + '.overrideEnabled', 1)
# mc.setAttr(currCtrl + '.overrideColor', 21)
mc.select(IkChain[-2])
mc.select(currCtrl, add = True)
mc.parentConstraint(mo = False)
mc.select(IkChain[-2])
mc.select(currCtrl, add = True)
sel = mc.ls(sl = True)
mc.select(sel[0:-1], r = True)
mc.RemoveConstraintTarget()
mc.xform(currCtrl, t = [currHandlePos[0], currHandlePos[1], currHandlePos[2]])
mc.select(currCtrl, r = True)
mc.FreezeTransformations()
# mc.select('makeNurbCircle3', d = True)
mc.duplicate()
mc.parentConstraint(currCtrl, currHandle, mo = True)
mc.rename(side + '_' + limb + '_IkHandle_CTRL1', side + '_' + limb + '_Switch')
mc.xform(s = [1.5, 1.5, 1.5])
SwitchCtrl = mc.ls(sl = True)
mc.pointConstraint(BndChain[-1], SwitchCtrl)
currCtrl = mc.ls(sl = True)[0]
mc.hide(IkChain)
AddSwitchAttributes(currCtrl, side, limb)
###################################################################################
def AddSwitchAttributes(currCtrl, side, limb):
mc.addAttr(currCtrl, sn = 'FK', nn = 'FK', min = 0, max = 1, dv = 0, k = True, h = True)
mc.addAttr(currCtrl, sn = 'IK', nn = 'IK', min = 0, max = 1, dv = 1, k = True, h = True)
mc.addAttr(currCtrl, sn = 'Switch', nn = 'FK|IK Switch', min = 0, max = 1, dv = 1, k = True)
mc.connectAttr(side + '_' + limb + '_Switch.FK', side + '_' + limb + '_FK_CTRL_01.visibility')
mc.connectAttr(side + '_' + limb + '_Switch.IK', side + '_' + limb + '_IkHandle_CTRL.visibility')
i = 1
while i < ChainLength + 2:
mc.setDrivenKeyframe(side + '_' + limb + '_BND_Chain_0' + str(i) + '_parentConstraint1.' + side + '_' + limb + '_IK_Chain_0' + str(i) + 'W1', cd = side + '_' + limb + '_Switch.Switch', v = 1.0)
mc.setDrivenKeyframe(side + '_' + limb + '_BND_Chain_0' + str(i) + '_parentConstraint1.' + side + '_' + limb + '_FK_Chain_0' + str(i) + 'W0', cd = side + '_' + limb + '_Switch.Switch', v = 0.0)
i += 1
mc.setDrivenKeyframe(side + '_' + limb + '_Switch.IK', cd = side + '_' + limb + '_Switch.Switch', v = 1.0)
mc.setDrivenKeyframe(side + '_' + limb + '_Switch.FK', cd = side + '_' + limb + '_Switch.Switch', v = 0.0)
mc.setAttr(side + '_' + limb + '_Switch.Switch', 0)
y = 1
while y < ChainLength + 2:
mc.setDrivenKeyframe(side + '_' + limb + '_BND_Chain_0' + str(y) + '_parentConstraint1.' + side + '_' + limb + '_IK_Chain_0' + str(y) + 'W1', cd = side + '_' + limb + '_Switch.Switch', v = 0.0)
mc.setDrivenKeyframe(side + '_' + limb + '_BND_Chain_0' + str(y) + '_parentConstraint1.' + side + '_' + limb + '_FK_Chain_0' + str(y) + 'W0', cd = side + '_' + limb + '_Switch.Switch', v = 1.0)
y += 1
mc.setDrivenKeyframe(side + '_' + limb + '_Switch.IK', cd = side + '_' + limb + '_Switch.Switch', v = 0.0)
mc.setDrivenKeyframe(side + '_' + limb + '_Switch.FK', cd = side + '_' + limb + '_Switch.Switch', v = 1.0)
mc.setAttr(side + '_' + limb + '_Switch.Switch', 1)
mc.select(cl = True)
###################################################################################
def setVars():
side = mc.textField("side", q = True, tx = True)
limb = mc.textField("limb", q = True, tx = True)
amount = mc.textField("amount", q = True, tx = True)
createChains(side, limb, amount)
closeWindow()
###################################################################################
def closeWindow():
mc.deleteUI("SetVariables")
感谢您的帮助!
没有尝试...我想原因是您使用字符串“setVars()”作为命令名称。如果您从主模块调用它,则没有 setVars(),而是一个名为 WhatEverYourModuelNameIs.setVars().
的函数您可以尝试像这样全部导入:
from WhatEverYourModuelNameIs import *
什么不是很好的解决方案,或者你可以尝试直接使用函数对象作为按钮的参数:
mc.button(label = "OK", command = setVars, bgc = (0.2, 0.92, 0.75))
好的,我试了一下,还是有一些问题。我想你直接从 Maya 中执行了你的主脚本?因为如果我把它放入一个模块并加载它,我会得到一个类似的错误,即找不到“close()”以及找不到“callFunc()”。两者的原因相同,不使用字符串即可轻松解决。
在您的主脚本中,您可以使用与上述相同的步骤替换按钮命令中的“close()”:
mc.button(label = 'Cancel', command = close, bgc = (0.92, 0.4, 0.4))
由于按钮命令会自动添加一个参数,因此您将为 close() 命令添加一个默认参数,例如 close(dummyArg)。
callFunc() 有点复杂,因为您使用了参数。这可以通过部分解决例如:
from functools import partial
for value in FolderContent:
mc.button(label = value, command = partial(callFunc, value), bgc = (0.25, 0.25, 0.25))
按钮调用将向调用添加另一个布尔值,以便 callFunc 将接收另一个参数,可以通过向 callFunc 定义添加另一个参数来捕获该参数,例如 callFunc(scriptName, value)。 这样你的函数对象就被使用了,而不是字符串。但是现在你有另一个问题,因为如果你第一次加载模块,你会得到一个错误
NameError: global name 'close' is not defined #
那是因为函数在我们使用的时候还没有定义,所以必须把函数定义放在UI定义函数之前。 现在,如果您的脚本被导入,您将遇到与字符串定义的函数类似的问题,但它们可以通过与主模块中相同的方式解决。
抱歉耽搁了大家,但我很高兴地报告现在正在运行! 结果确实是问题,我在执行 UI.
之前没有导入脚本这就是此时的动态 UI:
import maya.cmds as mc
import sys
import os
FolderContent = []
SysPath = 'C:/Users/mkzie/Documents/maya/2019/scripts/Personal'
sys.path.append(SysPath)
for file in os.listdir(SysPath):
if file.endswith(".py"):
FolderContent.append(file[0:-3])
print FolderContent
x = 0
for i in FolderContent:
exec('import %s') % (FolderContent[x])
x += 1
def UI(FolderContent):
x = 0
if mc.window("CXL_Interface", ex = True):
mc.deleteUI("CXL_Interface")
Interface = mc.window("CXL_Interface", s = True, rtf = True, h = 1)
# mc.scrollLayout()
for i in FolderContent:
ScriptName = FolderContent[x]
col = mc.columnLayout(cat = ('left', -1))
mc.frameLayout(l = ScriptName, cll = True, cl = True, w = 300, mh = 5, mw = 0)
exec('%s.init()') % (ScriptName)
mc.setParent( '..' )
mc.setParent( '..' )
x += 1
mc.setParent( '..' )
mc.setParent( '..' )
mc.separator(h = 1, st = 'none')
mc.columnLayout(h = 45, adj = True)
mc.separator(h = 5, st = 'shelf')
mc.button(l = 'Close Interface', c = 'close()', h = 40, w = 300, bgc = (.9, 0.2, 0.3))
mc.window(Interface, e = True, h = 1, w = 1)
mc.showWindow()
UI(FolderContent)
def callFunc(ScriptName):
exec('%s.init()') % (ScriptName)
def close():
mc.deleteUI("CXL_Interface")
通过在调用 UI 之前这样做,在各自的脚本中使用字符串甚至不是问题:
mc.button(l = "OK", c = 'BuildKinematics.setVars()', bgc = (.4, .75, .4))
我觉得还是有点粗糙
我才刚刚开始做类似的事情(可能需要 4 周),所以非常感谢改进。
非常感谢您的大力帮助! 干杯。