您可以进入我的提问历史,我对functools回答了很多import maya.cmds as cmdsimport randomimport functoolscolorList = cmds.ls('colorSet*')def createUI(pWindowTitle): windowID = 'myWindowID' if cmds.window( windowID, exists=True ): cmds.deleteUI(windowID ) cmds.window( windowID, title=pWindowTitle, s=False, rtf=True ) cmds.rowColumnLayout( numberOfColumns=1, columnWidth=[(1,200)]) cmds.text(label= 'Max Value Lock') noiseAttribute = cmds.floatSliderGrp( min=0.0, max=1.0, value=1, field=True) cmds.button( label='Noise', command=functools.partial(ui_addNoise, noiseAttribute) ) def cancelCallback( *pArgs ): if cmds.window( windowID, exists=True ): cmds.deleteUI( windowID) cmds.button( label='Cancel', command=cancelCallback ) cmds.showWindow()def ui_addNoise(noiseSlider, *args): value = cmds.floatSliderGrp(noiseSlider, q=True, value=True) addNoise(value)def pNoiseVerts(object, value): verts = range(cmds.polyEvaluate(object, vertex=True)) random.shuffle(verts) for id in verts: # you should never select things in maya, pass it as variable : vtx = '{}.vtx[{}]'.format(object, id) brightness = random.uniform(0.0, value) cmds.polyColorPerVertex(vtx, rgb=(brightness, brightness, brightness)) cmds.setAttr(object + '.displayColors', True)def addNoise(value): if len(colorList) > 0: cmds.delete(colorList) objects = cmds.ls( sl=True, long=True) if len(objects) > 0: setList = cmds.ls('colorSet*') result = cmds.polyColorSet ( create=True, colorSet='colorSet#') result = cmds.polyColorPerVertex ( rgb=[0.5,0.5,0.5]) result = cmds.polyColorSet ( create=True, colorSet='colorSet#') for object in objects: pNoiseVerts(object, value) else: cmds.inViewMessage (amg='Message: <hl>Please select an object first</hl>.', pos='midCenter', fade=True )createUI( 'Config')I have a piece of code which generates noise. The noise is generated inside a 0.0 to 1.0 range. As long as I set a defined number the code works. If I were to allow users to select maximum range for the number with a slider it stops working.I have a slider which replaces the value of 1.0 inside the brightness calculation. As soon as I replace 1.0 value inside Brightness with a slider generated value called noiseAttribute the code breaks. It gives no error and technically runs but just makes the object black instead of locking the color value. import maya.cmds as cmdsimport randomimport functoolscolorList = cmds.ls('colorSet*')def createUI( pWindowTitle, pNoiseVerts): windowID = 'myWindowID' if cmds.window( windowID, exists=True ): cmds.deleteUI(windowID ) cmds.window( windowID, title=pWindowTitle, s=False, rtf=True ) cmds.rowColumnLayout( numberOfColumns=1, columnWidth=[(1,200)]) cmds.text(label= 'Max Value Lock') noiseAttribute = cmds.floatSliderGrp( min=0.0, max=1.0, value=1, field=True) cmds.button( label='Noise', command=functools.partial (addNoise) ) def cancelCallback( *pArgs ): if cmds.window( windowID, exists=True ): cmds.deleteUI( windowID) cmds.button( label='Cancel', command=cancelCallback ) cmds.showWindow()def pNoiseVerts(object, noiseAttribute): verts = range(cmds.polyEvaluate(object, vertex=True)) random.shuffle(verts) for vertex in verts: cmds.select(object + '.vtx[' + str(vertex) + ']') brightness = random.uniform(0.0, noiseAttribute) cmds.polyColorPerVertex(rgb=(brightness, brightness, brightness)) cmds.setAttr(object + '.displayColors', True)def addNoise(noiseAttribute, *args): if len(colorList) > 0: cmds.delete(colorList) objects = cmds.ls( sl=True, long=True) if len(objects) > 0: setList = cmds.ls('colorSet*') result = cmds.polyColorSet ( create=True, colorSet='colorSet#') result = cmds.polyColorPerVertex ( rgb=[0.5,0.5,0.5]) result = cmds.polyColorSet ( create=True, colorSet='colorSet#') for object in objects: pNoiseVerts(object, noiseAttribute) else: cmds.inViewMessage (amg='Message: <hl>Please select an object first</hl>.', pos='midCenter', fade=True )createUI( 'Config', pNoiseVerts)As mentioned before the object turns black instead of having its max color value locked. 解决方案 You dont pass any arguments with your functoolsHere is one of my answer on the same topic : Need Help Making Buttons to perform for loops when you input a numberHow to print the value of intField in Maya pythonMaya Python - Using data from UIYou can go in my history of questions, I answered a lot about functoolsimport maya.cmds as cmdsimport randomimport functoolscolorList = cmds.ls('colorSet*')def createUI(pWindowTitle): windowID = 'myWindowID' if cmds.window( windowID, exists=True ): cmds.deleteUI(windowID ) cmds.window( windowID, title=pWindowTitle, s=False, rtf=True ) cmds.rowColumnLayout( numberOfColumns=1, columnWidth=[(1,200)]) cmds.text(label= 'Max Value Lock') noiseAttribute = cmds.floatSliderGrp( min=0.0, max=1.0, value=1, field=True) cmds.button( label='Noise', command=functools.partial(ui_addNoise, noiseAttribute) ) def cancelCallback( *pArgs ): if cmds.window( windowID, exists=True ): cmds.deleteUI( windowID) cmds.button( label='Cancel', command=cancelCallback ) cmds.showWindow()def ui_addNoise(noiseSlider, *args): value = cmds.floatSliderGrp(noiseSlider, q=True, value=True) addNoise(value)def pNoiseVerts(object, value): verts = range(cmds.polyEvaluate(object, vertex=True)) random.shuffle(verts) for id in verts: # you should never select things in maya, pass it as variable : vtx = '{}.vtx[{}]'.format(object, id) brightness = random.uniform(0.0, value) cmds.polyColorPerVertex(vtx, rgb=(brightness, brightness, brightness)) cmds.setAttr(object + '.displayColors', True)def addNoise(value): if len(colorList) > 0: cmds.delete(colorList) objects = cmds.ls( sl=True, long=True) if len(objects) > 0: setList = cmds.ls('colorSet*') result = cmds.polyColorSet ( create=True, colorSet='colorSet#') result = cmds.polyColorPerVertex ( rgb=[0.5,0.5,0.5]) result = cmds.polyColorSet ( create=True, colorSet='colorSet#') for object in objects: pNoiseVerts(object, value) else: cmds.inViewMessage (amg='Message: <hl>Please select an object first</hl>.', pos='midCenter', fade=True )createUI( 'Config') 这篇关于如何在计算中使用滑块值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-04 13:42