Blender 2.9, Python, hexadecimal color specification

Today's challenge is to use the hexadecimal color code used in HTML Specify the material color and reflect it on the solid. First, specify about 3 colors. For the color, I referred to "Bochan Dumpling / Tsuboya Confectionery" (Ehime souvenir). (I changed from Blender 2.8 to 2.9 a few days ago. The Python script for 2.8 seems to work with 2.9 as well.) bochadango1.png

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

import bpy

# ========= DELETE ALL mesh, light, camera,2 lines to delete all=========
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) #Look straight down at zero zero zero.
# update scene, if needed
dg = bpy.context.evaluated_depsgraph_get() 
dg.update()
# ================
# =========================== end of bpy_nh20

Recommended Posts

Blender 2.9, Python, hexadecimal color specification
Blender 2.8, Python, camera movement, random number color specification
blender, python, spiral staircase, color
Blender 2.9, Python background light color test
Blender 2.9 Python Extrude extrude
blender, python, spiral staircase
Run Blender with python
blender, python, sphere behavior
Blender 2.8, Python Cube Scaling
Hexadecimal handling in Python 3
[ev3dev × Python] Color sensor
[Blender x Python] Blender Python tips (11/100)
Blender 2.9, Python Buildings, Cars, Videos
[Python] Adjusting the color bar
Blender 2.9, Python Odd Even Building
Blender 2.8, Python, light Spot lighting
Blender Python API in Houdini (Python 3)
Generate 8 * 8 (64) cubes in Blender Python
Draw Sine Waves in Blender Python
[python] Clock that changes color (animation)
Blender 2.8, Python Cube, Lighting, Camera Circumference
Run mruby with Python or Blender
Install Pytorch on Blender 2.90 python on Windows
[Python] Adjusted the color map standard
[Blender x Python] Particle Animation (Part 1)
Python colorama Color memo (Cmder environment)
Multi-line size specification reading with python
Possible values for Tkinter color specification
Data formatting for Python / color plots
[Blender x Python] Let's master random !!
[Blender x Python] Let's master rotation !!
Access Blender Shader Nodes from Python
Easy modeling with Blender and Python
Get started with Python in Blender