Maya Python Script Job Attribute Change

Question:

I am trying to get one boolean attribute (A) to change another(B). The one to be controlled (B) already has a script job running it and so I can’t create a set driven key, direct connection, or expression to control it, so I’m trying another script job, since running the script function by itself achieves the desired result. I just can’t figure out how to tie that script to run to the attribute change (B) that I want to drive it by (A).

This is placed in a script node set to the open gui trigger (to load when maya opens as I understand it). Here’s a screenshot.

What am I missing here?

import maya.cmds as cmds

def togglePicker(pickerAttr):

    cmds.setAttr(pickerAttr, not 0)
    

nameSpace = cmds.ls(sl=True)[0].rpartition(':')[0]

if len(nameSpace) > 0:
    pickerAttr = nameSpace + ':Main.picker'
    myPickerAttr = nameSpace + ':MoverMain_Cntrl.Picker'
else:
    pickerAttr = 'Main.picker'
    myPickerAttr = 'MoverMain_Cntrl.Picker'
    
 
cmds.scriptJob(attributeChange=[myPickerAttr,togglePicker])
Asked By: Lucas

||

Answers:

Your script node is executed every time when maya loads a scene, not when it is started, at least that’s what the docs say. So every time you load a scene, a new scriptJob is created.

Your script should show an error message since the togglePicker() function is called without an argument, but it requires an argument. Even if it works, it will not work.. what you do at the moment is the following:
As soon as you turn on the MoverMain_Cntrl.Picker attribute, the togglePicker() function is called and turns it on, even if you turn it off. The pickerAttrvariable is not used. So you should have a look at your program logic.

You can solve the agrument problem by using the partial function like this:

import maya.cmds as cmds
from functools import partial

def togglePicker(pickerAttr):
    cmds.setAttr(pickerAttr, not 0)
    

nameSpace = cmds.ls(sl=True)[0].rpartition(':')[0]

if len(nameSpace) > 0:
    pickerAttr = nameSpace + ':Main.picker'
    myPickerAttr = nameSpace + ':MoverMain_Cntrl.Picker'
else:
    pickerAttr = 'Main.picker'
    myPickerAttr = 'MoverMain_Cntrl.Picker'
    
 
cmds.scriptJob(attributeChange=[myPickerAttr,partial(togglePicker, pickerAttr)])
Answered By: haggi krey

I got it to work! (previously I had switched to the script node to MEL so I could test the mel command mentioned in the comments that did work, but I forgot to switch back to python when I realized the selection issue I also mentioned in the comments).

So here’s what worked, where I know I’ll have to manually change the namespace name in case the scene file name changes:

import maya.cmds as cmds

def togglePicker():
    cmds.setAttr(pickerAttr, not 0)

if cmds.namespace(exists='ExtremeBallRig_v008'):
    pickerAttr = 'ExtremeBallRig_v008:Main.picker'
    myPickerAttr = 'ExtremeBallRig_v008:MoverMain_Cntrl.Picker'
else:
    pickerAttr = 'Main.picker'
    myPickerAttr = 'MoverMain_Cntrl.Picker'
    
cmds.scriptJob(attributeChange=[myPickerAttr,togglePicker])
Answered By: Lucas
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.