# ============================================================================
# CC0 "No Rights Reserved"
# To the extent possible under law, the author(s) have dedicated all copyright
# and related and neighboring rights to this software to the public domain
# worldwide. See http://creativecommons.org/publicdomain/zero/1.0/.
# ============================================================================

# Contracts an origin-centered cube to a sphere.
# Select nodes before using.


import math

for node_id in mw.selected_nodes():
    coord = mw.node(node_id)
    r = coord.x
    s = coord.y
    t = coord.z

    scale = max(abs(r), abs(s), abs(t))

    r /= scale
    s /= scale
    t /= scale

    R = math.sqrt(r ** 2 + s ** 2 + t ** 2)

    mw.set_node_x(node_id, scale * r / R)
    mw.set_node_y(node_id, scale * s / R)
    mw.set_node_z(node_id, scale * t / R)
