LoginSignup
1
1

Blender 2.9, Python, 16進で色指定

Last updated at Posted at 2020-09-20

今日の課題は、HTMLで使う16進のカラーコードを使って
マテリアル色指定して、それを立体に反映させること。
まずは3色ほど指定。色合いは 「坊ちゃん団子 / つぼや菓子舗」(愛媛みやげ)を参考にしました。(数日前 Blender 2.8から 2.9に変えました。 2.8用Pythonスクリプトは2.9でも動いている様子です。)
(2024.5.3 追記。Blender 4.1.1でもこのままのスクリプトで動作しました。 )
References : StackExchange Blender 2.8 (python) - How to set material color using hex value instead of RGB

bochadango1.png

#bpy_nh20 (material color using hex value)
#(HEX色指定 )Blender 2.8 (python) - How to set material color using hex value instead of RGB

import bpy

# ========= DELETE ALL mesh, light, camera, みな削除する2行 =========
for item in bpy.data.objects:
    bpy.data.objects.remove(item)

# ======NEW CAMERA
bpy.ops.object.camera_add(enter_editmode=False, align='VIEW', location=(4, -6, 4), rotation=(1.3, 0, 0.5))
##(1.3, 0, -0.3)
# ====== NEW CUBE 
bpy.ops.mesh.primitive_cube_add(size=2,  align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))

def hex_to_rgb( hex_value ):
    b = (hex_value & 0xFF) / 255.0
    g = ((hex_value >> 8) & 0xFF) / 255.0
    r = ((hex_value >> 16) & 0xFF) / 255.0
    return r, g, b

def add_material(obj, material_name, h):
    material = bpy.data.materials.get(material_name)
    if material is None:
        material = bpy.data.materials.new(material_name)
    material.use_nodes = True
    principled_bsdf = material.node_tree.nodes['Principled BSDF']
    if principled_bsdf is not None:
        principled_bsdf.inputs[0].default_value = (*hex_to_rgb(h), 1)  
    obj.active_material = material

#h = 0xE7E7FF
#h = 0x87ceeb #skyblue#87ceeb
#h = 0xadff2f #greenyellow#adff2f
#h = 0xee82ee #violet#ee82ee
#saddlebrown#8b4513
#moccasin#ffe4b5
#olive#808000

obj = bpy.context.object
add_material( obj, "test1", 0x8b4513)  #saddlebrown#8b4513

bpy.ops.mesh.primitive_cube_add(size=2,  align='WORLD', location=(0, 2, 2), scale=(1, 1, 1))
obj = bpy.context.object
add_material( obj, "test2", 0xffe4b5 ) #moccasin#ffe4b5

bpy.ops.mesh.primitive_cube_add(size=2,  align='WORLD', location=(0, 4, 4), scale=(1, 1, 1))
obj = bpy.context.object
add_material( obj, "test3", 0x808000 ) #olive#808000

# ============== "light_spot1" ==== HIGH 
# create light datablock, set attributes
light_data = bpy.data.lights.new(name="light_spot1", type='SPOT')
light_data.energy = 700
# create new object with our light datablock
light_object1 = bpy.data.objects.new(name="light_spot1", object_data=light_data)
# link light object
bpy.context.collection.objects.link(light_object1)
# make it active 
bpy.context.view_layer.objects.active = light_object1
#change location
light_object1.location = (-5, -7, 4)
light_object1.delta_rotation_euler = (1.3, 0, -0.3) #ゼロゼロゼロで真下を向く。
# update scene, if needed
dg = bpy.context.evaluated_depsgraph_get() 
dg.update()
# ================
# =========================== end of bpy_nh20
1
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
1
1