XSIPLUGINCALLBACK CStatus OutputImportTxt()
{
	// prepare the import.txt file
	CString strOut = CUtils::BuildPath(Get3DCoatParam( L"coatLocation" ).GetValue(), L"import.txt");

	std::ofstream tfw;
	tfw.open(strOut.GetAsciiString(), std::ios_base::out | std::ios_base::trunc );
	if (tfw.is_open())
	{
		CString objTmp = Get3DCoatParam( L"tempLocation" ).GetValue();
		tfw << objTmp.GetAsciiString();
		tfw << "\n";

		CString objOut = objTmp.GetSubString(0, objTmp.ReverseFindString(CUtils::Slash())+1);
		objOut += L"output.obj";

		tfw << objOut.GetAsciiString();
		tfw << "\n";

		int typePaint = Get3DCoatParam( L"typePaint" ).GetValue();

	//Paint mesh in 3D-Coat using per-pixel painting [ppp]
	//Paint mesh in 3D-Coat using microvertex painting [mv]
	//Paint mesh in 3D-Coat using Ptex [ptex]
	//Perform UV-mapping in 3D-Coat [uv]
	//Drop reference mesh to 3D-Coat [ref]
	//Drop retopo mesh as new layer in 3D-Coat [retopo]
	//Drop mesh in 3D-Coat as voxel object [vox] 
	//Drop mesh in 3D-Coat as new pen alpha [alpha]
	//Drop mesh in 3D-Coat as new merging primitive for voxels [prim]
        //Drop mesh in 3D-Coat as a curve profile [curv]
	//Drop mesh in 3D-Coat for Auto-retopology [autopo]

		CString strPaint = L"[";
		switch (typePaint)
		{
			case 0:
				strPaint += L"ppp";
				break;
			case 1:
				strPaint += L"mv";
				break;
			case 2:
				strPaint += L"ptex";
				break;
			case 3:
				strPaint += L"uv";
				break;
			case 4:
				strPaint += L"ref";
				break;
			case 5:
				strPaint += L"retopo";
				break;
			case 6:
				strPaint += L"vox";
				break;
			case 7:
				strPaint += L"alpha";
				break;
			case 8:
				strPaint += L"prim";
				break;
			case 9:
                		strPaint += L"curv";
				break;
			case 10:
				strPaint += L"autopo";
				break;
		}

		strPaint += L"]";

		tfw << strPaint.GetAsciiString();

		bool bExpSkipIm = Get3DCoatParam( L"bExpSkipImp" ).GetValue();
		if(bExpSkipIm)
		{
			tfw << "\n";
			tfw << "[SkipImport]";
		}

		bool bExpSkipExp = Get3DCoatParam( L"bExpSkipExp" ).GetValue();
		if(bExpSkipExp)
		{
			tfw << "\n";
			tfw << "[SkipExport]";
		}

		tfw.close();
	}

	return CStatus::OK;
}
XSIPLUGINCALLBACK CStatus OutputMaterials( Selection& in_sel )
{
	// prepare the mtl file
	Project prj = app.GetActiveProject();
	Scene scn = prj.GetActiveScene();
	CString tmpLocation = Get3DCoatParam( L"tempLocation" ).GetValue();

	ULONG npos = tmpLocation.ReverseFindString(L".");
	CString substr = tmpLocation.GetSubString(0, npos+1);
	CString strOut = substr + L"mtl";	

	//app.LogMessage(L"strOut:" + strOut);

	std::ofstream matfw;
	matfw.open(strOut.GetAsciiString(), std::ios_base::out | std::ios_base::trunc);
	if (matfw.is_open())
	{
		CRefArray tempMats;

		for(int i=0; i< in_sel.GetCount(); i++)
		{
			X3DObject xobj(in_sel.GetItem(i));

			// Get a geometry accessor from the selected object	
			Primitive prim = xobj.GetActivePrimitive();
			PolygonMesh mesh = prim.GetGeometry();
			if (!mesh.IsValid()) return CStatus::False;

			CGeometryAccessor ga = mesh.GetGeometryAccessor();

			// get the material objects used by the mesh
			CRefArray materials = ga.GetMaterials();

			for (LONG n=0; n < materials.GetCount(); n++)
			{
				bar.PutStatusText( L"materials" );

				Material mat(materials[n]);
				bool inMats = false;
				//app.LogMessage(CString(n) +L" : "+ CString(i)+ L" :" + mat.GetName());

				for(int m = 0; m < tempMats.GetCount(); m++)
				{
					Material tmat(tempMats[m]);
					if(mat.GetName() == tmat.GetName())
					{
						inMats = true;
						break;
					}
				}

				//app.LogMessage(CString(inMats));

				if(!inMats)
				{
					CString string = L"newmtl " + mat.GetName() + L"\n";
					matfw << string.GetAsciiString();

					Parameter surf = mat.GetParameters().GetItem(L"surface");
					Shader lShader(surf.GetSource());
					//app.LogMessage(L"shader: " + lShader.GetFullName());
					//app.LogMessage(L"shader: " + lShader.GetProgID());
					if ( lShader.GetProgID() == L"Softimage.material-phong.1" )
					{
						float r, g, b, a;

						lShader.GetColorParameterValue(L"ambient", r, g, b, a );
						CString ka = L"Ka " + FormatNumber(r) + L" " + FormatNumber(g) + L" " + FormatNumber(b);
						lShader.GetColorParameterValue(L"diffuse", r, g, b, a );
						CString kd = L"Kd " + FormatNumber(r) + L" " + FormatNumber(g) + L" " + FormatNumber(b);
						lShader.GetColorParameterValue(L"specular", r, g, b, a );
						CString ks = L"Ks " + FormatNumber(r) + L" " + FormatNumber(g) + L" " + FormatNumber(b);
						float ns = lShader.GetParameterValue(L"shiny");
						float d = 1.0f;
						CValue illum = 2;

						matfw << ka.GetAsciiString();
						matfw << "\n";
						matfw << kd.GetAsciiString();
						matfw << "\n";
						matfw << ks.GetAsciiString();
						matfw << "\n";
						matfw << "Ns ";
						matfw << FormatNumber(ns).GetAsciiString();
						matfw << "\n";
						matfw << "d ";
						matfw << FormatNumber(d).GetAsciiString();
						matfw << "\n";
						matfw << "illum ";
						matfw << illum.GetAsText().GetAsciiString();
						matfw << "\n";

						Parameter diff = lShader.GetParameters().GetItem(L"diffuse");
						Shader lImageD(diff.GetSource());

						if (lImageD.GetProgID() == L"Softimage.txt2d-image-explicit.1")
						{
							Parameter tex = lImageD.GetParameters().GetItem(L"tex");
							ImageClip2 lTextureD(tex.GetSource());

							//app.LogMessage( L"Found texture shader: " + lTexture.GetFullName() + L", Class: " + lTexture.GetClassIDName() + L", Type: " + lTexture.GetType() );
							app.LogMessage(L"texture GetFileName: " + lTextureD.GetFileName());
							matfw << "map_Kd ";
							matfw << lTextureD.GetFileName().GetAsciiString();
							matfw << "\n";
						}

						Parameter spec = lShader.GetParameters().GetItem(L"specular");
						Shader lImageS(spec.GetSource());

						if (lImageS.GetProgID() == L"Softimage.txt2d-image-explicit.1")
						{
							Parameter tex = lImageD.GetParameters().GetItem(L"tex");
							ImageClip2 lTextureS(tex.GetSource());

							//app.LogMessage( L"Found texture shader: " + lTexture.GetFullName() + L", Class: " + lTexture.GetClassIDName() + L", Type: " + lTexture.GetType() );
							//app.LogMessage(L"texture GetFileName: " + lTexture.GetFileName());
							matfw << "map_Ks ";
							matfw << lTextureS.GetFileName().GetAsciiString();
							matfw << "\n";
						}
					}
					tempMats.Add(mat);
					matfw << "\n";
					matfw << "\n";
				}
			}		
		}
		matfw.close();
	}
	return CStatus::OK;
}