How to use a slider value in calculations?

后端 未结 1 1376
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-28 13:33

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 sel

相关标签:
1条回答
  • 2021-01-28 14:16

    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')
    
    0 讨论(0)
提交回复
热议问题