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.)
#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