| 139 | } |
| 140 | |
| 141 | bool UsdSceneCacheFileFormat::WriteToFile( const SdfLayer& layer, const std::string& filePath, const std::string& comment, const FileFormatArguments& args) const |
| 142 | { |
| 143 | const auto perFrameWriteIt = args.find( "perFrameWrite" ); |
| 144 | const auto currentFrameIt = args.find( "currentFrame" ); |
| 145 | const auto firstFrameIt = args.find( "firstFrame" ); |
| 146 | const auto lastFrameIt = args.find( "lastFrame" ); |
| 147 | |
| 148 | auto frames = layer.ListAllTimeSamples(); |
| 149 | // only write current frame for per frame write |
| 150 | if ( perFrameWriteIt != args.end() && args.at( "perFrameWrite" ) == "1" ) |
| 151 | { |
| 152 | frames.clear(); |
| 153 | frames.insert( std::stof( args.at( "currentFrame" ) ) ); |
| 154 | } |
| 155 | // filter layer time sample inside frame range |
| 156 | else if ( firstFrameIt != args.end() && lastFrameIt != args.end() ) |
| 157 | { |
| 158 | std::set<double> filteredFrames; |
| 159 | const double firstFrame = std::stof( args.at( "firstFrame" ) ); |
| 160 | const double lastFrame = std::stof( args.at( "lastFrame" ) ); |
| 161 | for( const auto& frame : frames ) |
| 162 | { |
| 163 | if ( frame >= firstFrame && frame <= lastFrame ) |
| 164 | { |
| 165 | filteredFrames.insert( frame ); |
| 166 | } |
| 167 | } |
| 168 | frames = filteredFrames; |
| 169 | } |
| 170 | |
| 171 | UsdStageRefPtr stage = UsdStage::Open( layer.GetIdentifier() ); |
| 172 | ConstUSDScenePtr usdScene = new USDScene( stage, IndexedIO::Read ); |
| 173 | |
| 174 | auto fps = stage->GetTimeCodesPerSecond(); |
| 175 | SceneInterfacePtr outScene; |
| 176 | |
| 177 | outScene = SdfFileFormatSharedSceneWriters::get( filePath ); |
| 178 | if ( !outScene ) |
| 179 | { |
| 180 | IECore::msg( IECore::Msg::Error, "UsdSceneCacheFileFormat::WriteToFile", boost::format( "Invalid file path \"%s\" for layer \"%s\"." ) % filePath % layer.GetIdentifier() ); |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | SceneInterface::NameList childNames; |
| 185 | usdScene->childNames( childNames ); |
| 186 | for ( auto& childName : childNames ) |
| 187 | { |
| 188 | writeLocation( layer, usdScene, childName, outScene, fps, stage, frames ); |
| 189 | } |
| 190 | |
| 191 | // if we wrote the last frame of the layer, it's time to close the file |
| 192 | if ( currentFrameIt != args.end() && |
| 193 | lastFrameIt != args.end() && |
| 194 | perFrameWriteIt != args.end() && |
| 195 | args.at( "perFrameWrite" ) == "1" && |
| 196 | std::stof( args.at( "currentFrame" ) ) == std::stof( args.at( "lastFrame" ) ) |
| 197 | ) |
| 198 | { |
nothing calls this directly
no test coverage detected