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.