Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • app
  • src
commandlineexporter.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16*/
17
18#include <QFileInfo>
19#include <QTextStream>
20
21#include "editor.h"
22#include "fileformat.h"
23#include "layercamera.h"
24#include "layermanager.h"
25#include "mainwindow2.h"
26#include "movieexporter.h"
27#include "object.h"
28#include "playbackmanager.h"
29
30#include "commandlineexporter.h"
31
32#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
33const auto qEndl = Qt::endl;
34#else
35const auto qEndl = endl;
36#endif
37
38CommandLineExporter::CommandLineExporter(Editor *editor) :
39 mEditor(editor),
40 mOut(stdout, QIODevice::WriteOnly),
41 mErr(stderr, QIODevice::WriteOnly)
42{
43
44}
45
46bool CommandLineExporter::process(const QString &inputPath,
47 const QStringList &outputPaths,
48 const QString &camera,
49 int width,
50 int height,
51 int startFrame,
52 int endFrame,
53 bool transparency)
54{
55 LayerManager *layerManager = mEditor->layers();
56
57 if(inputPath.isEmpty())
58 {
59 mErr << tr("Error: No input file specified. An input project file argument is required when output path(s) are specified.") << qEndl;
60 return false;
61 }
62
63 Status s = mEditor->openObject(inputPath, [](int){}, [](int){});
64 if (!s.ok())
65 {
66 mErr << qEndl << qEndl << s.title() << qEndl << qEndl;
67 mErr << s.description() << qEndl << qEndl;
68 mErr << s.details().str() << qEndl << qEndl;
69 return false;
70 }
71
72 LayerCamera *cameraLayer = nullptr;
73 if (!camera.isEmpty())
74 {
75 cameraLayer = dynamic_cast<LayerCamera*>(layerManager->findLayerByName(camera, Layer::CAMERA));
76 if (cameraLayer == nullptr)
77 {
78 mErr << tr("Warning: the specified camera layer %1 was not found, ignoring.").arg(camera) << qEndl;
79 }
80 }
81 if (cameraLayer == nullptr)
82 {
83 cameraLayer = dynamic_cast<LayerCamera*>(layerManager->getLastCameraLayer());
84 Q_ASSERT(cameraLayer);
85 }
86
87 if (width < 0)
88 {
89 width = cameraLayer->getViewRect().width();
90 }
91 if (height < 0)
92 {
93 height = cameraLayer->getViewRect().height();
94 }
95 QSize exportSize(width, height);
96
97 Q_ASSERT(startFrame >= 1);
98 if (endFrame < 0)
99 {
100 endFrame = layerManager->animationLength(endFrame < -1);
101 }
102
103 Q_ASSERT(!outputPaths.empty());
104 for (const QString& outputPath : outputPaths)
105 {
106 // Detect format
107 QString format = detectFormatByFileNameExtension(outputPath);
108 if (format.isNull())
109 {
110 mErr << tr("Warning: Output format is not specified or unsupported. Using PNG.", "Command line warning") << qEndl;
111 format = "PNG";
112 }
113
114 if (isMovieFormat(format))
115 {
116 exportMovie(outputPath, cameraLayer, exportSize, startFrame, endFrame, transparency);
117 continue;
118 }
119
120 exportImageSequence(outputPath, format, cameraLayer, exportSize, startFrame, endFrame, transparency);
121 }
122
123 return true;
124}
125
126void CommandLineExporter::exportMovie(const QString &outputPath,
127 const LayerCamera *cameraLayer,
128 const QSize &exportSize,
129 int startFrame,
130 int endFrame,
131 bool transparency)
132{
133 if (transparency)
134 {
135 mErr << tr("Warning: Transparency is not currently supported in movie files", "Command line warning") << qEndl;
136 }
137
138 mOut << tr("Exporting movie...", "Command line task progress") << qEndl;
139
140 ExportMovieDesc desc;
141 desc.strFileName = outputPath;
142 desc.startFrame = startFrame;
143 desc.endFrame = endFrame;
144 desc.fps = mEditor->playback()->fps();
145 desc.exportSize = exportSize;
146 desc.strCameraName = cameraLayer->name();
147
148 MovieExporter ex;
149 ex.run(mEditor->object(), desc, [](float, float){}, [](float){}, [](const QString &){});
150 mOut << tr("Done.", "Command line task done") << qEndl;
151}
152
153void CommandLineExporter::exportImageSequence(const QString &outputPath,
154 const QString &format,
155 const LayerCamera *cameraLayer,
156 const QSize &exportSize,
157 int startFrame,
158 int endFrame,
159 bool transparency)
160{
161 mOut << tr("Exporting image sequence...", "Command line task progress") << qEndl;
162 mEditor->object()->exportFrames(startFrame,
163 endFrame,
164 cameraLayer,
165 exportSize,
166 outputPath,
167 format,
168 transparency,
169 false,
170 "",
171 true,
172 nullptr,
173 0);
174 mOut << tr("Done.", "Command line task done") << qEndl;
175}
CommandLineExporter::process
bool process(const QString &inputPath, const QStringList &outputPaths, const QString &camera, int width, int height, int startFrame, int endFrame, bool transparency)
Exports a Pencil2D file according to the specified options.
Definition: commandlineexporter.cpp:46
CommandLineExporter::CommandLineExporter
CommandLineExporter(Editor *editor)
Creates a new exporter instance.
Definition: commandlineexporter.cpp:38
Editor
Definition: editor.h:71
LayerCamera
Definition: layercamera.h:30
LayerManager
Definition: layermanager.h:31
LayerManager::animationLength
int animationLength(bool includeSounds=true)
Get the length of current project.
Definition: layermanager.cpp:369
MovieExporter
Definition: movieexporter.h:44
MovieExporter::run
Status run(const Object *obj, const ExportMovieDesc &desc, std::function< void(float, float)> majorProgress, std::function< void(float)> minorProgress, std::function< void(QString)> progressMessage)
Begin exporting the movie described by exportDesc.
Definition: movieexporter.cpp:78
Status
Definition: pencilerror.h:40
QIODevice
QList::empty
bool empty() const const
QObject::tr
QString tr(const char *sourceText, const char *disambiguation, int n)
QRect::height
int height() const const
QRect::width
int width() const const
QSize
QString
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString::isEmpty
bool isEmpty() const const
QString::isNull
bool isNull() const const
QStringList
Qt::endl
QTextStream & endl(QTextStream &stream)
ExportMovieDesc
Definition: movieexporter.h:31
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39