2
$\begingroup$

I want to be able to render a tile and then move to the left and render that tile

Specify tile size for Blender Cycles rendering via command line

So far I have gotten this... I have no idea how to make it loop to the next one. I'm new to this.

It runs without error but I'm sure its all wrong:

import bpy

for scene in bpy.data.scenes:
    mx = scene.render.border_max_x = 0
    sx = scene.render.border_min_x = 0
    sy = scene.render.border_min_y = 0
    my = scene.render.border_max_y = 0
    scene.render.use_border = True
    scene.render.use_crop_to_border = True
if 1 > 0:
    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
    int (my);
    int (mx);

    bpy.ops.render.render()
$\endgroup$
6
  • $\begingroup$ my math isnt done yet so dont make fun lololol $\endgroup$ – LordOdin Oct 27 '13 at 16:30
  • 4
    $\begingroup$ What is if 1 > 0 for? $\endgroup$ – wchargin Oct 27 '13 at 20:23
  • $\begingroup$ I'm quite sure why you assigned mx sx etc. to scene.render.border_min/max_x/y = 0, then assigned scene.render.border_min/max_x/y to mx, sx etc.? Do you want mx = 0, then scene.render.border_max_x = mx? $\endgroup$ – gandalf3 Oct 27 '13 at 23:03
  • $\begingroup$ And you are making a loop in initialization and after it you do an if that is always done (1 is greater than 0 all time). Take in mind that if you want to make the if statement executed in each scene you must tab in it (tabs are important in python). $\endgroup$ – PhoneixS Oct 28 '13 at 8:52
  • $\begingroup$ Could you use a while loop? If I understand what you want, couldn't you use a while loop instead of if 1 > 0:? This would repeat the render and increment mx and my until the statement after the while is false. $\endgroup$ – gandalf3 Oct 28 '13 at 19:12
2
$\begingroup$

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):

enter image description here

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_1.png


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_2.png


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

test_render_3.png

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)
$\endgroup$
4
  • 1
    $\begingroup$ @gandalf3 Completely off topic, but you made two separate edits that both added exactly 1426 characters to the post... this is either ridiculous coincidence or you have way too much time on your hands. $\endgroup$ – wchargin Nov 5 '13 at 4:03
  • $\begingroup$ @WChargin 0.o That's pretty incredible (And I certainly didn't plan it) $\endgroup$ – gandalf3 Nov 5 '13 at 4:16
  • $\begingroup$ gandalf is a wizard! $\endgroup$ – LordOdin Nov 6 '13 at 5:11
  • $\begingroup$ upvote for just the extent of explaining a simple problem that could have been fixed by watching a beginner python tutorial on youtube. $\endgroup$ – Pythogen Jan 14 '17 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.