Hi folks:
Once again, working w/Python in Maya, a 3D animation app.
I'm using a potentially time-consuming procedure to generate a list of commands, and I want to store the list in the Maya file.
The best solution I can think of is to convert the list to a string, which I can easily store and convert back to a list when I need it.
The items in the list are enclosed by tildes, which are necessary to define them as commands.
A simple example:

print cmndList
['cmds.sphere(n = "aBall", r = 1)', 'cmds.sphere(n = "aBall", r = 2)', 'cmds.sphere(n = "aBall", r = 3)', 'cmds.sphere(n = "aBall", r = 4)', 'cmds.sphere(n = "aBall", r = 5)', 'cmds.sphere(n = "aBall", r = 6)']
# To Execute:
for cmnd in cmndList:
	exec(cmnd)

Looking at this thread:
I used:

cmndsString = " ".join(["%s" % el for el in cmndList]) # which lost the tildes:
print cmndsString
cmds.sphere(n = "aBall", r = 1) cmds.sphere(n = "aBall", r = 2) cmds.sphere(n = "aBall", r = 3) cmds.sphere(n = "aBall", r = 4) cmds.sphere(n = "aBall", r = 5) cmds.sphere(n = "aBall", r = 6)

Also, it seems to me that I need to add an extra character for the split command when converting back to a list?
Suggestion would be appreciated.
Thanks much.

cmndList = ['cmds.sphere(n = "aBall", r = 1)', 'cmds.sphere(n = "aBall", r = 2)', 'cmds.sphere(n = "aBall", r = 3)', 'cmds.sphere(n = "aBall", r = 4)', 'cmds.sphere(n = "aBall", r = 5)', 'cmds.sphere(n = "aBall", r = 6)']

cmndString = ''

for i in cmndList:
  cmndString += '~~' + i

#back to list
newCmndList = cmndString.split('~~')

#newCmndList will have an empty string at newCmndList[0] so index from 1

That's how I would do it.

An more common alternative is

>>> L = ['cmds.sphere(n = "aBall", r = 1)', 'cmds.sphere(n = "aBall", r = 2)', 'cmds.sphere(n = "aBall", r = 3)', 'cmds.sphere(n = "aBall", r = 4)', 'cmds.sphere(n = "aBall", r = 5)', 'cmds.sphere(n = "aBall", r = 6)']
>>> import cPickle as Pickle
>>> s = Pickle.dumps(L) # list to string
>>> s
'(lp1\nS\'cmds.sphere(n = "aBall", r = 1)\'\np2\naS\'cmds.sphere(n = "aBall", r = 2)\'\np3\naS\'cmds.sphere(n = "aBall", r = 3)\'\np4\naS\'cmds.sphere(n = "aBall", r = 4)\'\np5\naS\'cmds.sphere(n = "aBall", r = 5)\'\np6\naS\'cmds.sphere(n = "aBall", r = 6)\'\np7\na.'
>>> print Pickle.loads(s) # string to list
['cmds.sphere(n = "aBall", r = 1)', 'cmds.sphere(n = "aBall", r = 2)', 'cmds.sphere(n = "aBall", r = 3)', 'cmds.sphere(n = "aBall", r = 4)', 'cmds.sphere(n = "aBall", r = 5)', 'cmds.sphere(n = "aBall", r = 6)']

See this is why I love this site; you can learn so much.

Wow, thanks a lot.
Both methods work and I gained a bit more insight into Python.
I really appreciate the help!

Glad I could help.

Gribouillis method would probably be the best way. I'm still considered novice at programming, and using Gribouillis method would probably return faster than mine.

Glad I could help.

Gribouillis method would probably be the best way. I'm still considered novice at programming, and using Gribouillis method would probably return faster than mine.

I don't know if it's faster, but Pickle can serialize lists containing more complex data. Also it's commonly used to convert python objects to strings when you want to send them through a network connection.

I don't know if it's faster, but Pickle can serialize lists containing more complex data. Also it's commonly used to convert python objects to strings when you want to send them through a network connection.

I'm a Python newbie, but it seems to me pickle is the way to go. Also, the reconstituted list doesn't contain an extra element.
Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.