2015-11-13 25 views
0

我在maxscript中有這個函數來檢查vert是否在相機框架中。我想要做的是將功能放在while循環中並將相機移回,並且每次相機移動時都應檢查對象是否在相機中。但是當我這樣做時,while循環中的函數不會更新,它總是返回第一個檢查的值。Maxscript函數不會在while循環中更新

fn getVertsInViewport theObject = 
(
local theVerts = #() --return array 
local theMesh = snapshotasmesh theObject --grab the mesh from top of the stack 
local theCount = theMesh.numverts --get the number of vertices 
local theTM = viewport.getTM() --get the current view's transformation 
local screen_width = renderWidth --get the current render height 
local screen_height = renderHeight --get the current render width 
for v = 1 to theCount do --loop through all vertices 
(
    local thePos = (getVert theMesh v) * theTM --transform vertex in view space 
    --get the world location of the upper left corner of the camera view at the depth of the vertex 
    local screen_origin = mapScreenToView [0,0] (thePos.z) [screen_width,screen_height] 
    --get the bottom right corner at the vertex depth 
    local end_screen = mapScreenToView [screen_width,screen_height] (thePos.z) [screen_width,screen_height] 
    --calculate the world size based on the two corners 
    local world_size = screen_origin-end_screen 
    --calculate the X and Y aspect factors 
    local x_aspect = screen_width/(abs world_size.x) 
    local y_aspect = screen_height/(abs world_size.y) 
    --calculate the screen coordinates using all the above data: 
    local screen_coords = point2 (x_aspect*(thePos.x-screen_origin.x)) (-(y_aspect*(thePos.y-screen_origin.y))) 
    --if the vertex is outside of the screen (negative or higher than the render size), collect it 
    if screen_coords.x <= 0 or screen_coords.y <= 0 or screen_coords.x > screen_width or screen_coords.y > screen_height then 
     append theVerts v 
)--end v loop 
delete theMesh --release the memory used by the TriMesh 
theVerts --return the collected vertices 
)--end fn verts in viewport 

maksimum = 3 
counter = 1 
while counter < maksimum do 
(
    move $Camera001 [0,-550,0] 
    cameraOK = getVertsInViewport $ 
    print (counter as string +" "+ "camera_pos = "+ $Camera001.pos.y as string +" : " +(cameraOK as string)) 
    counter += 1 
) 

回答

1

在移動相機後添加對forceCompleteRedraw()的調用。

我假設你有活動的視口設置到相機視圖,因爲你是viewport.getTM()來獲取相機轉換。重畫似乎允許視口刷新,尊重新的相機位置,並給出預期的結果。

請注意,MaxScript在運行時會阻止3ds Max的主線程。這意味着不會處理UI事件和其他類型的刷新活動,這可能會導致衝突。