# Possible to replay repetitive tasks using Python?

Is there a way to do a particular task manually, generate the code for it automatically and update that code to do some task repeatedly?

I'm looking for something like record macro functionality that's available in MS Excel.

If you pull down the Info header you will find that a fair amount of operations you can perform in Blender are printed there for relatively simple stuff such as adding a lamp or a modifier etc.

This example:

• scales it by 2.0
• translates on the Z axis by 1.0.
• sets the color to red
• shows the name
• renames the object.

• Select the lines (mouse-click, border-select or AKey)
• Ctrl+C to copy the selected lines.
• Paste into the text editor.
• add import bpy at the top of the text block.

After that, if you were to have a look at the api, you could cut out much of the fluff and just use said commands in a script to reuse whenever you need to provided it is simple enough and in the correct context.

For example:

bpy.ops.mesh.primitive_cube_add(
location=(0.0, 0.0, 0.0), rotation=(0.0, 0.0, 0.0),
layers=(False, False, False, False, False, False,
False, False, False, False, False, False,
False, False, False, False, False, False,
False, False))


... can be shortened to bpy.ops.mesh.primitive_cube_add() in most situations.

This is as close to a macro like functionality that you can get in Blender and it is linear so any actual repetitive bits will have to be crafted by you, loops etc. It is also worth noting that many of these commands need to be run in a specific context so I would recommend using it as a means of learning the correct calls instead of trying to reuse it directly.

## Note

For most operations, there is an accompanying line of code printed. Including changing numeric values, toggles... etc.

Excluded operations are typically not useful to redo, zooming/panning, setting the cursor location... etc.

• Note that not all operations are logged, and changes in the context (which operators rely on) don't necessarily appear in the operator log. – CodeManX Jan 20 '14 at 20:03
• @CoDEmanX I did say most, will add a bit more info shortly. – iKlsR Jan 20 '14 at 23:02
• I refered to "reuse whenever you need to", which sounded like you could run the copied ops whenever you want to. But it's not true, 'cause of the context requirements. Thus, it's not a proper macro system, 'cause it will mostly require touch-up to make it work (under several conditions). It can be misleading to see the operator log and think of it as a macro system. I would appreciate a full action serialization in Blender of course (to allow for true macro recording), but it's not going to happen any time soon. – CodeManX Jan 20 '14 at 23:50
• @iKlsR how to save and add in macro list and give hotkeys not mention. I have seen an video youtube.com/watch?v=zMxkU350SaE in that video the shortcut are made in the menu time its coming from beside the mouse pointer. – atek Nov 13 '16 at 14:38

As mentioned above, repetitive tasks in blender are scripted in python.

When blender is launched with --debug-wm argument , python expression corresponding to executed user actions are printed in console of Info panel and also can be a reference for simple macro script creation.

Also, not exactly same as Excel macros but macro recorder add-on is useful to record a a set of user's actions as python script, while editing a mesh for example and refactor it for reuse if necessary.

There is also paid nice UI based macro editing feature in Pie Menu Editor add-on

If you're using Linux, another option is to use the command line program xdotool. It allows you to simulating pressing certain keys. I just used the following command below to automate duplicating, shifting and rotating an object 20 times. The sleep 5 at the beginning is to give you enough time to move your mouse into the Blender window.

sleep 5 ; for i in {1..20} ; do xdotool type "Dgz-.15^Mrz5^M" ; done


The ^M sequence is a single character that represents pressing Enter. You can generate it by pressing <ctrl-v><enter>.