How to use a slider value in calculations?

2020-05-04 10:20发布

问题:

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

As mentioned before the object turns black instead of having its max color value locked.

回答1:

You dont pass any arguments with your functools Here is one of my answer on the same topic : 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

You can go in my history of questions, I answered a lot about 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')


标签: python maya