(
    delete objects

    global g_dist = 20.0
    global g_z = 10.0
    global g_size = 8.0
    global g_busy = false

    /* ---------- ヘルパー生成（D4まで拡張） ---------- */
    -- 第1列（固定）
    global g_A1 = point name:"A1" pos:[0, g_dist*4, g_z] size:g_size wirecolor:red cross:true
    global g_B1 = point name:"B1" pos:[0, g_dist*3, g_z] size:g_size wirecolor:blue cross:true
    global g_C1 = point name:"C1" pos:[0, g_dist*2, g_z] size:g_size wirecolor:green cross:true
    global g_D1 = point name:"D1" pos:[0, g_dist, g_z] size:g_size wirecolor:yellow cross:true

    -- 第2列
    global g_A2 = point name:"A2" pos:[g_dist, g_dist*4, g_z] size:g_size wirecolor:orange cross:true
    global g_B2 = point name:"B2" pos:[g_dist, g_dist*3, g_z] size:g_size wirecolor:yellow cross:true
    global g_C2 = point name:"C2" pos:[g_dist, g_dist*2, g_z] size:g_size wirecolor:(color 0 255 255) cross:true
    global g_D2 = point name:"D2" pos:[g_dist, g_dist, g_z] size:g_size wirecolor:(color 255 0 255) cross:true

    -- 第3列
    global g_A3 = point name:"A3" pos:[g_dist*2, g_dist*4, g_z] size:g_size wirecolor:pink cross:true
    global g_B3 = point name:"B3" pos:[g_dist*2, g_dist*3, g_z] size:g_size wirecolor:lime cross:true
    global g_C3 = point name:"C3" pos:[g_dist*2, g_dist*2, g_z] size:g_size wirecolor:cyan cross:true
    global g_D3 = point name:"D3" pos:[g_dist*2, g_dist, g_z] size:g_size wirecolor:plum cross:true

    -- 第4列（新規追加）
    global g_A4 = point name:"A4" pos:[g_dist*3, g_dist*4, g_z] size:g_size wirecolor:(color 255 128 0) cross:true
    global g_B4 = point name:"B4" pos:[g_dist*3, g_dist*3, g_z] size:g_size wirecolor:(color 0 128 255) cross:true
    global g_C4 = point name:"C4" pos:[g_dist*3, g_dist*2, g_z] size:g_size wirecolor:(color 128 0 255) cross:true
    global g_D4 = point name:"D4" pos:[g_dist*3, g_dist, g_z] size:g_size wirecolor:(color 255 0 128) cross:true

    /* ---------- 親行固定 ---------- */
    for p in #(g_A1, g_B1, g_C1, g_D1) do p.isFrozen = true

    /* ---------- ユーティリティ関数 ---------- */
    fn safeNormalize v =
    (
        local L = length v
        if L < 1e-4 then [1,0,0] else v/L
    )

    /* ---------- 平行四辺形維持（Z軸固定） ---------- */
    fn maintainParallelogram mover =
    (
        if g_busy then return()
        g_busy = true

        try
        (
            local baseVec, parent, dirVec
            case mover of
            (
                g_A2: (parent = g_A1; baseVec = [mover.pos.x - parent.pos.x, mover.pos.y - parent.pos.y, 0])
                g_B2: (parent = g_B1; baseVec = [mover.pos.x - parent.pos.x, mover.pos.y - parent.pos.y, 0])
                g_C2: (parent = g_C1; baseVec = [mover.pos.x - parent.pos.x, mover.pos.y - parent.pos.y, 0])
                g_D2: (parent = g_D1; baseVec = [mover.pos.x - parent.pos.x, mover.pos.y - parent.pos.y, 0])
                g_A3: (parent = g_A2; baseVec = [mover.pos.x - parent.pos.x, mover.pos.y - parent.pos.y, 0])
                g_B3: (parent = g_B2; baseVec = [mover.pos.x - parent.pos.x, mover.pos.y - parent.pos.y, 0])
                g_C3: (parent = g_C2; baseVec = [mover.pos.x - parent.pos.x, mover.pos.y - parent.pos.y, 0])
                g_D3: (parent = g_D2; baseVec = [mover.pos.x - parent.pos.x, mover.pos.y - parent.pos.y, 0])
                g_A4: (parent = g_A3; baseVec = [mover.pos.x - parent.pos.x, mover.pos.y - parent.pos.y, 0])
                g_B4: (parent = g_B3; baseVec = [mover.pos.x - parent.pos.x, mover.pos.y - parent.pos.y, 0])
                g_C4: (parent = g_C3; baseVec = [mover.pos.x - parent.pos.x, mover.pos.y - parent.pos.y, 0])
                g_D4: (parent = g_D3; baseVec = [mover.pos.x - parent.pos.x, mover.pos.y - parent.pos.y, 0])
            )

            if parent != undefined do
            (
                dirVec = safeNormalize(baseVec) * g_dist

                -- 各ポイントに応じた処理
                case mover of
                (
                    g_A2:
                    (
                        g_A2.pos = [g_A1.pos.x + dirVec.x, g_A1.pos.y + dirVec.y, g_A2.pos.z]
                        g_B2.pos = [g_B1.pos.x + dirVec.x, g_B1.pos.y + dirVec.y, g_B2.pos.z]
                        g_C2.pos = [g_C1.pos.x + dirVec.x, g_C1.pos.y + dirVec.y, g_C2.pos.z]
                        g_D2.pos = [g_D1.pos.x + dirVec.x, g_D1.pos.y + dirVec.y, g_D2.pos.z]

                        g_A3.pos = [g_A2.pos.x + (g_A3.pos.x - g_A2.pos.x), g_A2.pos.y + (g_A3.pos.y - g_A2.pos.y), g_A3.pos.z]
                        g_B3.pos = [g_B2.pos.x + (g_B3.pos.x - g_B2.pos.x), g_B2.pos.y + (g_B3.pos.y - g_B2.pos.y), g_B3.pos.z]
                        g_C3.pos = [g_C2.pos.x + (g_C3.pos.x - g_C2.pos.x), g_C2.pos.y + (g_C3.pos.y - g_C2.pos.y), g_C3.pos.z]
                        g_D3.pos = [g_D2.pos.x + (g_D3.pos.x - g_D2.pos.x), g_D2.pos.y + (g_D3.pos.y - g_D2.pos.y), g_D3.pos.z]

                        g_A4.pos = [g_A3.pos.x + (g_A4.pos.x - g_A3.pos.x), g_A3.pos.y + (g_A4.pos.y - g_A3.pos.y), g_A4.pos.z]
                        g_B4.pos = [g_B3.pos.x + (g_B4.pos.x - g_B3.pos.x), g_B3.pos.y + (g_B4.pos.y - g_B3.pos.y), g_B4.pos.z]
                        g_C4.pos = [g_C3.pos.x + (g_C4.pos.x - g_C3.pos.x), g_C3.pos.y + (g_C4.pos.y - g_C3.pos.y), g_C4.pos.z]
                        g_D4.pos = [g_D3.pos.x + (g_D4.pos.x - g_D3.pos.x), g_D3.pos.y + (g_D4.pos.y - g_D3.pos.y), g_D4.pos.z]
                    )

                    g_B2:
                    (
                        g_A2.pos = [g_A1.pos.x + dirVec.x, g_A1.pos.y + dirVec.y, g_A2.pos.z]
                        g_B2.pos = [g_B1.pos.x + dirVec.x, g_B1.pos.y + dirVec.y, g_B2.pos.z]
                        g_C2.pos = [g_C1.pos.x + dirVec.x, g_C1.pos.y + dirVec.y, g_C2.pos.z]
                        g_D2.pos = [g_D1.pos.x + dirVec.x, g_D1.pos.y + dirVec.y, g_D2.pos.z]

                        g_A3.pos = [g_A2.pos.x + (g_A3.pos.x - g_A2.pos.x), g_A2.pos.y + (g_A3.pos.y - g_A2.pos.y), g_A3.pos.z]
                        g_B3.pos = [g_B2.pos.x + (g_B3.pos.x - g_B2.pos.x), g_B2.pos.y + (g_B3.pos.y - g_B2.pos.y), g_B3.pos.z]
                        g_C3.pos = [g_C2.pos.x + (g_C3.pos.x - g_C2.pos.x), g_C2.pos.y + (g_C3.pos.y - g_C2.pos.y), g_C3.pos.z]
                        g_D3.pos = [g_D2.pos.x + (g_D3.pos.x - g_D2.pos.x), g_D2.pos.y + (g_D3.pos.y - g_D2.pos.y), g_D3.pos.z]

                        g_A4.pos = [g_A3.pos.x + (g_A4.pos.x - g_A3.pos.x), g_A3.pos.y + (g_A4.pos.y - g_A3.pos.y), g_A4.pos.z]
                        g_B4.pos = [g_B3.pos.x + (g_B4.pos.x - g_B3.pos.x), g_B3.pos.y + (g_B4.pos.y - g_B3.pos.y), g_B4.pos.z]
                        g_C4.pos = [g_C3.pos.x + (g_C4.pos.x - g_C3.pos.x), g_C3.pos.y + (g_C4.pos.y - g_C3.pos.y), g_C4.pos.z]
                        g_D4.pos = [g_D3.pos.x + (g_D4.pos.x - g_D3.pos.x), g_D3.pos.y + (g_D4.pos.y - g_D3.pos.y), g_D4.pos.z]
                    )

                    g_C2:
                    (
                        g_A2.pos = [g_A1.pos.x + dirVec.x, g_A1.pos.y + dirVec.y, g_A2.pos.z]
                        g_B2.pos = [g_B1.pos.x + dirVec.x, g_B1.pos.y + dirVec.y, g_B2.pos.z]
                        g_C2.pos = [g_C1.pos.x + dirVec.x, g_C1.pos.y + dirVec.y, g_C2.pos.z]
                        g_D2.pos = [g_D1.pos.x + dirVec.x, g_D1.pos.y + dirVec.y, g_D2.pos.z]

                        g_A3.pos = [g_A2.pos.x + (g_A3.pos.x - g_A2.pos.x), g_A2.pos.y + (g_A3.pos.y - g_A2.pos.y), g_A3.pos.z]
                        g_B3.pos = [g_B2.pos.x + (g_B3.pos.x - g_B2.pos.x), g_B2.pos.y + (g_B3.pos.y - g_B2.pos.y), g_B3.pos.z]
                        g_C3.pos = [g_C2.pos.x + (g_C3.pos.x - g_C2.pos.x), g_C2.pos.y + (g_C3.pos.y - g_C2.pos.y), g_C3.pos.z]
                        g_D3.pos = [g_D2.pos.x + (g_D3.pos.x - g_D2.pos.x), g_D2.pos.y + (g_D3.pos.y - g_D2.pos.y), g_D3.pos.z]

                        g_A4.pos = [g_A3.pos.x + (g_A4.pos.x - g_A3.pos.x), g_A3.pos.y + (g_A4.pos.y - g_A3.pos.y), g_A4.pos.z]
                        g_B4.pos = [g_B3.pos.x + (g_B4.pos.x - g_B3.pos.x), g_B3.pos.y + (g_B4.pos.y - g_B3.pos.y), g_B4.pos.z]
                        g_C4.pos = [g_C3.pos.x + (g_C4.pos.x - g_C3.pos.x), g_C3.pos.y + (g_C4.pos.y - g_C3.pos.y), g_C4.pos.z]
                        g_D4.pos = [g_D3.pos.x + (g_D4.pos.x - g_D3.pos.x), g_D3.pos.y + (g_D4.pos.y - g_D3.pos.y), g_D4.pos.z]
                    )

                    g_D2:
                    (
                        g_A2.pos = [g_A1.pos.x + dirVec.x, g_A1.pos.y + dirVec.y, g_A2.pos.z]
                        g_B2.pos = [g_B1.pos.x + dirVec.x, g_B1.pos.y + dirVec.y, g_B2.pos.z]
                        g_C2.pos = [g_C1.pos.x + dirVec.x, g_C1.pos.y + dirVec.y, g_C2.pos.z]
                        g_D2.pos = [g_D1.pos.x + dirVec.x, g_D1.pos.y + dirVec.y, g_D2.pos.z]

                        g_A3.pos = [g_A2.pos.x + (g_A3.pos.x - g_A2.pos.x), g_A2.pos.y + (g_A3.pos.y - g_A2.pos.y), g_A3.pos.z]
                        g_B3.pos = [g_B2.pos.x + (g_B3.pos.x - g_B2.pos.x), g_B2.pos.y + (g_B3.pos.y - g_B2.pos.y), g_B3.pos.z]
                        g_C3.pos = [g_C2.pos.x + (g_C3.pos.x - g_C2.pos.x), g_C2.pos.y + (g_C3.pos.y - g_C2.pos.y), g_C3.pos.z]
                        g_D3.pos = [g_D2.pos.x + (g_D3.pos.x - g_D2.pos.x), g_D2.pos.y + (g_D3.pos.y - g_D2.pos.y), g_D3.pos.z]

                        g_A4.pos = [g_A3.pos.x + (g_A4.pos.x - g_A3.pos.x), g_A3.pos.y + (g_A4.pos.y - g_A3.pos.y), g_A4.pos.z]
                        g_B4.pos = [g_B3.pos.x + (g_B4.pos.x - g_B3.pos.x), g_B3.pos.y + (g_B4.pos.y - g_B3.pos.y), g_B4.pos.z]
                        g_C4.pos = [g_C3.pos.x + (g_C4.pos.x - g_C3.pos.x), g_C3.pos.y + (g_C4.pos.y - g_C3.pos.y), g_C4.pos.z]
                        g_D4.pos = [g_D3.pos.x + (g_D4.pos.x - g_D3.pos.x), g_D3.pos.y + (g_D4.pos.y - g_D3.pos.y), g_D4.pos.z]
                    )

                    g_A3:
                    (
                        g_A3.pos = [g_A2.pos.x + dirVec.x, g_A2.pos.y + dirVec.y, g_A3.pos.z]
                        g_B3.pos = [g_B2.pos.x + dirVec.x, g_B2.pos.y + dirVec.y, g_B3.pos.z]
                        g_C3.pos = [g_C2.pos.x + dirVec.x, g_C2.pos.y + dirVec.y, g_C3.pos.z]
                        g_D3.pos = [g_D2.pos.x + dirVec.x, g_D2.pos.y + dirVec.y, g_D3.pos.z]

                        g_A4.pos = [g_A3.pos.x + (g_A4.pos.x - g_A3.pos.x), g_A3.pos.y + (g_A4.pos.y - g_A3.pos.y), g_A4.pos.z]
                        g_B4.pos = [g_B3.pos.x + (g_B4.pos.x - g_B3.pos.x), g_B3.pos.y + (g_B4.pos.y - g_B3.pos.y), g_B4.pos.z]
                        g_C4.pos = [g_C3.pos.x + (g_C4.pos.x - g_C3.pos.x), g_C3.pos.y + (g_C4.pos.y - g_C3.pos.y), g_C4.pos.z]
                        g_D4.pos = [g_D3.pos.x + (g_D4.pos.x - g_D3.pos.x), g_D3.pos.y + (g_D4.pos.y - g_D3.pos.y), g_D4.pos.z]
                    )

                    g_B3:
                    (
                        g_A3.pos = [g_A2.pos.x + dirVec.x, g_A2.pos.y + dirVec.y, g_A3.pos.z]
                        g_B3.pos = [g_B2.pos.x + dirVec.x, g_B2.pos.y + dirVec.y, g_B3.pos.z]
                        g_C3.pos = [g_C2.pos.x + dirVec.x, g_C2.pos.y + dirVec.y, g_C3.pos.z]
                        g_D3.pos = [g_D2.pos.x + dirVec.x, g_D2.pos.y + dirVec.y, g_D3.pos.z]

                        g_A4.pos = [g_A3.pos.x + (g_A4.pos.x - g_A3.pos.x), g_A3.pos.y + (g_A4.pos.y - g_A3.pos.y), g_A4.pos.z]
                        g_B4.pos = [g_B3.pos.x + (g_B4.pos.x - g_B3.pos.x), g_B3.pos.y + (g_B4.pos.y - g_B3.pos.y), g_B4.pos.z]
                        g_C4.pos = [g_C3.pos.x + (g_C4.pos.x - g_C3.pos.x), g_C3.pos.y + (g_C4.pos.y - g_C3.pos.y), g_C4.pos.z]
                        g_D4.pos = [g_D3.pos.x + (g_D4.pos.x - g_D3.pos.x), g_D3.pos.y + (g_D4.pos.y - g_D3.pos.y), g_D4.pos.z]
                    )

                    g_C3:
                    (
                        g_A3.pos = [g_A2.pos.x + dirVec.x, g_A2.pos.y + dirVec.y, g_A3.pos.z]
                        g_B3.pos = [g_B2.pos.x + dirVec.x, g_B2.pos.y + dirVec.y, g_B3.pos.z]
                        g_C3.pos = [g_C2.pos.x + dirVec.x, g_C2.pos.y + dirVec.y, g_C3.pos.z]
                        g_D3.pos = [g_D2.pos.x + dirVec.x, g_D2.pos.y + dirVec.y, g_D3.pos.z]

                        g_A4.pos = [g_A3.pos.x + (g_A4.pos.x - g_A3.pos.x), g_A3.pos.y + (g_A4.pos.y - g_A3.pos.y), g_A4.pos.z]
                        g_B4.pos = [g_B3.pos.x + (g_B4.pos.x - g_B3.pos.x), g_B3.pos.y + (g_B4.pos.y - g_B3.pos.y), g_B4.pos.z]
                        g_C4.pos = [g_C3.pos.x + (g_C4.pos.x - g_C3.pos.x), g_C3.pos.y + (g_C4.pos.y - g_C3.pos.y), g_C4.pos.z]
                        g_D4.pos = [g_D3.pos.x + (g_D4.pos.x - g_D3.pos.x), g_D3.pos.y + (g_D4.pos.y - g_D3.pos.y), g_D4.pos.z]
                    )

                    g_D3:
                    (
                        g_A3.pos = [g_A2.pos.x + dirVec.x, g_A2.pos.y + dirVec.y, g_A3.pos.z]
                        g_B3.pos = [g_B2.pos.x + dirVec.x, g_B2.pos.y + dirVec.y, g_B3.pos.z]
                        g_C3.pos = [g_C2.pos.x + dirVec.x, g_C2.pos.y + dirVec.y, g_C3.pos.z]
                        g_D3.pos = [g_D2.pos.x + dirVec.x, g_D2.pos.y + dirVec.y, g_D3.pos.z]

                        g_A4.pos = [g_A3.pos.x + (g_A4.pos.x - g_A3.pos.x), g_A3.pos.y + (g_A4.pos.y - g_A3.pos.y), g_A4.pos.z]
                        g_B4.pos = [g_B3.pos.x + (g_B4.pos.x - g_B3.pos.x), g_B3.pos.y + (g_B4.pos.y - g_B3.pos.y), g_B4.pos.z]
                        g_C4.pos = [g_C3.pos.x + (g_C4.pos.x - g_C3.pos.x), g_C3.pos.y + (g_C4.pos.y - g_C3.pos.y), g_C4.pos.z]
                        g_D4.pos = [g_D3.pos.x + (g_D4.pos.x - g_D3.pos.x), g_D3.pos.y + (g_D4.pos.y - g_D3.pos.y), g_D4.pos.z]
                    )

                    g_A4:
                    (
                        g_A4.pos = [g_A3.pos.x + dirVec.x, g_A3.pos.y + dirVec.y, g_A4.pos.z]
                        g_B4.pos = [g_B3.pos.x + dirVec.x, g_B3.pos.y + dirVec.y, g_B4.pos.z]
                        g_C4.pos = [g_C3.pos.x + dirVec.x, g_C3.pos.y + dirVec.y, g_C4.pos.z]
                        g_D4.pos = [g_D3.pos.x + dirVec.x, g_D3.pos.y + dirVec.y, g_D4.pos.z]
                    )

                    g_B4:
                    (
                        g_A4.pos = [g_A3.pos.x + dirVec.x, g_A3.pos.y + dirVec.y, g_A4.pos.z]
                        g_B4.pos = [g_B3.pos.x + dirVec.x, g_B3.pos.y + dirVec.y, g_B4.pos.z]
                        g_C4.pos = [g_C3.pos.x + dirVec.x, g_C3.pos.y + dirVec.y, g_C4.pos.z]
                        g_D4.pos = [g_D3.pos.x + dirVec.x, g_D3.pos.y + dirVec.y, g_D4.pos.z]
                    )

                    g_C4:
                    (
                        g_A4.pos = [g_A3.pos.x + dirVec.x, g_A3.pos.y + dirVec.y, g_A4.pos.z]
                        g_B4.pos = [g_B3.pos.x + dirVec.x, g_B3.pos.y + dirVec.y, g_B4.pos.z]
                        g_C4.pos = [g_C3.pos.x + dirVec.x, g_C3.pos.y + dirVec.y, g_C4.pos.z]
                        g_D4.pos = [g_D3.pos.x + dirVec.x, g_D3.pos.y + dirVec.y, g_D4.pos.z]
                    )

                    g_D4:
                    (
                        g_A4.pos = [g_A3.pos.x + dirVec.x, g_A3.pos.y + dirVec.y, g_A4.pos.z]
                        g_B4.pos = [g_B3.pos.x + dirVec.x, g_B3.pos.y + dirVec.y, g_B4.pos.z]
                        g_C4.pos = [g_C3.pos.x + dirVec.x, g_C3.pos.y + dirVec.y, g_C4.pos.z]
                        g_D4.pos = [g_D3.pos.x + dirVec.x, g_D3.pos.y + dirVec.y, g_D4.pos.z]
                    )
                )
            )
        )
        catch
        (
            format "Error in maintainParallelogram: %\n" (getCurrentException())
        )
        finally
        (
            g_busy = false
        )
    )

    /* ---------- 親からの距離をg_distに固定 ---------- */
    fn clampToParent child parent =
    (
        child.pos = parent.pos + safeNormalize(child.pos - parent.pos) * g_dist
    )

    /* ---------- 距離制約（完全版：全方向連鎖対応） ---------- */
    fn maintainOriginalConstraints mover =
    (
        if g_busy then return()
        g_busy = true

        try
        (
            /* --- 1. 逆伝播 (IK) + 特殊な連鎖処理 --- */
            case mover of
            (
                g_A4:
                (
                    g_A3.pos = g_A2.pos + safeNormalize(g_A4.pos - g_A2.pos) * g_dist
                    g_A4.pos = g_A3.pos + safeNormalize(g_A4.pos - g_A3.pos) * g_dist
                )
                g_B4:
                (
                    g_B3.pos = g_B2.pos + safeNormalize(g_B4.pos - g_B2.pos) * g_dist
                    g_B4.pos = g_B3.pos + safeNormalize(g_B4.pos - g_B3.pos) * g_dist
                )
                g_C4:
                (
                    g_C3.pos = g_C2.pos + safeNormalize(g_C4.pos - g_C2.pos) * g_dist
                    g_C4.pos = g_C3.pos + safeNormalize(g_C4.pos - g_C3.pos) * g_dist
                )
                g_D4:
                (
                    g_D3.pos = g_D2.pos + safeNormalize(g_D4.pos - g_D2.pos) * g_dist
                    g_D4.pos = g_D3.pos + safeNormalize(g_D4.pos - g_D3.pos) * g_dist

                    local dirDC = safeNormalize(g_C3.pos - g_D3.pos)
                    g_C3.pos = g_D3.pos + dirDC * g_dist; clampToParent g_C3 g_C2

                    local dirCB = safeNormalize(g_B3.pos - g_C3.pos)
                    g_B3.pos = g_C3.pos + dirCB * g_dist; clampToParent g_B3 g_B2

                    local dirBA = safeNormalize(g_A3.pos - g_B3.pos)
                    g_A3.pos = g_B3.pos + dirBA * g_dist; clampToParent g_A3 g_A2

                    local dirDC4 = safeNormalize(g_C4.pos - g_D4.pos)
                    g_C4.pos = g_D4.pos + dirDC4 * g_dist; clampToParent g_C4 g_C3

                    local dirCB4 = safeNormalize(g_B4.pos - g_C4.pos)
                    g_B4.pos = g_C4.pos + dirCB4 * g_dist; clampToParent g_B4 g_B3

                    local dirBA4 = safeNormalize(g_A4.pos - g_B4.pos)
                    g_A4.pos = g_B4.pos + dirBA4 * g_dist; clampToParent g_A4 g_A3
                )
                g_D3:
                (
                    g_D2.pos = g_D1.pos + safeNormalize(g_D3.pos - g_D1.pos) * g_dist
                    g_D3.pos = g_D2.pos + safeNormalize(g_D3.pos - g_D2.pos) * g_dist

                    local dirDC = safeNormalize(g_C2.pos - g_D2.pos)
                    g_C2.pos = g_D2.pos + dirDC * g_dist; clampToParent g_C2 g_C1

                    local dirCB = safeNormalize(g_B2.pos - g_C2.pos)
                    g_B2.pos = g_C2.pos + dirCB * g_dist; clampToParent g_B2 g_B1

                    local dirBA = safeNormalize(g_A2.pos - g_B2.pos)
                    g_A2.pos = g_B2.pos + dirBA * g_dist; clampToParent g_A2 g_A1

                    local dirDC3 = safeNormalize(g_C3.pos - g_D3.pos)
                    g_C3.pos = g_D3.pos + dirDC3 * g_dist; clampToParent g_C3 g_C2

                    local dirCB3 = safeNormalize(g_B3.pos - g_C3.pos)
                    g_B3.pos = g_C3.pos + dirCB3 * g_dist; clampToParent g_B3 g_B2

                    local dirBA3 = safeNormalize(g_A3.pos - g_B3.pos)
                    g_A3.pos = g_B3.pos + dirBA3 * g_dist; clampToParent g_A3 g_A2
                )
                g_D2:
                (
                    local dirDC = safeNormalize(g_C2.pos - g_D2.pos)
                    g_C2.pos = g_D2.pos + dirDC * g_dist; clampToParent g_C2 g_C1

                    local dirCB = safeNormalize(g_B2.pos - g_C2.pos)
                    g_B2.pos = g_C2.pos + dirCB * g_dist; clampToParent g_B2 g_B1

                    local dirBA = safeNormalize(g_A2.pos - g_B2.pos)
                    g_A2.pos = g_B2.pos + dirBA * g_dist; clampToParent g_A2 g_A1

                    local dirA3 = safeNormalize(g_A3.pos - g_A2.pos)
                    g_A3.pos = g_A2.pos + dirA3 * g_dist; clampToParent g_A3 g_A2

                    local dirB3 = safeNormalize(g_B3.pos - g_B2.pos)
                    g_B3.pos = g_B2.pos + dirB3 * g_dist; clampToParent g_B3 g_B2

                    local dirC3 = safeNormalize(g_C3.pos - g_C2.pos)
                    g_C3.pos = g_C2.pos + dirC3 * g_dist; clampToParent g_C3 g_C2

                    local dirD3 = safeNormalize(g_D3.pos - g_D2.pos)
                    g_D3.pos = g_D2.pos + dirD3 * g_dist; clampToParent g_D3 g_D2
                )
                g_C3:
                (
                    local dirCB = safeNormalize(g_B3.pos - g_C3.pos)
                    g_B3.pos = g_C3.pos + dirCB * g_dist; clampToParent g_B3 g_B2

                    local dirBA = safeNormalize(g_A3.pos - g_B3.pos)
                    g_A3.pos = g_B3.pos + dirBA * g_dist; clampToParent g_A3 g_A2

                    local dirC2 = safeNormalize(g_B2.pos - g_C2.pos)
                    g_B2.pos = g_C2.pos + dirC2 * g_dist; clampToParent g_B2 g_B1

                    local dirBA2 = safeNormalize(g_A2.pos - g_B2.pos)
                    g_A2.pos = g_B2.pos + dirBA2 * g_dist; clampToParent g_A2 g_A1

                    local dirC4 = safeNormalize(g_C4.pos - g_C3.pos)
                    g_C4.pos = g_C3.pos + dirC4 * g_dist; clampToParent g_C4 g_C3

                    local dirB4 = safeNormalize(g_B4.pos - g_B3.pos)
                    g_B4.pos = g_B3.pos + dirB4 * g_dist; clampToParent g_B4 g_B3

                    local dirD4 = safeNormalize(g_D4.pos - g_D3.pos)
                    g_D4.pos = g_D3.pos + dirD4 * g_dist; clampToParent g_D4 g_D3
                )
                g_C2:
                (
                    local dirCB = safeNormalize(g_B2.pos - g_C2.pos)
                    g_B2.pos = g_C2.pos + dirCB * g_dist; clampToParent g_B2 g_B1

                    local dirBA = safeNormalize(g_A2.pos - g_B2.pos)
                    g_A2.pos = g_B2.pos + dirBA * g_dist; clampToParent g_A2 g_A1

                    local dirA3 = safeNormalize(g_A3.pos - g_A2.pos)
                    g_A3.pos = g_A2.pos + dirA3 * g_dist; clampToParent g_A3 g_A2

                    local dirB3 = safeNormalize(g_B3.pos - g_B2.pos)
                    g_B3.pos = g_B2.pos + dirB3 * g_dist; clampToParent g_B3 g_B2

                    local dirC3 = safeNormalize(g_C3.pos - g_C2.pos)
                    g_C3.pos = g_C2.pos + dirC3 * g_dist; clampToParent g_C3 g_C2

                    local dirC4 = safeNormalize(g_C4.pos - g_C3.pos)
                    g_C4.pos = g_C3.pos + dirC4 * g_dist; clampToParent g_C4 g_C3

                    local dirB4 = safeNormalize(g_B4.pos - g_B3.pos)
                    g_B4.pos = g_B3.pos + dirB4 * g_dist; clampToParent g_B4 g_B3

                    local dirD4 = safeNormalize(g_D4.pos - g_D3.pos)
                    g_D4.pos = g_D3.pos + dirD4 * g_dist; clampToParent g_D4 g_D3
                )
                g_B3:
                (
                    local dirBA = safeNormalize(g_A3.pos - g_B3.pos)
                    g_A3.pos = g_B3.pos + dirBA * g_dist; clampToParent g_A3 g_A2

                    local dirB2 = safeNormalize(g_B2.pos - g_B3.pos)
                    g_B2.pos = g_B3.pos + dirB2 * g_dist; clampToParent g_B2 g_B1

                    local dirA2 = safeNormalize(g_A2.pos - g_B2.pos)
                    g_A2.pos = g_B2.pos + dirA2 * g_dist; clampToParent g_A2 g_A1

                    local dirB4 = safeNormalize(g_B4.pos - g_B3.pos)
                    g_B4.pos = g_B3.pos + dirB4 * g_dist; clampToParent g_B4 g_B3

                    local dirA4 = safeNormalize(g_A4.pos - g_A3.pos)
                    g_A4.pos = g_A3.pos + dirA4 * g_dist; clampToParent g_A4 g_A3

                    local dirC4 = safeNormalize(g_C4.pos - g_C3.pos)
                    g_C4.pos = g_C3.pos + dirC4 * g_dist; clampToParent g_C4 g_C3

                    local dirD4 = safeNormalize(g_D4.pos - g_D3.pos)
                    g_D4.pos = g_D3.pos + dirD4 * g_dist; clampToParent g_D4 g_D3
                )
                g_B2:
                (
                    local dirBA = safeNormalize(g_A2.pos - g_B2.pos)
                    g_A2.pos = g_B2.pos + dirBA * g_dist; clampToParent g_A2 g_A1

                    local dirBC = safeNormalize(g_C2.pos - g_B2.pos)
                    g_C2.pos = g_B2.pos + dirBC * g_dist; clampToParent g_C2 g_C1

                    local dirA3 = safeNormalize(g_A3.pos - g_A2.pos)
                    g_A3.pos = g_A2.pos + dirA3 * g_dist; clampToParent g_A3 g_A2

                    local dirB3 = safeNormalize(g_B3.pos - g_B2.pos)
                    g_B3.pos = g_B2.pos + dirB3 * g_dist; clampToParent g_B3 g_B2

                    local dirB4 = safeNormalize(g_B4.pos - g_B3.pos)
                    g_B4.pos = g_B3.pos + dirB4 * g_dist; clampToParent g_B4 g_B3

                    local dirC4 = safeNormalize(g_C4.pos - g_C3.pos)
                    g_C4.pos = g_C3.pos + dirC4 * g_dist; clampToParent g_C4 g_C3

                    local dirD4 = safeNormalize(g_D4.pos - g_D3.pos)
                    g_D4.pos = g_D3.pos + dirD4 * g_dist; clampToParent g_D4 g_D3
                )
                g_A3:
                (
                    local dirA2 = safeNormalize(g_A2.pos - g_A3.pos)
                    g_A2.pos = g_A3.pos + dirA2 * g_dist; clampToParent g_A2 g_A1

                    local dirAB = safeNormalize(g_B2.pos - g_A2.pos)
                    g_B2.pos = g_A2.pos + dirAB * g_dist; clampToParent g_B2 g_B1

                    local dirA4 = safeNormalize(g_A4.pos - g_A3.pos)
                    g_A4.pos = g_A3.pos + dirA4 * g_dist; clampToParent g_A4 g_A3

                    local dirB4 = safeNormalize(g_B4.pos - g_B3.pos)
                    g_B4.pos = g_B3.pos + dirB4 * g_dist; clampToParent g_B4 g_B3

                    local dirC4 = safeNormalize(g_C4.pos - g_C3.pos)
                    g_C4.pos = g_C3.pos + dirC4 * g_dist; clampToParent g_C4 g_C3

                    local dirD4 = safeNormalize(g_D4.pos - g_D3.pos)
                    g_D4.pos = g_D3.pos + dirD4 * g_dist; clampToParent g_D4 g_D3
                )
                g_A2:
                (
                    local dirAB = safeNormalize(g_B2.pos - g_A2.pos)
                    g_B2.pos = g_A2.pos + dirAB * g_dist; clampToParent g_B2 g_B1

                    local dirA3 = safeNormalize(g_A3.pos - g_A2.pos)
                    g_A3.pos = g_A2.pos + dirA3 * g_dist; clampToParent g_A3 g_A2

                    local dirA4 = safeNormalize(g_A4.pos - g_A3.pos)
                    g_A4.pos = g_A3.pos + dirA4 * g_dist; clampToParent g_A4 g_A3

                    local dirB3 = safeNormalize(g_B3.pos - g_B2.pos)
                    g_B3.pos = g_B2.pos + dirB3 * g_dist; clampToParent g_B3 g_B2

                    local dirB4 = safeNormalize(g_B4.pos - g_B3.pos)
                    g_B4.pos = g_B3.pos + dirB4 * g_dist; clampToParent g_B4 g_B3

                    local dirC4 = safeNormalize(g_C4.pos - g_C3.pos)
                    g_C4.pos = g_C3.pos + dirC4 * g_dist; clampToParent g_C4 g_C3

                    local dirD4 = safeNormalize(g_D4.pos - g_D3.pos)
                    g_D4.pos = g_D3.pos + dirD4 * g_dist; clampToParent g_D4 g_D3
                )
            )

            /* --- 2. 縦方向の距離制約（基本） --- */
            clampToParent g_A2 g_A1
            clampToParent g_B2 g_B1
            clampToParent g_C2 g_C1
            clampToParent g_D2 g_D1
            clampToParent g_A3 g_A2
            clampToParent g_B3 g_B2
            clampToParent g_C3 g_C2
            clampToParent g_D3 g_D2
            clampToParent g_A4 g_A3
            clampToParent g_B4 g_B3
            clampToParent g_C4 g_C3
            clampToParent g_D4 g_D3

            /* --- 3. 横方向の連鎖補正（第2列：A2→B2→C2→D2） --- */
            local dirAB = safeNormalize(g_B2.pos - g_A2.pos) * g_dist
            g_B2.pos = g_A2.pos + dirAB; clampToParent g_B2 g_B1

            local dirBC = safeNormalize(g_C2.pos - g_B2.pos) * g_dist
            g_C2.pos = g_B2.pos + dirBC; clampToParent g_C2 g_C1

            local dirCD = safeNormalize(g_D2.pos - g_C2.pos) * g_dist
            g_D2.pos = g_C2.pos + dirCD; clampToParent g_D2 g_D1

            /* --- 4. 横方向の連鎖補正（第3列：A3→B3→C3→D3） --- */
            local dirA3B3 = safeNormalize(g_B3.pos - g_A3.pos) * g_dist
            g_B3.pos = g_A3.pos + dirA3B3; clampToParent g_B3 g_B2

            local dirB3C3 = safeNormalize(g_C3.pos - g_B3.pos) * g_dist
            g_C3.pos = g_B3.pos + dirB3C3; clampToParent g_C3 g_C2

            local dirC3D3 = safeNormalize(g_D3.pos - g_C3.pos) * g_dist
            g_D3.pos = g_C3.pos + dirC3D3; clampToParent g_D3 g_D2

            /* --- 5. 横方向の連鎖補正（第4列：A4→B4→C4→D4） --- */
            local dirA4B4 = safeNormalize(g_B4.pos - g_A4.pos) * g_dist
            g_B4.pos = g_A4.pos + dirA4B4; clampToParent g_B4 g_B3

            local dirB4C4 = safeNormalize(g_C4.pos - g_B4.pos) * g_dist
            g_C4.pos = g_B4.pos + dirB4C4; clampToParent g_C4 g_C3

            local dirC4D4 = safeNormalize(g_D4.pos - g_C4.pos) * g_dist
            g_D4.pos = g_C4.pos + dirC4D4; clampToParent g_D4 g_D3
        )
        catch
        (
            format "Error in maintainOriginalConstraints: %\n" (getCurrentException())
        )
        finally
        (
            g_busy = false
        )
    )

    /* ---------- 統合制御 ---------- */
    fn maintainAllConstraints mover =
    (
        maintainParallelogram mover
        maintainOriginalConstraints mover
    )

    /* ---------- ハンドラ登録 ---------- */
    deleteAllChangeHandlers id:#CombinedConstraints
    when transform g_A2 changes id:#CombinedConstraints do maintainAllConstraints g_A2
    when transform g_B2 changes id:#CombinedConstraints do maintainAllConstraints g_B2
    when transform g_C2 changes id:#CombinedConstraints do maintainAllConstraints g_C2
    when transform g_D2 changes id:#CombinedConstraints do maintainAllConstraints g_D2
    when transform g_A3 changes id:#CombinedConstraints do maintainAllConstraints g_A3
    when transform g_B3 changes id:#CombinedConstraints do maintainAllConstraints g_B3
    when transform g_C3 changes id:#CombinedConstraints do maintainAllConstraints g_C3
    when transform g_D3 changes id:#CombinedConstraints do maintainAllConstraints g_D3
    when transform g_A4 changes id:#CombinedConstraints do maintainAllConstraints g_A4
    when transform g_B4 changes id:#CombinedConstraints do maintainAllConstraints g_B4
    when transform g_C4 changes id:#CombinedConstraints do maintainAllConstraints g_C4
    when transform g_D4 changes id:#CombinedConstraints do maintainAllConstraints g_D4

    format "Setup complete: A1〜D4 全方向連鎖完全版\n"
)
このMAXSCRIPTで    fn maintainParallelogram の中身が重複箇所が多すぎないか？