//--------------------------------------------------------------- String EffectTextureExporter::exportImage ( const MObject &texture ) { // Retrieve the texture filename MFnDependencyNode textureNode ( texture ); MString mayaName = textureNode.name(); MPlug filenamePlug = textureNode.findPlug ( ATTR_FILE_TEXTURE_NAME ); // Get the maya image id. String mayaImageId = DocumentExporter::mayaNameToColladaName ( textureNode.name() ); // Generate a COLLADA id for the new light object String colladaImageId; // Check if there is an extra attribute "colladaId" and use this as export id. MString attributeValue; DagHelper::getPlugValue ( texture, COLLADA_ID_ATTRIBUTE_NAME, attributeValue ); if ( attributeValue != EMPTY_CSTRING ) { // Generate a valid collada name, if necessary. colladaImageId = mDocumentExporter->mayaNameToColladaName ( attributeValue, false ); } else { // Generate a COLLADA id for the new light object colladaImageId = DocumentExporter::mayaNameToColladaName ( textureNode.name() ); } // Make the id unique and store it in a map for refernences. colladaImageId = mImageIdList.addId ( colladaImageId ); mMayaIdColladaImageId [mayaImageId] = colladaImageId; // Get the maya filename with the path to the file. MString mayaFileName; filenamePlug.getValue ( mayaFileName ); if ( mayaFileName.length () == 0 ) return NULL; String sourceFile = mayaFileName.asChar (); COLLADASW::URI sourceFileUri ( COLLADASW::URI::nativePathToUri ( sourceFile ) ); if ( sourceFileUri.getScheme ().empty () ) sourceFileUri.setScheme ( COLLADASW::URI::SCHEME_FILE ); COLLADASW::Image* colladaImage = exportImage ( mayaImageId, colladaImageId, sourceFileUri ); if ( colladaImage == NULL ) return NULL; // Export the node type, because PSD textures don't behave the same as File textures. String nodeType = texture.hasFn ( MFn::kPsdFileTexture ) ? MAYA_TEXTURE_PSDTEXTURE : MAYA_TEXTURE_FILETEXTURE; colladaImage->addExtraTechniqueParameter ( PROFILE_MAYA, MAYA_TEXTURE_NODETYPE, nodeType ); // Export whether this image is in fact an image sequence MPlug imgSeqPlug = textureNode.findPlug ( ATTR_IMAGE_SEQUENCE ); bool isImgSeq = false; imgSeqPlug.getValue ( isImgSeq ); colladaImage->addExtraTechniqueParameter ( PROFILE_MAYA, MAYA_TEXTURE_IMAGE_SEQUENCE, isImgSeq ); return colladaImage->getImageId(); }
// ------------------------------- COLLADASW::Image* EffectTextureExporter::exportImage ( const String& mayaImageId, const String& colladaImageId, const COLLADASW::URI& sourceUri ) { // Get the file name and the URI COLLADASW::URI fullFileNameURI; bool sourceFileExist = getTextureFileInfos ( sourceUri, fullFileNameURI ); String fullFileName = fullFileNameURI.toNativePath (); // Have we seen this texture node before? ImageMap::iterator exportedImagesIter = mExportedImageMap.find ( fullFileName ); if ( exportedImagesIter != mExportedImageMap.end() ) { COLLADASW::Image* colladaImage = ( *exportedImagesIter ).second; return colladaImage; } // Check, if we should copy the texture to the destination folder. if ( ExportOptions::copyTextures() ) { // Get the target file from source file. COLLADASW::URI targetUri = createTargetURI ( sourceUri ); if ( !exists ( sourceUri.toNativePath() ) ) { String message = "The source texture file doesn't exist! Filename = " + sourceUri.toNativePath(); std::cerr << message << std::endl; } else { // Copy the texture, if it isn't already there... if ( !exists( targetUri.toNativePath () ) ) { try { // Create the target directory, if necessary. // Note: some systems (window$) requires the string to be // enclosed in quotes when a space is present. COLLADASW::URI targetPathUri ( targetUri.getPathDir() ); create_directory ( targetPathUri.toNativePath() ); // Throws: basic_filesystem_error<Path> if // from_fp.empty() || to_fp.empty() ||!exists(from_fp) || !is_regular(from_fp) || exists(to_fp) copy_file ( path ( sourceUri.toNativePath() ), path ( targetUri.toNativePath() ) ); } catch ( std::exception ex ) { String message = "Could not successful create directory and copy file: " + sourceUri.toNativePath(); MGlobal::displayError( message.c_str() ); std::cerr << "[ERROR] Could not copy file " << sourceUri.toNativePath() << std::endl; } } } } // Create a new image structure COLLADASW::Image* colladaImage = new COLLADASW::Image ( fullFileNameURI, colladaImageId, mayaImageId ); // Export the original maya name. colladaImage->addExtraTechniqueParameter ( PROFILE_MAYA, PARAMETER_MAYA_ID, mayaImageId ); // Add this texture to our list of exported images mExportedImageMap[ fullFileName ] = colladaImage; return colladaImage; }