変数名に予約語を使うのはやめて
- User
変数名に予約語を使うのはやめて
Assistant
ご指摘ありがとうございます。確かに
$やwhenなどの予約語を変数名に使うのは避けるべきです。以下は修正版で、より安全な変数名を使用し、コールバック処理も改善したMAXScriptです:-- グリッドパラメータ gridCols = 6 -- 列数 gridRows = 5 -- 行数 gridStartPos = [0,0,0] -- グリッドの左下起点 gridEndPos = [500,400,0] -- グリッドの右上終点 -- 初期グリッド作成 colSpacing = (gridEndPos.x - gridStartPos.x)/(gridCols-1) rowSpacing = (gridEndPos.y - gridStartPos.y)/(gridRows-1) gridHelpers = #() for col = 1 to gridCols do ( for row = 1 to gridRows do ( posX = gridStartPos.x + (col-1)*colSpacing posY = gridStartPos.y + (row-1)*rowSpacing helper = Point name:("GridHelper_"+col as string+"_"+row as string) size:5 pos:[posX,posY,0] append gridHelpers helper ) ) -- グリッド再計算関数 fn updateGridLayout = ( helperPositions = for h in gridHelpers collect h.pos minX = amin (for p in helperPositions collect p.x) maxX = amax (for p in helperPositions collect p.x) minY = amin (for p in helperPositions collect p.y) maxY = amax (for p in helperPositions collect p.y) newColSpacing = (maxX - minX)/(gridCols-1) newRowSpacing = (maxY - minY)/(gridRows-1) for col = 1 to gridCols do ( for row = 1 to gridRows do ( index = (row-1)*gridCols + col currentHelper = gridHelpers[index] currentHelper.pos = [minX + (col-1)*newColSpacing, minY + (row-1)*newRowSpacing, 0] ) ) ) -- コールバック登録関数 fn registerGridCallback = ( callbacks.removeScripts id:#gridLayoutCallback callbackScript = """ if selection.count > 0 then ( local selectedHelper = selection[1] if findItem gridHelpers selectedHelper != 0 then ( updateGridLayout() ) ) """ callbacks.addScript #selectionSetChanged callbackScript id:#gridLayoutCallback ) -- コールバックを登録 registerGridCallback() -- コールバック解除用(必要に応じて使用) fn unregisterGridCallback = ( callbacks.removeScripts id:#gridLayoutCallback ) -- ヘルパー表示 print ("グリッドヘルパーが " + gridCols as string + "x" + gridRows as string + " で作成されました")改善点:
-
変数名の改善:
$や予約語を使用していた変数名を具体的な名前に変更helpers→gridHelpersspacingX→colSpacingなど、意味が明確になるように
-
コールバック処理の強化:
- 専用の登録/解除関数を追加
- コールバックスクリプトを文字列リテラルで定義
- コールバックIDを明確に指定
-
コードの可読性向上:
- ループ変数を
i,jからcol,rowに変更 - 位置計算の変数名を明確化
- ループ変数を
このスクリプトは、グリッド内の任意のヘルパーを移動すると、他のヘルパーが自動的に再配置され、すべてのセルが均等なサイズを維持します。コールバックは
unregisterGridCallback()で解除できます。-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.