Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
gltfimporter.cpp
Go to the documentation of this file.
1
// Copyright (C) 2026 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
#
include
"gltfimporter.h"
6
#
include
"gltfsceneconverter.h"
7
8
#
include
<
QtQuick3DAssetUtils
/
private
/
qssgqmlutilities_p
.
h
>
9
#
include
<
QtQuick3DAssetUtils
/
private
/
qssgsceneedit_p
.
h
>
10
#
include
<
QtQuick3DGltf
/
private
/
qssggltfparser_p
.
h
>
11
12
#
include
<
QtCore
/
qfile
.
h
>
13
#
include
<
QtCore
/
qfileinfo
.
h
>
14
#
include
<
QtCore
/
qjsondocument
.
h
>
15
#
include
<
QtQml
/
qqmlfile
.
h
>
16
17
QT_BEGIN_NAMESPACE
18
19
GltfImporter::GltfImporter()
20
{
21
QFile optionFile(QStringLiteral(
":/gltfimporter/options.json"
));
22
if
(optionFile.open(QIODevice::ReadOnly)) {
23
const
QByteArray options = optionFile.readAll();
24
m_options = QJsonDocument::fromJson(options).object();
25
}
26
}
27
28
QString GltfImporter::name()
const
29
{
30
return
QStringLiteral(
"gltf"
);
31
}
32
33
QStringList GltfImporter::inputExtensions()
const
34
{
35
if
(qEnvironmentVariableIsSet(
"QT_QUICK3D_DISABLE_NATIVE_GLTF"
))
36
return
{};
37
return
{ QStringLiteral(
"gltf"
), QStringLiteral(
"glb"
) };
38
}
39
40
QString GltfImporter::outputExtension()
const
41
{
42
return
QStringLiteral(
".qml"
);
43
}
44
45
QString GltfImporter::type()
const
46
{
47
return
QStringLiteral(
"Scene"
);
48
}
49
50
QString GltfImporter::typeDescription()
const
51
{
52
return
QObject::tr(
"glTF 2.0 Scene"
);
53
}
54
55
int
GltfImporter::priority()
const
56
{
57
// Above the Assimp plugin, which also claims gltf/glb
58
return
10;
59
}
60
61
QJsonObject GltfImporter::importOptions()
const
62
{
63
return
m_options;
64
}
65
66
static
QString
importImp
(
const
QUrl &url,
const
QJsonObject &options, QSSGSceneDesc::Scene &targetScene)
67
{
68
// Resolved the same way as in QSSGAssetImportManager
69
const
QString filePath = QQmlFile::isLocalFile(url) ? QQmlFile::urlToLocalFileOrQrc(url) : url.path();
70
71
const
QFileInfo sourceFile(filePath);
72
if
(!sourceFile.exists())
73
return
QLatin1String(
"File not found"
);
74
75
QSSGGltfParser parser;
76
QSSGGltfDocument document;
77
if
(!parser.parseFile(filePath, &document))
78
return
parser.errorMessage();
79
80
GltfSceneConverter
converter;
81
const
QString error = converter.convert(document, options, sourceFile, targetScene);
82
if
(!error.isEmpty())
83
return
error;
84
85
QSSGQmlUtilities::applyEdit(&targetScene, options);
86
87
return
QString();
88
}
89
90
QString GltfImporter::import(
const
QUrl &url,
const
QJsonObject &options, QSSGSceneDesc::Scene &scene)
91
{
92
return
importImp(url, options, scene);
93
}
94
95
QString GltfImporter::import(
const
QString &sourceFile,
const
QDir &savePath,
const
QJsonObject &options,
96
QStringList *generatedFiles)
97
{
98
QString errorString;
99
100
QSSGSceneDesc::Scene scene;
101
102
const
auto
sourceUrl = QUrl::fromLocalFile(sourceFile);
103
errorString = importImp(sourceUrl, options, scene);
104
105
if
(!errorString.isEmpty()) {
106
// Scene has no destructor
107
scene.cleanup();
108
return
errorString;
109
}
110
111
QFileInfo sourceFileInfo(sourceFile);
112
113
QString targetFileName = savePath.absolutePath() + QDir::separator() +
114
QSSGQmlUtilities::qmlComponentName(sourceFileInfo.completeBaseName()) +
115
QStringLiteral(
".qml"
);
116
QFile targetFile(targetFileName);
117
if
(!targetFile.open(QIODevice::WriteOnly)) {
118
errorString += QStringLiteral(
"Could not write to file: "
) + targetFileName;
119
}
else
{
120
QTextStream output(&targetFile);
121
QSSGQmlUtilities::writeQml(scene, output, savePath, options);
122
if
(generatedFiles)
123
generatedFiles->append(targetFileName);
124
}
125
scene.cleanup();
126
127
return
errorString;
128
}
129
130
QT_END_NAMESPACE
GltfSceneConverter
Definition
gltfsceneconverter.h:27
importImp
static QString importImp(const QUrl &url, const QJsonObject &options, QSSGSceneDesc::Scene &targetScene)
Definition
gltfimporter.cpp:66
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtquick3d
src
plugins
assetimporters
gltf
gltfimporter.cpp
Generated on
for Qt by
1.18.0