// ------------------------------- 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; }
// ------------------------------------------------------------ bool EffectTextureExporter::getTextureFileInfos ( const COLLADASW::URI &sourceUri, COLLADASW::URI &fullFileNameURI ) { bool returnValue = true; // Check if the file exist! String sourceUriString = sourceUri.toNativePath(); if ( ExportOptions::relativePaths() ) { // Different filename and URI, if we copy the textures to the destination directory! if ( ExportOptions::copyTextures() ) { // Get the URI of the COLLADA file. String targetColladaFile = mDocumentExporter->getFilename(); COLLADASW::URI targetColladaUri ( COLLADASW::URI::nativePathToUri ( targetColladaFile ) ); if ( targetColladaUri.getScheme ().empty () ) targetColladaUri.setScheme ( COLLADASW::URI::SCHEME_FILE ); // Get the URI of the copied texture file. COLLADASW::URI textureUri = createTargetURI ( sourceUri ); // Get the texture URI relative to the COLLADA file URI. bool success = false; COLLADASW::URI targetUri = textureUri.getRelativeTo ( targetColladaUri, success ); if ( !success ) { String message = "Not able to generate a relative path from " + textureUri.getURIString() + " to " + targetColladaUri.getURIString() + ". An absolute path will be written! "; MGlobal::displayError ( message.c_str() ); targetUri = textureUri; returnValue = false; } // Get the file URI fullFileNameURI = targetUri; } else { // Get the URI of the COLLADA file. String targetColladaFile = mDocumentExporter->getFilename(); COLLADASW::URI targetColladaUri ( COLLADASW::URI::nativePathToUri ( targetColladaFile ) ); if ( targetColladaUri.getScheme ().empty () ) targetColladaUri.setScheme ( COLLADASW::URI::SCHEME_FILE ); // Get the texture URI relative to the COLLADA file URI. bool success = false; COLLADASW::URI targetUri = sourceUri.getRelativeTo ( targetColladaUri, success ); if ( !success ) { String message = "Not able to generate a relative path from " + sourceUri.getURIString() + " to " + targetColladaUri.getURIString() + ". An absolute path will be written! "; MGlobal::displayError ( message.c_str() ); targetUri = sourceUri; returnValue = false; } // Get the file URI fullFileNameURI = targetUri; } } else { // Different filename and URI, if we copy the textures to the destination directory! if ( ExportOptions::copyTextures() ) { // Get the texture URI relative to the COLLADA file URI. COLLADASW::URI targetUri = createTargetURI ( sourceUri ); // Get the file URI fullFileNameURI = targetUri; } else { // Get the file URI fullFileNameURI = sourceUri; } } return returnValue; }
// ------------------------------- 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 ( mDocumentExporter->getOptions().getCopyImages() ) { // Get the target file from source file. COLLADASW::URI targetUri = createTargetURI ( sourceUri ); bool exists = COLLADABU::Utils::fileExistsAndIsReadable( sourceUri.toNativePath() ); if ( !exists ) { String message = "The source texture file doesn't exist! Filename = " + sourceUri.toNativePath(); GetCOREInterface()->Log()->LogEntry( SYSLOG_ERROR, DISPLAY_DIALOG, _M( "Image export problem" ),_M( "%s\n" ), message.c_str() ); } else { // Copy the texture, if it isn't already there... exists = COLLADABU::Utils::fileExistsAndIsReadable( targetUri.toNativePath() ); if ( !exists ) { 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() ); exists = COLLADABU::Utils::createDirectoryIfNeeded( targetPathUri.toNativePath() ); if( exists ) { // Throws: basic_filesystem_error<Path> if // from_fp.empty() || to_fp.empty() ||!exists(from_fp) || !is_regular(from_fp) || exists(to_fp) exists = COLLADABU::Utils::copyFile ( sourceUri.toNativePath(), targetUri.toNativePath() ); } } catch ( std::exception ex ) { exists = false; } if( !exists ) { String message = "Could not successful create directory and copy file: " + sourceUri.toNativePath(); GetCOREInterface()->Log()->LogEntry( SYSLOG_ERROR, DISPLAY_DIALOG, _M( "Image export problem" ),_M( "%s\nCould not copy file %s\n" ), message.c_str(), sourceUri.toNativePath().c_str() ); } } } } // 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; }