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_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
Beispiel #4
0
def build_default_node(inp: bpy.types.NodeSocket):
    """Creates a new node to give a not connected input socket a value"""
    null_node = 'new armory.logicnode.NullNode(this)'

    if isinstance(inp, arm.logicnode.arm_nodes.ArmCustomSocket):
        # ArmCustomSockets need to implement get_default_value()
        default_value = inp.get_default_value()
        if default_value is None:
            return null_node
        if isinstance(default_value, str):
            default_value = f'"{default_value}"'

        return f'new armory.logicnode.DynamicNode(this, {default_value})'

    if inp.bl_idname == 'ArmNodeSocketAction' or inp.bl_idname == 'ArmNodeSocketArray':
        return null_node
    if inp.bl_idname == 'ArmNodeSocketObject':
        return f'new armory.logicnode.ObjectNode(this, "{inp.get_default_value()}")'
    if inp.bl_idname == 'ArmNodeSocketAnimAction':
        # Backslashes are not allowed in f-strings so we need this variable
        default_value = inp.get_default_value().replace("\"", "\\\"")
        return f'new armory.logicnode.StringNode(this, "{default_value}")'
    if inp.type == 'VECTOR':
        return f'new armory.logicnode.VectorNode(this, {inp.default_value[0]}, {inp.default_value[1]}, {inp.default_value[2]})'
    elif inp.type == 'RGBA':
        return f'new armory.logicnode.ColorNode(this, {inp.default_value[0]}, {inp.default_value[1]}, {inp.default_value[2]}, {inp.default_value[3]})'
    elif inp.type == 'RGB':
        return f'new armory.logicnode.ColorNode(this, {inp.default_value[0]}, {inp.default_value[1]}, {inp.default_value[2]})'
    elif inp.type == 'VALUE':
        return f'new armory.logicnode.FloatNode(this, {inp.default_value})'
    elif inp.type == 'INT':
        return f'new armory.logicnode.IntegerNode(this, {inp.default_value})'
    elif inp.type == 'BOOLEAN':
        return f'new armory.logicnode.BooleanNode(this, {str(inp.default_value).lower()})'
    elif inp.type == 'STRING':
        default_value = inp.default_value.replace("\"", "\\\"")
        return f'new armory.logicnode.StringNode(this, "{default_value}")'

    return null_node
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
Beispiel #6
0
def build_default_node(inp: bpy.types.NodeSocket):
    """Creates a new node to give a not connected input socket a value"""

    is_custom_socket = isinstance(inp,
                                  arm.logicnode.arm_sockets.ArmCustomSocket)

    if is_custom_socket:
        # ArmCustomSockets need to implement get_default_value()
        default_value = inp.get_default_value()
        if isinstance(default_value, str):
            default_value = '"{:s}"'.format(default_value.replace('"', '\\"'))
        inp_type = inp.arm_socket_type  # any custom socket's `type` is "VALUE". might as well have valuable type information for custom nodes as well.
    else:
        if hasattr(inp, 'default_value'):
            default_value = inp.default_value
        else:
            default_value = None
        if isinstance(default_value, str):
            default_value = '"{:s}"'.format(default_value.replace('"', '\\"'))
        inp_type = inp.type

    # Don't write 'None' into the Haxe code
    if default_value is None:
        default_value = 'null'

    if inp_type == 'VECTOR':
        return f'new armory.logicnode.VectorNode(this, {default_value[0]}, {default_value[1]}, {default_value[2]})'
    elif inp_type == 'RGBA':
        return f'new armory.logicnode.ColorNode(this, {default_value[0]}, {default_value[1]}, {default_value[2]}, {default_value[3]})'
    elif inp_type == 'RGB':
        return f'new armory.logicnode.ColorNode(this, {default_value[0]}, {default_value[1]}, {default_value[2]})'
    elif inp_type == 'VALUE':
        return f'new armory.logicnode.FloatNode(this, {default_value})'
    elif inp_type == 'INT':
        return f'new armory.logicnode.IntegerNode(this, {default_value})'
    elif inp_type == 'BOOLEAN':
        return f'new armory.logicnode.BooleanNode(this, {str(default_value).lower()})'
    elif inp_type == 'STRING':
        return f'new armory.logicnode.StringNode(this, {default_value})'
    elif inp_type == 'NONE':
        return 'new armory.logicnode.NullNode(this)'
    elif inp_type == 'OBJECT':
        return f'new armory.logicnode.ObjectNode(this, {default_value})'
    elif is_custom_socket:
        return f'new armory.logicnode.DynamicNode(this, {default_value})'
    else:
        return 'new armory.logicnode.NullNode(this)'
Beispiel #7
0
def build_default_node(inp: bpy.types.NodeSocket):
    """Creates a new node to give a not connected input socket a value"""
    is_custom_socket = isinstance(inp,
                                  arm.logicnode.arm_sockets.ArmCustomSocket)

    if is_custom_socket:
        # ArmCustomSockets need to implement get_default_value()
        default_value = inp.get_default_value()
    else:
        if hasattr(inp, 'default_value'):
            default_value = inp.default_value
        else:
            default_value = None

    default_value = arm.node_utils.haxe_format_socket_val(
        default_value, array_outer_brackets=False)

    inp_type = arm.node_utils.get_socket_type(inp)

    if inp_type == 'VECTOR':
        return f'new armory.logicnode.VectorNode(this, {default_value})'
    elif inp_type == 'ROTATION':  # a rotation is internally represented as a quaternion.
        return f'new armory.logicnode.RotationNode(this, {default_value})'
    elif inp_type in ('RGB', 'RGBA'):
        return f'new armory.logicnode.ColorNode(this, {default_value})'
    elif inp_type == 'VALUE':
        return f'new armory.logicnode.FloatNode(this, {default_value})'
    elif inp_type == 'INT':
        return f'new armory.logicnode.IntegerNode(this, {default_value})'
    elif inp_type == 'BOOLEAN':
        return f'new armory.logicnode.BooleanNode(this, {default_value})'
    elif inp_type == 'STRING':
        return f'new armory.logicnode.StringNode(this, {default_value})'
    elif inp_type == 'NONE':
        return 'new armory.logicnode.NullNode(this)'
    elif inp_type == 'OBJECT':
        return f'new armory.logicnode.ObjectNode(this, {default_value})'
    elif is_custom_socket:
        return f'new armory.logicnode.DynamicNode(this, {default_value})'
    else:
        return 'new armory.logicnode.NullNode(this)'