Maxscript 函数不会在 while 循环中更新

Maxscript function doesn't update in while loop

我在 maxscript 中有这个函数来检查顶点是否在相机框架中。我想要做的是将函数放在一个 while 循环中并将相机移回,每次相机移动时它应该检查对象是否在相机中。但是当我这样做时,while 循环中的函数不会更新,它总是 returns 第一次检查的值。

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
 )

在移动相机后添加对 forceCompleteRedraw() 的调用。

我假设您已将活动视口设置为相机视图,因为您要 viewport.getTM() 以获得相机变换。重绘似乎允许视口刷新,尊重新的相机位置,并给出预期的结果。

请注意,MaxScript 在 运行 时会阻塞 3ds Max 的主线程。这意味着 UI 事件和其他类型的刷新 activity 未被处理,这可能导致此处发生冲突。