0

introducing that I'm new in python, I'm tring to use the intField command to set a number of iterations for this attribute: 'aiSubdivIterations'.

The script should work like that: when I run the scripts it open a window where I can set my value and when I press enter it should automatically set the same value in the 'aiSubdivIterations' slot.

import maya.cmds as pm

def aiSetIter(iterValue):   
    objSelect= pm.ls(sl=1, dag=True, leaf=True)
    for obj in objSelect:
        pm.setAttr( obj + '.aiSubdivIterations', iterValue)

pm.window(title = 'Interations')
pm.columnLayout ('mainColumn', adjustableColumn = True)
pm.gridLayout ('nameGridLayout01', numberOfRowsColumns = (2,2), cellWidthHeight = (80,30), parent = 'mainColumn')
pm.text (label = 'number')
pm.intField (minValue=0, maxValue=10, step=1, vcc = 'aiSetIter(iterValue)')
pm.showWindow()

Could some one help to make this script working?

Thanks in advance

Flavia

1 Answers1

0

the easiest for your script to work is to just specify the function you want to call (with no arguments and the rest of your code unchanged):

pm.intField('valueField', minValue=0, maxValue=10, step=1, vcc=aiSetIter)

You can assume that Maya's intField will call the specified function by automatically passing the value it stores. In the above case, your function will be called only when the UI is made visible or closed.

If you need your function to be called every time the value changes, you want to add a "changeCommand" (or "cc") flag too:

pm.intField('valueField', minValue=0, maxValue=10, step=1, vcc=aiSetIter, cc=aiSetIter)

For more complex scenarios, you might want to consider lambda functions (you can read here a nice post about it); this will allow delayed evaluation of your function. What follows is a super-simple example of a lambda function retrieving the current time when the intField value changes and passing it, along with the value itself, to myLambdaFunc:

import maya.cmds as pm
import time

def myLambdaFunc(iterValue, now):
    print iterValue, now

lambdafunc = lambda arg: myLambdaFunc(arg, time.time())

pm.window(title='Iterations')
pm.columnLayout ('mainColumn', adjustableColumn=True)
pm.gridLayout ('nameGridLayout01', numberOfRowsColumns=(2,2), cellWidthHeight=(80,30), parent='mainColumn')
pm.text (label='number')
pm.intField('valueField', minValue=0, maxValue=10, step=1, vcc=lambdafunc, cc=lambdafunc)
pm.showWindow(win)

EDIT:

A minor integration, since I bumped into this just now: https://theodox.github.io/2014/maya_callbacks_cheat_sheet

It is a useful and exhaustive article by theodox about Maya UI and callbacks, found via this; worth a read.

mapofemergence
  • 458
  • 3
  • 7