#include <iostream.h>
#include <stdlib.h>

#include <maya/MPxCommand.h>
#include <maya/MFnPlugin.h>
#include <maya/MArgList.h>
#include <maya/MItDag.h>
#include <maya/MGlobal.h>
#include <maya/MSelectionList.h>
#include <maya/MItSelectionList.h>
#include <maya/MFnDagNode.h>
#include <maya/MDGModifier.h>
#include <maya/MCommandResult.h>
#include <maya/MDagPath.h>
#include <maya/MArgDatabase.h>
#include <maya/MString.h>
#include <maya/MStringArray.h>
#include <maya/MPlug.h>
#include <maya/MPlugArray.h>
#include <maya/MFnLambertShader.h>
#include <maya/MColor.h>


class preSmooth : public MPxCommand
{
public:
			preSmooth() {};
	virtual	~preSmooth();

	static void*	creator();
	MStatus	doIt ( const MArgList& args );
	MObject	newTransform;
	MDGModifier	dgModifier;
};


preSmooth::~preSmooth(){}


void* preSmooth::creator()
{
	return new preSmooth();
}


MStatus preSmooth::doIt( const MArgList& args )
{
	MStatus		status;
	MObject		node;
	MFnDependencyNode	depFn;
	MDagPath		dagPath;
	MString		cmd, smoothObjName, smoothShape, smoothFacet;
	MString		shapeName, shapeType, connectSG;
	MStringArray		resultCmd;=09

	MString version =3D MGlobal::mayaVersion();
=09
	MSelectionList	sList;
	MGlobal::getActiveSelectionList( sList );
	MItSelectionList	itsl( sList );

	for ( ; !itsl.isDone(); itsl.next() )=20
	{
		itsl.getDagPath( dagPath );
		MFnDagNode	dagFn( dagPath, &status );
		if ( !status )
		{
			MGlobal::displayError( "Can't!!" );
			return status;
		}

		itsl.getDependNode( node );
		depFn.setObject( node );
		MString nodeName =3D depFn.name();
		MString nodeType =3D depFn.typeName();

		if ( nodeType=3D=3D"transform" ) {
			MGlobal::selectByName( nodeName );

			cmd =3D "listRelatives -s " + nodeName;
			MGlobal::executeCommand( cmd, resultCmd );
			shapeName =3D resultCmd[0];
		=09
			cmd =3D "nodeType " + shapeName;
			MGlobal::executeCommand( cmd, shapeType );
		=09
			if ( shapeType=3D=3D"mesh" )
			{
				smoothObjName =3D nodeName + "Smooth";
				MGlobal::selectByName( nodeName );

				cmd =3D "duplicate -n " + smoothObjName;
				MGlobal::executeCommand( cmd );

				cmd =3D "listRelatives -s " + smoothObjName;
				MGlobal::executeCommand( cmd, resultCmd );
				smoothShape =3D resultCmd[0];

				cmd =3D "connectAttr -f " + nodeName + ".outMesh " + smoothShape + =
".inMesh";
				MGlobal::executeCommand( cmd );

				cmd =3D "polySmooth -dv 1 -c 1 -ch 1 " + smoothShape;
				MGlobal::executeCommand( cmd, resultCmd );
				smoothFacet =3D resultCmd[0];

				if ( version=3D=3D"3.0" || version=3D=3D"30a" )=20
				{
					cmd =3D "setAttr " + smoothFacet + ".inputComponents";
					cmd +=3D " -type \"componentList\" 1 \"f[*]\"";
					MGlobal::executeCommand( cmd );				=09
				}
				else
				{
					cmd =3D "listConnections -s on -d off -t makeGroup " + smoothFacet;
					MGlobal::executeCommand( cmd, resultCmd );
					MString	inMakeGrp =3D resultCmd[0];
			=09
					cmd =3D "setAttr " + inMakeGrp + ".inputComponents";
					cmd +=3D " -type \"componentList\" 1 \"f[*]\"";
					MGlobal::executeCommand( cmd );
				}
			=09
				cmd =3D "connectionInfo -dfs " + shapeName + ".instObjGroups[0]";
				MGlobal::executeCommand( cmd, resultCmd );
				if ( resultCmd[0]!=3D"\0" )
				{
					for ( unsigned int h=3D0; h<resultCmd.length(); h++ )
					{
						connectSG =3D resultCmd[h];
						cmd =3D "disconnectAttr " + shapeName + ".instObjGroups[0] " + =
connectSG;
						MGlobal::executeCommand( cmd );
					}
				}
				else
				{
					cmd =3D "listRelatives -s " + smoothFacet;
					MGlobal::executeCommand( cmd, resultCmd );
					cmd =3D "connectAttr " + resultCmd[0] + ".instObjGroups[0] =
initialShadingGroup.dagSetMembers[0]";
					MGlobal::executeCommand( cmd );
				}

				cmd =3D "connectAttr " + nodeName + ".t " + smoothObjName + ".t";
				MGlobal::executeCommand( cmd );
				cmd =3D "connectAttr " + nodeName + ".r " + smoothObjName + ".r";
				MGlobal::executeCommand( cmd );
				cmd =3D "connectAttr " + nodeName + ".s " + smoothObjName + ".s";
				MGlobal::executeCommand( cmd );
				cmd =3D "connectAttr " + nodeName + ".sh " + smoothObjName + ".sh";
				MGlobal::executeCommand( cmd );
				cmd =3D "connectAttr " + nodeName + ".rp " + smoothObjName + ".rp";
				MGlobal::executeCommand( cmd );
				cmd =3D "connectAttr " + nodeName + ".rpt " + smoothObjName + =
".rpt";
				MGlobal::executeCommand( cmd );
				cmd =3D "connectAttr " + nodeName + ".sp " + smoothObjName + ".sp";
				MGlobal::executeCommand( cmd );
				cmd =3D "connectAttr " + nodeName + ".spt " + smoothObjName + =
".spt";
				MGlobal::executeCommand( cmd );
				cmd =3D "connectAttr " + nodeName + ".ro " + smoothObjName + ".ro";
				MGlobal::executeCommand( cmd );
				cmd =3D "connectAttr " + nodeName + ".ra " + smoothObjName + ".ra";
				MGlobal::executeCommand( cmd );

				MGlobal::unselectByName( smoothObjName );
				MGlobal::selectByName( nodeName );
			}
		}
	}
	return MS::kSuccess;
}


MStatus initializePlugin( MObject obj )
{
	MStatus   status;
	MFnPlugin plugin( obj, "K.Kakiuchi:Plug-in", "ver1.0", "Any");

	status =3D plugin.registerCommand( "preSmooth", preSmooth::creator );
	if (!status) {
		status.perror("registerCommand");
		return status;
	}
	return status;
}


MStatus uninitializePlugin( MObject obj)
{
	MStatus   status;
	MFnPlugin plugin( obj );

	status =3D plugin.deregisterCommand( "preSmooth" );
	if (!status) {
		status.perror("deregisterCommand");
		return status;
	}

	return status;
}


