Jsimmy020
Jsimmy020

Reputation: 1

How do I define a floatSlider using python2.7 in maya?

very new to scripting with python in maya so excuse my limited knowledge.

I need help figuring out how to define the variable for a floatSlider. I need two float sliders for the assignment I'm doing. I need one that will change the size of the selected or specified objects, and I need another that will use MASH to change the count of that object.

I have script with those sliders and a Distribute button laid out. I'm not sure what I need to include to link the scale of the object to the slider I have.

This is the code I have so far:

from maya import cmds

if cmds.window('mainUI2', exists=True):
    cmds.deleteUI

win = cmds.window("mainUI2", title="Bush Generator", widthHeight=(300, 300))

# Layout
cmds.columnLayout(adjustableColumn=True)
cmds.text(label='Bush Generator')
cmds.button(label='Distribute', command='DistributeMesh()')
cmds.text(label=' ')
# need help defining Leaf_size
Leaf_size = cmds.floatSlider(min=0, max=100, value=0, step=1)

# I tried another type of slider
LeafScale = cmds.intSliderGrp(min=0, max=100, f=True)

cmds.text(label='Leaf Size')
# need defining Leaf_amount and linking to mash count
Leaf_amount = cmds.floatSlider(min=0, max=100, value=0, step=1)
cmds.text(label='Leaf Amount')


# Bush tool
def DistributeMesh():
    cmds.loadPlugin("MASH", quiet=True)
    import MASH.api as mapi
    count = 3000

    source_mesh = "pCube2"
    scatter_mesh = "pSphere1"

    source_shape = cmds.listRelatives(scatter_mesh, children=True)[0]

    cmds.select(source_mesh)
    mash_network = mapi.Network()
    mash_network.createNetwork(name="Test", geometry="Instancer")

    # set to use meshes to scatter
    cmds.setAttr(mash_network.distribute + ".arrangement", 4)
    cmds.setAttr(mash_network.distribute + ".pointCount", count)

    # connect mesh
    cmds.connectAttr(
        source_shape + ".worldMesh[0]",
        mash_network.distribute + ".inputMesh",
        force=True)


cmds.showWindow(win)

Upvotes: 0

Views: 271

Answers (1)

Green Cell
Green Cell

Reputation: 4777

Scale is a float value so you can use cmds.floatSliderGrp to set the source mesh's scale. First you have to define a separate function that will be triggered when you change the value of floatSliderGrp, then in floatSliderGrp set its changeCommand parameter to that function:

from maya import cmds

# Define a function that will be called when the slider changes values.
def on_size_slider_changed(value):
    source_mesh = "pCube2"
    if cmds.objExists(source_mesh):  # Check if it exists.
        cmds.setAttr("{}.scale".format(source_mesh), value, value, value)  # Set its scale.

if cmds.window('mainUI2', exists=True):
    cmds.deleteUI

win = cmds.window("mainUI2", title="Bush Generator", widthHeight=(300, 300))

# Layout
cmds.columnLayout(adjustableColumn=True)
cmds.text(label='Bush Generator')
cmds.button(label='Distribute', command='DistributeMesh()')

# Use `changeCommand` to define what function it should call.
leaf_size_slider = cmds.floatSliderGrp(label="Size", field=True, min=0, max=100, value=1, changeCommand=on_size_slider_changed)

# Bush tool
def DistributeMesh():
    cmds.loadPlugin("MASH", quiet=True)
    import MASH.api as mapi
    count = 3000

    source_mesh = "pCube2"
    scatter_mesh = "pSphere1"

    source_shape = cmds.listRelatives(scatter_mesh, children=True)[0]

    cmds.select(source_mesh)
    mash_network = mapi.Network()
    mash_network.createNetwork(name="Test", geometry="Instancer")

    # set to use meshes to scatter
    cmds.setAttr(mash_network.distribute + ".arrangement", 4)
    cmds.setAttr(mash_network.distribute + ".pointCount", count)

    # connect mesh
    cmds.connectAttr(
        source_shape + ".worldMesh[0]",
        mash_network.distribute + ".inputMesh",
        force=True)


cmds.showWindow(win)

Dragging the slider will now set the scale of the cube. Though to be honest the structure of the code here is very messy and a bit too hard-coded (think about how it would work with the current selection instead of explicitly using the object's names)

Upvotes: 1

Related Questions