This repository has been archived by the owner on Jul 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binding_SofaModule.cpp
972 lines (791 loc) · 31.3 KB
/
Binding_SofaModule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include "PythonMacros.h"
#include <SofaPython/config.h>
#include "Binding_SofaModule.h"
#include "Binding_BaseObject.h"
#include "Binding_BaseState.h"
#include "Binding_Node.h"
#include "PythonFactory.h"
#include "PythonToSofa.inl"
#include <sofa/core/ObjectFactory.h>
#include <sofa/core/ObjectFactory.h>
#include <sofa/gui/BaseGUI.h>
#include <sofa/gui/BaseViewer.h>
#include <sofa/gui/GUIManager.h>
#include <sofa/helper/GenerateRigid.h>
#include <sofa/simulation/Simulation.h>
#include <sofa/simulation/SceneLoaderFactory.h>
#include <sofa/gui/ArgumentParser.h>
#include <sofa/core/CategoryLibrary.h>
#include <sofa/helper/AdvancedTimer.h>
#include <sofa/helper/logging/Messaging.h>
using sofa::helper::logging::ComponentInfo ;
using sofa::helper::logging::SofaComponentInfo;
#include <sofa/helper/Utils.h>
#include "SceneLoaderPY.h"
#include <sofa/helper/system/PluginManager.h>
using namespace sofa::helper;
using namespace sofa::core;
using namespace sofa::core::objectmodel;
using namespace sofa::type;
using namespace sofa::defaulttype;
using namespace sofa::component;
#include <sofa/simulation/Node.h>
using namespace sofa::simulation;
using sofa::helper::Utils ;
using sofa::helper::system::PluginManager ;
using sofa::simulation::Node ;
using sofa::simulation::SceneLoaderPY ;
using sofa::gui::BaseGUI ;
using sofa::gui::GUIManager ;
using sofa::gui::BaseViewer ;
using sofa::PythonFactory ;
#include <string>
#include <vector>
/// set the viewer resolution
static PyObject * Sofa_getSofaPythonVersion(PyObject * /*self*/, PyObject *)
{
return Py_BuildValue("s", SOFAPYTHON_VERSION_STR);
}
static PyObject * Sofa_createNode(PyObject * /*self*/, PyObject * args)
{
char *name;
if (!PyArg_ParseTuple(args, "s",&name)) {
return nullptr;
}
Node::SPtr node = Node::create( name );
return PythonFactory::toPython(node.get());
}
/// object factory
static PyObject * Sofa_createObject(PyObject * /*self*/, PyObject * args, PyObject * kw) {
char *type;
if (!PyArg_ParseTuple(args, "s", &type)) {
return nullptr;
}
SP_MESSAGE_DEPRECATED( "Sofa.createObject is deprecated; use Sofa.Node.createObject instead." )
/// temporarily, the name is set to the type name.
/// if a "name" parameter is provided, it will overwrite it.
BaseObjectDescription desc(type,type);
if (PyDict_Size(kw)>0)
{
PyObject* keys = PyDict_Keys(kw);
PyObject* values = PyDict_Values(kw);
for (int i=0; i<PyDict_Size(kw); i++)
{
desc.setAttribute(PyString_AsString(PyList_GetItem(keys,i)),PyString_AsString(PyList_GetItem(values,i)));
}
Py_DecRef(keys);
Py_DecRef(values);
}
BaseObject::SPtr obj = ObjectFactory::getInstance()->createObject(0,&desc);//.get();
if (obj==0)
{
SP_MESSAGE_ERROR( "createObject "<<desc.getName().c_str()<<" of type "<<desc.getAttribute("type","") )
PyErr_BadArgument();
return nullptr;
}
/// by default, it will always be at least a BaseObject...
return PythonFactory::toPython(obj.get());
}
static PyObject * Sofa_getObject(PyObject * /*self*/, PyObject * /*args*/)
{
// deprecated on date 2012/07/18
SP_MESSAGE_DEPRECATED( "Sofa.getObject(BaseContext,path) is deprecated. Please use BaseContext.getObject(path) instead." )
PyErr_BadArgument();
return nullptr;
}
static PyObject * Sofa_getChildNode(PyObject * /*self*/, PyObject * /*args*/)
{
// deprecated on date 2012/07/18
SP_MESSAGE_DEPRECATED( "Sofa.getChildNode(Node,path) is deprecated. Please use Node.getChild(path) instead." )
PyErr_BadArgument();
return nullptr;
}
/// send a text message to the GUI
static PyObject * Sofa_sendGUIMessage(PyObject * /*self*/, PyObject * args) {
char *msgType;
char *msgValue;
if (!PyArg_ParseTuple(args, "ss",&msgType,&msgValue)) {
return nullptr;
}
BaseGUI *gui = GUIManager::getGUI();
if (!gui)
{
SP_MESSAGE_ERROR( "sendGUIMessage("<<msgType<<","<<msgValue<<"): no GUI!" )
return Py_BuildValue("i",-1);
}
gui->sendMessage(msgType,msgValue);
return nullptr;
}
/// ask the GUI to save a screenshot
static PyObject * Sofa_saveScreenshot(PyObject * /*self*/, PyObject * args) {
char *filename;
if (!PyArg_ParseTuple(args, "s",&filename)) {
return nullptr;
}
BaseGUI *gui = GUIManager::getGUI();
if (!gui)
{
SP_MESSAGE_ERROR( "saveScreenshot("<<filename<<"): no GUI!" )
return Py_BuildValue("i",-1);
}
gui->saveScreenshot(filename);
return Py_BuildValue("i",0);
}
/// set the viewer resolution
static PyObject * Sofa_setViewerResolution(PyObject * /*self*/, PyObject * args) {
int width, height;
if (!PyArg_ParseTuple(args, "ii", &width, &height)) {
return nullptr;
}
BaseGUI *gui = GUIManager::getGUI();
if (!gui)
{
SP_MESSAGE_ERROR( "setViewerResolution("<<width<<","<<height<<"): no GUI!" )
return Py_BuildValue("i",-1);
}
gui->setViewerResolution(width,height);
return Py_BuildValue("i",0);
}
/// set the viewer resolution
static PyObject * Sofa_setViewerBackgroundColor(PyObject * /*self*/, PyObject * args)
{
float r = 0.0f, g = 0.0f, b = 0.0f;
sofa::type::RGBAColor color;
if (!PyArg_ParseTuple(args, "fff", &r, &g, &b)) {
return nullptr;
}
color[0] = r; color[1] = g; color[2] = b;
for (int i = 0; i < 3; ++i){
if (color[i] < 00.f || color[i] > 1.0) {
PyErr_BadArgument();
return nullptr;
}
}
color.set(r,g,b,1.0);
BaseGUI *gui = GUIManager::getGUI();
if (!gui)
{
SP_MESSAGE_ERROR( "setViewerBackgroundColor("<<r<<","<<g<<","<<b<<"): no GUI!" )
return Py_BuildValue("i",-1);
}
gui->setBackgroundColor(color);
return Py_BuildValue("i",0);
}
/// Check if a viewer is available
static PyObject * Sofa_hasViewer(PyObject* self, PyObject* args)
{
SOFA_UNUSED(self);
SOFA_UNUSED(args);
BaseGUI *gui = GUIManager::getGUI();
if (!gui)
{
SP_MESSAGE_ERROR( "hasViewer: no GUI!" )
return Py_BuildValue("i",-1);
}
return Py_BuildValue("i", gui->getViewer() != nullptr);
}
/// set the viewer camera
static PyObject * Sofa_setViewerCamera(PyObject * /*self*/, PyObject * args)
{
float px = 0.0f, py = 0.0f, pz = 0.0f;
float qx = 0.0f, qy = 0.0f, qz = 0.0f, qw = 1.0f;
if (!PyArg_ParseTuple(args, "fffffff", &px, &py, &pz, &qx, &qy, &qz, &qw)) {
return nullptr;
}
BaseGUI *gui = GUIManager::getGUI();
if (!gui)
{
SP_MESSAGE_ERROR( "setViewerCamera: no GUI!" )
return Py_BuildValue("i",-1);
}
BaseViewer * viewer = gui->getViewer();
if (!viewer)
{
SP_MESSAGE_ERROR( "setViewerCamera: no Viewer!" )
return Py_BuildValue("i",-1);
}
viewer->setView(sofa::type::Vector3(px,py,pz),sofa::type::Quat<SReal>(qx,qy,qz,qw));
return Py_BuildValue("i",0);
}
static PyObject * Sofa_getViewerCamera(PyObject * /*self*/, PyObject *) {
sofa::type::Vector3 pos;
sofa::type::Quat<SReal> orient;
BaseGUI *gui = GUIManager::getGUI();
if (!gui)
{
SP_MESSAGE_ERROR( "getViewerCamera: no GUI!" )
return Py_BuildValue("i",-1);
}
BaseViewer * viewer = gui->getViewer();
if (!viewer)
{
SP_MESSAGE_ERROR( "getViewerCamera: no Viewer! ")
return Py_BuildValue("i",-1);
}
viewer->getView(pos,orient);
return Py_BuildValue("fffffff",pos.x(),pos.y(),pos.z(),orient[0],orient[1],orient[2],orient[3]);
}
/// from a mesh, a density and a 3d scale, computes a mass, a center of mass, a
/// diagonal inertia matrix and an inertia rotation
static PyObject * Sofa_generateRigid(PyObject * /*self*/, PyObject * args) {
char* meshFilename;
double density;
double sx = 1, sy = 1, sz = 1;
double rx = 0, ry = 0, rz = 0;
if (!PyArg_ParseTuple(args, "sd|dddddd", &meshFilename, &density,
&sx, &sy, &sz,
&rx, &ry, &rz)) {
return nullptr;
}
sofa::helper::GenerateRigidInfo rigid;
if( !sofa::helper::generateRigid<sofa::defaulttype::Rigid3Mass>( rigid, meshFilename, density, Vector3(sx,sy,sz), Vector3(rx,ry,rz) ) )
exit(0);
return Py_BuildValue("ddddddddddd",rigid.mass
,rigid.com[0],rigid.com[1],rigid.com[2]
,rigid.inertia_diagonal[0],rigid.inertia_diagonal[1],rigid.inertia_diagonal[2]
,rigid.inertia_rotation[0],rigid.inertia_rotation[1],rigid.inertia_rotation[2],rigid.inertia_rotation[3]
);
}
/// save a sofa scene from python
static PyObject * Sofa_exportGraph(PyObject * /*self*/, PyObject * args) {
char* filename;
PyObject* pyNode;
if (!PyArg_ParseTuple(args, "Os", &pyNode, &filename)) {
return nullptr;
}
BaseNode* node = sofa::py::unwrap<BaseNode>( pyNode );
if (!node) {
SP_PYERR_SETSTRING_INVALIDTYPE("BaseNode*") ;
return nullptr;
}
getSimulation()->exportGraph( down_cast<Node>(node), filename );
return Py_BuildValue("i",0);
}
static PyObject * Sofa_updateVisual(PyObject * /*self*/, PyObject * args) {
PyObject* pyNode;
if (!PyArg_ParseTuple(args, "O", &pyNode)) {
return nullptr;
}
BaseNode* basenode = sofa::py::unwrap<BaseNode>( pyNode );
if (!basenode) {
SP_PYERR_SETSTRING_INVALIDTYPE("BaseNode*") ;
return nullptr;
}
Node* node = down_cast<Node>(basenode);
Simulation* simulation = getSimulation();
simulation->updateVisual(node);
return nullptr;
}
static const std::string s_emitter = "PythonScript";
template<class Action>
static PyObject* parse_emitter_message_then(PyObject* args, const Action& action) {
PyObject* py_emitter {nullptr};
PyObject* py_message {nullptr};
const size_t argSize = PyTuple_Size(args);
if( argSize == 1 ) {
/// no emitter
char* message;
if( !PyArg_ParseTuple(args, "s", &message) ) {
return NULL;
}
action(ComponentInfo::SPtr(new ComponentInfo(s_emitter)), message, PythonEnvironment::getPythonCallingPointAsFileInfo());
} else if( argSize == 2 ) {
/// SOURCE, "Message"
if( !PyArg_ParseTuple(args, "OO", &py_emitter, &py_message) ){
return NULL;
}
if( !PyString_Check(py_message) ){
PyErr_SetString(PyExc_TypeError, "The second parameter must be a string");
return NULL;
}
if( PyString_Check(py_emitter) ){
action(ComponentInfo::SPtr(new ComponentInfo(PyString_AsString(py_emitter))), PyString_AsString(py_message), PythonEnvironment::getPythonCallingPointAsFileInfo() );
}else if (PyObject_IsInstance(py_emitter, reinterpret_cast<PyObject*>(&SP_SOFAPYTYPEOBJECT(Base)))) {
Base* base=(((PySPtr<Base>*)py_emitter)->object).get();
action(ComponentInfo::SPtr(new SofaComponentInfo(base)), PyString_AsString(py_message), PythonEnvironment::getPythonCallingPointAsFileInfo() );
}else{
PyErr_SetString(PyExc_TypeError, "The first parameter must be a string or a Sofa.Base");
return NULL;
}
} else if( argSize == 3 ){
/// "Message", "FILENAME", LINENO
char* message;
char* filename;
int lineno;
if( !PyArg_ParseTuple(args, "ssi", &message, &filename, &lineno) ) {
return NULL;
}
action(ComponentInfo::SPtr(new ComponentInfo(s_emitter)), message, SOFA_FILE_INFO_COPIED_FROM(filename, lineno));
} else if (argSize == 4 ){
/// SOURCE, "Message", "FILENAME", LINENO
char* filename;
int lineno;
if( !PyArg_ParseTuple(args, "OOsi", &py_emitter, &py_message, &filename, &lineno) ){
return NULL;
}
if( !PyString_Check(py_message) ){
PyErr_SetString(PyExc_TypeError, "The second parameter must be a string");
return NULL;
}
if( PyString_Check(py_emitter) ){
action(ComponentInfo::SPtr(new ComponentInfo(PyString_AsString(py_emitter))),
PyString_AsString(py_message), SOFA_FILE_INFO_COPIED_FROM(filename, lineno));
}else if (PyObject_IsInstance(py_emitter, reinterpret_cast<PyObject*>(&SP_SOFAPYTYPEOBJECT(Base)))) {
Base* base=(((PySPtr<Base>*)py_emitter)->object).get();
action(ComponentInfo::SPtr(new SofaComponentInfo(base)),
PyString_AsString(py_message), SOFA_FILE_INFO_COPIED_FROM(filename, lineno));
}else{
PyErr_SetString(PyExc_TypeError, "The first parameter must be a string or a Sofa.Base");
return NULL;
}
}
Py_RETURN_NONE;
}
static PyObject * Sofa_msg_info(PyObject * /*self*/, PyObject * args) {
return parse_emitter_message_then(args, [](const ComponentInfo::SPtr& emitter, const char* message, const sofa::helper::logging::FileInfo::SPtr& fileinfo) {
msg_info(emitter) << message << fileinfo;
});
}
static PyObject * Sofa_msg_deprecated(PyObject * /*self*/, PyObject * args) {
return parse_emitter_message_then(args, [](const ComponentInfo::SPtr& emitter, const char* message, const sofa::helper::logging::FileInfo::SPtr& fileinfo) {
msg_deprecated(emitter) << message << fileinfo;
});
}
static PyObject * Sofa_msg_warning(PyObject * /*self*/, PyObject * args) {
return parse_emitter_message_then(args, [](const ComponentInfo::SPtr& emitter, const char* message, const sofa::helper::logging::FileInfo::SPtr& fileinfo) {
msg_warning(emitter) << message << fileinfo;
});
}
static PyObject * Sofa_msg_error(PyObject * /*self*/, PyObject * args) {
return parse_emitter_message_then(args, [](const ComponentInfo::SPtr& emitter, const char* message, const sofa::helper::logging::FileInfo::SPtr& fileinfo) {
msg_error(emitter) << message << fileinfo;
});
}
static PyObject * Sofa_msg_fatal(PyObject * /*self*/, PyObject * args) {
return parse_emitter_message_then(args, [](const ComponentInfo::SPtr& emitter, const char* message, const sofa::helper::logging::FileInfo::SPtr& fileinfo) {
msg_fatal(emitter) << message << fileinfo;
});
}
static PyObject * Sofa_loadScene(PyObject * /*self*/, PyObject * args)
{
char *filename;
if (!PyArg_ParseTuple(args, "s",&filename)) {
return nullptr;
}
if( sofa::helper::system::SetDirectory::GetFileName(filename).empty() || // no filename
sofa::helper::system::SetDirectory::GetExtension(filename).empty() ) // filename with no extension
return nullptr;
sofa::simulation::SceneLoader *loader = SceneLoaderFactory::getInstance()->getEntryFileName(filename);
if (loader)
{
const std::vector<std::string> sceneArgs = sofa::gui::ArgumentParser::extra_args();
Node::SPtr node = loader->load(filename, false, sceneArgs);
return PythonFactory::toPython(node.get());
}
SP_MESSAGE_ERROR( "Sofa_loadScene: extension ("
<< sofa::helper::system::SetDirectory::GetExtension(filename)<<") not handled" );
return nullptr;
}
static PyObject * Sofa_unload(PyObject * /*self*/, PyObject * args)
{
PyObject* pyNode;
if (!PyArg_ParseTuple(args, "O", &pyNode)) {
return nullptr;
}
Node* node = sofa::py::unwrap<Node>(pyNode);
if (!node) {
return nullptr;
}
sofa::simulation::getSimulation()->unload( node );
return nullptr;
}
static PyObject * Sofa_loadPythonSceneWithArguments(PyObject * /*self*/, PyObject * args)
{
size_t argSize = PyTuple_Size(args);
if( !argSize ) {
PyErr_SetString(PyExc_RuntimeError, "Sofa_loadPythonSceneWithArguments: should have at least a filename as arguments") ;
return nullptr;
}
char *filename = PyString_AsString(PyTuple_GetItem(args,0));
if( sofa::helper::system::SetDirectory::GetFileName(filename).empty() ) {// no filename
PyErr_SetString(PyExc_RuntimeError, "Empty filename.") ;
return nullptr;
}
std::vector<std::string> arguments;
for( size_t i=1 ; i<argSize ; i++ ) {
arguments.push_back( PyString_AsString(PyTuple_GetItem(args,i)) );
}
SceneLoaderPY loader;
Node::SPtr root;
loader.loadSceneWithArguments(filename, arguments, &root);
return PythonFactory::toPython(root.get());
}
static PyObject * Sofa_loadPlugin(PyObject * /*self*/, PyObject * args)
{
char *pluginName;
if (!PyArg_ParseTuple(args, "s", &pluginName)) {
return nullptr;
}
PluginManager& pluginManager = PluginManager::getInstance();
const std::string path = pluginManager.findPlugin(pluginName);
if (path != "")
{
if (!PluginManager::getInstance().pluginIsLoaded(path))
{
if (PluginManager::getInstance().loadPlugin(path))
{
const std::string guiPath = pluginManager.findPlugin( std::string( pluginName ) + "_" + PluginManager::s_gui_postfix);
if (guiPath != "")
{
PluginManager::getInstance().loadPlugin(guiPath);
}
}
}
} else {
std::stringstream ss;
ss << "cannot find plugin '" << pluginName << "'";
PyErr_SetString(PyExc_EnvironmentError, ss.str().c_str());
return nullptr;
}
return PyString_FromString(path.c_str());
}
static PyObject * Sofa_path(PyObject * /*self*/, PyObject * /*args*/) {
return PyString_FromString(Utils::getSofaPathPrefix().c_str());
}
static PyObject * Sofa_getCategories(PyObject * self, PyObject * args)
{
SOFA_UNUSED(self) ;
char* className;
if (!PyArg_ParseTuple(args, "s", &className)) {
return nullptr;
}
std::vector<std::string> categories;
ObjectFactory* factory = ObjectFactory::getInstance();
if (factory->hasCreator(className))
{
ObjectFactory::ClassEntry entry = factory->getEntry(className);
ObjectFactory::CreatorMap::iterator it2 = entry.creatorMap.begin();
if( it2 != entry.creatorMap.end())
{
ObjectFactory::Creator::SPtr c = it2->second;
const objectmodel::BaseClass* objClass = c->getClass();
CategoryLibrary::getCategories(objClass,categories);
}
}
PyObject *pyList = PyList_New(categories.size());
for (unsigned int i=0; i<categories.size(); ++i)
PyList_SetItem(pyList,i, PyString_FromString(categories[i].c_str())) ;
return pyList ;
}
static PyObject * Sofa_getAvailableComponents(PyObject * /*self*/, PyObject * args)
{
if(PyTuple_Size(args))
{
PyErr_SetString(PyExc_RuntimeError, "This function expects no arguments.");
return nullptr;
}
std::vector<ObjectFactory::ClassEntry::SPtr> entries ;
ObjectFactory::getInstance()->getAllEntries(entries) ;
PyObject *pyList = PyList_New(entries.size());
for (size_t i=0; i<entries.size(); i++){
PyObject *tuple = PyList_New(2);
PyList_SetItem(tuple, 0, Py_BuildValue("s", entries[i]->className.c_str()));
PyList_SetItem(tuple, 1, Py_BuildValue("s", entries[i]->description.c_str()));
PyList_SetItem(pyList, (Py_ssize_t)i, tuple);
}
return pyList;
}
static PyObject * Sofa_getAliasesFor(PyObject * /*self*/, PyObject * args)
{
char* componentname;
if (!PyArg_ParseTuple(args, "s", &componentname)) {
return nullptr;
}
const ObjectFactory::ClassEntry& entry = ObjectFactory::getInstance()->getEntry(componentname) ;
PyObject *pyList = PyList_New(entry.aliases.size());
unsigned int i=0;
for (auto& alias : entry.aliases){
PyList_SetItem(pyList, (Py_ssize_t)i, Py_BuildValue("s", alias.c_str()));
i++;
}
return pyList;
}
static PyObject * Sofa_getComponentsFromTarget(PyObject *self, PyObject * args)
{
SOFA_UNUSED(self) ;
char* targetname;
if (!PyArg_ParseTuple(args, "s", &targetname)) {
return nullptr;
}
std::vector<ObjectFactory::ClassEntry::SPtr> entries;
ObjectFactory::getInstance()->getEntriesFromTarget(entries, targetname);
PyObject *pyList = PyList_New(0);
for (auto& entry : entries){
PyObject* value = Py_BuildValue("s", entry->className.c_str()) ;
if(!PySequence_Contains(pyList, value))
{
PyList_Append(pyList, value);
}
else
{
Py_DECREF(value) ;
}
}
return pyList;
}
// -----------------
/**
* Method : Sofa_clear
* Desc : Wrapper for python usage. Clear the timer.
* Param : PyObject*, self - Object of the python script
* Return : NULL
*/
static PyObject * Sofa_timerClear(PyObject* /*self*/, PyObject * /*args*/)
{
AdvancedTimer::clear(); // Method call
Py_RETURN_NONE;
}
/**
* Method : Sofa_isEnabled
* Desc : Wrapper for python usage. Return if the timer is enable or not.
* Param : PyObject*, self - Object of the python script
* Param : PyObject*, args - given arguments to apply to the method
* Return : NULL
*/
static PyObject * Sofa_timerIsEnabled(PyObject* /*self*/, PyObject *args)
{
char* id;
bool answer = false;
if(!PyArg_ParseTuple(args, "s", &id))
{
return nullptr;
}
answer = AdvancedTimer::isEnabled(id); // Method call
if(answer)
{
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
/**
* Method : Sofa_setEnabled
* Desc : Wrapper for python usage. /!\ Need to pass an int in arguments insteed of a bool in the python script.
* Param : PyObject*, self - Object of the python script
* Param : PyObject*, args - given arguments to apply to the method
* Return : NULL
*/
static PyObject * Sofa_timerSetEnabled(PyObject* /*self*/, PyObject *args)
{
char* id;
PyObject* val;
if(!PyArg_ParseTuple(args, "sO", &id, &val))
{
Py_RETURN_NONE;
}
AdvancedTimer::setEnabled(id, PyObject_IsTrue(val)); // Method call
Py_RETURN_NONE;
}
/**
* Method : Sofa_getInterval
* Desc : Wrapper for python usage.
* Param : PyObject*, self - Object of the python script
* Param : PyObject*, args - given arguments to apply to the method
* Return : NULL
*/
static PyObject * Sofa_timerGetInterval(PyObject* /*self*/, PyObject *args)
{
char* id;
int answer = 0;
if(!PyArg_ParseTuple(args, "s", &id))
{
return nullptr;
}
answer = AdvancedTimer::getInterval(id); // Method call
return PyInt_FromLong(static_cast<long int>(answer));
}
/**
* Method : Sofa_setInterval
* Desc : Wrapper for python usage.
* Param : PyObject*, self - Object of the python script
* Param : PyObject*, args - given arguments to apply to the method
* Return : NULL
*/
static PyObject * Sofa_timerSetInterval(PyObject* /*self*/, PyObject *args)
{
char* id;
int newValue = 0;
if(!PyArg_ParseTuple(args, "si", &id, &newValue))
{
return nullptr;
}
AdvancedTimer::setInterval(id, newValue); // Method call
Py_RETURN_NONE;
}
/**
* Method : Sofa_begin
* Desc : Wrapper for python usage.
* Param : PyObject*, self - Object of the python script
* Param : PyObject*, args - given arguments to apply to the method
* Return : NULL
*/
static PyObject * Sofa_timerBegin(PyObject* /*self*/, PyObject *args)
{
char* id;
if(!PyArg_ParseTuple(args, "s", &id))
{
return nullptr;
}
AdvancedTimer::begin(id); // Method call
Py_RETURN_NONE;
}
/**
* Method : Sofa_timerStepBegin
* Desc : Wrapper for python usage.
* Param : PyObject*, args - given arguments to apply to the method
* Return : NULL
*/
static PyObject * Sofa_timerStepBegin(PyObject*, PyObject* args)
{
char* id;
if(!PyArg_ParseTuple(args, "s", &id))
{
return nullptr;
}
AdvancedTimer::stepBegin(id); // Method call
Py_RETURN_NONE;
}
/**
* Method : Sofa_timerStepEnd
* Desc : Wrapper for python usage.
* Param : PyObject*, args - given arguments to apply to the method
* Return : NULL
*/
static PyObject * Sofa_timerStepEnd(PyObject*, PyObject* args)
{
char* id;
if(!PyArg_ParseTuple(args, "s", &id))
{
return nullptr;
}
AdvancedTimer::stepEnd(id); // Method call
Py_RETURN_NONE;
}
/**
* Method : Sofa_timerEnd
* Desc : Wrapper for python usage.
* Param : PyObject*, self - Object of the python script
* Param : PyObject*, args - given arguments to apply to the method
* Return : string
*/
static PyObject * Sofa_timerEnd(PyObject* /*self*/, PyObject *args)
{
char* id = nullptr;
void* tempNode = nullptr;
Node* node = nullptr;
std::string result;
if(!PyArg_ParseTuple(args, "sO", &id, &tempNode))
{
return nullptr;
}
node = down_cast<Node>(((PySPtr<Base>*)tempNode)->object->toBaseNode());
result = AdvancedTimer::end(id, node->getTime(), node->getDt());
if(std::string("null").compare(result) == 0)
Py_RETURN_NONE;
return PyString_FromString(result.c_str()); // Method call
}
/**
* Method : Sofa_timerSetOutputType
* Desc : Wrapper for python usage. Used to change output type of the given timer
* Param : PyObject*, self - Object of the python script
* Param : PyObject*, args - given arguments to apply to the method
* Return : NULL
*/
static PyObject * Sofa_timerSetOutputType(PyObject* /*self*/, PyObject *args)
{
char* id = nullptr;
char* newOutputType = nullptr;
if(!PyArg_ParseTuple(args, "ss", &id, &newOutputType))
{
return nullptr;
}
AdvancedTimer::setOutputType(id, newOutputType);
Py_RETURN_NONE;
}
static constexpr const char* addPluginRepository_DOC =
R"DOC(
Adds a plugin repository path.
:param path: The repository's directory path
:type path: str
)DOC";
static PyObject * Sofa_addPluginRepository(PyObject *, PyObject *arg)
{
const char *path;
if (!PyArg_ParseTuple(arg, "s", &path))
return NULL;
sofa::helper::system::PluginRepository.addFirstPath(path);
return Py_None;
}
/// Methods of the module
SP_MODULE_METHODS_BEGIN(Sofa)
SP_MODULE_METHOD(Sofa,getSofaPythonVersion)
SP_MODULE_METHOD(Sofa,createNode)
SP_MODULE_METHOD_KW(Sofa,createObject)
SP_MODULE_METHOD(Sofa,getObject) // deprecated on date 2012/07/18
SP_MODULE_METHOD(Sofa,getChildNode) // deprecated on date 2012/07/18
SP_MODULE_METHOD(Sofa,sendGUIMessage)
SP_MODULE_METHOD(Sofa,saveScreenshot)
SP_MODULE_METHOD(Sofa,setViewerResolution)
SP_MODULE_METHOD(Sofa,setViewerBackgroundColor)
SP_MODULE_METHOD(Sofa,hasViewer)
SP_MODULE_METHOD(Sofa,setViewerCamera)
SP_MODULE_METHOD(Sofa,getViewerCamera)
SP_MODULE_METHOD(Sofa,generateRigid)
SP_MODULE_METHOD(Sofa,exportGraph)
SP_MODULE_METHOD(Sofa,updateVisual)
SP_MODULE_METHOD(Sofa,msg_info)
SP_MODULE_METHOD(Sofa,msg_deprecated)
SP_MODULE_METHOD(Sofa,msg_warning)
SP_MODULE_METHOD(Sofa,msg_error)
SP_MODULE_METHOD(Sofa,msg_fatal)
SP_MODULE_METHOD(Sofa,loadScene)
SP_MODULE_METHOD(Sofa,unload)
SP_MODULE_METHOD(Sofa,loadPythonSceneWithArguments)
SP_MODULE_METHOD(Sofa,loadPlugin)
SP_MODULE_METHOD(Sofa,path)
SP_MODULE_METHOD_DOC(Sofa,getCategories,"Return from a given component type (className) a list of categories it belongs to")
SP_MODULE_METHOD_DOC(Sofa,getAvailableComponents, "Returns the list of the available components in the factory.")
SP_MODULE_METHOD_DOC(Sofa,getAliasesFor, "Returns the list of the aliases for a given component")
SP_MODULE_METHOD_DOC(Sofa,getComponentsFromTarget, "Returns a string with the component contained in a given targets (plugins)")
SP_MODULE_METHOD_DOC(Sofa,addPluginRepository, addPluginRepository_DOC)
SP_MODULE_METHOD_DOC(Sofa, timerClear, "Method : Sofa_clear \nDesc : Wrapper for python usage. Clear the timer. \nParam : PyObject*, self - Object of the python script \nReturn : return None")
SP_MODULE_METHOD_DOC(Sofa, timerIsEnabled, "Method : Sofa_isEnabled \nDesc : Wrapper for python usage. Return if the timer is enable or not. \nParam : PyObject*, self - Object of the python script \nParam : PyObject*, args - given arguments to apply to the method \nReturn : None")
SP_MODULE_METHOD_DOC(Sofa, timerSetEnabled, "Method : Sofa_setEnabled \nDesc : Wrapper for python usage. /!\\ Need to pass an int in arguments insteed of a bool in the python script. \nParam : PyObject*, self - Object of the python script \nParam : PyObject*, args - given arguments to apply to the method \nReturn : None")
SP_MODULE_METHOD_DOC(Sofa, timerGetInterval, "Method : Sofa_getInterval \nDesc : Wrapper for python usage. \nParam : PyObject*, self - Object of the python script \nParam : PyObject*, args - given arguments to apply to the method \nReturn : None")
SP_MODULE_METHOD_DOC(Sofa, timerSetInterval, "Method : Sofa_setInterval \nDesc : Wrapper for python usage. \nParam : PyObject*, self - Object of the python script \nParam : PyObject*, args - given arguments to apply to the method \nReturn : None")
SP_MODULE_METHOD_DOC(Sofa, timerBegin, "Method : Sofa_begin \nDesc : Wrapper for python usage. \nParam : PyObject*, self - Object of the python script \nParam : PyObject*, args - given arguments to apply to the method \nReturn : return None")
SP_MODULE_METHOD_DOC(Sofa, timerStepBegin, "Method : Sofa_timerStepBegin \nDesc : Wrapper for python usage. \nParam : PyObject*, args - given arguments to apply to the method \nReturn : None")
SP_MODULE_METHOD_DOC(Sofa, timerStepEnd, "Method : Sofa_timerStepEnd \nDesc : Wrapper for python usage. \nParam : PyObject*, args - given arguments to apply to the method \nReturn : None")
SP_MODULE_METHOD_DOC(Sofa, timerSetOutputType, "Method : Sofa_timerSetOutputType \nDesc : Wrapper for python usage. \nParam : PyObject*, self - Object of the python script \nParam : PyObject*, args - given arguments to apply to the method \nReturn : None")
SP_MODULE_METHOD_DOC(Sofa, timerEnd, "Method : Sofa_timerEnd \nDesc : Wrapper for python usage. Used to change output type of the given timer \nParam : PyObject*, self - Object of the python script \nParam : PyObject*, args - given arguments to apply to the method \nReturn : return None")
SP_MODULE_METHODS_END