def create_materials(c_model): ret = [] for material in Materials: ret.append(bpy.data.materials.new(material.name)) for i in ret: if 'plot' in i.name: base = Color() base.hsv = c_model['base'] color = base base_color(i, color) elif 'street' in i.name: color1 = Color() color1.hsv = c_model['color1'] color = color1 base_color(i, color) elif 'river' in i.name: color2 = Color() color2.hsv = c_model['color2'] color = color2 base_color(i, color) elif 'building' in i.name: color3 = Color() color3.hsv = c_model['color3'] color = color3 base_color(i, color) elif 'park' in i.name: color4 = Color() color4.hsv = c_model['color4'] color = color4 base_color(i, color) return ret
def get_color_id(index, count, jitter=False): # Get unique color color = Color() indexList = [ 0, 171, 64, 213, 32, 96, 160, 224, 16, 48, 80, 112, 144, 176, 208, 240, 8, 24, 40, 56, 72, 88, 104, 120, 136, 152, 168, 184, 200, 216, 232, 248, 4, 12, 20, 28, 36, 44, 52, 60, 68, 76, 84, 92, 100, 108, 116, 124, 132, 140, 148, 156, 164, 172, 180, 188, 196, 204, 212, 220, 228, 236, 244, 252, 2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 54, 58, 62, 66, 70, 74, 78, 82, 86, 90, 94, 98, 102, 106, 110, 114, 118, 122, 126, 130, 134, 138, 142, 146, 150, 154, 158, 162, 166, 170, 174, 178, 182, 186, 190, 194, 198, 202, 206, 210, 214, 218, 222, 226, 230, 234, 238, 242, 246, 250, 254, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 128, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 192, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255 ] i = 0 if index > 255: while index > 255: index -= 256 i += 1 if jitter: color.hsv = ((indexList[index] + 1 / (2**i)) / 256), 0.9, 1.0 else: color.hsv = (index / (count)), 0.9, 1.0 return color
def __update_suzanne(self, props): # make object if not "Temperature_Suzanne" in bpy.data.objects: bpy.ops.mesh.primitive_monkey_add() bpy.context.active_object.name = "Temperature_Suzanne" suzanne_obj = bpy.data.objects["Temperature_Suzanne"] # make material if len(bpy.data.materials) == 0: bpy.ops.material.new() # make material slot if len(suzanne_obj.material_slots) == 0: bpy.context.scene.objects.active = suzanne_obj bpy.ops.object.material_slot_add() suzanne_obj.material_slots[0].material = bpy.data.materials['Material'] mtrl = suzanne_obj.material_slots[0].material # change color min_temp = -10 max_temp = 50 temp_range = max_temp - min_temp color = Color() color.hsv = ( 1.0 - (((props.temperature-min_temp)/(temp_range-10))*270+90)/360, 0.95, 0.85 ) mtrl.diffuse_color = color
def setup_materials(materials_array, materials): if materials is not None: material_count = len(set(materials.values())) c = Color() for i in range(material_count): c.hsv = (i / float(material_count), 0.7, 0.25) materials_array.append(make_material("CelticKnot", c))
def brick_colour(self, brick_size, brick_name): height = brick_size[2] width = brick_size[0] depth = brick_size[1] if brick_name in bpy.data.materials: mat = bpy.data.materials[brick_name] else: colour = self.base_colours[depth] if height == 1: colour = [colour[0], colour[1] - 0.2, colour[2]] if width == 2: colour = [colour[0] + 0.028, colour[1], colour[2]] c = Color() c.hsv = colour[0], colour[1], colour[2] mat_name_string = brick_name mat = bpy.data.materials.new(name=mat_name_string) mat.diffuse_color = c return mat
def execute(self, context): from zlib import crc32 objects = context.selected_objects if not objects: self.report({'ERROR'}, 'No objects selected') return {'CANCELLED'} seed = self.seed power = self.power materials = set() for obj in objects: for slot in obj.material_slots: materials.add(slot.material) for mat in materials: data = bytearray(mat.name, 'utf8') data.append(seed) hsh = crc32(data) color = Color() color.hsv = ((hsh & 0xFF) / 0xFF, (((hsh >> 8) & 3) / 3 * 0.5 + 0.5) * power, ((hsh >> 2) & 1) * (0.5 * power) + 0.5) color = [color.r, color.g, color.b] if IS_28: color.append(1.0) # alpha mat.diffuse_color = color return {'FINISHED'}
def __update_text(self, props): if not "Temperature_Text" in bpy.data.objects: bpy.ops.object.text_add() bpy.context.active_object.name = "Temperature_Text" text_obj = bpy.data.objects["Temperature_Text"] if text_obj.type == 'FONT': text_obj.data.body = "{0:.1f}".format(props.temperature) # make material slot if len(text_obj.material_slots) == 0: bpy.context.scene.objects.active = text_obj bpy.ops.object.material_slot_add() text_obj.material_slots[0].material = bpy.data.materials['Material'] mtrl = text_obj.material_slots[0].material # change color min_temp = -10 max_temp = 50 temp_range = max_temp - min_temp color = Color() color.hsv = ( 1.0 - (((props.temperature-min_temp)/(temp_range-10))*270+90)/360, 0.95, 1.0 ) mtrl.diffuse_color = color
def init(count): global matcount, groundmat matcount = count for i in range(matcount): bpy.ops.material.new() new_mat = bpy.data.materials[ -1] # the new material is the last one in the list # new_mat.name = f"test_material_{i}" new_mat.use_nodes = True # ---- Principled BDSF inputs ----- https://blender.stackexchange.com/questions/105463/principled-shader-inputs # 0: Base Color # 4: Metallic # 7: Roughness # Base color h = random.random() s = 1.0 v = random.random() + 0.2 a = 1.0 c = Color() c.hsv = h, s, v new_mat.node_tree.nodes["Principled BSDF"].inputs[0].default_value = ( c.r, c.g, c.b, a) # Metallic new_mat.node_tree.nodes["Principled BSDF"].inputs[ 4].default_value = random.random() # Roughness new_mat.node_tree.nodes["Principled BSDF"].inputs[ 7].default_value = random.random() matlist.append(new_mat) # Ground material bpy.ops.material.new() groundmat = bpy.data.materials[ -1] # the new material is the last one in the list groundmat.name = f"ground" groundmat.use_nodes = True # Base color groundmat.node_tree.nodes["Principled BSDF"].inputs[0].default_value = (0, 0, 0, 1) # Metallic groundmat.node_tree.nodes["Principled BSDF"].inputs[4].default_value = 0 # Roughness groundmat.node_tree.nodes["Principled BSDF"].inputs[7].default_value = 0.4
def randomly_change_color(material_to_change): """ Takes in object and changes its color using hsv """ color = Color() hue = random.random() color.hsv = (hue, 1, 1) rgba = [color.r, color.g, color.b, 1] material_to_change.node_tree.nodes["Principled BSDF"].inputs[ 0].default_value = rgba
def get_random_color(): global random_hsvs, random_index h, s, v = random_hsvs[random_index] random_index = (random_index + 1) % len(random_hsvs) h = (h + (random.random() * 0.1 - 0.05)) % 1 s = (s - (random.random() * 0.1)) v = (v - (random.random() * 0.1)) c = Color((0, 0, 0)) c.hsv = (h, s, v) return c
def getRandomColor(seed=None, hue=None, saturation=None, value=None): if seed is None: random.seed() else: random.seed(seed) if hue is None: hue = random.random() if saturation is None: saturation = random.random() if value is None: value = random.random() color = Color() color.hsv = hue, saturation, value return color
def getRandomColor(seed = None, hue = None, saturation = None, value = None): if seed is None: random.seed() else: random.seed(seed) if hue is None: hue = random.random() if saturation is None: saturation = random.random() if value is None: value = random.random() color = Color() color.hsv = hue, saturation, value return color
def set_param_value_from_json(node, input_id, input_data): input = find_socket_by_id(node.inputs, input_id) if input.bl_idname == "NodeSocketVectorEuler": input.default_value = input_data[:3] input.default_value.order = input_data[3] elif input.bl_idname == "NodeSocketColor": c = Color() c.hsv = input_data input.default_value = [*c[:], 1.0] else: input.default_value = input_data
def randomly_change_color(material_to_change): """ Changes the Principled BSDF color of a material to a random color """ color = Color() hue = random.random() * .2 # random between 0 and .2 saturation = random.random() * .6 + .2 # random between .2 and .8 color.hsv = (hue, saturation, 1) rgba = [color.r, color.g, color.b, 1] material_to_change.node_tree.nodes["Principled BSDF"].inputs[ 0].default_value = rgba
def convert(value, method): if method == 'BW2W': return (value.r + value.g + value.b) / 3 elif method == 'W2BW': col = Color((value, value, value)) return col elif method == 'HSV2W': return 1 - (value.h / 0.66) elif method == 'W2HSV': col = Color() col.hsv = (0.66 * (1 - value), 1, 1) return col
def convert(value, method): if method == 'BW2W': return (value.r + value.g+ value.b)/3 elif method == 'W2BW': col = Color((value, value, value)) return col elif method == 'HSV2W': return 1-(value.h / 0.66) elif method == 'W2HSV': col = Color() col.hsv = (0.66*(1-value), 1, 1) return col
def hsv_to_rgb(input, val, i_sub=-1): c = Color(input.default_value[:3]) if i_sub == 0: c.h = val elif i_sub == 1: c.s = val elif i_sub == 2: c.v = val elif i_sub < 0: c.hsv = val return [*c[:], 1.0] # Return RGB values
def __draw_analog(region, props, prefs): start_x = prefs.ana_position[0] end_x = prefs.ana_position[0] + 400.0 * prefs.digi_scale_x base_y = prefs.ana_position[1] long_len_y1 = 3.0 long_len_y2 = 10.0 middle_len = 6.0 short_len = 3.0 min_temp = -10 max_temp = 50 temp_range = max_temp - min_temp interval = (end_x - start_x) / temp_range blf.size(0, 12, 72) color = Color() color.hsv = ( 1.0 - (((props.temperature-min_temp)/(temp_range-10))*270+90)/360, 0.8, 1.0 ) bgl_draw_rect( start_x, region.height - base_y, start_x + interval * (props.temperature - min_temp), region.height - base_y + long_len_y2, (color[0], color[1], color[2], 0.6) ) bgl_draw_line(start_x, region.height - base_y, end_x, region.height - base_y) for t in range(min_temp, max_temp + 1): x1 = start_x + interval * (t - min_temp) x2 = x1 if t % 10 == 0: y1 = region.height - base_y - long_len_y1 y2 = region.height - base_y + long_len_y2 bgl_draw_line(x1, y1, x2, y2) blf.position(0, x1 - 2.0, y1 - 15.0, 0) blf.draw(0, "{0}".format(t)) elif t % 5 == 0: y1 = region.height - base_y y2 = region.height - base_y + middle_len bgl_draw_line(x1, y1, x2, y2) else: y1 = region.height - base_y y2 = region.height - base_y + short_len bgl_draw_line(x1, y1, x2, y2)
def prepare_objects_for_bake_matid(objs): def create_temp_nodes(mat, color): pb_output_node = new_pb_output_node(mat) pb_emission_node = new_pb_emission_node(mat, color) # activate temp output material_output = get_active_output(mat) # orig mat output if material_output: material_output.is_active_output = False pb_output_node.is_active_output = True # link pb_emission_node to material_output mat.node_tree.links.new(pb_emission_node.outputs[0], pb_output_node.inputs['Surface']) if not isinstance(objs, list): objects = [objects] materials = [] for obj in objs: for mat_slot in obj.material_slots: if mat_slot.material: if mat_slot.material not in materials: materials.append(mat_slot.material) prefs = bpy.context.preferences.addons[PB_PACKAGE].preferences if prefs.mat_id_algorithm == 'HUE': from mathutils import Color n_materials = len(materials) s = prefs.mat_id_saturation v = prefs.mat_id_value for mat_index, mat in enumerate(materials): h = mat_index / n_materials c = Color([0, 0, 0]) c.hsv = h, s, v color = c.r, c.g, c.b, 1.0 create_temp_nodes(mat, color) elif prefs.mat_id_algorithm == 'NAME': from hashlib import sha1 for mat in materials: s = mat.name.encode('utf-8') h = int(sha1(s).hexdigest(), base=16) r = h % 256 / 256 g = (h >> 32) % 256 / 256 b = (h >> 16) % 256 / 256 color = r, g, b, 1.0 create_temp_nodes(mat, color)
def set_random_lighting(self): if 'Environment Texture' in bpy.context.scene.world.node_tree.nodes.keys(): bpy.context.scene.world.node_tree.nodes.remove(bpy.context.scene.world.node_tree.nodes['Environment Texture']) env = bpy.context.scene.world.node_tree.nodes.new("ShaderNodeTexEnvironment") env.interpolation = 'Cubic' env.image = bpy.data.images[random.randint(0, len(bpy.data.images) - 1)] # env.image = bpy.data.images["Render Result"] bpy.context.scene.world.node_tree.links.new( bpy.context.scene.world.node_tree.nodes['Environment Texture'].outputs['Color'], bpy.context.scene.world.node_tree.nodes['Background'].inputs['Color'] ) bpy.context.scene.world.node_tree.nodes['Background'].inputs[1].default_value = random.uniform(0.001, 0.2) c = Color() c.hsv = 0.0, 0.0, random.uniform(0.3, 0.7) self.model.material_slots[0].material.node_tree.nodes["Principled BSDF"].inputs[0].default_value = c.r, c.g, c.b, 1.0
def set_sun_angle (azimuth, altitude): ''' Args: altitude: angle from surface, in degrees azimuth: angle from the north, in degrees. On the east azimuth equals +90 ''' # note: azimuth lamp direction is the opposite to sun position yaw = - (azimuth - 90) * pi / 180 pitch = (90 - altitude) * pi / 180 # set orientation sun = bpy.data.objects['-Sun'] sun.rotation_euler = Euler((0, pitch, yaw), 'ZXY') # two opposite colors -- noon and sunset c_noon = np.asarray([0.125, 0.151, 1]) c_sunset = np.asarray([0, 0.274, 1]) # get the mix between them according to the time of the day k = pitch / (pi/2) # [0, 1], 0 -- noon, 1 - sunset c = Color() c.hsv = tuple(c_noon * (1 - k) + c_sunset * k) print ('set_sun_angle: pitch=%f, k=%f, c=(%.3f, %.3f, %.3f)' % (pitch, k, c[0], c[1], c[2])) sun.data.color = c
def falloff_material_HSV(h, s=0.9, v=0.9): mat = bpy.data.materials.new('FalloffMaterial') diffuse_color = Color() diffuse_color.hsv = ((h % 1.0), s, v) # Diffuse mat.diffuse_shader = 'LAMBERT' mat.use_diffuse_ramp = True mat.diffuse_ramp_input = 'NORMAL' mat.diffuse_ramp_blend = 'ADD' mat.diffuse_ramp.elements[0].color = (1, 1, 1, 1) mat.diffuse_ramp.elements[1].color = (1, 1, 1, 0) mat.diffuse_color = diffuse_color mat.diffuse_intensity = 1.0 # Specular mat.specular_intensity = 0.0 # Shading mat.emit = 0.05 mat.translucency = 0.2 return mat
def hsv_to_rgb(hsv): c = Color((0, 0, 0)) c.hsv = hsv return c
def update_value_hsv(self, context): col = Color() col.hsv = self.value_hsv self["value"] = srgb_to_linear(col) utils_node.force_viewport_update(self, context)
def update_value_hsv(self, context): col = Color() col.hsv = self.value_hsv self["value"] = srgb_to_linear(col)
def background_color_HSV(horizon_color=(0.0, 0.0, 0.051), zenith_color=(0, 0, 0.01), ambient_color=(0,0,0), paper_sky=True, blend_sky=False, real_sky=False): h, z, a = Color(), Color(), Color() h.hsv = horizon_color z.hsv = zenith_color a.hsv = ambient_color background(h, z, a, paper_sky, blend_sky, real_sky)
ob_new.data = copyobj.data.copy() ob_new.scale = copyobj.scale ob_new.location = copyobj.location # Link new object to the given scene and select it scene.objects.link(ob_new) ob_new.select = True return ob_new torus = bpy.data.objects['Torus'] scene = bpy.data.scenes['Scene'] c = Color() numtor = 50 for i in range(1,numtor+1): name = 'Torus' + str(i) newtorus = duplicateObject(scene, name, torus) newtorus.location = Vector((0, 0, -20*i)) matname = 'TorMat' + str(i) newmat = bpy.data.materials.new(matname) c.hsv = float(i-1) / float(numtor), 1.0, 0.735 newmat.diffuse_color = c.r, c.g, c.b newmat.emit = 1. newtorus.active_material = newmat newconstraint = newtorus.constraints.new('CHILD_OF') newconstraint.target = torus newconstraint.use_scale_x = False newconstraint.use_scale_y = False newconstraint.use_scale_z = False
# Link new object to the given scene and select it scene.objects.link(ob_new) ob_new.select = True return ob_new # delete all pre-existing orbs besides the original for obj in bpy.context.scene.objects: obj.select = ( obj.name[:3] == "Orb" or obj.name[:5] == "Glass") and ( obj.name != "Orb" and obj.name != "Glass") bpy.ops.object.delete() orb = bpy.data.objects['Orb'] glass = bpy.data.objects['Glass'] scene = bpy.data.scenes['Scene'] c = Color() numorb = 150 random.seed(0) for i in range(1, numorb+1): name = 'Orb' + str(i) glassname = 'Glass' + str(i) neworb = duplicateObject(scene, name, orb) neworb.location = Vector((-20+40*random.random(), -20+40*random.random(), 20*random.random())) newglass = duplicateObject(scene, glassname, glass) newglass.location = neworb.location matname = 'OrbMat' + str(i) newmat = bpy.data.materials.new(matname) c.hsv = random.random(), 1.0, 0.735 newmat.diffuse_color = c.r, c.g, c.b newmat.emit = 0. neworb.active_material = newmat
def execute(self, context): coll = bpy.context.view_layer.active_layer_collection.collection total_polygons = 0 results = {} for obj in coll.objects: if hasattr(obj.data, 'polygons') and obj.hide_get() is False: if 'Decimate' in obj.modifiers: results[obj.name] = obj.modifiers["Decimate"].face_count total_polygons += obj.modifiers["Decimate"].face_count else: results[obj.name] = len(obj.data.polygons) total_polygons += len(obj.data.polygons) sorted_results = sorted(results.items(), key=operator.itemgetter(1), reverse=True) if sorted_results == []: print('No objects with polygons') return {'FINISHED'} top_result = sorted_results[0][1] last_result = sorted_results[-1][1] if top_result == last_result: self.report({'INFO'}, "All objects have the same number of polygons") return {'FINISHED'} for result in sorted_results: obj = bpy.context.scene.objects[result[0]] if obj.active_material is None: obj.original_material.add().add_material(None) elif "PKHG" not in obj.active_material.name: obj.original_material.clear() if (len(obj.material_slots) > 1): for index, material_slot in enumerate(obj.material_slots): original_material = obj.original_material.add() original_material.add_material(material_slot.material) for face in obj.data.polygons: if (face.material_index == index): original_material.add_face(face.index) else: obj.original_material.add().add_material( obj.material_slots[0].material) obj.data.materials.clear() else: active_material = obj.active_material obj.data.materials.clear() bpy.data.materials.remove(active_material) c = Color() c.hsv = (0.0, (result[1] - last_result) / (top_result - last_result), 1.0) mat = bpy.data.materials.new("PKHG") mat.diffuse_color = (c.r, c.g, c.b, 1.0) obj.active_material = mat bpy.ops.object.select_all(action='DESELECT') bpy.data.objects[sorted_results[0][0]].select_set(True) bpy.ops.view3d.view_selected() return {'FINISHED'}
def update_value_hsv(self, context): col = Color() col.hsv = self.value_hsv self["value"] = col
def get_random_color(self): color = Color() # set value and saturation to 1 - provides best overlay visibility color.hsv = (random.random(), 1, 1) return color
def get_color_id(index, count): # Get unique color color = Color() color.hsv = (index / (count)), 0.9, 1.0 return color