-- 初期化:既存オブジェクト・ハンドラー・コールバック削除
--delete objects
try(deleteAllChangeHandlers id:#GridHandler) catch()
try(unregisterRedrawViewsCallback GW_drawHelperGrid) catch()
-- グローバル設定
global g_boneLengths = #() -- 各ボーンの長さを保存する2次元配列
global g_z = 0.0 -- Z 座標
global g_size = 8.0 -- ヘルパーサイズ
global g_rows, g_cols
global g_fkChains, g_grid, g_lineSegs
global isUpdating = false
-- ユーティリティ関数
fn safeNormalize v =
(
local len = length v
if len < 1e-4 then [1,0,0] else normalize v
)
fn randomColor =
(
color (random 64 255) (random 64 255) (random 64 255)
)
fn arrayMax arr =
(
if arr.count == 0 then return 0
local m = arr[1]
for v in arr where v > m do m = v
m
)
-- シーン内の BoneGeometry チェインを収集(ボーン長さも記録)
fn collectFKChainsFromScene =
(
-- BoneGeometry の全取得
local allBones = for o in selection where classOf o == BoneGeometry collect o
-- ルート抽出:親が BoneGeometry でないもの
local roots = for b in allBones where (b.parent == undefined or classOf b.parent != BoneGeometry) collect b
-- ルートごとに子孫をたどってチェイン化し、ボーン長さも記録
g_fkChains = #()
g_boneLengths = #()
for r in roots do
(
local chain = #()
local lengths = #()
local cur = r
while cur != undefined and classOf cur == BoneGeometry do
(
append chain cur
-- 次のボーンまでの距離を計算
local childrenBones = for c in cur.children where classOf c == BoneGeometry collect c
if childrenBones.count > 0 then
(
local nextBone = childrenBones[1]
local boneLength = distance cur.pos nextBone.pos
append lengths boneLength
cur = nextBone
)
else
(
-- 最後のボーンの場合、デフォルト長さを使用
append lengths 20.0
cur = undefined
)
)
append g_fkChains chain
append g_boneLengths lengths
)
-- 行/列数を更新
g_rows = g_fkChains.count
g_cols = arrayMax (for ch in g_fkChains collect ch.count)
)
-- ヘルパーを生成&グリッド配列に配置
fn createHelpersFromBones =
(
-- 空の2次元グリッドを確保
g_grid = for i = 1 to g_rows collect (for j = 1 to g_cols collect undefined)
-- 各チェイン・各ボーン位置にヘルパー生成
for i = 1 to g_rows do
for j = 1 to g_fkChains[i].count do
(
local bone = g_fkChains[i][j]
local pos = bone.transform.row4
local label = bit.intAsChar(64+i) + j as string
g_grid[i][j] = point name:("H_" + label) pos:pos size:g_size cross:true box:true wirecolor:(randomColor())
)
rebuildGridLineCache()
)
-- ヘルパー削除
fn deleteHelpers =
(
if g_grid != undefined do
(
for row in g_grid do for p in row where isValidNode p do delete p
g_grid = undefined
g_lineSegs = #()
)
)
-- 最初の列を固定(isFrozen)
fn freezeFirstColumn =
(
for i = 1 to g_rows do
(
local p = g_grid[i][1]
if p != undefined and isValidNode p then p.isFrozen = true
)
)
-- 線分キャッシュ生成(行・列の隣接点)
fn rebuildGridLineCache =
(
g_lineSegs = #()
if g_grid == undefined then return()
-- 横線
for i = 1 to g_rows do
for j = 1 to (g_cols-1) do
(
local p1 = g_grid[i][j]; local p2 = g_grid[i][j+1]
if isValidNode p1 and isValidNode p2 then append g_lineSegs #(p1.pos, p2.pos)
)
-- 縦線
for j = 1 to g_cols do
for i = 1 to (g_rows-1) do
(
local p1 = g_grid[i][j]; local p2 = g_grid[i+1][j]
if isValidNode p1 and isValidNode p2 then append g_lineSegs #(p1.pos, p2.pos)
)
)
-- 平行四辺形維持(ボーン長さ対応版)
fn maintainParallelogram mover =
(
if isUpdating then return()
isUpdating = true
try
(
if g_grid == undefined then
(
format "Error: g_grid is undefined\n"
isUpdating = false
return()
)
-- moverがどのポイントか特定
local moverRow = undefined
local moverCol = undefined
local found = false
for r = 1 to g_rows while not found do
(
for c = 1 to g_cols while not found do
(
if g_grid[r][c] == mover then
(
moverRow = r
moverCol = c
found = true
)
)
)
if not found then
(
format "Error: mover not found in g_grid\n"
isUpdating = false
return()
)
print ("Moving column: " + moverCol as string)
-- 親は左隣の列(同じ行)
if moverCol > 1 then
(
local parentCol = moverCol - 1
local parentPoint = g_grid[moverRow][parentCol]
-- この列のボーン長さを取得
local boneLength = if g_boneLengths[moverRow].count >= parentCol then g_boneLengths[moverRow][parentCol] else 20.0
local baseVec = [mover.pos.x - parentPoint.pos.x, mover.pos.y - parentPoint.pos.y, 0]
local dirVec = safeNormalize(baseVec) * boneLength
-- 同じ列の全行を調整(各行のボーン長さを使用)
for r = 1 to g_rows do
(
if g_grid[r][parentCol] != undefined and g_grid[r][moverCol] != undefined then
(
local rowBoneLength = if g_boneLengths[r].count >= parentCol then g_boneLengths[r][parentCol] else 20.0
local rowDirVec = safeNormalize(dirVec) * rowBoneLength
g_grid[r][moverCol].pos = [g_grid[r][parentCol].pos.x + rowDirVec.x,
g_grid[r][parentCol].pos.y + rowDirVec.y,
g_grid[r][moverCol].pos.z]
)
)
-- 右側の列を追随(各ボーン長さを維持)
for c = moverCol + 1 to g_cols do
(
for r = 1 to g_rows do
(
if g_grid[r][c] != undefined and g_grid[r][c-1] != undefined then
(
local rowBoneLength = if g_boneLengths[r].count >= (c-1) then g_boneLengths[r][c-1] else 20.0
local originalDir = safeNormalize(g_grid[r][c].pos - g_grid[r][c-1].pos)
g_grid[r][c].pos = g_grid[r][c-1].pos + originalDir * rowBoneLength
)
)
)
)
)
catch
(
format "Error in maintainParallelogram: %\n" (getCurrentException())
)
isUpdating = false
)
-- adjustGrid(BFS + ボーン長さ対応)
fn adjustGrid mover =
(
if isUpdating then return()
isUpdating = true
try (
local queue = #()
local processed = #()
append queue mover
append processed mover
while queue.count > 0 do
(
local current = queue[1]
deleteItem queue 1
local row = undefined
local col = undefined
for i = 1 to g_rows do
(
for j = 1 to g_cols do
(
if g_grid[i][j] == current do (
row = i
col = j
exit
)
)
if row != undefined do exit
)
if row != undefined and col != undefined then
(
local neighbors = #()
if row > 1 do append neighbors g_grid[row-1][col]
if row < g_rows do append neighbors g_grid[row+1][col]
if col > 1 do append neighbors g_grid[row][col-1]
if col < g_cols do append neighbors g_grid[row][col+1]
for nb in neighbors where not nb.isFrozen and findItem processed nb == 0 do
(
-- 適切なボーン長さを取得
local boneLength = 20.0 -- デフォルト値
-- 横方向の移動の場合
if row == undefined or col == undefined then continue
local nbRow = undefined
local nbCol = undefined
for i = 1 to g_rows do
(
for j = 1 to g_cols do
(
if g_grid[i][j] == nb do (
nbRow = i
nbCol = j
exit
)
)
if nbRow != undefined do exit
)
if nbRow != undefined and nbCol != undefined then
(
-- 横方向の隣接
if nbRow == row then
(
if nbCol < col and g_boneLengths[row].count >= nbCol then
boneLength = g_boneLengths[row][nbCol]
else if col < nbCol and g_boneLengths[row].count >= col then
boneLength = g_boneLengths[row][col]
)
-- 縦方向の隣接(同じ列間で平均値を使用)
else if nbCol == col then
(
if g_boneLengths[row].count >= col-1 and g_boneLengths[nbRow].count >= col-1 and col > 1 then
boneLength = (g_boneLengths[row][col-1] + g_boneLengths[nbRow][col-1]) / 2.0
)
)
local dir = safeNormalize(current.pos - nb.pos)
nb.pos = current.pos - dir * boneLength
append queue nb
append processed nb
)
)
)
-- 列方向の距離調整(横、ボーン長さ使用)
for i = 1 to g_rows do (
for j = 2 to g_cols do (
local prev = g_grid[i][j-1]
local curr = g_grid[i][j]
if not curr.isFrozen do (
local boneLength = if g_boneLengths[i].count >= (j-1) then g_boneLengths[i][j-1] else 20.0
local dir = safeNormalize(curr.pos - prev.pos)
curr.pos = prev.pos + dir * boneLength
)
)
)
-- 行方向の距離調整(縦、平均ボーン長さ使用)
for j = 1 to g_cols do (
for i = 2 to g_rows do (
local prev = g_grid[i-1][j]
local curr = g_grid[i][j]
if not curr.isFrozen do (
-- 縦方向は前の列のボーン長さの平均を使用
local avgLength = 20.0
if j > 1 then
(
local sum = 0.0
local count = 0
if g_boneLengths[i-1].count >= (j-1) then (sum += g_boneLengths[i-1][j-1]; count += 1)
if g_boneLengths[i].count >= (j-1) then (sum += g_boneLengths[i][j-1]; count += 1)
if count > 0 then avgLength = sum / count
)
local dir = safeNormalize(curr.pos - prev.pos)
curr.pos = prev.pos + dir * avgLength
)
)
)
)
catch (
format "? adjustGrid exception: %\n" (getCurrentException())
)
rebuildGridLineCache()
isUpdating = false
)
-- ボーンをヘルパー位置に同期
fn updateBonesFromHelpers =
(
for i = 1 to g_rows do
(
local chain = g_fkChains[i]
for j = 1 to chain.count-1 do
(
local p1 = g_grid[i][j]
local p2 = g_grid[i][j+1]
if p1 == undefined or p2 == undefined then continue
local dir = p2.pos - p1.pos
if length dir < 1e-4 then continue
local fwd = normalize dir
-- ここを三項演算子から if/else に変更
local up
if abs(dot fwd [0,0,1]) > 0.99 then
(
up = [0,1,0]
)
else
(
up = [0,0,1]
)
local right = normalize (cross up fwd)
up = normalize (cross fwd right)
local mtx = matrix3 fwd up (cross fwd up) p1.pos
chain[j].transform = mtx
)
)
)
fn maintainAllConstraints mover =
(
maintainParallelogram mover
adjustGrid mover
)
-- ハンドラー登録
fn setupHandlers =
(
try(deleteAllChangeHandlers id:#GridHandler) catch()
for i = 1 to g_rows do for j = 2 to g_cols do
(
local h = g_grid[i][j]
if h != undefined and isValidNode h then
(
local cmd = "when transform g_grid[" + i as string + "][" + j as string + "] changes id:#GridHandler do " +
"(maintainAllConstraints g_grid[" + i as string + "][" + j as string + "]; updateBonesFromHelpers())"
execute cmd
)
)
)
-- 描画コールバック
fn GW_drawHelperGrid =
(
if g_lineSegs == undefined or g_lineSegs.count == 0 then return()
gw.setTransform(matrix3 1)
gw.setColor #line (color 0 255 128)
for seg in g_lineSegs do gw.polyline #(seg[1], seg[2]) false
gw.enlargeUpdateRect #whole
)
-- UI:ワンストップ実行/停止
rollout gridSetupRollout "等間隔グリッドUI" width:260 height:140
(
spinner spnHelperSize "ヘルパーサイズ:" range:[1,100,g_size] type:#float scale:0.1 width:240 align:#left
checkbutton chkOneStop "ワンストップ実行" checked:false width:240 align:#left
on spnHelperSize changed val do
(
g_size = val
if g_grid != undefined do (
for row in g_grid do
for obj in row where isValidNode obj do
obj.size = val
)
)
on chkOneStop changed state do
(
if state then
(
collectFKChainsFromScene()
if g_rows > 0 then
(
createHelpersFromBones()
freezeFirstColumn()
setupHandlers()
registerRedrawViewsCallback GW_drawHelperGrid
format "? ワンストップ実行:ヘルパー生成~ハンドラー登録完了\n"
-- ボーン長さ情報を表示
format "? ボーン長さ情報:\n"
for i = 1 to g_boneLengths.count do
(
format " Chain %: " i
for j = 1 to g_boneLengths[i].count do
format "%.2f " g_boneLengths[i][j]
format "\n"
)
)
else format "? ボーンチェインが見つかりませんでした\n"
)
else
(
try(deleteAllChangeHandlers id:#GridHandler) catch()
deleteHelpers()
try(unregisterRedrawViewsCallback GW_drawHelperGrid) catch()
redrawViews()
format "? ワンストップ停止:ヘルパー&ハンドラー削除\n"
)
)
)
-- フロータ表示
global gridSetupFloater
try(closeRolloutFloater gridSetupFloater) catch()
gridSetupFloater = newRolloutFloater "等間隔グリッドUI" 280 160
addRollout gridSetupRollout gridSetupFloater