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

时间:2015-11-13 09:08:27

标签: maxscript

我在maxscript中有这个函数来检查顶点是否在相机框架中。我想要做的是将功能放在while循环中并将相机移回,每次相机移动时都应该检查对象是否在相机中。但是当我这样做时,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 个答案:

答案 0 :(得分:1)

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

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

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