PYTHON life-saving INCANTATIONS

The idea is to collect all your SHORTEST PYTHON COMMANDS that one could LEARN BY HEART as time-savers. Posts should include a little context for use and advantages of the command. If possible to keep this thread neatly browsable, authors can discuss elsewhere before editing their posts for update after any magic formula’s refinement . For a clear display, surround the python text with

 and[ /CODE] tags (square brackets included). Variations on other's posts are encouraged. And since I should start, here is one, found elsewhere and even shortened a little for that very purpose :
 
When you want to delete all the IPO curves of selected objects at once, instead of browsing through each:

import Blender
[OBJ.clearIpo() for OBJ in Blender.Object.GetSelected()]

For anyone working on long simulations (ga, ea, particularly):
(edit)


import pickle
# to save
pickle.dump(object,filename)
#to load again when everything has crashed and gone wrong
object =pickle.load(filename)

Without that (or writing an equivalent) I would have lost days worth of data on many occasions.

Thanks IanC but what are ga and ea? :o

for mr. Ray:

ge == game engine

http://docs.python.org/lib/module-pickle.html
with blender - at least on Windows - cPickle would be more appropriate because it’s baked in the pythonxx.dll and said to be faster.


import pickle

# to save
pickle.dump(object, filename)

# to load again when everything has crashed and gone wrong
object = pickle.load(filename)

beautified (w/o even importing the tokenizer !)

– that’s the one from Mr. IanC (!)

Just noticed your post! Thanks Tedi :slight_smile:

but “ga”, ea" ? could you edit please?

Well, if you don’t know what they are you probably aren’t doing simulations with them, are you :stuck_out_tongue:

ge == game engine

Not quite :slight_smile: . Genetic algorithms and evolutionary algorithms. I’ve done week long simulations quite a few times, and saving along the way is very, very useful. Particularly if you allow the critters to modify themselves, because they can create code that crashes the system. Pickling occasionally means this can leave you with a only a few hours lost.

Thanks for the edit and explanations: I will have to dig into these genetic or evolutionnary algorythms, they seem interesting. For now, the simulations I know anything about are more physics, fluids, etc… is there any chance this pickle thing can be applied with them too?

simple hook for linting with pyflakes
http://divmod.org/projects/pyflakes

reads scripts from blender text editor, lints it - if the script is SAVED TO DISK first.
so there are no unwanted surprises.

if you want it in menus, you can add your header where you want it …


import Blender
import pyflakes, compiler

"""
For use with pyflakes: http://divmod.org/projects/pyflakes
"""
alltxt = Blender.Text.Get()
option = 'SELECT SCRIPT TO CHECK :%t'

for i in xrange(len(alltxt)): option += '|' + alltxt[i].getName() + '%x' + str(i)

menu = Blender.Draw.PupMenu(option)

if menu != -1 and alltxt[menu].getFilename():
	file = Blender.Text.Get(alltxt[menu].getName()).getFilename()
	code = open (file, 'r').read()
	tree = compiler.parse(code)
	warn = pyflakes.Checker(tree)

	for warning in warn.messages: print warning

else: Blender.Draw.PupMenu('ERROR :%t| save the script to disk first!')

is there any chance this pickle thing can be applied with them too.

Anything that is done in python :slight_smile: It saves any python objects to a file, including self defined classes. I’ve used it to add functions to existing objects/correct them. Save to file, correct the class definition, load and run!

Cool IanC, Thanks tedi, though it’s a little long to learn by heart but there’s no problem to have higher level formulas’ like spell casting… :slight_smile: …If they are worth the typing effort in terms of workflow (this is the main point here).

Here is for applying a material to several objects at once. I would like to shorten it like the one in the first post, but I don’t really get how this square brackets syntax works.

import Blender
mat= Blender.Material.Get('Material') 
for OBJ in Blender.Object.GetSelected():
 OBJ.colbits = 1<<0
 OBJ.setMaterials([mat])