Interactive/AmbientOcclusionExample.py

##
## (C) Copyright 2008 Autodesk, Inc.  All rights reserved.
##
## Use of this software is subject to the terms of the Autodesk license
## agreement provided at the time of installation or download, or which
## otherwise accompanies this software in either electronic or hard copy
## form.
##

from SceneGraphUtilities    import CollectLeafNodes
from ScriptRunner           import ScriptRunner

import ModelIO

class GenerateAmbientOcclusion( ScriptRunner ):
    def __init__( self, scriptName, args ):
        ScriptRunner.__init__( self, scriptName, args )

    def Main( self ):
        """
        This script reads a simple scene and computes ambient occlusion.
        """
        exampleFile = 'C:/Example.a3s'
        resultFile = 'C:/Result.a3s'

        raysPerSample = 128
        resolutionFactor = 1.0
        shadowSpread = 180.0
        useDropOff = False
        dropOffDistance = 5.0
        dropOffRate = 1.0
        chordLengthParameters = False
        selfShadowCorrection = 0.01
        BlurAmount = 5
        minTileSize = 6
        useEnvFloor = True

        # Load the scene - one hour timeout.
        self.sendMessageSync( 'APPLICATION_LOAD_SCENE',
                              ( exampleFile, ),
                              userTimeout = 3600 )
        self.assert_( self.document() is not None )

        # Use all visible nodes.
        #
        nodeIds = None
        if self.document().has(ModelIO.id) :
            nodeIds = tuple(CollectLeafNodes(
                                self.document().get( ModelIO.id ).root, 
                                [lambda node: node.getIsVisible()], True ))

        # Send a message to compute the occlusion.  10 hour timeout.
        #
        if nodeIds :
            self.sendMessageSync( 'MATERIAL_COMPUTE_OCCLUSION',
                                  ( nodeIds,
                                    raysPerSample,
                                    resolutionFactor,
                                    shadowSpread,
                                    useDropOff,
                                    dropOffDistance,
                                    dropOffRate,
                                    chordLengthParameters,
                                    selfShadowCorrection,
                                    BlurAmount,
                                    minTileSize,
                                    useEnvFloor,
                                    ),
                                  userTimeout = 36000 )

        # Save the scene.
        #
        self.sendMessageSync( 'STATUSBAR_SET_TEXT',
                      (("Saving scene with occlusion to %s" % (resultFile)),))
        self.sendMessageSync( 'APPLICATION_SAVE_SCENE', ( resultFile, ) )


# ===============================================================
def instantiate( scriptName, args ):
    return GenerateAmbientOcclusion( scriptName, args )
# ===============================================================