PDA

View Full Version : Q: Python List as definition input


f3rry
07-11-2012, 02:56 AM
Hi,

How can I insert a list as an argument in a definition (in the scriptJob below)?

eg:

import maya.cmds as mc

def testCommandA () :
sel = mc.ls(sl=1)
selString = ''
for i in sel[:-1] :
selString += '\"' + i + '\",'
selString += '\"' + sel[-1] + '\"'
mc.scriptJob(runOnce=1,attributeChange=["pCube1.ty", 'testCommandB.testComandB(' + selString + ')'])

def testCommand (*selectionList) :
for i in selectionList :
print i



At the moment I have to extract the list(sel) into a long string in quotes separated by commas (selString), ugly, but I'm sure there is a more efficient way to do it.

Thanks for any help

H2o-KiD
07-11-2012, 04:28 AM
Hi f3rry, I am not really sure what you are trying to achieve but...

import maya.cmds as mc

def testCommandA():
sel = mc.ls(sl=1)
selString = ''
for i in sel[:-1] :
selString = '"%s", "%s"' %(i, sel[-1])
mc.scriptJob(runOnce=1,attributeChange=["pCube1.ty", 'testCommandB.testComandB(' + selString + ')'])

def testCommand(*selectionList):
for i in selectionList:
print i

Copied this example from somewhere:

name = raw_input("who are you?")
print "hello %s" % (name,)

The %s token allows me to insert (and potentially format) a string. Notice that the %s token is replaced by whatever I pass to the string after the % symbol. Notice also that I am using a tuple here as well (when you only hava one string using a tuple is optional) to illustrate that multiple strings can be inserted/formatted in one statement.

H2o-KiD
07-11-2012, 04:39 AM
As for the definition arguments,
import maya.cmds as cmds

def testA(selected):
for each in selected:
print(each)
to use "testA" function, some simple example:-
selection = cmds.ls(selection=True)
testA(selection)
or
testA( cmds.ls(selection=True) )
I hope it helps :D

f3rry
07-11-2012, 07:11 AM
Thanks for your answer.

Do you see the testCommandB being called inside the scriptJob? At the moment I am converting my list into a long string which is not efficient because the definition can only take 255 arguments. If I have more than 255 objects selected the script stop working.

I know I can do def test(*selection) but I cant seem to place it inside the scriptJob. It simply won't take a list as an argument.

H2o-KiD
07-11-2012, 08:33 AM
Maybe if you don't mind posting your testCommandB as well, and be more clear on what you are trying to do. :shrug:

NaughtyNathan
07-11-2012, 03:00 PM
my recommended course of action would be to NOT pass an array(list) into the function called by the scriptJob.

1) Let's assume the array is simply the selected items. Don't pass anything and just "get selection" in the called function.

2) Let's assume the function HAS TO accept an array. Pass an empty array and in the function do:
if not array: array = mc.ls(sl=1)

3) Let's assume you cannot edit the function and it has to accept an array. first write out how it should be in full:

function( ['item','item','item','item'] )

so you can see how it should look. notice that the array(list) should be enclosed in [ ] (or () for tuples). This should now give you a clue as to how your string should look that you pass into the scriptJob callback:

'testCommandB.testComandB( [ ' + selString + ' ] )'

additionally, don't do that `selString +=` nonsense, it's very unefficient and not pythonic. do something like this:
sel = mc.ls(sl=1)
selString = '","'.join(sel)
mc.scriptJob(runOnce=1,attributeChange=["pCube1.ty", 'testCommandB.testComandB( ["' + selString + '"] )'])

f3rry
07-12-2012, 01:23 AM
Thanks Nathan,

That's exactly what I did, combining all the array into a long string but python definition (in maya, i tried), can only take up to 255 arguments so if I have more than 255 objects selected the script will not run.

At the moment this is how I run it :


import maya.cmds as mc
selection = mc.ls(sl=1)

def testCommandA () :
mc.scriptJob(runOnce=1,attributeChange=["pCube1.ty", 'testCommandB.testComandB(')'])

def testCommandB () :
print selection



That whole code is saved and run as testCommandA.py
Although it still not as smooth as I expected. Sometimes it said that the selection is never defined. I still trying to find out why...

@H2o-KiD: See what I can do. The actual script is a bit messy. Thanks for your help too

NaughtyNathan
07-12-2012, 10:09 AM
you don't want to pass the individual arguments as separate arguments, (limited to 255) you want to pass them all together as a single list. See the difference between:mod.function( 'item1','item2','item3','item4' )
and:mod.function( ['item1','item2','item3','item4'] )The first has 4 args and the second has only 1 argument. This latter approach is what you can and need to use to solve this issue properly if you really have to pass a (string) selection list to your function.

f3rry
07-12-2012, 01:56 PM
Thats correct. Please forgive me.
Thanks a lot

f3rry
07-13-2012, 02:39 AM
I also just found out one can do this with list:

a=['a','b','c','d']
b='%s' % a

I like python :)

CGTalk Moderation
07-13-2012, 02:39 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.