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.
While loop:
If I understand what you are trying to do, you want a while
statement 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
Explanation:
Here is an explanation of what each line does:
import bpy
This imports the bpy
module.
var = 0
This assigns "var" to a value, 0
. See the wiki for more about variables.
for scene in bpy.data.scenes:
This is a for statement
that will repeat the code indented after this line for each scene in bpy.data.scenes
. (this will also assign scene
as a kind of variable containing the location of the current scene being iterated through.)
scene.render.use_border = True
This sets blender's variable use_border
to True
. This is the same as ticking the check box in the GUI (Render settings > Dimensions):
To illustrate this, you can run bpy.context.scene.render.use_border = True
in the Python console and the Border check box will become enabled in the GUI.
scene.render.use_crop_to_border = True
This enables Crop to border ([use_crop_to_border][13]
)
while var < 3:
Test if var
is equal to a value less than 3
. If true, then execute the indented code following this line and test if var < 3
again. If false, skip to the next unindented code.
var = var + 1
Assign var
to the result of var + 1
(increment by one)
scene.render.border_max_x = mx
Set blender's variable border_max_x
to the current value of the variable mx
.
bpy.context.scene.render.filepath = "/tmp/test_render_" + str(var) + ".png"
Use bpy.context
to get the active scene and set the scene render property filepath
to a string. to use the variable var
(type INT
) in the string, it needs to be converted to a string type with str(var)
. See the wiki for more info about strings and variables.
bpy.ops.render.render
(write_still=True)
This renders the image and writes it to the path specified in filepath
.
Result:
The above code outputs the following images in /tmp/
when run in the Text editor:
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
.
Here is a code that moves to the next "tile" over:
import bpy
var = 0
mx = 0
sx = -.2
sy = 0
my = .2
for scene in bpy.data.scenes:
scene.render.use_border = True
scene.render.use_crop_to_border = True
while var < 3:
var = var + 1
mx = mx + .2
sx = sx + .2
print (mx)
print (sx)
scene.render.border_max_x = mx
scene.render.border_min_x = sx
scene.render.border_min_y = sy
scene.render.border_max_y = my
bpy.context.scene.render.filepath = "/tmp/test_render_" + str(var) + ".png"
bpy.ops.render.render(write_still=True)
if 1 > 0
for? $\endgroup$mx
sx
etc. toscene.render.border_min/max_x/y = 0
, then assignedscene.render.border_min/max_x/y
tomx
,sx
etc.? Do you wantmx = 0
, thenscene.render.border_max_x = mx
? $\endgroup$while
loop? If I understand what you want, couldn't you use awhile loop
instead ofif 1 > 0:
? This would repeat the render and incrementmx
andmy
until the statement after thewhile
is false. $\endgroup$