如何将floatField连接到变量?

时间:2018-11-04 18:27:57

标签: python maya

我正在处理一个脚本,该脚本需要将floatField连接到在函数中调用的变量。我尝试了connectControl,但是它仍然会出错并且无法正常工作。有什么建议吗?

这是我的用户界面块

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

这是给我问题的功能

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

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

因此您想将变量传递到ui中。这是一个大话题,主要的答案是使用lambda或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))

有关更多信息,请转到我上面链接的主题,如果您仍有疑问,请不要犹豫,欢呼!