1
/***************************************************************************
2
* Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
24
#include <Base/Interpreter.h>
26
#include "SoQtOffscreenRendererPy.h"
31
SoQtOffscreenRendererPy::SoQtOffscreenRendererPy(Py::PythonClassInstance* self, Py::Tuple& args, Py::Dict& kwds)
32
: Py::PythonClass<SoQtOffscreenRendererPy>(self, args, kwds), renderer(SbViewportRegion())
34
this->setViewportRegion(args);
37
SoQtOffscreenRendererPy::~SoQtOffscreenRendererPy() = default;
39
Py::Object SoQtOffscreenRendererPy::repr()
42
s << "<SoQtOffscreenRenderer at " << this << ">";
43
return Py::String(s.str());
46
Py::Object SoQtOffscreenRendererPy::setViewportRegion(const Py::Tuple& args)
49
if (!PyArg_ParseTuple(args.ptr(), "hh", &w, &h)) {
50
throw Py::Exception();
53
renderer.setViewportRegion(SbViewportRegion(w, h));
56
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, setViewportRegion)
58
Py::Object SoQtOffscreenRendererPy::getViewportRegion()
60
const SbViewportRegion& vpr = renderer.getViewportRegion();
61
SbVec2s size = vpr.getWindowSize();
63
return Py::TupleN(Py::Long(size[0]), Py::Long(size[1]));
65
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getViewportRegion)
67
Py::Object SoQtOffscreenRendererPy::setBackgroundColor(const Py::Tuple& args)
69
float r, g, b, a = 1.0f;
70
if (!PyArg_ParseTuple(args.ptr(), "fff|f", &r, &g, &b, &a)) {
71
throw Py::Exception();
74
renderer.setBackgroundColor(SbColor4f(r, g, b, a));
78
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, setBackgroundColor)
80
Py::Object SoQtOffscreenRendererPy::getBackgroundColor()
82
SbColor4f color = renderer.getBackgroundColor();
83
return Py::TupleN(Py::Float(color[0]), Py::Float(color[1]), Py::Float(color[2]), Py::Float(color[3]));
85
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getBackgroundColor)
87
Py::Object SoQtOffscreenRendererPy::setNumPasses(const Py::Tuple& args)
90
if (!PyArg_ParseTuple(args.ptr(), "i", &num)) {
91
throw Py::Exception();
94
renderer.setNumPasses(num);
98
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, setNumPasses)
100
Py::Object SoQtOffscreenRendererPy::getNumPasses()
102
return Py::Long(renderer.getNumPasses());
104
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getNumPasses)
106
Py::Object SoQtOffscreenRendererPy::setInternalTextureFormat(const Py::Tuple& args)
109
if (!PyArg_ParseTuple(args.ptr(), "I", &format)) {
110
throw Py::Exception();
113
renderer.setInternalTextureFormat(format);
117
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, setInternalTextureFormat)
119
Py::Object SoQtOffscreenRendererPy::getInternalTextureFormat()
121
return Py::Long(static_cast<unsigned long>(renderer.internalTextureFormat()));
123
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getInternalTextureFormat)
125
Py::Object SoQtOffscreenRendererPy::render(const Py::Tuple& args)
128
if (!PyArg_ParseTuple(args.ptr(), "O", &proxy)) {
129
throw Py::Exception();
134
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoNode *", proxy, &ptr, 0);
135
auto node = static_cast<SoNode*>(ptr);
138
ok = renderer.render(node);
140
return Py::Boolean(ok);
142
catch (const Base::Exception& e) {
144
throw Py::Exception();
147
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, render)
149
Py::Object SoQtOffscreenRendererPy::writeToImage(const Py::Tuple& args)
151
const char* filename;
152
if (!PyArg_ParseTuple(args.ptr(), "s", &filename)) {
153
throw Py::Exception();
157
renderer.writeToImage(img);
158
img.save(QString::fromUtf8(filename));
162
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, writeToImage)
164
Py::Object SoQtOffscreenRendererPy::getWriteImageFiletypeInfo()
166
QStringList list = renderer.getWriteImageFiletypeInfo();
167
Py::Tuple tuple(list.size());
169
for (const auto& item : list) {
170
tuple[index++] = Py::String(item.toStdString());
175
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getWriteImageFiletypeInfo)
177
void SoQtOffscreenRendererPy::init_type()
179
behaviors().name("Gui.SoQtOffscreenRenderer");
180
behaviors().doc("Python interface for SoQtOffscreenRenderer");
182
// you must have overwritten the virtual functions
183
behaviors().supportRepr();
185
PYCXX_ADD_VARARGS_METHOD(setViewportRegion, setViewportRegion, "setViewportRegion(int, int)");
186
PYCXX_ADD_NOARGS_METHOD(getViewportRegion, getViewportRegion, "getViewportRegion() -> tuple");
187
PYCXX_ADD_VARARGS_METHOD(setBackgroundColor, setBackgroundColor, "setBackgroundColor(float, float, float, [float])");
188
PYCXX_ADD_NOARGS_METHOD(getBackgroundColor, getBackgroundColor, "getBackgroundColor() -> tuple");
189
PYCXX_ADD_VARARGS_METHOD(setNumPasses, setNumPasses, "setNumPasses(int)");
190
PYCXX_ADD_NOARGS_METHOD(getNumPasses, getNumPasses, "getNumPasses() -> int");
191
PYCXX_ADD_VARARGS_METHOD(setInternalTextureFormat, setInternalTextureFormat, "setInternalTextureFormat(int)");
192
PYCXX_ADD_NOARGS_METHOD(getInternalTextureFormat, getInternalTextureFormat, "getInternalTextureFormat() -> int");
193
PYCXX_ADD_VARARGS_METHOD(render, render, "render(node)");
194
PYCXX_ADD_VARARGS_METHOD(writeToImage, writeToImage, "writeToImage(string)");
195
PYCXX_ADD_NOARGS_METHOD(getWriteImageFiletypeInfo, getWriteImageFiletypeInfo, "getWriteImageFiletypeInfo() -> tuple");
197
behaviors().readyType();