| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 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. * |
| 10 |
* * |
| 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. * |
| 15 |
* * |
| 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 * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <Base/Interpreter.h> |
| 25 |
|
| 26 |
#include "SoQtOffscreenRendererPy.h" |
| 27 |
|
| 28 |
|
| 29 |
using namespace Gui; |
| 30 |
|
| 31 |
SoQtOffscreenRendererPy::SoQtOffscreenRendererPy( |
| 32 |
Py::PythonClassInstance* self, |
| 33 |
Py::Tuple& args, |
| 34 |
Py::Dict& kwds |
| 35 |
) |
| 36 |
: Py::PythonClass<SoQtOffscreenRendererPy>(self, args, kwds) |
| 37 |
, renderer(SbViewportRegion()) |
| 38 |
{ |
| 39 |
this->setViewportRegion(args); |
| 40 |
} |
| 41 |
|
| 42 |
SoQtOffscreenRendererPy::~SoQtOffscreenRendererPy() = default; |
| 43 |
|
| 44 |
Py::Object SoQtOffscreenRendererPy::repr() |
| 45 |
{ |
| 46 |
std::stringstream s; |
| 47 |
s << "<SoQtOffscreenRenderer at " << this << ">"; |
| 48 |
return Py::String(s.str()); |
| 49 |
} |
| 50 |
|
| 51 |
Py::Object SoQtOffscreenRendererPy::setViewportRegion(const Py::Tuple& args) |
| 52 |
{ |
| 53 |
short w, h; |
| 54 |
if (!PyArg_ParseTuple(args.ptr(), "hh", &w, &h)) { |
| 55 |
throw Py::Exception(); |
| 56 |
} |
| 57 |
|
| 58 |
renderer.setViewportRegion(SbViewportRegion(w, h)); |
| 59 |
return Py::None(); |
| 60 |
} |
| 61 |
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, setViewportRegion) |
| 62 |
|
| 63 |
Py::Object SoQtOffscreenRendererPy::getViewportRegion() |
| 64 |
{ |
| 65 |
const SbViewportRegion& vpr = renderer.getViewportRegion(); |
| 66 |
SbVec2s size = vpr.getWindowSize(); |
| 67 |
|
| 68 |
return Py::TupleN(Py::Long(size[0]), Py::Long(size[1])); |
| 69 |
} |
| 70 |
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getViewportRegion) |
| 71 |
|
| 72 |
Py::Object SoQtOffscreenRendererPy::setBackgroundColor(const Py::Tuple& args) |
| 73 |
{ |
| 74 |
float r, g, b, a = 1.0f; |
| 75 |
if (!PyArg_ParseTuple(args.ptr(), "fff|f", &r, &g, &b, &a)) { |
| 76 |
throw Py::Exception(); |
| 77 |
} |
| 78 |
|
| 79 |
renderer.setBackgroundColor(SbColor4f(r, g, b, a)); |
| 80 |
|
| 81 |
return Py::None(); |
| 82 |
} |
| 83 |
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, setBackgroundColor) |
| 84 |
|
| 85 |
Py::Object SoQtOffscreenRendererPy::getBackgroundColor() |
| 86 |
{ |
| 87 |
SbColor4f color = renderer.getBackgroundColor(); |
| 88 |
return Py::TupleN(Py::Float(color[0]), Py::Float(color[1]), Py::Float(color[2]), Py::Float(color[3])); |
| 89 |
} |
| 90 |
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getBackgroundColor) |
| 91 |
|
| 92 |
Py::Object SoQtOffscreenRendererPy::setNumPasses(const Py::Tuple& args) |
| 93 |
{ |
| 94 |
int num; |
| 95 |
if (!PyArg_ParseTuple(args.ptr(), "i", &num)) { |
| 96 |
throw Py::Exception(); |
| 97 |
} |
| 98 |
|
| 99 |
renderer.setNumPasses(num); |
| 100 |
|
| 101 |
return Py::None(); |
| 102 |
} |
| 103 |
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, setNumPasses) |
| 104 |
|
| 105 |
Py::Object SoQtOffscreenRendererPy::getNumPasses() |
| 106 |
{ |
| 107 |
return Py::Long(renderer.getNumPasses()); |
| 108 |
} |
| 109 |
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getNumPasses) |
| 110 |
|
| 111 |
Py::Object SoQtOffscreenRendererPy::setInternalTextureFormat(const Py::Tuple& args) |
| 112 |
{ |
| 113 |
unsigned int format; |
| 114 |
if (!PyArg_ParseTuple(args.ptr(), "I", &format)) { |
| 115 |
throw Py::Exception(); |
| 116 |
} |
| 117 |
|
| 118 |
renderer.setInternalTextureFormat(format); |
| 119 |
|
| 120 |
return Py::None(); |
| 121 |
} |
| 122 |
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, setInternalTextureFormat) |
| 123 |
|
| 124 |
Py::Object SoQtOffscreenRendererPy::getInternalTextureFormat() |
| 125 |
{ |
| 126 |
return Py::Long(static_cast<unsigned long>(renderer.internalTextureFormat())); |
| 127 |
} |
| 128 |
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getInternalTextureFormat) |
| 129 |
|
| 130 |
Py::Object SoQtOffscreenRendererPy::render(const Py::Tuple& args) |
| 131 |
{ |
| 132 |
PyObject* proxy; |
| 133 |
if (!PyArg_ParseTuple(args.ptr(), "O", &proxy)) { |
| 134 |
throw Py::Exception(); |
| 135 |
} |
| 136 |
|
| 137 |
try { |
| 138 |
void* ptr = nullptr; |
| 139 |
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoNode *", proxy, &ptr, 0); |
| 140 |
auto node = static_cast<SoNode*>(ptr); |
| 141 |
bool ok = false; |
| 142 |
if (node) { |
| 143 |
ok = renderer.render(node); |
| 144 |
} |
| 145 |
return Py::Boolean(ok); |
| 146 |
} |
| 147 |
catch (const Base::Exception& e) { |
| 148 |
e.setPyException(); |
| 149 |
throw Py::Exception(); |
| 150 |
} |
| 151 |
} |
| 152 |
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, render) |
| 153 |
|
| 154 |
Py::Object SoQtOffscreenRendererPy::writeToImage(const Py::Tuple& args) |
| 155 |
{ |
| 156 |
const char* filename; |
| 157 |
if (!PyArg_ParseTuple(args.ptr(), "s", &filename)) { |
| 158 |
throw Py::Exception(); |
| 159 |
} |
| 160 |
|
| 161 |
QImage img; |
| 162 |
renderer.writeToImage(img); |
| 163 |
img.save(QString::fromUtf8(filename)); |
| 164 |
|
| 165 |
return Py::None(); |
| 166 |
} |
| 167 |
PYCXX_VARARGS_METHOD_DECL(SoQtOffscreenRendererPy, writeToImage) |
| 168 |
|
| 169 |
Py::Object SoQtOffscreenRendererPy::getWriteImageFiletypeInfo() |
| 170 |
{ |
| 171 |
QStringList list = renderer.getWriteImageFiletypeInfo(); |
| 172 |
Py::Tuple tuple(list.size()); |
| 173 |
int index = 0; |
| 174 |
for (const auto& item : list) { |
| 175 |
tuple[index++] = Py::String(item.toStdString()); |
| 176 |
} |
| 177 |
|
| 178 |
return tuple; |
| 179 |
} |
| 180 |
PYCXX_NOARGS_METHOD_DECL(SoQtOffscreenRendererPy, getWriteImageFiletypeInfo) |
| 181 |
|
| 182 |
void SoQtOffscreenRendererPy::init_type() |
| 183 |
{ |
| 184 |
behaviors().name("Gui.SoQtOffscreenRenderer"); |
| 185 |
behaviors().doc("Python interface for SoQtOffscreenRenderer"); |
| 186 |
|
| 187 |
// you must have overwritten the virtual functions |
| 188 |
behaviors().supportRepr(); |
| 189 |
|
| 190 |
PYCXX_ADD_VARARGS_METHOD(setViewportRegion, setViewportRegion, "setViewportRegion(int, int)"); |
| 191 |
PYCXX_ADD_NOARGS_METHOD(getViewportRegion, getViewportRegion, "getViewportRegion() -> tuple"); |
| 192 |
PYCXX_ADD_VARARGS_METHOD( |
| 193 |
setBackgroundColor, |
| 194 |
setBackgroundColor, |
| 195 |
"setBackgroundColor(float, float, float, [float])" |
| 196 |
); |
| 197 |
PYCXX_ADD_NOARGS_METHOD(getBackgroundColor, getBackgroundColor, "getBackgroundColor() -> tuple"); |
| 198 |
PYCXX_ADD_VARARGS_METHOD(setNumPasses, setNumPasses, "setNumPasses(int)"); |
| 199 |
PYCXX_ADD_NOARGS_METHOD(getNumPasses, getNumPasses, "getNumPasses() -> int"); |
| 200 |
PYCXX_ADD_VARARGS_METHOD( |
| 201 |
setInternalTextureFormat, |
| 202 |
setInternalTextureFormat, |
| 203 |
"setInternalTextureFormat(int)" |
| 204 |
); |
| 205 |
PYCXX_ADD_NOARGS_METHOD( |
| 206 |
getInternalTextureFormat, |
| 207 |
getInternalTextureFormat, |
| 208 |
"getInternalTextureFormat() -> int" |
| 209 |
); |
| 210 |
PYCXX_ADD_VARARGS_METHOD(render, render, "render(node)"); |
| 211 |
PYCXX_ADD_VARARGS_METHOD(writeToImage, writeToImage, "writeToImage(string)"); |
| 212 |
PYCXX_ADD_NOARGS_METHOD( |
| 213 |
getWriteImageFiletypeInfo, |
| 214 |
getWriteImageFiletypeInfo, |
| 215 |
"getWriteImageFiletypeInfo() -> tuple" |
| 216 |
); |
| 217 |
|
| 218 |
behaviors().readyType(); |
| 219 |
} |