0

Python novice here, please excuse me if I'm a bit off on my terminology (and please correct me :) but is it possible to pass flags and values to a 2nd function and for that function in turn to pass them on to a third function?

So something like this:

def changeLayers(var1, var2, layers, flag, value):
  # do lots of stuff with var1 and var2...
  # finally: 
  for layer in layers:
    changeLayerState(layer, flag = value)


changeLayers('foo', 'bar', someLayers, 'visible', True)
changeLayers('foo', 'bar', otherLayers, 'layerState', 'normal')

So the desired effect is running something like changeLayers('foo', 'bar', someLayers, 'visible', True) and eventually having changeLayerState(layer, visible = True) be evaluated.

If I run something like that I get a Syntax Error, I assume because I'm passing it the string 'visible' as a flag.

This simplifies it a bit but is the essence of what I'm trying to do. Is there a way to do this?

Hope that makes sense..

EDIT: So I was using pseudocode to cast a wide net and not scare anyone off who isn't familiar with Maya Python, but I guess that was more confusing. So here is a snippet that looks at all the display layers in a scene, turns all the ones that end in "Control" to invisible and sets all the ones that end in "Geometry" to a normal layerState (which is to say, not referenced or templated, so that they can be easily selected in the viewport):

import maya.cmds as cmds
layers = cmds.ls(type='displayLayer')
controlLayers = []
geomLayers = []

for layer in layers:
    if layer.endswith('Control'):
        conLayers.append(layer)
    elif layer.endswith('Geometry'):
        geomLayers.append(layer)   

for layer in controlLayers:
    cmds.layerButton(layer, edit = True, layerVisible = False)
    cmds.setAttr(layer + '.visiblity' , False)

for layer in geomLayers:
    cmds.layerButton(layer, edit = True, layerState = 'normal')            
    cmds.setAttr(layer + '.displayType', 0)

And that works. But if I try to functionize those lines that change the layer states like this:

import maya.cmds as cmds
layers = cmds.ls(type='displayLayer')
controlLayers = []
geomLayers = []

def changeLayerState(layers, flag, value, attr, attrValue):
    for layer in layers:
        cmds.layerButton(layer, edit = True, flag = value)            
        cmds.setAttr(layer + '.' + attr, attrValue)

for layer in layers:
    if layer.endswith('Controls'):
        controlLayers.append(layer)
    elif layer.endswith('Geometry'):
        geomLayers.append(layer)   

changeLayerState(geomLayers, 'layerState', 'normal', 'displayType', 0)
changeLayerState(controlLayers, 'layerVisible', False, 'visibility', False)

I error out:

// Error: Invalid flag 'flag'
# Traceback (most recent call last):
#   File "<maya console>", line 17, in <module>
#   File "<maya console>", line 8, in changeLayerState
# TypeError: Invalid flag 'flag' //

Does that make more sense of what I'm trying to do? I'm trying to pass a flag to use on a function from another function.

D Logan
  • 3
  • 2
  • You can pass as many values (flags) as you want, there's no reason why it wouldnt be possible, but without the knowledge of the error you're getting no one will be able to help you. `SyntaxError` means there's a syntax error in your code, so post the whole error traceback and a relevant code where that happens. – yedpodtrzitko Feb 11 '17 at 22:57

1 Answers1

0

If youre trying to call changeLayerState and it expects a flag like changeLayerState(something, someflag=somevalue) you pass a dictionary with flags and values using the ** keyword argument syntax

def changeLayers(var1, var2, layers, flag, value):
    # etc
    keyword_args = {flag: value}
    for layer in layers:
       changeLayerState(layer, **keyword_args)
Community
  • 1
  • 1
theodox
  • 12,028
  • 3
  • 23
  • 36
  • yeah or directly passed as keywords in the inputs – DrWeeny Feb 11 '17 at 23:23
  • it looks like he won't know the flags until runtime -- he'd have to pass them all with defaults and parse his outer function to id which ones were specified by 'flag' and 'value' otherwise' – theodox Feb 11 '17 at 23:24
  • Thank you! I had tried using kwargs but it didn't click. Now it does and this works. – D Logan Feb 12 '17 at 00:44