1
$\begingroup$

I am rendering using a python script that renders 5 view angles of an object. So 5 renders, each with a different camera. The code for one of these view angles is:

import bpy
import mathutils
import sys

# Store the current scene inside a variable, that way we can access it later on

scene = bpy.context.scene

# store collection strings

x = bpy.data.collections
y = bpy.data.scenes["CONFIGURATOR 800 x 600"].view_layers["ALL"]
z = bpy.context.view_layer

# render 800 x 600    

n = b.name
bpy.context.scene.camera = bpy.context.scene.objects["FMR PAN DOWN"]
bpy.context.scene.cycles.samples = 100
scene.render.resolution_x = 800
scene.render.resolution_y = 600
scene.render.resolution_percentage = 100
scene.render.use_border = False
scene.render.image_settings.file_format = 'PNG'
bpy.data.scenes["CONFIGURATOR 800 x 600"].render.filepath = '/BlenderPythonTest/render_%s 800x600.png' % n
bpy.ops.render.render(write_still = 1)

However! When I run this the scene's camera changed to another random camera upon rendering.

If I comment the rendering out, the active scene camera has, in this case, changed to "FMR PAN DOWN".

I have identified where I have gone wrong but can't seem to fix it. The camera it changes to for rendering is the camera highlighted in the animation panel here:

enter image description here

I have tried to use:

bpy.ops.render.render(write_still = 1, use_viewport=True)

to simulate render image(F12) rather than render animation (Ctrl-f12). But nothing...

$\endgroup$
2
1
$\begingroup$

@batFINGER got me on the right path. I was cycling through renders using different cameras to create different views of a product so hopefully this will be of use to somebody.

Firstly and importantly, you have to prepare the file manually. Click on the animation tab, go to the Dope sheet, delete all the markers you had previously and just add one marker on the first frame (0). Rename that marker "F_00".

Back to the scripting tab. In the code, create a list of the camera object names, assign a starting and ending frame number, in this case "0". Select the timeline marker and assign a camera to the marker using the code below. Rendering code followed and as @SergeL said in batFINGER's comment above, the new camera just replaced the previous one and the scene rendered perfectly from all angles.

import bpy
import mathutils
import sys

# Store the current scene inside a variable, that way we can access it later on

scene = bpy.context.scene

# store collection strings

x = bpy.data.collections
y = bpy.data.scenes["CONFIGURATOR 800 x 600"].view_layers["ALL"]
z = bpy.context.view_layer


###############################################################
########## CYCLE THROUGH THE CAMERAS AND RENDER ###############
###############################################################

camviews = [
            "FMR ISO",
            "FMR PAN DOWN",            
            "FMR FULL FRONT"
           ]

for camname in camviews:
    
    bpy.data.scenes["CONFIGURATOR 800 x 600"].frame_start = 0
    bpy.data.scenes["CONFIGURATOR 800 x 600"].frame_end = 0
    

    #################################################
    ##### I PUT MY SCENE RESET CODE HERE ############
    #################################################
 
    # change camera

    bpy.context.scene.timeline_markers["F_00"].select = True
    bpy.context.scene.timeline_markers["F_00"].camera = bpy.context.scene.objects[camname]

    ###############################################################
    ############ CODE TO SELECT WHAT OBJECTS TO RENDER ############
    ###############################################################

    # Make the plates visible and render

    sideplates = bpy.data.collections["RACK 1 - 1 RACK FRAME"].children

    for s in sideplates:
        s.hide_render=False
        n = s.name

        # render 800 x 600 and save camera name and part name to a folder on the pc

        bpy.context.scene.cycles.samples = 1
        scene.render.resolution_x = 800
        scene.render.resolution_y = 600
        scene.render.resolution_percentage = 100
        scene.render.use_border = False
        scene.render.image_settings.file_format = 'PNG'
        bpy.data.scenes["CONFIGURATOR 800 x 600"].render.filepath = '/BlenderPythonTest/Single Rack/Configurator/Rack Frame/%(2)s/800x600/%(1)s 800x600.png' % {"1" : n, "2" : camname} 
        bpy.ops.render.render(write_still = 1)
                
        s.hide_render=True
$\endgroup$

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.