意図通りの挙動はするが動作が非常に遅い、なにか解決案は?
- User
意図通りの挙動はするが動作が非常に遅い、なにか解決案は?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
意図通りの挙動はするが動作が非常に遅い、なにか解決案は?
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
「遅い」=①無駄な再計算回数が多い ②ビューポートの再描画が頻発 ③.NET タイマー/MAXScript でのポーリング‐ループが重い──の 3 点が主因です。
特に .NET Timer 方式は“毎 50 ms * 常時 * 全フレーム”でチェックを回すため、移動が終わっても 150 ms 間はずっと再計算→再描画をしています。
A) 「移動が完了した瞬間だけ」⾼速で 1 回補正
B) 補正時は再描画を抑制
C) そもそも MAX 標準の制御系(スクリプトコントローラ/位置制約)でポーリングを無くす
以下、実装しやすい順に 3 案を紹介します。
────────────────────────
■ 案 1:nodeEventCallback で「移動完了時だけ」補正
────────────────────────
1 回だけ計算→ポジション設定→終わり。タイマーを完全に廃止できます。
(
/* 0) まず既存のコールバックをクリア */
try( nodeEventCallback removeAll:#DistanceFixCB ) catch()
/* 1) 距離補正関数 */
fn keepDistance node other dist =
(
if isValidNode node and isValidNode other then
(
local d = distance node.pos other.pos
if abs(d-dist) > 0.01 do
(
/* redraw 抑制 */
disableSceneRedraw()
animate off
other.pos = node.pos + normalize(other.pos-node.pos)*dist
animate on
enableSceneRedraw()
)
)
)
/* 2) ヘルパー生成(お使いのコードをそのまま使えます) */
delete objects
local d = 20
local pH = #(point name:"H1" pos:[ 10, 10,0],
point name:"H2" pos:[-10, 10,0],
point name:"H3" pos:[-10,-10,0],
point name:"H4" pos:[ 10,-10,0])
pH[3].parent = pH[2] -- H3→H2
pH[4].parent = pH[1] -- H4→H1
/* 3) nodeEventCallback を登録
#transformed = マウスを離した瞬間やスクリプトで transform 完了した瞬間のみ発火 */
nodeEventCallback \
transformed:{ keepDistance $.node pH[4] d } \
nodes:pH[3] id:#DistanceFixCB
nodeEventCallback \
transformed:{ keepDistance $.node pH[3] d } \
nodes:pH[4] id:#DistanceFixCB
format "nodeEventCallback 方式 起動\n"
)
メリット
・移動終了時 1 回だけ実⾏→ほぼラグなし
・ビューポート再描画を stop/start で包むので画面チラ付き激減
デメリット
・Max 2014 以前では nodeEventCallback が無い
────────────────────────
■ 案 2:when‐handler+redraw 抑制(タイマー無し)
────────────────────────
when transform <node> changes は 1TICK ごとに呼ばれますが、ビューポート再描画を抑えれば体感速度はかなり向上します。
ポイント
• 補正側のノードは キー打ち禁止 (animate off)
• 補正中に disableSceneRedraw()/enableSceneRedraw() で 1-フレームまとめ描画
(
when handlers delete id:#DistFix
delete objects
local dist = 20
local h1 = point name:"H1" pos:[ 10, 10,0]
local h2 = point name:"H2" pos:[-10, 10,0]
local h3 = point name:"H3" pos:[-10,-10,0]
local h4 = point name:"H4" pos:[ 10,-10,0]
h3.parent = h2; h4.parent = h1
local adjusting = false
fn fix a b =
(
if adjusting then return()
adjusting = true
disableSceneRedraw()
animate off
b.pos = a.pos + normalize (b.pos-a.pos)*dist
animate on
enableSceneRedraw()
adjusting = false
)
when transform h3 changes id:#DistFix do fix h3 h4
when transform h4 changes id:#DistFix do fix h4 h3
format "when 方式起動\n"
)
十分軽い場合はこの案で OK。
────────────────────────
■ 案 3:スクリプトコントローラ / 位置制約 (最軽量)
────────────────────────
「動かしたノード=親」「追従ノード=子」という構図を捨て、
どちらも“位置スクリプトコントローラ”で距離を計算させる。
評価は Max の依存グラフ内で行われるため、MAXScript でトラップするより速いです。
例:Helper_04 の Position にスクリプトコントローラを割り当て
(Track View → Position → Assign Controller → Position Script)
-- Controller のスクリプト(例) ---------
node03 = $Helper_03
dist = 20 -- CA にして UI から弄っても良い
in coordsys world
(
dir = normalize( node03.pos - this.pos )
node03.pos - dir*dist
)
------------------------------------------
同様に Helper_03 側にも “逆向き” スクリプトを割り当てます。
依存ループを避けるため、片⽅だけ スクリプトにし、もう⽚⽅はユーザが直接動かす運用でも可。
(片追従に割り切れば最も軽く、ループも無し)
disableSceneRedraw()+フラグ制御まずは案1 or 案2に差し替えてみてください。ほぼリアルタイムでストレスなく動くはずです。