(
-- 既存のチェンジハンドラを削除(スクリプトを再実行する際に必要)
when handlers delete id:#HelperDistanceControl
format "Existing handlers with ID #HelperDistanceControl deleted.\n"
-- 全オブジェクトを削除
delete objects
format "All objects deleted.\n"
/*------------------------------------------
2. ユーザー設定
------------------------------------------*/
local dist = 20 -- 正方形の辺の長さ
local zHeight = 0
local helperSize = 10
local targetDist = 20.0 -- Helper_03とHelper_04の目標距離
local half = dist / 2.0
local posArr = #(
[ half, half, zHeight],
[-half, half, zHeight],
[-half, -half, zHeight],
[ half, -half, zHeight]
)
/*------------------------------------------
3. ヘルパーの生成&配置
------------------------------------------*/
local hp01 = Point name:"Helper_01" pos:posArr[1] size:helperSize cross:true box:false
local hp02 = Point name:"Helper_02" pos:posArr[2] size:helperSize cross:true box:false
local hp03 = Point name:"Helper_03" pos:posArr[3] size:helperSize cross:true box:false
local hp04 = Point name:"Helper_04" pos:posArr[4] size:helperSize cross:true box:false
/*------------------------------------------
4. リンク設定
------------------------------------------*/
hp03.parent = hp02
hp04.parent = hp01
format "Helpers created and linked.\n"
/*------------------------------------------
5. 距離制御用のチェンジハンドラ
------------------------------------------*/
local isAdjusting = false -- 補正中フラグ(無限ループ防止用)
-- Helper_03 が動いたときの処理
when transform hp03 changes id:#HelperDistanceControl do
(
if not isAdjusting and isValidNode hp04 then
(
isAdjusting = true
animate off
local dir = normalize(hp04.pos - hp03.pos)
hp04.pos = hp03.pos + (dir * targetDist)
animate on
isAdjusting = false
)
)
-- Helper_04 が動いたときの処理
when transform hp04 changes id:#HelperDistanceControl do
(
if not isAdjusting and isValidNode hp03 then
(
isAdjusting = true
animate off
local dir = normalize(hp03.pos - hp04.pos)
hp03.pos = hp04.pos + (dir * targetDist)
animate on
isAdjusting = false
)
)
format "\nFinished! Dynamic distance control is active.\n"
completeRedraw()
)
このスクリプトの特徴
when 構文の正しい使用:
when transform <node> changes do ... の形式で、ノードの変換(位置、回転、スケール)の変更を監視します。