以下は
1.「予約語(if / do / when など)や Max の既存クラス名(helper など)を一切変数名に流用しない」
2.「コールバックのスコープ問題を回避するため、必要な配列や ID を global に宣言する」
3.「ノードを動かした瞬間 (#nodeTransform イベント) にグリッド全体を再レイアウトする」
という 3 点を徹底した完全版 MAXScript です。
スクリプト全体を 1 つのコードブロックとして実行してください。
/*─────────────────────────────────────────────
等面積 6×5 グリッド ‑ オートレイアウト
author : your_name
date : 2024-xx-xx
─────────────────────────────────────────────*/
(
/* ========= 定数 ========= */
global EA_rows = 5 -- 行数 (Y 方向)
global EA_cols = 6 -- 列数 (X 方向)
global EA_points = #() -- グリッド Point ノード格納用
global EA_cbID = #EqualArea -- コールバック ID
/* ========= グリッド生成 ========= */
fn EA_createGrid p0 p1 =
(
/* 既存ノードを削除したい場合はここで消す
for n in EA_points where isValidNode n do delete n
*/
EA_points = #() -- 初期化
local dx = (p1.x - p0.x) / (EA_cols - 1)
local dy = (p1.y - p0.y) / (EA_rows - 1)
for c = 1 to EA_cols do
(
for r = 1 to EA_rows do
(
local pos = [p0.x + (c-1)*dx , p0.y + (r-1)*dy , 0]
local nd = point pos:pos size:5 name:("EApt_"+c as string+"_"+r as string)
append EA_points nd
)
)
)
/* ========= レイアウト再計算 ========= */
fn EA_relayout =
(
if EA_points.count == 0 then return()
/* 現在のバウンディングを取得 */
local minX = amin (for n in EA_points collect n.pos.x)
local maxX = amax (for n in EA_points collect n.pos.x)
local minY = amin (for n in EA_points collect n.pos.y)
local maxY = amax (for n in EA_points collect n.pos.y)
local dx = (maxX - minX) / (EA_cols - 1)
local dy = (maxY - minY) / (EA_rows - 1)
for c = 1 to EA_cols do
(
for r = 1 to EA_rows do
(
local idx = (r-1)*EA_cols + c
local nd = EA_points[idx]
if isValidNode nd do
nd.pos = [minX + (c-1)*dx , minY + (r-1)*dy , 0]
)
)
)
/* ========= コールバック登録 / 解除 ========= */
fn EA_registerCallback =
(
callbacks.removeScripts id:EA_cbID -- 既存をクリア
local cbStr =
"
if selection.count != 0 do
(
local n = selection[selection.count] -- 直近に動かしたノード
if isValidNode n and (findItem EA_points n) != 0 do EA_relayout()
)
"
callbacks.addScript #nodeTransform cbStr id:EA_cbID
)
fn EA_removeCallback = ( callbacks.removeScripts id:EA_cbID )
/* ========= 実行例 ========= */
EA_createGrid [0,0,0] [500,400,0] -- 左下 / 右上 座標
EA_registerCallback()
format "Equal-area grid (% by %) has been created. Move any grid point to see auto-adjustment.\n" EA_cols EA_rows
)
スクリプトを実行すると
• scene に 6×5=30 個の Point ヘルパー (名前は EApt_列_行) が生成
• どれか 1 つを移動 → コールバックが発火し、外接矩形を再計算 → 全ポイントを均等に張り直し
となり、常に「各セルの面積が同一」の状態が保たれます。
補足
・コールバックを停止したい場合は
EA_removeCallback()
・行/列数を変えたい場合は EA_rows / EA_cols を実行前に変更してください。
予約語・組み込みクラス名は一切変数に流用していませんのでご安心ください。