An if
statement does not cause the code following it to loop, it merely executes the code indented after the if
only if the statement following the if
(in this case 1 > 0
) is true.
If I understand what you are trying to do, you instead want a while
loop instead of an if
statement:
var = 0 # define the variable "var"
while 10 > var: # while 10 is greater than whatever value "var" is equal too
var = var + 1 # increment var by one
print (var) # print var to the console
This will execute the code indented after while 10 > var:
until var
is no longer less than 10
.
The result of running the above code is:
1
2
3
4
5
6
7
8
9
10
So if we apply this to your code:
import bpy
var = 0 # assign these variables to 0
mx = 0 # I removed these from the scene for loop because you don't need to set them again for eaach scene.
sx = 0
sy = 0
my = 0
for scene in bpy.data.scenes: # loop through each scene in the .blend and set these values in each one:
scene.render.use_border = True # enable borders
scene.render.use_crop_to_border = True # enable crop to border
while var < 3: # as long as "var" is equal to a number less than 3, execute:
var = var + 1 # increase "var" by one (very important if we don't want to run the loop for infinity)
my = my + .2
mx = mx + .2
scene.render.border_max_x = mx
scene.render.border_min_x = 0
scene.render.border_min_y = 0
scene.render.border_max_y = my
# vv var is an INT, so we convert it to a string for the perposes of using it in the output image file name
bpy.context.scene.render.filepath = "/tmp/test_render_" + str(var) + ".png" # bpy.context.scene gives us the currently open scene, so we can set the path for outputting images for only that scene.
bpy.ops.render.render(write_still=True) # I added the write_still=True so the render is writted to the file spcified in scene.render.filepath
This code outputs the following images in /tmp/
:
test_render_1.png
is created with the border settings set to these values:
scene.render.border_max_x = .2
scene.render.border_min_x = 0
scene.render.border_min_y = 0
scene.render.border_max_y = .2
test_render_2.png
is created with the border settings set to these values:
scene.render.border_max_x = .4
scene.render.border_min_x = 0
scene.render.border_min_y = 0
scene.render.border_max_y = .4
test_render_3.png
is created with the border settings set to these values:
scene.render.border_max_x = .6
scene.render.border_min_x = 0
scene.render.border_min_y = 0
scene.render.border_max_y = .6
The total number of images is limited to 3
because the third time the loop code is executed, the variable var
is incremented to 3
which is not less than 3
.