0
$\begingroup$

I'm trying to produce animation frames for a rotated animation with everything else on scene left intact.

My rendering code looks as follows:

import bpy
import os
import math

# Set the output directory for the rendered images
output_directory = "/tmp/test-blender-render"

# Set the number of frames and the output image format
start_frame = 1
end_frame = 20
output_format = 'PNG'

# Rotate animation parent
pad_object_name = 'animation-platform'
# Set the rotation angle in degrees
rotation_angle_degrees = 45.0
# Convert the rotation angle to radians
rotation_angle_radians = math.radians(rotation_angle_degrees)
# Get the reference to the object
obj = bpy.data.objects.get(pad_object_name)
if obj is None:
    raise Exception("There is no platform object for this animation")
# Set the rotation mode to Euler
obj.rotation_mode = 'XYZ'
# Set the Z-axis rotation
obj.rotation_euler[2] += rotation_angle_radians

# Set the render settings
bpy.context.scene.render.image_settings.file_format = output_format

# Create the output directory if it doesn't exist
os.makedirs(output_directory, exist_ok=True)

# Set the output path template
output_path_template = os.path.join(output_directory, "frame_####.{}".format(output_format.lower()))

# Set the output file format and output path
bpy.context.scene.render.image_settings.file_format = output_format
bpy.context.scene.render.filepath = output_path_template

# Set the frame range
bpy.context.scene.frame_start = start_frame
bpy.context.scene.frame_end = end_frame

# Render the animation
bpy.ops.render.render(animation=True)

however rendered frame look exactly the same, nothing changes in viewport either.

When I execute first part of the script (by deleting everything past Set the render settings comment) the object rotates correctly. In contrast, when I run the script above nothing changes at all.

I suspect that bpy.ops.render.render needs some key to apply rotation properly?

To add to the above - I'm fine to revert revert rotation once rendering finishes - in fact this is what I initially planned to write but got stuck with rendering somehow ignoring rotation completely.

Edit 1: This question applies to Blender 3.6.0. Blender 2.x might have a bit different way of accomplishing the same.

$\endgroup$
5
  • $\begingroup$ i just would guess that you have keyframes on the rotation value of that object? these would overwrite your python change. if not, pls provide blend file so we can check it out. thx $\endgroup$
    – Chris
    Jul 16, 2023 at 17:54
  • $\begingroup$ Here is a reduced example: drive.google.com/file/d/1VfmlhxPM6mkVwAyLJry3xAHOyM_yXnig/view - it has a script that has rotation_angle_degrees which doesn't seem to be applied properly. $\endgroup$
    – J. A.
    Jul 16, 2023 at 23:39
  • $\begingroup$ ok, sorry, didn't look close enough. You render the animation in one line (bpy.ops.render.render(animation=True) -> this renders the whole animation. No code is executed in between this command. So nothing changes there. So either you insert keyframes for your rotation in your python code, or your make a loop and render single images. $\endgroup$
    – Chris
    Jul 17, 2023 at 6:54
  • $\begingroup$ Thank you for taking a look! Would you mind elaborating how to render single images with the above code? My knowledge of blender python is a bit limited. I tried adding other parameters to the render call to no avail. If you have a spare minute - please, write an answer so I could accept it. $\endgroup$
    – J. A.
    Jul 17, 2023 at 17:26
  • $\begingroup$ to add to the above: the part in my question that reads "however rendered frame look exactly the same, nothing changes in viewport either" should better be reformulated as "however rendered animation looks as if an object wasn't rotated at all, also rotation doesn't apply to the object after running the script". $\endgroup$
    – J. A.
    Jul 17, 2023 at 17:28

1 Answer 1

0
$\begingroup$

try this:

import bpy
import math

for i in range(0,4):

    rotation_angle_degrees = 15.0
    # Convert the rotation angle to radians
    rotation_angle_radians = math.radians(rotation_angle_degrees)
    # Get the reference to the object
    obj = bpy.context.active_object
    if obj is None:
        raise Exception("There is no platform object for this animation")
    # Set the rotation mode to Euler
    obj.rotation_mode = 'XYZ'
    # Set the Z-axis rotation
    obj.rotation_euler[2] += rotation_angle_radians

    name = "\\tmp\\" + str(i)
    
    bpy.data.scenes["Scene"].render.filepath = name
    
    

    # Render the animation
    bpy.ops.render.render(write_still=True)

Note: the name, which is path + name might be different on your system. I am working on a Macbook and this works for me. So please change that so that it works for you.

result on my computer:

enter image description here

$\endgroup$
2
  • $\begingroup$ it wouldn't render the whole animation, would it? My sample code posted in my original post would render all animation frames. $\endgroup$
    – J. A.
    Jul 17, 2023 at 20:59
  • $\begingroup$ To add to the above: the purpose of my script is to render exact same animation sequence, but taken at a different angle with only one object rotated on the viewport - with lighting, environment, etc staying intact. Sorry if it wasn't clear in my original post. This is why I try to rotate that animation-platform object but not the camera. $\endgroup$
    – J. A.
    Jul 17, 2023 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .