回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在创建一个自定义的浮动窗口,在那里我可以有一个按钮,像一个架子,为我调用和运行脚本。非常有用的造型艺术家像我这样谁不做很多脚本!:P我希望在不必太多修改脚本的情况下解决此问题,但我们将看到必须执行的操作!:D个</p>
<p>所以现在我有两个脚本,脚本A和脚本B。脚本B保存为“我的自定义脚本”_姓名.py". 我试着用脚本运行它,但是变量从来没有正确定义过。你知道吗</p>
<p>当我运行Script\u A并将Script\u B保存在适当的位置时,UI甚至不会加载,因为它在定义的第一个变量上出错。你知道吗</p>
<pre><code># Error: NameError: file C:/Users/polyshifter/Documents/maya/2019/prefs/scripts\my_custom_script_name.py line 2: global name 'mc' is not defined #
</code></pre>
<p>所以我们还没找到其他变量,它就坏了。你知道吗</p>
<p>编写脚本</p>
<pre><code>import maya.cmds as mc
import maya.mel as mel
# Create a custom floating window with
if mc.window('ToolsWindow', q=True, exists=True):
mc.deleteUI('ToolsWindow')
if mc.workspaceControl('ToolsWorkspaceControl', q=True, exists=True):
mc.deleteUI('ToolsWorkspaceControl')
mc.window('ToolsWindow')
mc.tabLayout('ToolsTabs')
###IMPORT PANEL###
tabMenu = mc.columnLayout("Menu", adj=True)
separator_long = mc.separator(
height=10,
style='in')
mc.button(label="MyCustomScript", command = "my_custom_script_com()")
mc.setParent('..')
mc.showWindow()
#### My custom script ####
def my_custom_script_com(*args):
import my_custom_script_name
my_custom_script_name.run_my_custom_script_ui()
</code></pre>
<p>脚本B</p>
<pre><code>def run_my_custom_script_ui():
import maya.cmds as mc
if mc.window('my_script_ui', q=True, exists=True):
mc.deleteUI('my_script_ui')
else:
mc.window('my_script_ui')
mc.columnLayout("Menu", adj=True)
mc.button(label="Please_print", command = "please_print()")
mc.setParent('..')
mc.showWindow()
def please_print():
print "OMG IT'S FINALLY WORKING!! YAY!!"
</code></pre>
<p>我曾尝试将脚本\u B放在脚本\u A的末尾,例如:</p>
<pre><code>def my_custom_script_com(*args):
import maya.cmds as mc
if mc.window('my_script_ui', q=True, exists=True):
mc.deleteUI('my_script_ui')
else:
mc.window('my_script_ui')
mc.columnLayout("Menu", adj=True)
mc.button(label="Please_print", command = "please_print()")
mc.setParent('..')
mc.showWindow()
def please_print():
print "OMG IT'S FINALLY WORKING!! YAY!!"
</code></pre>
<p>第一个用户界面加载,第二个用户界面加载,我看到了我的按钮,但当我点击它时,我得到:</p>
<pre><code># Error: NameError: file <maya console> line 1: name 'please_print' is not defined #
</code></pre>
<p>如果有太多不需要的代码或者任何我试图尽可能把它切碎的东西,我道歉。这是我的第一个问题!你知道吗</p>
<p>让这些变量正常工作的任何帮助都将是惊人的!<;3提前谢谢!你知道吗</p>
<h2>非常感谢大家的帮助!西奥多能够完美地解决这个问题!你知道吗</h2>
<h2>然而。。。。这个问题一直存在于我的非简体字中!你知道吗</h2>
<p>我有一个艺术家莱昂纳多·莱齐的剧本。”<a href="https://www.linkedin.com/pulse/super-bool-tool-python-leonardo-iezzi/" rel="nofollow noreferrer">https://www.linkedin.com/pulse/super-bool-tool-python-leonardo-iezzi/</a>“
当我使用他的脚本并试图将其实现到我的脚本中时,它会导致错误。首先pma没有定义,然后他的定义就没有了。你知道吗</p>
<p>我试过制作他的UI(教程和主界面)并将它们定义为命令,但这对我来说也没有效果。原因是(根据我今天学到的:D),在调用脚本\u B(现在的布尔脚本)之后,maya不再具有这些函数。你知道吗</p>
<p>我找到的唯一有效的方法是,将布尔脚本放在我的主脚本中,这样它就可以使用了。然后将他的UI定义为命令,然后用我的按钮从脚本中单击列出这两个命令。然后运行它。但是,请返回并删除脚本中的定义,然后取消删除,然后重新运行脚本。因此,定义仍然是从以前运行的定义,但现在的用户界面是全局性的。。。很明显,当你重新启动maya的时候,这种情况就会中断,我完全不知道如何定义它们,同时仍然可以调用它们(是这样吗?)当我点击按钮。你知道吗</p>
<p>这显然是一个黑客和愚蠢的做法,但我不明白如何让“超级布尔工具”从我的脚本运行,只有通过采购超级布尔_工具.py你知道吗</p>
<p>下面是我到目前为止的想法,但这里的想法是<strong><em>而不是</em></strong>在我的shelf ui脚本中运行它。我想调用python脚本,这样它就可以独立运行了。你知道吗</p>
<pre><code>import maya.cmds as mc
import maya.mel as mel
import pymel.all as pma
BOOLEANMODE_ADD = 1
BOOLEANMODE_SUBTRACT = 2
PRIMITIVE_CUBE = 0
PRIMITIVE_CYLINDER = 1
PRIMITIVE_SPHERE = 2
PRIMITIVE_CUSTOM = 3
def cleanUp ():
pma.delete (constructionHistory=True)
#pma.select ()
pma.delete ("*_ctrl*")
def hider(option):
if (option == 0):
pma.hide ()
elif (option == 1):
pma.select ("*_ctrl*")
pma.hide ()
elif (option == 2):
pma.select ("*_ctrl*")
pma.showHidden ()
pma.select (clear=True)
def fixMaterial():
pma.hyperShade( assign="lambert1" )
def triangulate():
pma.polyTriangulate()
def creator(primitives):
selection = pma.ls(sl=True)
for x in selection:
if primitives == PRIMITIVE_CUBE:
a = makeCube() #Create cube
if primitives == PRIMITIVE_CYLINDER:
a = makeCyl() #Create cyl
if primitives == PRIMITIVE_SPHERE:
a = makeSphere() #Create sphere
if primitives == PRIMITIVE_CUSTOM:
a = selection[1]
x = selection[0]
pma.select (a)
b = createController(a)
meshConstrainer (b,a)
operator(x,a)
pma.select (b)
def operator(meshA, meshB):
booleanmode = get_boolean_mode()
pma.polyBoolOp( meshA, meshB, op=booleanmode, n="basemesh" )
pma.hyperShade( assign="lambert1" ) #REMINDER: Need to be replaced with the actual assigned material and not with a lambert1 so for who is working with other materials can easyly keep using that
def get_boolean_mode():
if pma.radioButton(addRadioB, query = True, select = True) :
return BOOLEANMODE_ADD
if pma.radioButton(subRadioB, query = True, select = True) :
return BOOLEANMODE_SUBTRACT
return None
def makeCube():
cubeTransform = pma.polyCube(n="cubetobool", w=1, h=1, d=1, sx=1, sy=1, sz=1)[0]
return cubeTransform
def makeCyl():
cylTransform = pma.polyCylinder(n="cubetobool", r=1, h=2, sx=20)[0]
return cylTransform
def makeSphere():
sphereTransform = pma.polySphere(n="cubetobool", r=1, sx=20, sy=20, cuv=2)[0]
return sphereTransform
def meshConstrainer(constrainer, constrained):
pma.scaleConstraint( constrainer, constrained, maintainOffset=True)
pma.parentConstraint( constrainer, constrained, maintainOffset=True)
def createController(object):
#object = pma.ls(sl=True)
pivotObj = pma.xform(object,query=True,t=True,worldSpace=True)
edges = pma.filterExpand(pma.polyListComponentConversion(te=1),sm=32,ex=1) # convert edges to curve ancd create one object
for edge in edges:
vtx = pma.ls(pma.polyListComponentConversion(edge,fe=1,tv=1),fl=1)
p1 = pma.pointPosition(vtx[0])
p2 = pma.pointPosition(vtx[1])
curves = pma.curve(n="line_ctrl_curve", d=1,p=(p1,p2))
ctrl = pma.curve (n="bool_ctrl", d=1,ws=True, p=pivotObj)
pma.xform (centerPivots=True)
for curveEdge in pma.ls ("line_ctrl*"):
pma.parent(curveEdge,ctrl, s=1, r=1)
pma.rename(curveEdge, "shapeunused")
transforms = pma.ls(type='transform')
deleteList = []
for tran in transforms:
if pma.nodeType(tran) == 'transform':
children = pma.listRelatives(tran, c=True)
if children is None:
#print '%s, has no childred' %(tran)
deleteList.append(tran)
if not deleteList:
pma.delete(deleteList)
return ctrl
#################TUTORIAL
def super_bool_tut():
windowNameTut = "Tutorial"
if (pma.window(windowNameTut , exists=True)):
pma.deleteUI(windowNameTut)
windowTutorial = pma.window(windowNameTut, title = windowNameTut, width = 400, height = 300, backgroundColor = [0.2, 0.2, 0.2])
pma.columnLayout( "testColumn", adjustableColumn = True)
pma.text("intro", label = "This tool is a super tool to make booleans wrote by Leonardo Iezzi. To make it works correctly, you need to have your base mesh already even if just a cube. With your base mesh selected just press one of the three buttons on the windows to subtract or add those primitives. If you want to use a custom mesh for the operation: select your base mesh then the custom one (it's important to pick your base mesh first) and then press the 'Use custom mesh' button. After you have done, select your base mesh and press 'Clean Up.'",wordWrap= True, height = 100, backgroundColor = [0.2, 0.2, 0.2], align='left', parent = "testColumn")
#pma.text("first", label = "1- Select always your main mesh first",wordWrap= True, height = 40, backgroundColor = [0.2, 0.2, 0.2], align='left', parent = "testColumn")
#pma.text("secondo", label = "2- In case you want to use a custom mesh: Select first your main mesh then the mesh you want to add or subtract",wordWrap= True, height = 40, backgroundColor = [0.2, 0.2, 0.2], align='left', parent = "testColumn")
#pma.text("third", label = "3- Everythong should works",wordWrap= True, height = 40, backgroundColor = [0.2, 0.2, 0.2], align='left', parent = "testColumn")
pma.separator(parent = "testColumn", height=20)
pma.button("goit", label = "Got it", width = 120, height = 40, backgroundColor = [0.5, 0.5, 0.5], parent = "testColumn", command = "pma.deleteUI(windowNameTut)")
pma.showWindow()
################################################################################################UI#################################################
# @@@@@@@ THIS IS POLYSHIFTER!! I HAVE ADDED THIS AS A FUNCTION INSTEAD OF LEAVING IT UNINDENTED
def super_bool_ui():
windowName = "SuperBool"
windowSize = (120, 200)
if (pma.window(windowName , exists=True)):
pma.deleteUI(windowName)
window = pma.window( windowName, title= windowName, width = 120, height = 200 )
pma.columnLayout( "mainColumn", adjustableColumn = True)
################################################################################################UI#################################################
pma.gridLayout("nameGridLayout01", numberOfRowsColumns = (1,4), cellWidthHeight = (40,40), parent = "mainColumn")
pma.symbolButton("nameButton1", image = "polyCube.png", width = 40, height = 40, backgroundColor = [0.2, 0.2, 0.2], parent = "nameGridLayout01", command = "creator(PRIMITIVE_CUBE)")
pma.symbolButton("nameButton2", image = "polyCylinder.png", width = 40, height = 40, backgroundColor = [0.2, 0.2, 0.2], parent = "nameGridLayout01", command = "creator(PRIMITIVE_CYLINDER)")
pma.symbolButton("nameButton3", image = "polySphere.png", width = 40, height = 40, backgroundColor = [0.2, 0.2, 0.2], parent = "nameGridLayout01", command = "creator(PRIMITIVE_SPHERE)")
pma.columnLayout("columnLayoutName01", adjustableColumn = True, backgroundColor = [0.2, 0.2, 0.2])
pma.radioCollection("collection10", parent = "columnLayoutName01")
subRadioB = pma.radioButton("subRadio", select = True, label = "Sub")
addRadioB = pma.radioButton("addRadio", label = "Add")
pma.setParent( '..' )
pma.setParent( '..' )
################################################################################################UI#################################################
pma.separator(parent = "mainColumn", height=20)
pma.button("customMeshB", label = "Use Custom Mesh", width = 120, height = 40, backgroundColor = [0.2, 0.2, 0.2], parent = "mainColumn", command = "creator(PRIMITIVE_CUSTOM)")
pma.separator(parent = "mainColumn", height=20)
################################################################################################UI#################################################
pma.gridLayout("nameGridLayout03", numberOfRowsColumns = (1,3), cellWidthHeight = (53,40), parent = "mainColumn")
pma.button("hidSelB", label = "Hide Sel", height = 40, backgroundColor = [0.2, 0.2, 0.2], parent = "nameGridLayout03", command = "hider(0)")
pma.button("hidAllB", label = "Hide All", height = 40, backgroundColor = [0.2, 0.2, 0.2], parent = "nameGridLayout03", command = "hider(1)")
pma.button("showAll", label = "Show All", height = 40, backgroundColor = [0.2, 0.2, 0.2], parent = "nameGridLayout03", command = "hider(2)")
pma.separator(parent = "mainColumn", height=20)
pma.button("clean", label = "Clean Up", width = 120, height = 40, backgroundColor = [0.2, 0.2, 0.2], parent = "mainColumn", command = "cleanUp()")
pma.separator(parent = "mainColumn", height=20)
################################################################################################UI#################################################
################################################################################################UI#################################################
pma.gridLayout("nameGridLayout02", numberOfRowsColumns = (1,2), cellWidthHeight = (80,40), parent = "mainColumn")
pma.button("triangB", label = "Triangulate", width = 120, height = 40, backgroundColor = [0.2, 0.2, 0.2], parent = "nameGridLayout02", command = "triangulate()")
pma.button("fixMatB", label = "FixMaterial", width = 120, height = 40, backgroundColor = [0.2, 0.2, 0.2], parent = "nameGridLayout02", command = "fixMaterial()")
################################################################################################UI#################################################
pma.showWindow()
###################################################################
################## ###################
################## END OF SUPER BOOL TOOL ###################
################## ###################
###################################################################
###################################################################
################## ###################
################## BEGINNING OF MY UI SCRIPT ###################
################## ###################
###################################################################
# Create a custom floating window with
if mc.window('ToolsWindow', q=True, exists=True):
mc.deleteUI('ToolsWindow')
if mc.workspaceControl('ToolsWorkspaceControl', q=True, exists=True):
mc.deleteUI('ToolsWorkspaceControl')
mc.window('ToolsWindow')
mc.tabLayout('ToolsTabs')
#########################################################
################## IMPORTING PANEL ################
#########################################################
tabMenu = mc.columnLayout("Menu", adj=True)
separator_long = mc.separator(
height=10,
style='in')
mc.button(label="MyCustomScript", command = "my_custom_script_com()")
mc.setParent('..')
mc.showWindow()
#### My custom script ####
def my_custom_script_com(*args):
super_bool_tool_ui()
super_bool_tool_tut()
</code></pre>