Interactive/ShotsHtmlCustom.py

##
# (C) Copyright 2012 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.
##

# You will need Autodesk Showcase 2007 (version 2007.11 or later).
#
 
# 
# After that, if you save movie from shots in a directory that
# contains files SaveMovieShotBody.html. SaveMovieShotHeader.html
# and SaveMovieShotFooter.html, an html file will be created
# with the movie file (same name, html extension) which will be
# made of:
#
# SaveMovieShotHeader.html
# SaveMovieShotBody.html with some changes for each shot (see below)
# SaveMovieShotFooter.html
#
# SaveMovieShotBody.html has:
#  * all occurrences of SHOT_NAME replaced by the actual shot name;
#  * all occurrences of SHOT_THUMBNAIL replaced by a pointer to the
#    location of the shot thumbnail image;
#  * all occurrences of SHOT_MOVIE replaced by the pointer to the movie.
# 

__all__ = ['ShotsHtmlCustom', 'instantiate']

from UserCustomization  import UserCustomBase, CustomInfo
from MessageInterpreter import MessageInterpreter
from Application        import theApplication

import ShotMessages

import os, os.path

class ShotsHtmlCustom (UserCustomBase):
    def __init__(self):
        #UserCustomBase.__init__(self)
        self.__myShowHQIMenu = None
        self.__myShowHQIId = None
        self.__myInterpreter = ShotsHtmlInterpreter()

    def getInterpreter(self,isInteractive):
        if isInteractive:
            return self.__myInterpreter
        return None


def instantiate():
    return ShotsHtmlCustom()


class ShotsHtmlInterpreter( MessageInterpreter ):

    def __init__( self ):
        MessageInterpreter.__init__( self )
        self.reset()


    def deactivate(self):
        MessageInterpreter.deactivate(self)


    def reset(self):
        self.__myIsRecording = False
        self.__myNowRecording = False
        self.__myIsSavingHTML = False
        self.__myFrameBasename = None
        self.__myHtmlName = None


    def __getShotByName( self, shotName ):
        for shot in theApplication.getCurrentDocument.getShots().shots:
            if shot.getName() == shotName:
                return shot
        return None


    def SHOT_SET_RECORDING_MODE( self, message ):
        ( self.__myIsRecording, isSlideRecording ) = message.data


    def SAVE_SHOT_AS_MOVIE_AT_RESOLUTION( self, message ):
        ( frameWidth, frameHeight, fps, outputType, 
          SWAALevel, splitShots, self.__myFrameBasename, shotName, compression, rtquality ) = message.data

        if outputType == 0 and splitShots:
            self.__myIsSavingHTML = True


    def SHOT_PLAY_ALL( self, message ):
        if not self.__myIsSavingHTML or \
           not self.__myIsRecording or \
           not theApplication.getCurrentDocument.getShots():
            return

        self.__myNowRecording = True

        dir = os.path.dirname(self.__myFrameBasename)
        self.__myHeaderName = os.path.join(dir,"SaveMovieShotHeader.html")
        self.__myBodyName = os.path.join(dir,"SaveMovieShotBody.html")
        self.__myFooterName = os.path.join(dir,"SaveMovieShotFooter.html")
        self.__myHtmlName = self.__myFrameBasename + ".html"

        if os.path.isfile(self.__myHeaderName) and \
           os.path.isfile(self.__myFooterName) and \
           os.path.isfile(self.__myBodyName):

            hf = open(self.__myHeaderName,'r')
            hfd = hf.read()
            hf.close()

            f = open(self.__myHtmlName,'w')
            f.write(hfd)
            f.close()
        else:
            self.__myIsSavingHTML = False
            self.__myHtmlName = None


    def SHOT_PLAYING_DONE( self, message ):
        if not self.__myNowRecording:
            return

        ff = open(self.__myFooterName,'r')
        ffd = ff.read()
        ff.close()

        f = open(self.__myHtmlName,'a')
        f.write(ffd)
        f.close()

        self.reset()


    def SHOT_STARTED( self, message ):
        if not self.__myNowRecording:
            return

        (shotName,) = message.data
        shot = self.__getShotByName( shotName )
        thumbnail = "SHOT_THUMBNAIL"
        if shot:
            thumbnail = shot.getThumbnailImage("Keyframe")

        # Only works with 
        mn = os.path.basename(self.__myFrameBasename + (" (%s)" % (shotName)) + ".avi")

        bf = open(self.__myBodyName,'r')
        bfd = [ bfi.replace('SHOT_NAME',shotName).replace('SHOT_MOVIE',mn).replace('SHOT_THUMBNAIL',thumbnail)
                for bfi in bf.readlines() ]
        bf.close()

        f = open(self.__myHtmlName,'a')
        f.writelines(bfd)
        f.close()


def info():
    customInfo = CustomInfo()
    customInfo.vendor = 'Autodesk'
    customInfo.version = '3.0'
    customInfo.api = '2013'
    customInfo.shortInfo = "Create an html file with the movie file."
    customInfo.longInfo = \
"""If you save movie from shots in a directory that contains files SaveMovieShotHeader.html, \
SaveMovieShotBody.html and SaveMovieShotFooter.html, an html file will be created with the movie \
file (same name, html extension) which will be made of:

SaveMovieShotHeader.html
SaveMovieShotBody.html with some changes for each shot (see below)
SaveMovieShotFooter.html

SaveMovieShotBody.html has:
* all occurrences of SHOT_NAME replaced by the actual shot name;
* all occurrences of SHOT_THUMBNAIL replaced by a pointer to the location of the shot thumbnail image;
* all occurrences of SHOT_MOVIE replaced by the pointer to the movie.
"""
    return customInfo