如何在计算中使用滑块值?

时间:2019-04-08 00:22:05

标签: python maya

我有一段代码会产生噪音。噪声在0.0到1.0范围内产生。只要我设置一个定义的数字,代码就可以工作。如果我允许用​​户使用滑块选择该数字的最大范围,它将停止工作。

我有一个滑块,该滑块替换了亮度计算中的1.0值。一旦我用滑块生成的名为noiseAttribute的值替换Brightness中的1.0值,代码就会中断。它没有错误,并且在技术上可以运行,只是将对象变成黑色,而不是锁定颜色值。

import maya.cmds as cmds
import random
import functools

colorList = 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)

如前所述,对象变为黑色,而不是锁定其最大颜色值。

1 个答案:

答案 0 :(得分:2)

您不使用functools传递任何参数 这是我对同一主题的回答之一:Need Help Making Buttons to perform for loops when you input a number

How to print the value of intField in Maya python

Maya Python - Using data from UI

您可以进入我的提问历史,我对functools回答了很多

import maya.cmds as cmds
import random
import functools

colorList = 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')