def set_random_color(input: bpy.types.NodeSocket, umin: list, umax: list, i_sub=-1): """ Calculates a random color based on the HSV ranges supplied in umin and umax. The value of the input will be set to this color. If i_sub is supplied, only this index will be randomized, else all 3 channels will be randomized. Returns: A list with 1 or 3 new HSV values, based on whether i_sub was supplied or not. """ assert i_sub < 3, "i_sub can not be greater than 2 as it corresponds to an index in a color vector (and alpha is not supported)!" if i_sub < 0: val = [ misc.color_clamp(random.normalvariate(umin.x, umax.x)), random.uniform(umin.y, umax.y), random.uniform(umin.z, umax.z) ] elif i_sub in (1, 2): val = random.uniform(umin[i_sub], umax[i_sub]) else: val = misc.color_clamp(random.normalvariate( umin[i_sub], umax[i_sub])) # Sample the Hue from a normal distribution rgb = hsv_to_rgb(input, val, i_sub=i_sub) input.default_value = rgb return val
def set_random_vector(input: bpy.types.NodeSocket, umin: list, umax: list, i_sub=-1): """ Calculates a list of random values based on the ranges supplied in umin and umax. The value of the input will be set to this list. If i_sub is supplied, only this index will be randomized, else all 3 values will be randomized. Returns: A list with 1 or 3 random values, based on whether i_sub was supplied or not. """ assert i_sub < 3, "i_sub can not be greater than 2 as it corresponds to an index in a vector (and Blender only has 3D vectors)." if i_sub < 0: val = [ random.uniform(umin.x, umax.x), random.uniform(umin.y, umax.y), random.uniform(umin.z, umax.z) ] input.default_value = val else: val = random.uniform(umin[i_sub], umax[i_sub]) input.default_value[i_sub] = val return val
def set_socket_value_range(socket: bpy.types.NodeSocket, default_value: float = 0.0, min_value: float = 0.0, max_value: float = 1.0) -> None: assert socket.type == "VALUE" socket.default_value = default_value socket.min_value = min_value socket.max_value = max_value
def set_random_value_for_input(input: bpy.types.NodeSocket, i_sub=-1): """ Sets a new random value for an input based on its type. Arguments: input - A NodeSocket with property group 'user_props' Returns: The normalized label corresponding to the randomized value (or values if vector type) as a list """ assert input.user_props, "Input of type {} does not have property 'user_props'!".format( input.type) umin = input.user_props.user_min umax = input.user_props.user_max if input.type == "RGBA": val = set_random_color(input, umin, umax, i_sub=i_sub) elif input.type == "VECTOR": val = set_random_vector(input, umin, umax, i_sub=i_sub) elif input.type == "VALUE": val = random.uniform(umin, umax) input.default_value = val elif input.type == "INT": val = random.randint(umin, umax) input.default_value = val else: raise TypeError("Input of type {} not supported!".format(input.type)) normalized_lbl = [] umin = misc.list_(umin) umax = misc.list_(umax) val = misc.list_(val) if i_sub >= 0: normalized_lbl.append(misc.normalize(val[0], umin[i_sub], umax[i_sub])) else: for x in range(len(val)): normalized_lbl.append(misc.normalize(val[x], umin[x], umax[x])) return normalized_lbl