aboutsummaryrefslogtreecommitdiffstats
path: root/share/qtcreator/qml/qmlpuppet/qml2puppet/import3d/import3d.cpp
blob: 70857065eeedef03cb3fc9029b601371bcd7a2e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "import3d.h"

#ifdef IMPORT_QUICK3D_ASSETS
#include <QtQuick3DAssetImport/private/qssgassetimportmanager_p.h>
#endif

#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QScopedPointer>
#include <QTextStream>
#include <QTimer>

namespace Import3D
{

void import3D([[maybe_unused]] const QString &sourceAsset,
              [[maybe_unused]] const QString &outDir,
              [[maybe_unused]] const QString &options)
{
    QString errorStr;
#ifdef IMPORT_QUICK3D_ASSETS
    QScopedPointer importer {new QSSGAssetImportManager};

    QJsonParseError error;
    QJsonDocument optDoc = QJsonDocument::fromJson(options.toUtf8(), &error);

    if (!optDoc.isNull() && optDoc.isObject()) {
        QJsonObject optObj = optDoc.object();
#if (QT_VERSION >= QT_VERSION_CHECK(6, 4, 0))
        const auto &optionsMap = optObj;
#else
        const auto optionsMap = optObj.toVariantMap();
#endif // QT_VERSION >= 6.4.0
        if (importer->importFile(sourceAsset, outDir, optionsMap, &errorStr)
                != QSSGAssetImportManager::ImportState::Success) {
        }
    } else {
        errorStr = QObject::tr("Failed to parse import options: %1").arg(error.errorString());
    }
#endif
    if (!errorStr.isEmpty()) {
        qWarning() << __FUNCTION__ << "Failed to import asset:" << errorStr << outDir;

        // Write the error into a file in outDir to pass it to creator side
        QString errorFileName = outDir + "/__error.log";
        QFile file(errorFileName);
        if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
            QTextStream out(&file);
            out << errorStr;
            file.close();
        }
    }

    QTimer::singleShot(0, nullptr, []() {
        qApp->quit();
    });
}

}