我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用mathutils.Color()。
def __init__(self): self.SPOT = int() self.SUN = int() self.NORMAL = int() self.HEMI = int() self.type = None self.energy = float() self.shadowClipStart = float() self.shadowClipEnd = float() self.shadowFrustumSize = float() self.shadowBindId = int() self.shadowMapType = int() self.shadowBias = float() self.shadowBleedBias = float() self.useShadow = bool() self.shadowColor = mathutils.Color() self.shadowMatrix = mathutils.Matrix() self.distance = float() self.color = [0,0,0] self.lin_attenuation = float() self.quad_attenuation = float() self.spotsize = float() self.spotblend = float() self.staticShadow = bool()
def __init__(self, op='', data=0.0, inputs=()): if op: self.type = 'op' if type(op) == Color: print(end='') self.op = op self.value = data self.inputs = inputs if op == 'mixC' and inputs[1] == None: pass else: self.type = 'val' self.value = data self.exported = None # exported blender node
def __init__(self): self.finalcolorc = Holder(Node(data=Color())) self.finalcolora = Holder(Node(data=0.0)) self.vertexcolorc = Holder(Node(data=Color())) self.vertexcolora = Holder(Node(data=0.0)) self.reg1c = Holder(Node(data=Color())) self.reg1a = Holder(Node(data=0.0)) self.reg2c = Holder(Node(data=Color())) self.reg2a = Holder(Node(data=0.0)) self.reg3c = Holder(Node(data=Color())) self.reg3a = Holder(Node(data=0.0)) self.texcoords = [] self.vcolorindex = 0 self.textures = [None]*8 self.konsts = [None]*8 self.flag = 0 # scenegraph use # those functions are used for node computation
def __init__(self): # Index of the vertex in the Blender buffer self.blenderIndex = None # Position of the vertex: Vector((0.0, 0.0, 0.0)) self.pos = None # Normal of the vertex: Vector((0.0, 0.0, 0.0)) self.normal = None # Color of the vertex: (0, 0, 0, 0)...(255, 255, 255, 255) self.color = None # UV coordinates of the vertex: Vector((0.0, 0.0))..Vector((1.0, 1.0)) self.uv = None # UV2 coordinates of the vertex: Vector((0.0, 0.0))..Vector((1.0, 1.0)) self.uv2 = None # Tangent of the vertex: Vector((0.0, 0.0, 0.0, 0.0)) self.tangent = None # Bitangent of the vertex: Vector((0.0, 0.0, 0.0)) self.bitangent = None # Bones weights: list of tuple(boneIndex, weight) self.weights = None # returns True is this vertex is a changed morph of vertex 'other'
def light(self, en, scene, name): """ en: dxf entity name: ignored; exists to make separate and merged objects methods universally callable from _call_types() Creates, links and returns a new light object depending on the type and color of the dxf entity. """ # light_type : distant = 1; point = 2; spot = 3 if self.import_light: type_map = ["NONE", "SUN", "POINT", "SPOT"] layer = self.dwg.layers[en.layer] lamp = bpy.data.lamps.new(en.name, type_map[en.light_type]) if en.color != 256: aci = en.color else: aci = layer.color c = dxfgrabber.aci_to_true_color(aci) lamp.color = Color(c.rgb()) if en.light_type == 3: lamp.spot_size = en.hotspot_angle o = bpy.data.objects.new(en.name, lamp) o.location = self.proj(en.position) dir = self.proj(en.target) - self.proj(en.position) o.rotation_quaternion = dir.rotation_difference(Vector((0, 0, -1))) scene.objects.link(o) return o
def random_id(length=8): """ Generates a random alphanumeric id string. """ tlength = int(length / 2) rlength = int(length / 2) + int(length % 2) chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] text = "" for i in range(0, rlength): text += random.choice(chars) text += str(hex(int(time.time())))[2:][-tlength:].rjust(tlength, '0')[::-1] return text #============================================= # Color correction functions #=============================================
def DrawLeftText(text, xt, yt, Size, Color, self): font_id = 0 # Offset Shadow Sshadow_x = 2 Sshadow_y = -2 blf.size(font_id, Size, 72) blf.position(font_id, xt + Sshadow_x, yt + Sshadow_y, 0) bgl.glColor4f(0.0, 0.0, 0.0, 1.0) blf.draw(font_id, text) blf.position(font_id, xt, yt, 0) if Color is not None: mColor = mathutils.Color((Color[0], Color[1], Color[2])) bgl.glColor4f(mColor.r, mColor.g, mColor.b, 1.0) else: bgl.glColor4f(1.0, 1.0, 1.0, 1.0) blf.draw(font_id, text) # Draw text (Right position)
def DrawRightText(text, xt, yt, Size, Color, self): font_id = 0 # Offset Shadow Sshadow_x = 2 Sshadow_y = -2 blf.size(font_id, Size, 72) blf.position(font_id, xt + Sshadow_x - blf.dimensions(font_id, text)[0], yt + Sshadow_y, 0) bgl.glColor4f(0.0, 0.0, 0.0, 1.0) blf.draw(font_id, text) blf.position(font_id, xt - blf.dimensions(font_id, text)[0], yt, 0) if Color is not None: mColor = mathutils.Color((Color[0], Color[1], Color[2])) bgl.glColor4f(mColor.r, mColor.g, mColor.b, 1.0) else: bgl.glColor4f(1.0, 1.0, 1.0, 1.0) blf.draw(font_id, text) # Opengl draws
def update_color_1(self, context): for world in bpy.data.worlds: if world.node_tree is not None: for node in world.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 1"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color1 update_caller(self, input_name="Color 1") for mat in bpy.data.materials: if mat.node_tree is not None: for node in mat.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 1"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color1 update_caller(self, input_name="Color 1") for lamps in bpy.data.lamps: if lamps.node_tree is not None: for node in lamps.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 1"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color1 update_caller(self, input_name="Color 1") return None
def update_color_2(self, context): for world in bpy.data.worlds: if world.node_tree is not None: for node in world.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 2"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color2 update_caller(self, input_name="Color 2") for mat in bpy.data.materials: if mat.node_tree is not None: for node in mat.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 2"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color2 update_caller(self, input_name="Color 2") for lamps in bpy.data.lamps: if lamps.node_tree is not None: for node in lamps.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 2"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color2 update_caller(self, input_name="Color 2") return None
def update_color_3(self, context): for world in bpy.data.worlds: if world.node_tree is not None: for node in world.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 3"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color3 update_caller(self, input_name="Color 3") for mat in bpy.data.materials: if mat.node_tree is not None: for node in mat.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 3"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color3 update_caller(self, input_name="Color 3") for lamps in bpy.data.lamps: if lamps.node_tree is not None: for node in lamps.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 3"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color3 update_caller(self, input_name="Color 3") return None
def update_color_4(self, context): for world in bpy.data.worlds: if world.node_tree is not None: for node in world.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 4"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color4 update_caller(self, input_name="Color 4") for mat in bpy.data.materials: if mat.node_tree is not None: for node in mat.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 4"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color4 update_caller(self, input_name="Color 4") for lamps in bpy.data.lamps: if lamps.node_tree is not None: for node in lamps.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 4"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color4 update_caller(self, input_name="Color 4") return None
def update_color_5(self, context): for world in bpy.data.worlds: if world.node_tree is not None: for node in world.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 5"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color5 update_caller(self, input_name="Color 5") for mat in bpy.data.materials: if mat.node_tree is not None: for node in mat.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 5"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color5 update_caller(self, input_name="Color 5") for lamps in bpy.data.lamps: if lamps.node_tree is not None: for node in lamps.node_tree.nodes: if node.bl_idname == 'spectrum_palette.node': node.outputs["Color 5"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color5 update_caller(self, input_name="Color 5") return None
def draw(self, context): settings = context.material.pbr_export_settings self.layout.label('Base Color:') box = self.layout.box() box.prop(settings, 'base_color_factor', text='Factor') box.prop_search(settings, 'base_color_texture', bpy.data, 'textures') self.layout.label('Roughness:') box = self.layout.box() box.prop(settings, 'metallic_factor', text='Metallic') box.prop(settings, 'roughness_factor', text='Factor') box.prop_search(settings, 'metal_roughness_texture', bpy.data, 'textures') self.layout.label('Emissive:') box = self.layout.box() box.prop(settings, 'emissive_factor', text='Factor') box.prop_search(settings, 'emissive_texture', bpy.data, 'textures') self.layout.prop_search(settings, 'normal_texture', bpy.data, 'textures') self.layout.prop_search(settings, 'occlusion_texture', bpy.data, 'textures')
def shoot(self): obj = self.owner vec = mathutils.Vector((0, 1, 0)) sender = obj previous = obj.worldPosition lastHitObject = obj while True: mirror, location, normal = sender.rayCast(sender.worldPosition + vec, None, 60) bge.render.drawLine(previous, location, mathutils.Color((255, 0, 0))) previous = location if mirror != None and mirror.name.find("Mirror") != -1: lastHitObject = mirror vec = vec.reflect(normal) print(mirror) sender = mirror else: #At this point mirror is the last object hit (not an actual mirror) print("Reached end") #print(mirror) break print("Last Hit Object:") print(lastHitObject)
def DrawCenterText(text, xt, yt, Size, Color, self): font_id = 0 # Offset Shadow Sshadow_x = 2 Sshadow_y = -2 blf.size(font_id, Size, 72) blf.position(font_id, xt + Sshadow_x - blf.dimensions(font_id, text)[0] / 2, yt + Sshadow_y, 0) bgl.glColor4f(0.0, 0.0, 0.0, 1.0) blf.draw(font_id, text) blf.position(font_id, xt - blf.dimensions(font_id, text)[0] / 2, yt, 0) if Color is not None: mColor = mathutils.Color((Color[0], Color[1], Color[2])) bgl.glColor4f(mColor.r, mColor.g, mColor.b, 1.0) else: bgl.glColor4f(1.0, 1.0, 1.0, 1.0) blf.draw(font_id, text) # Draw text (Left position)
def __init__(self): self.shader = BL_Shader() self.blending = (0,0) self.alpha = float() self.hardness = int() self.emit = float() self.ambient = float() self.specularAlpha = float() self.specularIntensity = float() self.diffuseIntensity = float() self.specularColor = mathutils.Color() self.diffuseColor = mathutils.Color() self.textures = BL_Texture()
def __init__(self): self.KX_MIST_QUADRATIC = int() self.KX_MIST_LINEAR = int() self.KX_MIST_INV_QUADRATIC = int() self.mistEnable = bool() self.mistStart = float() self.mistDistance = float() self.mistIntensity = float() self.mistType = None self.mistColor = mathutils.Color() self.horizonColor = mathutils.Color() self.zenithColor = mathutils.Color() self.ambientColor = mathutils.Color() self.exposure = float() self.range = float()
def __add__(self, other): if not isinstance(other, Node): other = Node(data=other) if other.type == 'val': if other.value in (0, Color((0,0,0)), Vector((0,0,0))): return self # useless addition if other is zero if self.type == 'val': if self.value in (0, Color((0,0,0)), Vector((0,0,0))): return other # useless addition if other is zero return Node('ADD', inputs=(self, other))
def __mul__(self, other): if not isinstance(other, Node): other = Node(data=other) if other.type == 'val': if other.value in (1, Color((1,1,1)), Vector((1,1,1))): return self # useless addition if other is zero if self.type == 'val': if self.value in (1, Color((1,1,1)), Vector((1,1,1))): return other # useless addition if other is zero return Node('MULTIPLY', inputs=(self, other))
def __sub__(self, other): if not isinstance(other, Node): other = Node(data=other) if other.type == 'val': if other.value in (0, Color((0,0,0)), Vector((0,0,0))): return self # useless addition if other is zero if self.type == 'val': if self.value in (0, Color((0,0,0)), Vector((0,0,0))): return other # useless addition if other is zero return Node('SUBTRACT', inputs=(self, other))
def getMods(self, dest, bias, scale, clamp, type): if bias == 0: ret = dest elif bias <= 2: if bias == 2: val = -0.5 else: val = 0.5 if type == 1: ret = Node(data=Color((val, val, val))) else: ret = Node(data=val) ret = dest + ret else: print("getMods(): unknown bias", bias) if type == 1: ret = Node(data=Color((0,0,0))) else: ret = Node(data=0) if scale == 0: pass elif scale == 1: ret *= (Node('triple', Node(data=2)), 2)[type] # auto select type elif scale == 2: ret *= (Node('triple', Node(data=4)), 4)[type] elif scale == 3: ret *= (Node('triple', Node(data=0.5)), 0.5)[type] else: print("getMods(): unknown scale", scale) if clamp: ret = Node('clamp', ret) return ret
def add_err_material(obj): mat = bpy.data.materials.new("ERROR MATERIAL") mat.diffuse_color = mathutils.Color((0,0,1)) mat.specular_color = mathutils.Color((1,1,1)) mat.diffuse_intensity = 1 add_material(obj, mat) return mat
def add_vcolor(mesh, representation, layerID): """copies vertex colors (layer `layerID`) from the representation to the blender mesh""" vx_layer = mesh.vertex_colors.new("v_color_"+str(layerID)) vx_layer_a = mesh.vertex_colors.new("v_color_alpha_"+str(layerID)) # alpimg = bpy.data.images.new(mesh.name+'_vcol_alpha_'+str(layerID), 256, 256) # XCX image method buggy -> disabled for num, com in enumerate(representation.loops): vx_layer.data[num].color = mathutils.Color(com.VColors[layerID][:3]) vx_layer_a.data[num].color = mathutils.Color(tuple(com.VColors[layerID][3])*3)
def object_colors_calc(rules, objects): from mathutils import Color rules_cb = [getattr(rule_test, rule.type) for rule in rules] rules_blend = [(1.0 - rule.factor, rule.factor) for rule in rules] rules_color = [Color(rule.color) for rule in rules] rules_cache = [{} for i in range(len(rules))] rules_inv = [rule.use_invert for rule in rules] for obj in objects: is_set = False obj_color = Color(obj.color[0:3]) for (rule, test_cb, color, blend, cache, use_invert) \ in zip(rules, rules_cb, rules_color, rules_blend, rules_cache, rules_inv): if test_cb(obj, rule, cache) is not use_invert: if is_set is False: obj_color = color else: # prevent mixing colors loosing saturation obj_color_s = obj_color.s obj_color = (obj_color * blend[0]) + (color * blend[1]) obj_color.s = (obj_color_s * blend[0]) + (color.s * blend[1]) is_set = True if is_set: obj.show_wire_color = True obj.color[0:3] = obj_color
def execute(self, context): obj = context.object armature = obj.data if not hasattr(armature, 'rigify_colors'): return {'FINISHED'} groups = ['Root', 'IK', 'Special', 'Tweak', 'FK', 'Extra'] for g in groups: if g in armature.rigify_colors.keys(): continue armature.rigify_colors.add() armature.rigify_colors[-1].name = g armature.rigify_colors[g].select = Color((0.3140000104904175, 0.7839999794960022, 1.0)) armature.rigify_colors[g].active = Color((0.5490000247955322, 1.0, 1.0)) armature.rigify_colors[g].standard_colors_lock = True if g == "Root": armature.rigify_colors[g].normal = Color((0.43529415130615234, 0.18431372940540314, 0.41568630933761597)) if g == "IK": armature.rigify_colors[g].normal = Color((0.6039215922355652, 0.0, 0.0)) if g== "Special": armature.rigify_colors[g].normal = Color((0.9568628072738647, 0.7882353663444519, 0.0470588281750679)) if g== "Tweak": armature.rigify_colors[g].normal = Color((0.03921568766236305, 0.21176472306251526, 0.5803921818733215)) if g== "FK": armature.rigify_colors[g].normal = Color((0.11764706671237946, 0.5686274766921997, 0.03529411926865578)) if g== "Extra": armature.rigify_colors[g].normal = Color((0.9686275124549866, 0.250980406999588, 0.0941176563501358)) return {'FINISHED'}
def gamma_correct(color): corrected_color = Color() for i, component in enumerate(color): corrected_color[i] = linsrgb_to_srgb(color[i]) return corrected_color
def init(self, context): self.outputs.new('NodeSocketColor', "Color 1") self.outputs.new('NodeSocketColor', "Color 2") self.outputs.new('NodeSocketColor', "Color 3") self.outputs.new('NodeSocketColor', "Color 4") self.outputs.new('NodeSocketColor', "Color 5") self.outputs["Color 1"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color1 self.outputs["Color 2"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color2 self.outputs["Color 3"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color3 self.outputs["Color 4"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color4 self.outputs["Color 5"].default_value = bpy.context.scene.kaleidoscope_spectrum_props.color5 self.width = 226
def execute(self, context): global before_shuffle_colors global shuffle_time kaleidoscope_spectrum_props = bpy.context.scene.kaleidoscope_spectrum_props col1 = Color() col2 = Color() col3 = Color() col4 = Color() col5 = Color() for i in range(1, 6): exec("col"+str(i)+".r = kaleidoscope_spectrum_props.color"+str(i)+"[0]") exec("col"+str(i)+".g = kaleidoscope_spectrum_props.color"+str(i)+"[1]") exec("col"+str(i)+".b = kaleidoscope_spectrum_props.color"+str(i)+"[2]") if shuffle_time == 1: before_shuffle_colors.clear() before_shuffle_colors.extend([(col1.r, col1.g, col1.b, 1.0), (col2.r, col2.g, col2.b, 1.0), (col3.r, col3.g, col3.b, 1.0), (col4.r, col4.g, col4.b, 1.0), (col5.r, col5.g, col5.b, 1.0)]) index = [1, 2, 3, 4, 5] random.shuffle(index) for i in range(1, 6): exec("kaleidoscope_spectrum_props.color"+str(index[i-1])+" = col"+str(i)+".r, col"+str(i)+".g, col"+str(i)+".b, 1.0") current_history() shuffle_time=shuffle_time+1 return{'FINISHED'}
def execute(self, context): global shuffle_time global before_shuffle_colors kaleidoscope_spectrum_props = bpy.context.scene.kaleidoscope_spectrum_props color1 = Color() color2 = Color() color3 = Color() color4 = Color() color5 = Color() for i in range(1, 6): exec("color"+str(i)+".r = kaleidoscope_spectrum_props.color"+str(i)+"[0]") exec("color"+str(i)+".g = kaleidoscope_spectrum_props.color"+str(i)+"[1]") exec("color"+str(i)+".b = kaleidoscope_spectrum_props.color"+str(i)+"[2]") if shuffle_time == 1: before_shuffle_colors.clear() before_shuffle_colors.extend([(color1.r, color1.g, color1.b, 1.0), (color2.r, color2.g, color2.b, 1.0), (color3.r, color3.g, color3.b, 1.0), (color4.r, color4.g, color4.b, 1.0), (color5.r, color5.g, color5.b, 1.0)]) for i in range(1, 6): exec("kaleidoscope_spectrum_props.color"+str(6-i)+"[0] = color"+str(i)+".r") exec("kaleidoscope_spectrum_props.color"+str(6-i)+"[1] = color"+str(i)+".g") exec("kaleidoscope_spectrum_props.color"+str(6-i)+"[2] = color"+str(i)+".b") current_history() shuffle_time = shuffle_time+1 return{'FINISHED'}
def highLight(self, frame): """Colour the nodes in the interface to reflect the output""" preferences = bpy.context.user_preferences.addons[__package__].preferences if preferences.use_node_color: hue, sat, val = self.resultLog[frame] self.bpyNode.use_custom_color = True c = mathutils.Color() c.hsv = hue, sat, val self.bpyNode.color = c self.bpyNode.keyframe_insert("color")
def highLight(self, frame): preferences = bpy.context.user_preferences.addons[__package__].preferences if preferences.use_node_color: if frame in self.resultLog: hue, sat, val = self.resultLog[frame] else: hue = 0.0 sat = 0.0 val = 1.0 self.bpyNode.use_custom_color = True c = mathutils.Color() c.hsv = hue, sat, val self.bpyNode.color = c self.bpyNode.keyframe_insert("color")
def set_emissive_factor(self, value): material = self.id_data material.emit = mathutils.Color(value).v * 2.0
def average_color(self, color, count): if count != 0: return Color(color / count).freeze() else: return Color(color).freeze()
def marble_color(x): clr = Color((0.0, 0.0, 0.0)) x = math.sqrt(x + 1.0) * .707 clr.g = green + .8 * x x = math.sqrt(x) clr.r = red + .6 * x clr.b = blue + .4 * x return clr[:]
def srgb_to_linear(inp): if type(inp) == float: return srgb_to_linear_per_element(inp) elif type(inp) == Color: c = inp.copy() for i in range(3): c[i] = srgb_to_linear_per_element(c[i]) return c
def linear_to_srgb(inp): if type(inp) == float: return linear_to_srgb_per_element(inp) elif type(inp) == Color: c = inp.copy() for i in range(3): c[i] = linear_to_srgb_per_element(c[i]) return c