2

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.

J.Nada
  • 93
  • 7
  • 1
    Hard to guess what your issue is based on the provided information. I personally don't think you need a modal operator. If you're new to python, it's a pain to debug the modal anyway. I'd suggest have a look into e.g: https://blender.stackexchange.com/questions/39303/blender-script-import-model-and-render-it, share one of the svg files and how you'd like to transform them, see my comment here: https://blender.stackexchange.com/questions/233915/blender-edit-svg-as-xml-before-importing-to-change-the-names-of-the-curves#comment396146_233915 – brockmann Aug 13 '21 at 10:49
  • Been reading about handlers but I can't see how to tell blender that the render is done and it should resume the loop. I simplified my scene to test things, but I can't even acomplish it here (edited the OP). – J.Nada Aug 13 '21 at 17:01
  • 1
    Way better. Adapted your code to render all mesh objects in the scene: https://pasteall.org/hwPc/raw one by one. However, to get the usual render window you'd have to implement the modal... – brockmann Aug 13 '21 at 19:53
  • 1
    That worked! Thank you very much!! I can see the code is much more elegant and I should read up on some things you used there. Thanks for the motivation on this bad Friday. – J.Nada Aug 13 '21 at 20:01
  • 1
    Cool, glad it's helpful. In short: you're iterating through all objects in the blend-file bpy.data.objects which might cause errors, recommend iterate through all objects of the scene in context bpy.context.scene.objects instead, also python comes with enumerate() - no need for this javacript style counter var. Minor correction: enumerate(C.scene.objects): actually should be enumerate(obj_list):, you get the idea... – brockmann Aug 14 '21 at 07:01
  • All great advice, thank you. I didn't know about enumerate, but have been using it since your reply. Thanks for the reply and also for throwing some better code, made me search what it did and read up. Now I'm a bnit better at it. – J.Nada Aug 15 '21 at 01:56

0 Answers0