Search code examples
pythonmayafrustumculling

How change Level of Detail Attribute of objects to boundingbox which are in frustum of moving camera?


I am trying implement frustum culling in maya , where I am turning Level of Detail to boundingbox [cmds.setAttr(object + '.overrideLevelOfDetail',0)] if they are not in the frustum of the selected camera : refer to the image for clear reference.

enter image description here

I have written this script till now, using this I can get the required result, but I want it to be dynamic, so if camera is moving it will set the display of objects depending upon their position as of now I need re run the script to be able to reflect the changes

Here is the code that I worked on till now : frustum culling maya

Any help would be appreciated

Thanks & Regards

EDIT :

change of camera location

still bbox after selecting objects


Solution

  • You probably need to implement a event callback.. for example I changed your code a bit and added a timeChange callback. So when timeline moves it update proper.

    def updateFrustum(arg):
        skip_meshes = ['camera1', 'front', 'persp', 'side', 'top']
        meshes = cmds.ls(type="transform")
        
        req_meshes = list(set(meshes) - set(skip_meshes))
        mesh_status = {}
        selected_cam = "camera1"
        for mesh in req_meshes:
            flag = in_frustum(selected_cam, mesh)
            if flag == 1:
                mesh_status[mesh] = True
            else:
                mesh_status[mesh] = False
        
        
        for key, value in mesh_status.items():
            if mesh_status[key] == True:
                cmds.setAttr(key + '.overrideEnabled', 1)
                cmds.setAttr(key + '.overrideLevelOfDetail',0)
            else:
                cmds.setAttr(key + '.overrideEnabled', 1)
                cmds.setAttr(key + '.overrideLevelOfDetail',1)
            cmds.select(key)        
        
        cmds.select(cl=True)
    
    callback = OpenMaya.MEventMessage().addEventCallback( 'timeChanged', updateFrustum )