| 1 |
/* |
| 2 |
* Copyright (c) 2011-2022, The DART development contributors |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* The list of contributors can be found at: |
| 6 |
* https://github.com/dartsim/dart/blob/master/LICENSE |
| 7 |
* |
| 8 |
* This file is provided under the following "BSD-style" License: |
| 9 |
* Redistribution and use in source and binary forms, with or |
| 10 |
* without modification, are permitted provided that the following |
| 11 |
* conditions are met: |
| 12 |
* * Redistributions of source code must retain the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer. |
| 14 |
* * Redistributions in binary form must reproduce the above |
| 15 |
* copyright notice, this list of conditions and the following |
| 16 |
* disclaimer in the documentation and/or other materials provided |
| 17 |
* with the distribution. |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 |
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 26 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 27 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 29 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 |
* POSSIBILITY OF SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <dart/dart.hpp> |
| 34 |
#include <dart/gui/osg/osg.hpp> |
| 35 |
#include <pybind11/eigen.h> |
| 36 |
#include <pybind11/pybind11.h> |
| 37 |
|
| 38 |
PYBIND11_DECLARE_HOLDER_TYPE(T, ::osg::ref_ptr<T>, true); |
| 39 |
|
| 40 |
namespace py = pybind11; |
| 41 |
|
| 42 |
namespace dart { |
| 43 |
namespace python { |
| 44 |
|
| 45 |
void Viewer(py::module& m) |
| 46 |
{ |
| 47 |
::py::class_<osgViewer::View, ::osg::ref_ptr<osgViewer::View>>(m, "osgViewer") |
| 48 |
.def(::py::init<>()) |
| 49 |
.def( |
| 50 |
"addEventHandler", |
| 51 |
+[](osgViewer::View* self, osgGA::GUIEventHandler* eventHandler) { |
| 52 |
self->addEventHandler(eventHandler); |
| 53 |
}, |
| 54 |
::py::arg("eventHandler")); |
| 55 |
|
| 56 |
auto viewer |
| 57 |
= ::py::class_< |
| 58 |
dart::gui::osg::Viewer, |
| 59 |
osgViewer::View, |
| 60 |
dart::common::Subject, |
| 61 |
::osg::ref_ptr<dart::gui::osg::Viewer>>(m, "Viewer") |
| 62 |
.def(::py::init<>()) |
| 63 |
.def( |
| 64 |
::py::init([](const Eigen::Vector4f& clearColor) { |
| 65 |
return new ::dart::gui::osg::Viewer( |
| 66 |
gui::osg::eigToOsgVec4d(clearColor)); |
| 67 |
}), |
| 68 |
::py::arg("clearColor")) |
| 69 |
.def(::py::init<const osg::Vec4&>(), ::py::arg("clearColor")) |
| 70 |
.def( |
| 71 |
"captureScreen", |
| 72 |
+[](dart::gui::osg::Viewer* self, const std::string& filename) { |
| 73 |
self->captureScreen(filename); |
| 74 |
}, |
| 75 |
::py::arg("filename")) |
| 76 |
.def( |
| 77 |
"record", |
| 78 |
+[](dart::gui::osg::Viewer* self, |
| 79 |
const std::string& directory) { self->record(directory); }, |
| 80 |
::py::arg("directory")) |
| 81 |
.def( |
| 82 |
"record", |
| 83 |
+[](dart::gui::osg::Viewer* self, |
| 84 |
const std::string& directory, |
| 85 |
const std::string& prefix) { |
| 86 |
self->record(directory, prefix); |
| 87 |
}, |
| 88 |
::py::arg("directory"), |
| 89 |
::py::arg("prefix")) |
| 90 |
.def( |
| 91 |
"record", |
| 92 |
+[](dart::gui::osg::Viewer* self, |
| 93 |
const std::string& directory, |
| 94 |
const std::string& prefix, |
| 95 |
bool restart) { self->record(directory, prefix, restart); }, |
| 96 |
::py::arg("directory"), |
| 97 |
::py::arg("prefix"), |
| 98 |
::py::arg("restart")) |
| 99 |
.def( |
| 100 |
"record", |
| 101 |
+[](dart::gui::osg::Viewer* self, |
| 102 |
const std::string& directory, |
| 103 |
const std::string& prefix, |
| 104 |
bool restart, |
| 105 |
std::size_t digits) { |
| 106 |
self->record(directory, prefix, restart, digits); |
| 107 |
}, |
| 108 |
::py::arg("directory"), |
| 109 |
::py::arg("prefix"), |
| 110 |
::py::arg("restart"), |
| 111 |
::py::arg("digits")) |
| 112 |
.def( |
| 113 |
"pauseRecording", |
| 114 |
+[](dart::gui::osg::Viewer* self) { self->pauseRecording(); }) |
| 115 |
.def( |
| 116 |
"isRecording", |
| 117 |
+[](const dart::gui::osg::Viewer* self) -> bool { |
| 118 |
return self->isRecording(); |
| 119 |
}) |
| 120 |
.def( |
| 121 |
"switchDefaultEventHandler", |
| 122 |
+[](dart::gui::osg::Viewer* self, bool on) { |
| 123 |
self->switchDefaultEventHandler(on); |
| 124 |
}, |
| 125 |
::py::arg("on")) |
| 126 |
.def( |
| 127 |
"switchHeadlights", |
| 128 |
+[](dart::gui::osg::Viewer* self, bool on) { |
| 129 |
self->switchHeadlights(on); |
| 130 |
}, |
| 131 |
::py::arg("on")) |
| 132 |
.def( |
| 133 |
"checkHeadlights", |
| 134 |
+[](const dart::gui::osg::Viewer* self) -> bool { |
| 135 |
return self->checkHeadlights(); |
| 136 |
}) |
| 137 |
.def( |
| 138 |
"setLightingMode", |
| 139 |
&dart::gui::osg::Viewer::setLightingMode, |
| 140 |
::py::arg("lightingMode")) |
| 141 |
.def("getLightingMode", &dart::gui::osg::Viewer::getLightingMode) |
| 142 |
.def( |
| 143 |
"addWorldNode", |
| 144 |
+[](dart::gui::osg::Viewer* self, |
| 145 |
dart::gui::osg::WorldNode* newWorldNode) { |
| 146 |
self->addWorldNode(newWorldNode); |
| 147 |
}, |
| 148 |
::py::arg("newWorldNode")) |
| 149 |
.def( |
| 150 |
"addWorldNode", |
| 151 |
+[](dart::gui::osg::Viewer* self, |
| 152 |
dart::gui::osg::WorldNode* newWorldNode, |
| 153 |
bool active) { self->addWorldNode(newWorldNode, active); }, |
| 154 |
::py::arg("newWorldNode"), |
| 155 |
::py::arg("active")) |
| 156 |
.def( |
| 157 |
"removeWorldNode", |
| 158 |
+[](dart::gui::osg::Viewer* self, |
| 159 |
dart::gui::osg::WorldNode* oldWorldNode) { |
| 160 |
self->removeWorldNode(oldWorldNode); |
| 161 |
}, |
| 162 |
::py::arg("oldWorldNode")) |
| 163 |
.def( |
| 164 |
"removeWorldNode", |
| 165 |
+[](dart::gui::osg::Viewer* self, |
| 166 |
std::shared_ptr<dart::simulation::World> oldWorld) { |
| 167 |
self->removeWorldNode(oldWorld); |
| 168 |
}, |
| 169 |
::py::arg("oldWorld")) |
| 170 |
.def( |
| 171 |
"addAttachment", |
| 172 |
+[](dart::gui::osg::Viewer* self, |
| 173 |
dart::gui::osg::ViewerAttachment* attachment) { |
| 174 |
self->addAttachment(attachment); |
| 175 |
}, |
| 176 |
::py::arg("attachment")) |
| 177 |
.def( |
| 178 |
"removeAttachment", |
| 179 |
+[](dart::gui::osg::Viewer* self, |
| 180 |
dart::gui::osg::ViewerAttachment* attachment) { |
| 181 |
self->removeAttachment(attachment); |
| 182 |
}, |
| 183 |
::py::arg("attachment")) |
| 184 |
.def( |
| 185 |
"setupDefaultLights", |
| 186 |
+[](dart::gui::osg::Viewer* self) { |
| 187 |
self->setupDefaultLights(); |
| 188 |
}) |
| 189 |
.def( |
| 190 |
"setUpwardsDirection", |
| 191 |
+[](dart::gui::osg::Viewer* self, const osg::Vec3& up) { |
| 192 |
self->setUpwardsDirection(up); |
| 193 |
}, |
| 194 |
::py::arg("up")) |
| 195 |
.def( |
| 196 |
"setUpwardsDirection", |
| 197 |
+[](dart::gui::osg::Viewer* self, const Eigen::Vector3d& up) { |
| 198 |
self->setUpwardsDirection(up); |
| 199 |
}, |
| 200 |
::py::arg("up")) |
| 201 |
.def( |
| 202 |
"setWorldNodeActive", |
| 203 |
+[](dart::gui::osg::Viewer* self, |
| 204 |
dart::gui::osg::WorldNode* node) { |
| 205 |
self->setWorldNodeActive(node); |
| 206 |
}, |
| 207 |
::py::arg("node")) |
| 208 |
.def( |
| 209 |
"setWorldNodeActive", |
| 210 |
+[](dart::gui::osg::Viewer* self, |
| 211 |
dart::gui::osg::WorldNode* node, |
| 212 |
bool active) { self->setWorldNodeActive(node, active); }, |
| 213 |
::py::arg("node"), |
| 214 |
::py::arg("active")) |
| 215 |
.def( |
| 216 |
"setWorldNodeActive", |
| 217 |
+[](dart::gui::osg::Viewer* self, |
| 218 |
std::shared_ptr<dart::simulation::World> world) { |
| 219 |
self->setWorldNodeActive(world); |
| 220 |
}, |
| 221 |
::py::arg("world")) |
| 222 |
.def( |
| 223 |
"setWorldNodeActive", |
| 224 |
+[](dart::gui::osg::Viewer* self, |
| 225 |
std::shared_ptr<dart::simulation::World> world, |
| 226 |
bool active) { self->setWorldNodeActive(world, active); }, |
| 227 |
::py::arg("world"), |
| 228 |
::py::arg("active")) |
| 229 |
.def( |
| 230 |
"simulate", |
| 231 |
+[](dart::gui::osg::Viewer* self, bool on) { |
| 232 |
self->simulate(on); |
| 233 |
}, |
| 234 |
::py::arg("on")) |
| 235 |
.def( |
| 236 |
"isSimulating", |
| 237 |
+[](const dart::gui::osg::Viewer* self) -> bool { |
| 238 |
return self->isSimulating(); |
| 239 |
}) |
| 240 |
.def( |
| 241 |
"allowSimulation", |
| 242 |
+[](dart::gui::osg::Viewer* self, bool allow) { |
| 243 |
self->allowSimulation(allow); |
| 244 |
}, |
| 245 |
::py::arg("allow")) |
| 246 |
.def( |
| 247 |
"isAllowingSimulation", |
| 248 |
+[](const dart::gui::osg::Viewer* self) -> bool { |
| 249 |
return self->isAllowingSimulation(); |
| 250 |
}) |
| 251 |
.def( |
| 252 |
"enableDragAndDrop", |
| 253 |
::py::overload_cast<dart::gui::osg::InteractiveFrame*>( |
| 254 |
&dart::gui::osg::Viewer::enableDragAndDrop), |
| 255 |
::py::return_value_policy::reference_internal, |
| 256 |
::py::arg("frame")) |
| 257 |
.def( |
| 258 |
"enableDragAndDrop", |
| 259 |
::py::overload_cast<dart::dynamics::SimpleFrame*>( |
| 260 |
&dart::gui::osg::Viewer::enableDragAndDrop), |
| 261 |
::py::return_value_policy::reference_internal, |
| 262 |
::py::arg("frame")) |
| 263 |
.def( |
| 264 |
"enableDragAndDrop", |
| 265 |
::py::overload_cast< |
| 266 |
dart::dynamics::SimpleFrame*, |
| 267 |
dart::dynamics::Shape*>( |
| 268 |
&dart::gui::osg::Viewer::enableDragAndDrop), |
| 269 |
::py::return_value_policy::reference_internal, |
| 270 |
::py::arg("frame"), |
| 271 |
::py::arg("shape")) |
| 272 |
.def( |
| 273 |
"enableDragAndDrop", |
| 274 |
::py::overload_cast<dart::dynamics::BodyNode*, bool, bool>( |
| 275 |
&dart::gui::osg::Viewer::enableDragAndDrop), |
| 276 |
::py::return_value_policy::reference_internal, |
| 277 |
::py::arg("bodyNode"), |
| 278 |
::py::arg_v("useExternalIK", true), |
| 279 |
::py::arg_v("useWholeBody", false)) |
| 280 |
.def( |
| 281 |
"enableDragAndDrop", |
| 282 |
::py::overload_cast<dart::dynamics::Entity*>( |
| 283 |
&dart::gui::osg::Viewer::enableDragAndDrop), |
| 284 |
::py::return_value_policy::reference_internal, |
| 285 |
::py::arg("entity")) |
| 286 |
.def( |
| 287 |
"disableDragAndDrop", |
| 288 |
::py::overload_cast<dart::gui::osg::InteractiveFrameDnD*>( |
| 289 |
&dart::gui::osg::Viewer::disableDragAndDrop), |
| 290 |
::py::arg("dnd")) |
| 291 |
.def( |
| 292 |
"disableDragAndDrop", |
| 293 |
::py::overload_cast<dart::gui::osg::SimpleFrameDnD*>( |
| 294 |
&dart::gui::osg::Viewer::disableDragAndDrop), |
| 295 |
::py::arg("dnd")) |
| 296 |
.def( |
| 297 |
"disableDragAndDrop", |
| 298 |
::py::overload_cast<dart::gui::osg::SimpleFrameShapeDnD*>( |
| 299 |
&dart::gui::osg::Viewer::disableDragAndDrop), |
| 300 |
::py::arg("dnd")) |
| 301 |
.def( |
| 302 |
"disableDragAndDrop", |
| 303 |
::py::overload_cast<dart::gui::osg::BodyNodeDnD*>( |
| 304 |
&dart::gui::osg::Viewer::disableDragAndDrop), |
| 305 |
::py::arg("dnd")) |
| 306 |
.def( |
| 307 |
"disableDragAndDrop", |
| 308 |
::py::overload_cast<dart::gui::osg::DragAndDrop*>( |
| 309 |
&dart::gui::osg::Viewer::disableDragAndDrop), |
| 310 |
::py::arg("dnd")) |
| 311 |
.def( |
| 312 |
"getInstructions", |
| 313 |
+[](const dart::gui::osg::Viewer* self) -> const std::string& { |
| 314 |
return self->getInstructions(); |
| 315 |
}, |
| 316 |
::py::return_value_policy::reference_internal) |
| 317 |
.def( |
| 318 |
"addInstructionText", |
| 319 |
+[](dart::gui::osg::Viewer* self, |
| 320 |
const std::string& _instruction) { |
| 321 |
self->addInstructionText(_instruction); |
| 322 |
}, |
| 323 |
::py::arg("instruction")) |
| 324 |
.def( |
| 325 |
"updateViewer", |
| 326 |
+[](dart::gui::osg::Viewer* self) { self->updateViewer(); }) |
| 327 |
.def( |
| 328 |
"updateDragAndDrops", |
| 329 |
+[](dart::gui::osg::Viewer* |
| 330 |
self) { self->updateDragAndDrops(); }) |
| 331 |
.def( |
| 332 |
"setVerticalFieldOfView", |
| 333 |
+[](dart::gui::osg::Viewer* self, double fov) { |
| 334 |
self->setVerticalFieldOfView(fov); |
| 335 |
}, |
| 336 |
::py::arg("fov")) |
| 337 |
.def( |
| 338 |
"getVerticalFieldOfView", |
| 339 |
+[](const dart::gui::osg::Viewer* self) -> double { |
| 340 |
return self->getVerticalFieldOfView(); |
| 341 |
}) |
| 342 |
.def( |
| 343 |
"run", |
| 344 |
+[](dart::gui::osg::Viewer* self) |
| 345 |
-> int { return self->run(); }) |
| 346 |
.def( |
| 347 |
"frame", +[](dart::gui::osg::Viewer* self) { self->frame(); }) |
| 348 |
.def( |
| 349 |
"frame", |
| 350 |
+[](dart::gui::osg::Viewer* self, double simulationTime) { |
| 351 |
self->frame(simulationTime); |
| 352 |
}) |
| 353 |
.def( |
| 354 |
"setUpViewInWindow", |
| 355 |
+[](dart::gui::osg::Viewer* self, |
| 356 |
int x, |
| 357 |
int y, |
| 358 |
int width, |
| 359 |
int height) { |
| 360 |
self->setUpViewInWindow(x, y, width, height); |
| 361 |
}) |
| 362 |
.def( |
| 363 |
"setCameraHomePosition", |
| 364 |
+[](dart::gui::osg::Viewer* self, |
| 365 |
const Eigen::Vector3d& eye, |
| 366 |
const Eigen::Vector3d& center, |
| 367 |
const Eigen::Vector3d& up) { |
| 368 |
self->getCameraManipulator()->setHomePosition( |
| 369 |
gui::osg::eigToOsgVec3(eye), |
| 370 |
gui::osg::eigToOsgVec3(center), |
| 371 |
gui::osg::eigToOsgVec3(up)); |
| 372 |
|
| 373 |
self->setCameraManipulator(self->getCameraManipulator()); |
| 374 |
}) |
| 375 |
.def( |
| 376 |
"setCameraMode", |
| 377 |
&gui::osg::Viewer::setCameraMode, |
| 378 |
py::arg("mode")) |
| 379 |
.def("getCameraMode", &gui::osg::Viewer::getCameraMode); |
| 380 |
|
| 381 |
::py::enum_<dart::gui::osg::Viewer::LightingMode>(viewer, "LightingMode") |
| 382 |
.value("NO_LIGHT", dart::gui::osg::Viewer::NO_LIGHT) |
| 383 |
.value("HEADLIGHT", dart::gui::osg::Viewer::HEADLIGHT) |
| 384 |
.value("SKY_LIGHT", dart::gui::osg::Viewer::SKY_LIGHT); |
| 385 |
|
| 386 |
::py::enum_<dart::gui::osg::CameraMode>(m, "CameraMode") |
| 387 |
.value("RGBA", dart::gui::osg::CameraMode::RGBA) |
| 388 |
.value("DEPTH", dart::gui::osg::CameraMode::DEPTH); |
| 389 |
|
| 390 |
} // namespace python |
| 391 |
|
| 392 |
} // namespace python |
| 393 |
} // namespace dart |