Alright, after some testing, I got it to work... It's an extremely simple test case and I'm sure the code could be made more efficient, but it's the first time I got it working - so I thought I'd go ahead and post what I ended up with to help anyone else who comes across both this issue and this thread looking for an answer/help.
some notes:
- the code is in python 2.7 (I was testing in maya 2020)
- the file I created to do the test has two 'controls' named 'lf_test' and 'rt_test'
- the controls have an enum attr named 'match' with 'IKtoFK' and 'FKtoIK' enums
- there are two poly cubes representing ik controls named 'lf_IK' and 'rt_IK'
- there are two poly spheres representing fk controls named 'lf_FK' and 'rt_FK'
- you can open the file after running the code to create the scriptNode and it will work (no namespaces)
- you can import that same file multiple times with different namespaces and each of those work as well
- you can also have one imported without a namespace and others with a namespace and they will all work
- obviously your real matching script will be more complex
code_str = '''
import maya.cmds as mc
namespace = ''
jobs = [j for j in mc.scriptJob(listJobs=True) if 'test.match' in j]
for node in mc.ls('*:test_matchIKFK'):
ns = '%s:' %node.partition(':')[0]
found = False
for job in jobs:
if ns in job:
found=True
break
else:
if found:
continue
else:
namespace = ns
break
def ikToFk(ns, side):
pos = mc.xform('%s%s_FK' %(ns, side), q=True, t=True)
mc.xform('%s%s_IK' %(ns, side), t=pos)
def fkToIk(ns, side):
pos = mc.xform('%s%s_IK' %(ns, side), q=True, t=True)
mc.xform('%s%s_FK' %(ns, side), t=pos)
def matchIKFK():
node = mc.ls(sl=True)[0]
if ':' in node:
ns, _, name = node.partition(':')
ns = '%s:' %ns
side = name.partition('_')[0]
else:
ns = ''
side = node.partition('_')[0]
opt = mc.getAttr('%s.match' %node)
if opt == 0:
ikToFk(ns, side)
elif opt == 1:
fkToIk(ns, side)
mc.scriptJob(attributeChange=['%slf_test.match' %namespace, matchIKFK], killWithScene=True, compressUndo=True)
mc.scriptJob(attributeChange=['%srt_test.match' %namespace, matchIKFK], killWithScene=True, compressUndo=True)
'''
mc.scriptNode(scriptType=2, beforeScript=code_str, name='test_matchIKFK', sourceType='python')
Are there any obvious optimizations for the scriptJob/scriptNode specific stuff that I may not have noticed yet?