예제 #1
0
def join_macros(context, operator, term, nodes, links):
    '''Macro that places a list join nodes and connects the selected nodes to them'''

    selected_nodes = context.selected_nodes

    if not selected_nodes:
        operator.report({"ERROR_INVALID_INPUT"}, 'No selected nodes to join')
        return
    tree = nodes[0].id_data
    previous_state = tree.sv_process
    tree.sv_process = False
    # get bounding box of all selected nodes
    _, maxx, _, maxy = nodes_bounding_box(selected_nodes)

    # find out which sockets to connect
    socket_numbers = term.replace("join", "")
    if len(socket_numbers) == 1:  # one socket
        socket_indices = [int(socket_numbers) - 1]
    else:  # multiple sockets
        socket_indices = [int(n) - 1 for n in socket_numbers]

    # Create List Join nodes
    join_nodes = []
    for i, s in enumerate(socket_indices):
        join_nodes.append(nodes.new('ListJoinNode'))
        join_nodes[i].location = maxx + 100, maxy - (
            180 + (22 * (len(selected_nodes)))) * i

    sorted_nodes = sorted(selected_nodes,
                          key=lambda n: n.location.y,
                          reverse=True)

    # link the nodes to ListJoin nodes
    for i, node in enumerate(sorted_nodes):
        for j, n in enumerate(socket_indices):
            if len(node.outputs) > n:
                links.new(node.outputs[n], join_nodes[j].inputs[i])

    if all(node.outputs[0].bl_idname == "SvVerticesSocket"
           for node in sorted_nodes):
        viewer_node = nodes.new("SvVDExperimental")
        viewer_node.location = join_nodes[0].location.x + join_nodes[
            0].width + 100, maxy

        # link the output switch node to the SvVDExperimental node
        links.new(join_nodes[0].outputs[0], viewer_node.inputs[0])
        if len(socket_indices) > 1:
            links.new(join_nodes[1].outputs[0],
                      viewer_node.inputs[socket_indices[1]])
        if len(socket_indices) > 2:
            links.new(join_nodes[2].outputs[0],
                      viewer_node.inputs[socket_indices[2]])

    tree.sv_process = previous_state
    tree.update()
    operator.report({'INFO'}, 'Nodes Joined')
예제 #2
0
def switch_macros(context, operator, term, nodes, links):
    '''Macro that places a switch node and connects the selected nodes to it'''

    selected_nodes = context.selected_nodes

    _, maxx, _, maxy = nodes_bounding_box(selected_nodes)

    switch_node = nodes.new('SvInputSwitchNodeMOD')
    switch_node.location = maxx + 100, maxy

    # find out which sockets to connect
    socket_numbers = term.replace("switch", "")

    if len(socket_numbers) == 1:
        socket_indices = [0]
    else:
        socket_indices = [int(n) - 1 for n in socket_numbers]

    switch_node.num_sockets_per_set = len(socket_indices)
    sorted_nodes = sorted(selected_nodes,
                          key=lambda n: n.location.y,
                          reverse=True)

    # link the nodes to InputSwitch node
    get_indices_for_groupnum = switch_node.get_local_function(
        "get_indices_for_groupnum")

    for i, node in enumerate(sorted_nodes):
        destination_indices = get_indices_for_groupnum(switch_node.node_state,
                                                       i)
        for j, n in enumerate(socket_indices):
            remapped_index = destination_indices[j]
            links.new(node.outputs[n], switch_node.inputs[remapped_index])

    if all(node.outputs[0].bl_idname == "SvVerticesSocket"
           for node in sorted_nodes):
        viewer_node = nodes.new("SvViewerDrawMk4")
        viewer_node.location = switch_node.location.x + switch_node.width + 100, maxy

        # link the input switch node to the ViewerDraw node
        for n, i in enumerate(socket_indices):
            links.new(switch_node.outputs[n], viewer_node.inputs[i])

        switch_node.process_node(context)
예제 #3
0
def math_macros(context, operator, term, nodes, links):
    '''Macro that places a math node and connects the selected nodes to it'''

    selected_nodes = context.selected_nodes
    if not selected_nodes:
        operator.report({"ERROR_INVALID_INPUT"}, 'No selected nodes to use')
        return

    selected_nodes = selected_nodes[:2]
    if any(len(node.outputs) < 1 for node in selected_nodes):
        operator.report({"ERROR_INVALID_INPUT"}, 'No valid nodes to use')
        return
    tree = nodes[0].id_data
    previous_state = tree.sv_process
    tree.sv_process = False
    # get bounding box of all selected nodes
    minx, maxx, miny, maxy = nodes_bounding_box(selected_nodes)
    # find out which sockets to connect
    operator = term.replace("math", "")
    sorted_nodes = sorted(selected_nodes,
                          key=lambda n: n.location.y,
                          reverse=True)

    is_vector = all(node.outputs[0].bl_idname == "SvVerticesSocket"
                    for node in sorted_nodes)

    if operator == 'MUL':
        if is_vector:
            math_node = nodes.new('SvVectorMathNodeMK3')
            math_node.current_op = 'CROSS'
        else:

            if (sorted_nodes[0].outputs[0].bl_idname == "SvVerticesSocket"):
                math_node = nodes.new('SvVectorMathNodeMK3')
                math_node.current_op = 'SCALAR'

            elif len(sorted_nodes) > 1 and (
                    sorted_nodes[1].outputs[0].bl_idname
                    == "SvVerticesSocket"):
                math_node = nodes.new('SvVectorMathNodeMK3')
                math_node.current_op = 'SCALAR'
                sorted_nodes = [sorted_nodes[1], sorted_nodes[0]]

            else:
                math_node = nodes.new('SvScalarMathNodeMK4')
                math_node.current_op = operator
    else:
        if is_vector:
            math_node = nodes.new('SvVectorMathNodeMK3')
            math_node.current_op = operator
        else:
            math_node = nodes.new('SvScalarMathNodeMK4')
            math_node.current_op = operator

    math_node.location = maxx + 100, maxy
    # link the nodes to Math node
    for i, node in enumerate(sorted_nodes):
        links.new(node.outputs[0], math_node.inputs[i])

    if is_vector:
        viewer_node = nodes.new("SvVDExperimental")
        viewer_node.location = math_node.location.x + math_node.width + 100, maxy

        # link the output math node to the ViewerDraw node
        links.new(math_node.outputs[0], viewer_node.inputs[0])

    tree.sv_process = previous_state
    tree.update()