renderViewRenderRegionCmd.cpp
#include <maya/MSimple.h>
#include <maya/MIOStream.h>
#include <maya/MRenderView.h>
#include <maya/M3dView.h>
#include <math.h>
#include <maya/MSyntax.h>
#include <maya/MArgDatabase.h>
class renderViewRenderRegion : public MPxCommand 
{                                                       
public:                                 
        renderViewRenderRegion() {};
        ~renderViewRenderRegion() {};
        virtual MStatus doIt ( const MArgList& );
        
        static void*    creator();
        
        static MSyntax  newSyntax();
        MStatus parseSyntax (MArgDatabase &argData);
        static const char * cmdName;
private:
        bool doNotClearBackground;                              
};                                                                                                      
static const char * kDoNotClearBackground               = "-b";
static const char * kDoNotClearBackgroundLong   = "-background";
const char * renderViewRenderRegion::cmdName = "renderViewRenderRegion";
void* renderViewRenderRegion::creator()                                 
{                                                                                                       
        return new renderViewRenderRegion;                                      
}                                                                                                       
MSyntax renderViewRenderRegion::newSyntax()
{
        MStatus status;
        MSyntax syntax;
        syntax.addFlag( kDoNotClearBackground, kDoNotClearBackgroundLong );
        CHECK_MSTATUS_AND_RETURN(status, syntax);
        return syntax;
}
MStatus renderViewRenderRegion::parseSyntax (MArgDatabase &argData)
{
        
        doNotClearBackground = argData.isFlagSet( kDoNotClearBackground );
        
        return MS::kSuccess;
}
MStatus initializePlugin( MObject obj )                 
{                                                                                                                       
        MFnPlugin       plugin( obj, PLUGIN_COMPANY, "4.5" );   
        MStatus         stat;                                                                           
        stat = plugin.registerCommand(  renderViewRenderRegion::cmdName,
                                                                        renderViewRenderRegion::creator,
                                                                        renderViewRenderRegion::newSyntax);     
        if ( !stat )                                                                                            
                stat.perror( "registerCommand" );                                                       
        return stat;                                                                                            
}                                                                                                                               
MStatus uninitializePlugin( MObject obj )                                               
{                                                                                                                               
        MFnPlugin       plugin( obj );                                                                  
        MStatus         stat;                                                                                   
        stat = plugin.deregisterCommand( renderViewRenderRegion::cmdName );                             
        if ( !stat )                                                                    
                stat.perror( "deregisterCommand" );                     
        return stat;                                                                    
}
RV_PIXEL evaluate(int x, int y)
{
        unsigned int distance = (unsigned int) sqrt(double((x*x) + (y*y)));
        RV_PIXEL pixel;
        
        pixel.b = 255.0f;               
        
        pixel.g = pixel.r = 155.0f + (distance % 20) * 5;
        pixel.a = 255.0f;
        return pixel;
}
MStatus renderViewRenderRegion::doIt( const MArgList& args )
{
        MStatus stat = MS::kSuccess;
        unsigned int resX = 640;
        unsigned int resY = 480;
        
        
        
        if (!MRenderView::doesRenderEditorExist())
        {
                setResult( "Cannot renderViewRenderRegion in batch render mode. "
                                   "Please run in interactive mode, "
                                   "so that the render editor exists." );
                return MS::kFailure;
        }
        
        MArgDatabase    argData( syntax(), args );
        parseSyntax( argData );
        
        
        
        
        M3dView curView = M3dView::active3dView();
        MDagPath camDagPath;
        curView.getCamera( camDagPath );
        cout<<"Region rendering camera"<<camDagPath.fullPathName().asChar()<<endl;
        if( MRenderView::setCurrentCamera( camDagPath ) != MS::kSuccess )
        {
                setResult( "renderViewRenderRegion: error occurred in setCurrentCamera." );
                return MS::kFailure;
        }
        
        
        unsigned int regionLeft, regionRight, regionBottom, regionTop;
        stat = MRenderView::getRenderRegion( regionLeft, regionRight, 
                                                                                 regionBottom, regionTop );
        if( stat != MS::kSuccess )
        {
                setResult( "renderViewRenderRegion: error occurred in getRenderRegion." );
                return MS::kFailure;
        }
        
        
        
        stat = MRenderView::startRegionRender( resX, resY, 
                                                                                   regionLeft, regionRight, 
                                                                                   regionBottom, regionTop,
                                                                                   doNotClearBackground );
        if( stat == MS::kSuccess )
        {
                cout<<"Rendering Region ("<<regionLeft<<","<<regionBottom
                        <<") -> ("<<regionRight<<","<<regionTop<<")"<<endl;
                unsigned int width = regionRight - regionLeft + 1;
                unsigned int height = regionTop - regionBottom + 1;
                unsigned int numPixels = width * height;
                unsigned int middleX = width / 2;
                unsigned int middleY = height / 2;
                
                
                RV_PIXEL* pixels = new RV_PIXEL[numPixels];
                
                
                
                for( unsigned int x = 0; x < width; x++ )
                {
                        for( unsigned int y = 0; y < height; y++ )
                        {
                                int index = y*width + x;
                                int xCoord = x - middleX;
                                int yCoord = y - middleY;
                                pixels[index] = evaluate( xCoord, yCoord );
                        }
                }
        
                
                
                stat = MRenderView::updatePixels( regionLeft, regionRight, 
                                                                                  regionBottom, regionTop, 
                                                                                  pixels );
                if( stat != MS::kSuccess )
                {
                        setResult( "renderViewRenderRegion: error occurred in updatePixels." );
                        return MS::kFailure;
                }
                
                
                stat = MRenderView::refresh( regionLeft, regionRight, 
                                                                         regionBottom, regionTop );
                if( stat != MS::kSuccess )
                {
                        setResult( "renderViewRenderRegion: error occurred in refresh." );
                        return MS::kFailure;
                }
        }
        else
        {
                setResult( "renderViewRenderRegion: error occurred in startRegionRender." );
                return MS::kFailure;
        }
        
        
        stat = MRenderView::endRender();
        if( stat != MS::kSuccess )
        {
                setResult( "renderViewRenderRegion: error occurred in endRender." );
                return MS::kFailure;
        }
        setResult( "renderViewRenderRegion completed." );
        return stat;
}