Do not create temporary directory in ModelBaker and copy model directly to the original output folder

This commit is contained in:
sabrina-shanman 2019-03-18 11:56:34 -07:00
parent c29b3a8c35
commit cf40ed953b
3 changed files with 30 additions and 59 deletions

View file

@ -50,38 +50,12 @@ ModelBaker::ModelBaker(const QUrl& inputModelURL, TextureBakerThreadGetter input
_textureThreadGetter(inputTextureThreadGetter), _textureThreadGetter(inputTextureThreadGetter),
_hasBeenBaked(hasBeenBaked) _hasBeenBaked(hasBeenBaked)
{ {
auto tempDir = PathUtils::generateTemporaryDir();
if (tempDir.isEmpty()) {
handleError("Failed to create a temporary directory.");
return;
}
_modelTempDir = tempDir;
_originalModelFilePath = _modelTempDir.filePath(_modelURL.fileName());
qDebug() << "Made temporary dir " << _modelTempDir;
qDebug() << "Origin file path: " << _originalModelFilePath;
{
auto bakedFilename = _modelURL.fileName(); auto bakedFilename = _modelURL.fileName();
if (!hasBeenBaked) { if (!hasBeenBaked) {
bakedFilename = bakedFilename.left(bakedFilename.lastIndexOf('.')); bakedFilename = bakedFilename.left(bakedFilename.lastIndexOf('.'));
bakedFilename += BAKED_FBX_EXTENSION; bakedFilename += BAKED_FBX_EXTENSION;
} }
_bakedModelURL = _bakedOutputDir + "/" + bakedFilename; _bakedModelURL = _bakedOutputDir + "/" + bakedFilename;
}
}
ModelBaker::~ModelBaker() {
if (_modelTempDir.exists()) {
if (!_modelTempDir.remove(_originalModelFilePath)) {
qCWarning(model_baking) << "Failed to remove temporary copy of model file:" << _originalModelFilePath;
}
if (!_modelTempDir.rmdir(".")) {
qCWarning(model_baking) << "Failed to remove temporary directory:" << _modelTempDir;
}
}
} }
void ModelBaker::setOutputURLSuffix(const QUrl& outputURLSuffix) { void ModelBaker::setOutputURLSuffix(const QUrl& outputURLSuffix) {
@ -136,7 +110,8 @@ void ModelBaker::initializeOutputDirs() {
} }
} }
if (QDir(_originalOutputDir).exists()) { QDir originalOutputDir { _originalOutputDir };
if (originalOutputDir.exists()) {
if (_mappingURL.isEmpty()) { if (_mappingURL.isEmpty()) {
qWarning() << "Output path" << _originalOutputDir << "already exists. Continuing."; qWarning() << "Output path" << _originalOutputDir << "already exists. Continuing.";
} }
@ -144,8 +119,16 @@ void ModelBaker::initializeOutputDirs() {
qCDebug(model_baking) << "Creating original output folder" << _originalOutputDir; qCDebug(model_baking) << "Creating original output folder" << _originalOutputDir;
if (!QDir().mkpath(_originalOutputDir)) { if (!QDir().mkpath(_originalOutputDir)) {
handleError("Failed to create original output folder " + _originalOutputDir); handleError("Failed to create original output folder " + _originalOutputDir);
return;
} }
} }
if (originalOutputDir.isReadable()) {
// The output directory is available. Use that to write/read the original model file
_originalOutputModelPath = originalOutputDir.filePath(_modelURL.fileName());
} else {
handleError("Unable to write to original output folder " + _originalOutputDir);
}
} }
void ModelBaker::saveSourceModel() { void ModelBaker::saveSourceModel() {
@ -154,7 +137,7 @@ void ModelBaker::saveSourceModel() {
// load up the local file // load up the local file
QFile localModelURL { _modelURL.toLocalFile() }; QFile localModelURL { _modelURL.toLocalFile() };
qDebug() << "Local file url: " << _modelURL << _modelURL.toString() << _modelURL.toLocalFile() << ", copying to: " << _originalModelFilePath; qDebug() << "Local file url: " << _modelURL << _modelURL.toString() << _modelURL.toLocalFile() << ", copying to: " << _originalOutputModelPath;
if (!localModelURL.exists()) { if (!localModelURL.exists()) {
//QMessageBox::warning(this, "Could not find " + _modelURL.toString(), ""); //QMessageBox::warning(this, "Could not find " + _modelURL.toString(), "");
@ -162,13 +145,7 @@ void ModelBaker::saveSourceModel() {
return; return;
} }
// make a copy in the output folder localModelURL.copy(_originalOutputModelPath);
if (!_originalOutputDir.isEmpty()) {
qDebug() << "Copying to: " << _originalOutputDir << "/" << _modelURL.fileName();
localModelURL.copy(_originalOutputDir + "/" + _modelURL.fileName());
}
localModelURL.copy(_originalModelFilePath);
// emit our signal to start the import of the model source copy // emit our signal to start the import of the model source copy
emit modelLoaded(); emit modelLoaded();
@ -199,13 +176,13 @@ void ModelBaker::handleModelNetworkReply() {
qCDebug(model_baking) << "Downloaded" << _modelURL; qCDebug(model_baking) << "Downloaded" << _modelURL;
// grab the contents of the reply and make a copy in the output folder // grab the contents of the reply and make a copy in the output folder
QFile copyOfOriginal(_originalModelFilePath); QFile copyOfOriginal(_originalOutputModelPath);
qDebug(model_baking) << "Writing copy of original model file to" << _originalModelFilePath << copyOfOriginal.fileName(); qDebug(model_baking) << "Writing copy of original model file to" << _originalOutputModelPath << copyOfOriginal.fileName();
if (!copyOfOriginal.open(QIODevice::WriteOnly)) { if (!copyOfOriginal.open(QIODevice::WriteOnly)) {
// add an error to the error list for this model stating that a duplicate of the original model could not be made // add an error to the error list for this model stating that a duplicate of the original model could not be made
handleError("Could not create copy of " + _modelURL.toString() + " (Failed to open " + _originalModelFilePath + ")"); handleError("Could not create copy of " + _modelURL.toString() + " (Failed to open " + _originalOutputModelPath + ")");
return; return;
} }
if (copyOfOriginal.write(requestReply->readAll()) == -1) { if (copyOfOriginal.write(requestReply->readAll()) == -1) {
@ -216,10 +193,6 @@ void ModelBaker::handleModelNetworkReply() {
// close that file now that we are done writing to it // close that file now that we are done writing to it
copyOfOriginal.close(); copyOfOriginal.close();
if (!_originalOutputDir.isEmpty()) {
copyOfOriginal.copy(_originalOutputDir + "/" + _modelURL.fileName());
}
// emit our signal to start the import of the model source copy // emit our signal to start the import of the model source copy
emit modelLoaded(); emit modelLoaded();
} else { } else {
@ -229,9 +202,9 @@ void ModelBaker::handleModelNetworkReply() {
} }
void ModelBaker::bakeSourceCopy() { void ModelBaker::bakeSourceCopy() {
QFile modelFile(_originalModelFilePath); QFile modelFile(_originalOutputModelPath);
if (!modelFile.open(QIODevice::ReadOnly)) { if (!modelFile.open(QIODevice::ReadOnly)) {
handleError("Error opening " + _originalModelFilePath + " for reading"); handleError("Error opening " + _originalOutputModelPath + " for reading");
return; return;
} }
hifi::ByteArray modelData = modelFile.readAll(); hifi::ByteArray modelData = modelFile.readAll();
@ -243,7 +216,7 @@ void ModelBaker::bakeSourceCopy() {
{ {
auto serializer = DependencyManager::get<ModelFormatRegistry>()->getSerializerForMediaType(modelData, _modelURL, ""); auto serializer = DependencyManager::get<ModelFormatRegistry>()->getSerializerForMediaType(modelData, _modelURL, "");
if (!serializer) { if (!serializer) {
handleError("Could not recognize file type of model file " + _originalModelFilePath); handleError("Could not recognize file type of model file " + _originalOutputModelPath);
return; return;
} }
hifi::VariantHash serializerMapping = _mapping; hifi::VariantHash serializerMapping = _mapping;

View file

@ -46,7 +46,6 @@ public:
ModelBaker(const QUrl& inputModelURL, TextureBakerThreadGetter inputTextureThreadGetter, ModelBaker(const QUrl& inputModelURL, TextureBakerThreadGetter inputTextureThreadGetter,
const QString& bakedOutputDirectory, const QString& originalOutputDirectory = "", bool hasBeenBaked = false); const QString& bakedOutputDirectory, const QString& originalOutputDirectory = "", bool hasBeenBaked = false);
virtual ~ModelBaker();
void setOutputURLSuffix(const QUrl& urlSuffix); void setOutputURLSuffix(const QUrl& urlSuffix);
void setMappingURL(const QUrl& mappingURL); void setMappingURL(const QUrl& mappingURL);
@ -86,10 +85,9 @@ protected:
QString _bakedOutputDir; QString _bakedOutputDir;
QString _originalOutputDir; QString _originalOutputDir;
TextureBakerThreadGetter _textureThreadGetter; TextureBakerThreadGetter _textureThreadGetter;
QString _originalOutputModelPath;
QString _outputMappingURL; QString _outputMappingURL;
QUrl _bakedModelURL; QUrl _bakedModelURL;
QDir _modelTempDir;
QString _originalModelFilePath;
protected slots: protected slots:
void handleModelNetworkReply(); void handleModelNetworkReply();

View file

@ -49,9 +49,9 @@ void FSTBaker::bakeSourceCopy() {
return; return;
} }
QFile fstFile(_originalModelFilePath); QFile fstFile(_originalOutputModelPath);
if (!fstFile.open(QIODevice::ReadOnly)) { if (!fstFile.open(QIODevice::ReadOnly)) {
handleError("Error opening " + _originalModelFilePath + " for reading"); handleError("Error opening " + _originalOutputModelPath + " for reading");
return; return;
} }
@ -60,25 +60,25 @@ void FSTBaker::bakeSourceCopy() {
auto filenameField = _mapping[FILENAME_FIELD].toString(); auto filenameField = _mapping[FILENAME_FIELD].toString();
if (filenameField.isEmpty()) { if (filenameField.isEmpty()) {
handleError("The '" + FILENAME_FIELD + "' property in the FST file '" + _originalModelFilePath + "' could not be found"); handleError("The '" + FILENAME_FIELD + "' property in the FST file '" + _originalOutputModelPath + "' could not be found");
return; return;
} }
auto modelURL = _mappingURL.adjusted(QUrl::RemoveFilename).resolved(filenameField); auto modelURL = _mappingURL.adjusted(QUrl::RemoveFilename).resolved(filenameField);
auto bakeableModelURL = getBakeableModelURL(modelURL); auto bakeableModelURL = getBakeableModelURL(modelURL);
if (bakeableModelURL.isEmpty()) { if (bakeableModelURL.isEmpty()) {
handleError("The '" + FILENAME_FIELD + "' property in the FST file '" + _originalModelFilePath + "' could not be resolved to a valid bakeable model url"); handleError("The '" + FILENAME_FIELD + "' property in the FST file '" + _originalOutputModelPath + "' could not be resolved to a valid bakeable model url");
return; return;
} }
auto baker = getModelBakerWithOutputDirectories(bakeableModelURL, _textureThreadGetter, _bakedOutputDir, _originalOutputDir); auto baker = getModelBakerWithOutputDirectories(bakeableModelURL, _textureThreadGetter, _bakedOutputDir, _originalOutputDir);
_modelBaker = std::unique_ptr<ModelBaker>(dynamic_cast<ModelBaker*>(baker.release())); _modelBaker = std::unique_ptr<ModelBaker>(dynamic_cast<ModelBaker*>(baker.release()));
if (!_modelBaker) { if (!_modelBaker) {
handleError("The model url '" + bakeableModelURL.toString() + "' from the FST file '" + _originalModelFilePath + "' (property: '" + FILENAME_FIELD + "') could not be used to initialize a valid model baker"); handleError("The model url '" + bakeableModelURL.toString() + "' from the FST file '" + _originalOutputModelPath + "' (property: '" + FILENAME_FIELD + "') could not be used to initialize a valid model baker");
return; return;
} }
if (dynamic_cast<FSTBaker*>(_modelBaker.get())) { if (dynamic_cast<FSTBaker*>(_modelBaker.get())) {
// Could be interesting, but for now let's just prevent infinite FST loops in the most straightforward way possible // Could be interesting, but for now let's just prevent infinite FST loops in the most straightforward way possible
handleError("The FST file '" + _originalModelFilePath + "' (property: '" + FILENAME_FIELD + "') references another FST file. FST chaining is not supported."); handleError("The FST file '" + _originalOutputModelPath + "' (property: '" + FILENAME_FIELD + "') references another FST file. FST chaining is not supported.");
return; return;
} }
_modelBaker->setMappingURL(_mappingURL); _modelBaker->setMappingURL(_mappingURL);