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:
2.0
1.0
.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(
radius=1.0, view_align=False, enter_editmode=False,
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.
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.
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>
.