[PYTHON] How to set the output resolution for each keyframe in Blender

2020-11-19 00_11_40-StrokesPlus.jpg

I decided to use Blender's render image for the draft of the manga. It is assumed that the render image for each key frame will be pasted into the manga creation software in correspondence with "1 frame = 1 key frame".

Normally, comic frames have different aspect ratios for each frame. However, Blender does not allow you to set the resolution for each keyframe. You have to use the same resolution for all keyframes or switch manually frame by frame. Both are inconvenient.

Therefore, I thought about how to set the resolution for each key frame. Blender allows you to set a string ** marker ** in keyframes, so I decided to record the aspect ratio of the resolution there.

In general horizontal reading manga, the vertical and horizontal size of the frame is indefinite, but this time it is a vertical reading manga, and it is assumed that the width of the frame is always constant at 2480px. The method of specifying the height and width at the same time is supplemented at the end.

Execution environment

Thing you want to do

screenshot.1596102429.png

Realization policy

Setup steps

Enter the aspect ratio in the marker

In the Timeline panel, press M to add a marker to the keyframe and Ctrl + M to enter the aspect ratio as the marker name.

Enter the number Y / X this time. For example, X: Y = 4: 3 is 0.75, 1: 1 is 1, and 1: 2 is 2.

By the way, you can set multiple markers in one frame, but in this program, the first value found will be used as the aspect ratio.

Write a python script

Then open the Text Editor panel and enter the following code.

set_resolution_from_marker.py


import bpy

RESOLUTION_X = 2480
DEFAULT_RATIO = 1.0


def find_ratio_from_current_keyframe():
    #Search for the marker set for the current keyframe from all markers
    marker_items =  bpy.context.scene.timeline_markers.items()
    current = bpy.data.scenes["Scene"].frame_current
    markers = [item[0] for item in marker_items if item[1].frame == current]
    
    #Returns the default ratio if the marker does not exist
    if len(markers) < 1:
        print(f"No marker is set to current keyframe. Now ratio is {DEFAULT_RATIO}.")
        return DEFAULT_RATIO

    print("Found marker(s): ", markers)
    #Converts the marker string to a number and returns it
    for m in markers:
        try:
            ratio = float(m)
            print("Ratio is set to ", ratio)
            return ratio
        except ValueError as e:
            print(e)
            continue

    #Returns the default ratio if there are no markers that can be converted to numbers
    print(f"Marker is set to current keyframe but not valid number. Now ratio is {DEFAULT_RATIO}.")
    return DEFAULT_RATIO


def update_resolution(scene):
    scene.render.resolution_x = RESOLUTION_X
    scene.render.resolution_y = RESOLUTION_X * find_ratio_from_current_keyframe()
    

#Set callback to update resolution when changing keyframes
bpy.app.handlers.frame_change_pre.append(update_resolution)

ℹ️ Brief commentary

Run the script

Run "Run Script (Alt + P)" in the Text Editor panel.

After that, Resolution X & Y is set automatically every time you switch keyframes.

Supplement

What if I want to be able to set both Resolution X & Y?

This time it was a slightly special case of "X = 2480px fixed", but if you want to set X and Y at the same time, you can easily handle it by changing the marker notation and Python script.

For example, write 1280,720 in the marker, split the string with commas in a Python script, and assign the first value to X and the second value to Y.

Can't it be realized with a driver?

I could not do it. This is because the driver cannot set any of the values under the Scene property, including Resolution.

Why doesn't the resolution update when I enter or change a marker?

I couldn't find a callback that could be called at that time: disappointed: Sorry for your inconvenience, but please advance the keyframe by one with the arrow keys and move it back.

Reference link

Recommended Posts

How to set the output resolution for each keyframe in Blender
How to know the current directory in Python in Blender
[Blender] How to dynamically set the selection of EnumProperty
[Python] How to output the list values in order
Output the specified table of Oracle database in Python to Excel for each file
How to set the development environment for each project with VSCode + Python extension + Miniconda
How to set the html class attribute in Django's forms.py
How to overwrite the output to the console
How to find the cumulative sum / sum for each group using DataFrame in Spark [Python version]
[Introduction to Python] How to use the in operator in a for statement?
[Blender] How to set shape_key with script
How to set the server time to Japanese time
How to get colored output to the console
Switch the module to be loaded for each execution environment in Python
How to use the C library in Python
How to specify the launch browser for JupyterLab 3.0.0
How to use MkDocs for the first time
How to set browser location in Headless Chrome
How to erase the characters output by Python
How to get the files in the [Python] folder
How to output "Ketsumaimo" as standard output in Python
How to find the correlation for categorical variables
How to set CPU affinity for process threads
How to count the number of elements in Django and output to a template
How to set up public key authentication in ssh
How to set a shared folder with the host OS in CentOS7 on VirtualBOX
How to retrieve the nth largest value in Python
[For beginners] How to use say command in python!
How to get the variable name itself in python
How to run the Ansible module added in Ansible Tower
The easiest way to set up Last-Modified in Flask
How to get the number of digits in Python
How to create a submenu with the [Blender] plugin
How to run python in virtual space (for MacOS)
Set update rules for each parameter in Chainer v2
Set the form DateField to type = date in Django
How to eliminate garbled characters in matplotlib output image
How to use the exists clause in Django's queryset
[Introduction to Udemy Python3 + Application] 30. How to use the set
How to use the model learned in Lobe in Python
Find a guideline for the number of processes / threads to set in the application server
How to count the number of occurrences of each element in the list in Python with weight
How to turn the for statement when there are multiple values for one key in the dictionary
How to set variables that can be used throughout the Django app-useful for templates, etc.-
Define a task to set the fabric env in YAML
How to write custom validations in the Django REST Framework
How to find the optimal number of clusters in k-means
Set the DateTime type output format in your Django template
How to set up Ubuntu for Windows Subsystem for Linux 2 (WSL2)
[python] How to check if the Key exists in the dictionary
How to support x-nullable for swagger in python's jsonschema library
Test code to check for broken links in the page
Output "Draw ferns programmatically" to the drawing process in Python
How to debug the Python standard library in Visual Studio
How to import Python library set up in EFS to Lambda
Check the operation of Python for .NET in each environment
[python] How to use the library Matplotlib for drawing graphs
How to output a document in pdf format with Sphinx
How to use the __call__ method in a Python class
Change the standard output destination to a file in Python
How to define multiple variables in a python for statement