# Automate random shape generator and export viewport render with python

I originally thought I could just keyframe and export the animation of the 'random seed' property of the addon but blender just gives me an error:

"random_seed" property cannot be animated

Now I'm trying to write a python script that uses the random shape generator addon and exports and saves the viewport rendered image of each shape. I want to be able to export an arbitrary number of random shapes into a series of pngs.

These are the steps I'm trying to automate:

Add +1 to the random seed

Render from the viewport

Save it

Add +1 to the name of the image

Save it

Delete the object so a new one can be created

This is my script that's not working:

import bpy

for x in range(0,1):
bpy.ops.mesh.shape_generator(random_seed=1001, update_draw_only=True, show_seed_panel=True)
bpy.ops.render.opengl()
bpy.ops.render.render(use_viewport=True)
bpy.ops.file.filenum(increment=1)
bpy.ops.file.execute()
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)



Is there a better way of doing this?

range(0,1) just generates a list containing only '0', so that's only looping once. I presume you had it set like that just for testing.

When I ran your script it crashed on the bpy.ops.file.filenum(increment=1) line. Changing context can be complicated, so I'd suggest instead controlling the filepath directly by assigning bpy.data.scenes['Scene'].render.filepath, and numerically increasing it yourself using the x variable.

Also, you don't need to call both bpy.ops.render.opengl() and bpy.ops.render.render(). The former is the one you want - you just need to pass it either animation=True or write_still=True to specify what it should save, while the latter render function is the full-on F12 'render', which requires a camera.

To give a different seed to the shape_generator call each time, you can use the x loop variable added on to whatever base seed you want. For example, if you loop for x in range(6) that will repeat with x == 0 the first time, up to x == 5 the last time.

Here's a version showing these suggestions.

import bpy
from os.path import join

folder = "C:\\Users\\acro\\random_shapes\\"
scn = bpy.data.scenes['Scene']

for x in range(42):
bpy.ops.mesh.shape_generator(random_seed=1000+x, update_draw_only=True, show_seed_panel=True)
# set the save destination:
fname = "randomShape_%02d.png" % x
scn.render.filepath = join(folder, fname)
bpy.ops.render.opengl(write_still=True)

bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

• I updated the file path to a folder on my desktop, but no files are showing up: "C:\\Users\\me\\Desktop\\randomExport\\" Did I do it right or is something else off? – Timothy M Aug 28 at 15:50
• Never mind. I'm on a Mac so the location was off. It's working perfectly now. – Timothy M Aug 28 at 20:02
• ... I actually came up with this answer on Mac as well, but then adapted it to a windows-like example because I assumed that was the more likely OS! Was in the process of re-testing it to look for more possible errors. Sorry, I should have mentioned the backslashes/forward slashes filepath nonsense. Glad it worked for you. – acro Aug 28 at 20:08