LoginSignup
12
13

More than 5 years have passed since last update.

Blenderでカーテンジェネレータを作る

Posted at

概要

blenderでカーテン画像を生成するプログラムを作ってみました。元ネタは二次元から三次元を作る方法 — サーバ内でBlenderを動かしてみたら意外にもイケていたという話 - pixiv insideから。

環境

バージョン:2.74、レンダリングエンジン:cycles

準備

まずはblenderを起動し、
blender1.png

カーテンのある風景を作ります。今回のはシンプルにカーテンがあるだけの部屋で、白いカーテンはレースカーテンで外からの光が透けているかんじになります。ちなみにカーテンの作り方についてはModeling with Cloth Simulation in Blender - YouTubeを参考にしました。とてもわかりやすく説明されています。
blender2.png

実装

左右のカーテンそれぞれにマテリアルが設定してあります。マテリアル名は curtain.leftcurtain.right としておきます。

  1. テクスチャを設定
  2. レンダリング
  3. pngで出力

この処理をプログラムで行います。pythonで書くと以下のようになります。

import sys
import os
import bpy

# How to pass command line arguments to a Blender Python script? - Blender Stack Exchange
# http://blender.stackexchange.com/questions/6817/how-to-pass-command-line-arguments-to-a-blender-python-script
argv = sys.argv
[left, right, out] = argv[argv.index("--") + 1:]  # get all args after "--"

def update_filepath(material_name, filepath):
    t = bpy.data.materials[material_name].node_tree.nodes.get('Image Texture')
    t.image.filepath = filepath

update_filepath('curtain.left', os.path.abspath(left))
update_filepath('curtain.right', os.path.abspath(right))

bpy.ops.render.render()
bpy.data.images['Render Result'].save_render(filepath = os.path.abspath(out + '.png'))

これを実行すると

$ blender --background path/to/file.blend --python path/to/script.py -- left.png right.png rendered

こうなる!ちょっとノイズが乗ってしまいましたが時間がないのでここまで。
out.png

参考

12
13
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
12
13