0

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.

boomstick
  • 53
  • 1
  • 9
  • What is the slider generated value? – l'L'l Apr 08 '19 at 00:24
  • It can be anything between a 0.0 to 1.0. This is set inside noiseAttribute under 'Max Value Lock'. Users can define what that value is. If I just set it to be 1.0 instead of using (0.0, noiseAttribute) the code runs as intended – boomstick Apr 08 '19 at 00:25
  • What if you try `0.9`? – l'L'l Apr 08 '19 at 00:29
  • As long as I set the value myself inside brightness (0.0, 1.0) any value works. (0.0, 0.9) limits the use of values correctly. If I set the range of the slider from 0.0 to 0.9 it still turns the object completely black if I call noiseAttribute instead of a specific value. – boomstick Apr 08 '19 at 00:35
  • Sounds like the slider isn't setting the value correctly (if at all). Have you verified that the value is actually set? And when you manually set it where are you doing that? – l'L'l Apr 08 '19 at 00:43
  • Well supposedly I'm defining it in `noiseAttribute = cmds.floatSliderGrp( min=0.0, max=1.0, value=1, field=True)` but when I run a print command on noiseAttribute it prints "False" – boomstick Apr 08 '19 at 01:13
  • Try doing `print(noiseAttribute)` after the line that sets it. – l'L'l Apr 08 '19 at 01:29
  • If I print it right after the line that sets it I get this myWindowID|rowColumnLayout72|floatSliderGrp43 – boomstick Apr 08 '19 at 01:37
  • So it’s obviously not setting the value to a floating number; you’ll need to convert it somehow. – l'L'l Apr 08 '19 at 01:39
  • 1
    Hey @l'L'l Just wanted to thank you for trying to help me as well! Your initial suspicion on me not passing the value was correct! – boomstick Apr 08 '19 at 02:44

1 Answers1

2

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')
DrWeeny
  • 2,487
  • 1
  • 14
  • 17
  • Hey! Thank you for the answer. This works as intended. The links you attached are also very helpful. Also thanks to @Green-Cell for trying to help me as well! – boomstick Apr 08 '19 at 02:43
  • no problem ui are always a pain ^^ Also when you are making one, try to track how your informations are passed through, don't heistate to put print everywhere to see how args are passing in the ui – DrWeeny Apr 08 '19 at 02:48