I'm very new to python and programming so I brute forced myself into writing a script but I have been stuck for days and just can't solve this issue.
My script imports a set of 3 svg files, sequentially, applies some transformations, render and save the final image, delete the collection. Then do the same transformations to the next file and so on. But I am having problems with the render part. It works well for one object (if I run it outside the loop), but as soon as I run it as part of a for loop, Blender either closes, or freezes, or I get errors. I have tried some things with the handlers but can't find a solution.
The script is pretty long as I am new, and it uses a couple of text blocks as modules where I wrote the functions. Is it OK if I share a schematic of it?
What it does is something like this:
files = [file1, file2, file3]
for file in files:
importSVG(file)
#Applies the transforms to the objects that belong
#to a collection that matches the file name
doTransformations(file)
#Render, save image and the collection is deleted
#with a function added to the handler
renderSaveImageDeleteColl()
I am going to try to decypher if this answer can be the solution but it's too complex for my level.
Edit: Simplified the scene. I have 5 cubes. What I want the script to do is to render a picture of each single cube. But I end up with a single render of cube 5.
import bpy
cubes = []
folder = "C:\Users\User1\Desktop\Blender Test\TestRenders\"
filePath = ""
for obj in bpy.data.objects:
if obj.name.startswith("Cube"):
cubes.append(obj)
#Hide all objects from render
obj.hide_render = True
a = 1
for cube in cubes:
#Show current object
cube.hide_render = False
#Set file name
filePath = folder + "Cube" + str(a)
bpy.context.scene.render.filepath = filePath
#Render an image of the current object
bpy.ops.render.render("INVOKE_DEFAULT",animation=False, write_still=True)
#Hide the object again
cube.hide_render = True
a += 1
Is there a standard method to use in order to let the loop continue once the render is finished?
Thank you and apologies for my convoluted post.
bpy.data.objectswhich might cause errors, recommend iterate through all objects of the scene in contextbpy.context.scene.objectsinstead, also python comes withenumerate()- no need for this javacript style counter var. Minor correction:enumerate(C.scene.objects):actually should beenumerate(obj_list):, you get the idea... – brockmann Aug 14 '21 at 07:01