1

I am working on a script where I need to connect a floatField to a variable that is called in a function. I have tried a connectControl but it still errors out and will not work. Any suggestions?

This is my UI block

##### UI #####
def createUI ():
    # Delete
    if cmds.window("TerrainUI", exists= True):
        cmds.deleteUI("TerrainUI")
    cmds.windowPref( 'TerrainUI', remove=True )
    # Create Window
    window=cmds.window ("TerrainUI", title="Terrain UI", w=350, h=750, mnb=0, mxb=0, sizeable=0)
    mainLayout=cmds.columnLayout(w=300, h=750,cal="left")
    ### Make Stuff ###
    cmds.button(label="Cleanup",w=300,h=25,c=cleanup)
    cmds.button(label="Create Viewport Lights",w=300,h=25,c=createViewportLights)
    cmds.button(label="Create Render Lights",w=300,h=25,c=createRenderLights)
    cmds.button(label="Create Plane",w=300,h=25,c=createPlane)
    cmds.rowColumnLayout( numberOfRows=16 )
    ## NOISE CONTROL ##
    cmds.text(label="Threshold")
    thresh=cmds.floatField( minValue=-1, maxValue=1, precision=4, step=.01 )
    #cmds.connectControl(thresh,"t")
    cmds.text(label="Depth Map")
    depmap=cmds.floatField( minValue=-1, maxValue=1, precision=4, step=.01 )
    #cmds.connectControl(depmap,"dm")
    cmds.text(label="Size Randomization")
    sizeran=cmds.floatField( minValue=-1, maxValue=1, precision=4, step=.01 )
    #cmds.connectControl(sizeran,"sr")
    cmds.button(label="Create Noise",w=300,h=25,c=noise)
    cmds.button(label="Color Plane",w=300,h=25,c=colorPlane)
    cmds.button(label="Create Trees",w=300,h=25,c=makeTree)
    cmds.button(label="Color Trees",w=300,h=25,c=treeShade)
    cmds.button(label="Create Mushrooms",w=300,h=25,c=makeMush)
    cmds.button(label="Color Mushrooms",w=300,h=25,c=mushShade)
    cmds.button(label="Create Clouds",w=300,h=25,c=makeCloud)
    cmds.button(label="Color Clouds",w=300,h=25,c=cloudShade)
    cmds.button(label="Create Moon",w=300,h=25,c=makeMoon)
    cmds.button(label="Color Moon",w=300,h=25,c=moonShade)
    # Show Window
    cmds.showWindow(window)

And this is the function giving me issues

def noise(t,dm,sr):
    if cmds.objExists("noise*"):
        cmds.delete("noise*")
    cmds.select("terrain_plane")
    cmds.textureDeformer(en=.9, s=1.5, o=0, d="Normal", ps="World")
    cmds.rename("textureDeformerHandle1", tPfx+"deform")
    cmds.shadingNode('noise', asShader=True)
    cmds.setAttr ("noise1.threshold", t)
    cmds.setAttr ("noise1.depthMax", dm)
    cmds.setAttr ("noise1.sizeRand", sr)
    cmds.connectAttr ("noise1.outColor", "textureDeformer1.texture")

Any help would be appreciated. Thanks!

1 Answers1

0

so you want to pass variables into ui. This is a big topic and the main answer is to use lambda or partial : Using Sliders in Maya with Python to control Shapes - intSliderGrp

from functools import partial

def noise(t,dm,sr, *args):
    # * args is used to skip default arguments passed by maya ui (a boolean pass as last)
    if cmds.objExists("noise*"):
        cmds.delete("noise*")
    cmds.select("terrain_plane")
    cmds.textureDeformer(en=.9, s=1.5, o=0, d="Normal", ps="World")
    cmds.rename("textureDeformerHandle1", tPfx+"deform")
    cmds.shadingNode('noise', asShader=True)
    cmds.setAttr ("noise1.threshold", t)
    cmds.setAttr ("noise1.depthMax", dm)
    cmds.setAttr ("noise1.sizeRand", sr)
    cmds.connectAttr ("noise1.outColor", "textureDeformer1.texture")

# with partial you can pass your three arguments.
# it is written partial(function, argument1, arguments2, ....etc)
button = cmds.button(label="Create Noise",w=300,h=25, c=partial(noise, t, dm, sr))

for more information go to the topic i linked above, if you have still questions, don't hesitate, cheers !

DrWeeny
  • 2,487
  • 1
  • 14
  • 17