LoginSignup
2
1

More than 1 year has passed since last update.

カメラの下絵を読み込んでレンダリング解像度を設定するアドオン

Posted at

Blender 2.8以降には カメラビューで画像を下絵として表示する機能があります
下絵にカメラの構図を合わせてレンダリングするといった場合
下絵とレンダリングの解像度を合わせるという操作が必要になることがあるので
画像の読み込みからレンダリング解像度の設定まで行うアドオンを作ってみました。

y_LoadBGimage_28.py
bl_info = {
    "name": "load BackGround Image28",
    "author": "Yukimi",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "View3D ",
    "description": "下絵画像の読み込みとそれに合わせたレンダリング解像度の設定 ",   
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Interface"
}

import bpy
import os
from bpy_extras.io_utils import (
        ImportHelper,
        )

class IMAGE_OT_SetBackground(bpy.types.Operator):
    bl_idname = "import.backgroundimages"
    bl_label = "import background image"
    # ファイルオープンで選択されたファイル
    filepath: bpy.props.StringProperty(subtype='FILE_PATH')
    def execute(self, context):
        img = load_bg_img(context, self.filepath)
        #画像を下絵に設定
        set_camera_bg(context, img)
        return {'FINISHED'}  
    def invoke(self, context, event):
        context.window_manager.fileselect_add(self)
        return {'RUNNING_MODAL'}

def set_camera_bg(context, img):
    '''画像オブジェクトをシーンのカメラの下絵に設定'''
    camera = context.scene.camera
    camera.data.show_background_images = True
    bg = camera.data.background_images.new()
    bg.image = img
    # 前面表示
    bg.display_depth = 'FRONT'
    bg.frame_method = 'FIT'
    return

def load_bg_img(context, filepath):
    #イメージの読み込み
    img = bpy.data.images.load(filepath)
    #レンダリング解像度の設定
    context.scene.render.resolution_x = img.size[0]
    context.scene.render.resolution_y = img.size[1]
    return (img)

# メニューを構築する関数
def menu_func(self, context):
    self.layout.operator(IMAGE_OT_SetBackground.bl_idname, text="下絵を読み込んでレンダリングサイズ設定")

classes = (
    IMAGE_OT_SetBackground,
    )
def register():
    #classes
    for cls in classes:
        bpy.utils.register_class(cls)
    #menu
    bpy.types.TOPBAR_MT_file_import.append(menu_func)

def unregister():
    #menu
    bpy.types.TOPBAR_MT_file_import.remove(menu_func)
    #classes
    for cls in classes:
        bpy.utils.unregister_class(cls)

if __name__ == "__main__":
    register()

スクリプトを保存して プレファレンスのアドオン項目のインストールボタンで表示されるファイル選択から保存したファイルを選択し
 load BackGround Image28 を有効にしてください

ファイルのインポートメニューに「下絵を読み込んでレンダリングサイズ設定」の項目が追加されます
image.png
この項目を実行して 画像ファイルを選択すると
読み込んだ画像のサイズにレンダリングサイズを指定して
現在シーンでレンダリングに使用しているカメラの下絵に画像を設定します。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1