2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Blender×python]くだらないけどなぜかずっと見ちゃう クリスタル幾何山⛰

2
Last updated at Posted at 2025-08-21

画面収録-2025-08-21-22.37.05.gif

なんだこれは と聞こえてきそうなものを作った
なぜかって。
最近、業務効率のためにシステム開発しているけど、全然わくわくしない

どんどん保守的思考になり奇抜な発想ができなくなっている
嗚呼。

思うがまま、わくわくした気持ちでpython開発したい
ってことで作りました

Blender×Pythonで「ガラス風クリスタルの積み木」で波打つように

↓ 大まかにこんな大雑把なプロンプトを投げました

クリスタルでぷにぷにしていて色んな色で重ねたい ピラミッド型にして 波打つようにアニメーションをつけたい。これをブレンダーのpythonで表現したい

✨ポイントは1つ「クリスタル風」✨

ShaderNodeBsdfGlass を使ってガラスの質感を表現
IOR(屈折率) を 1.45 に設定 → 本物のガラスっぽい見え方
Roughness(粗さ) を 0.05 に設定 → 少しだけ曇った感じに調整

↓こちらの関数で指定しています

python

def create_crystal_material(color):
    mat = bpy.data.materials.new(name="Crystal")
    mat.use_nodes = True
    nodes = mat.node_tree.nodes
    links = mat.node_tree.links
    for n in nodes:
        if n.name != "Material Output":
            nodes.remove(n)
    glass = nodes.new(type="ShaderNodeBsdfGlass")
    glass.inputs["Color"].default_value = color
    glass.inputs["IOR"].default_value = 1.45
    glass.inputs["Roughness"].default_value = 0.05
    links.new(glass.outputs["BSDF"], nodes["Material Output"].inputs["Surface"])
    return mat

全体コード

全体コードはこちら↓
python

import bpy
import random
import math

# 既存オブジェクト削除
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()


# クリスタル用マテリアル
def create_crystal_material(color):
    mat = bpy.data.materials.new(name="Crystal")
    mat.use_nodes = True
    nodes = mat.node_tree.nodes
    links = mat.node_tree.links
    for n in nodes:
        if n.name != "Material Output":
            nodes.remove(n)
    glass = nodes.new(type="ShaderNodeBsdfGlass")
    glass.inputs["Color"].default_value = color
    glass.inputs["IOR"].default_value = 1.45
    glass.inputs["Roughness"].default_value = 0.05
    links.new(glass.outputs["BSDF"], nodes["Material Output"].inputs["Surface"])
    return mat

# 積み木の山
cube_size = 1.0
gap = 0.05
total_levels = 6   # 6段で ~91個

cubes = []
for level in range(total_levels):
    size = total_levels - level
    z = level * (cube_size + gap)
    for i in range(size):
        for j in range(size):
            x = (i - size/2) * (cube_size + gap)
            y = (j - size/2) * (cube_size + gap)
            bpy.ops.mesh.primitive_cube_add(size=cube_size, location=(x, y, z))
            cube = bpy.context.active_object
            
            # BEVEL
            bpy.ops.object.modifier_add(type='BEVEL')
            cube.modifiers["Bevel"].width = 0.1
            cube.modifiers["Bevel"].segments = 5
            
            # Material
            color = [random.random(), random.random(), random.random(), 1]
            mat = create_crystal_material(color)
            cube.data.materials.append(mat)
            
            cubes.append((cube, x, y, z))

# アニメーションをキーで作成
frame_start = 1
frame_end = 300
speed = 0.2
height = 0.5

for frame in range(frame_start, frame_end+1):
    for cube, x, y, base_z in cubes:
        offset = (x + y) * 0.5
        z = base_z + math.sin(frame*speed + offset) * height
        cube.location.z = z
        cube.keyframe_insert(data_path="location", frame=frame, index=2)  # Z座標のみ

# シーン設定
bpy.context.scene.frame_start = frame_start
bpy.context.scene.frame_end = frame_end
bpy.context.scene.render.engine = 'CYCLES'

その他

何も縛りがなく、ただただくだらないものをつくる 

こんなプログラミングもたまにはいいものです

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?