Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
******************************************************************************/

#include <SofaPython3/Sofa/Core/Binding_Base.h>
#include <SofaPython3/Sofa/Core/Binding_BaseObject.h>
#include <SofaPython3/Sofa/Core/Binding_BaseObject_doc.h>
#include <SofaPython3/Sofa/Core/Binding_BaseComponent.h>
#include <SofaPython3/Sofa/Core/Binding_BaseComponent_doc.h>
#include <SofaPython3/Sofa/Core/Binding_Controller.h>
#include <SofaPython3/PythonFactory.h>

Expand Down Expand Up @@ -61,11 +61,11 @@ namespace py { using namespace pybind11; }

using sofa::core::objectmodel::BaseData;
using sofa::core::objectmodel::Base;
using sofa::core::objectmodel::BaseObject;
using sofa::core::objectmodel::BaseComponent;

namespace sofapython3
{
py::object getItem(const BaseObject& self, const std::string& path)
py::object getItem(const BaseComponent& self, const std::string& path)
{
if (path.empty())
return py::cast(self);
Expand All @@ -79,27 +79,27 @@ py::object getItem(const BaseObject& self, const std::string& path)
throw py::value_error("Invalid syntax"); // should never get there
}

std::string getLinkPath(const BaseObject *self)
std::string getLinkPath(const BaseComponent *self)
{
return std::string("@")+self->getPathName();
}

void computeBBox(BaseObject *self)
void computeBBox(BaseComponent *self)
{
self->computeBBox(sofa::core::execparams::defaultInstance(), false);
}

py::list getSlaves(BaseObject &self)
py::list getSlaves(BaseComponent &self)
{
const BaseObject::VecSlaves& slaves = self.getSlaves();
const BaseComponent::VecSlaves& slaves = self.getSlaves();
py::list slaveList;
for (auto slave : slaves){
slaveList.append(py::cast(slave));
}
return slaveList;
}

py::object getContext(const BaseObject &self)
py::object getContext(const BaseComponent &self)
{
const sofa::core::objectmodel::BaseContext* context = self.getContext();
if (context){
Expand All @@ -108,16 +108,16 @@ py::object getContext(const BaseObject &self)
return py::none();
}

py::object getMaster(const BaseObject &self)
py::object getMaster(const BaseComponent &self)
{
const BaseObject* master = self.getMaster();
const BaseComponent* master = self.getMaster();
if (master){
return py::cast(master);
}
return py::none();
}

py::object getTarget(BaseObject *self)
py::object getTarget(BaseComponent *self)
{
if (!self)
return py::none();
Expand All @@ -133,7 +133,7 @@ py::object getTarget(BaseObject *self)
return py::none() ;
}

py::object getCategories(BaseObject *self)
py::object getCategories(BaseComponent *self)
{
std::vector<std::string> categories;
const sofa::core::objectmodel::BaseClass* c=self->getClass();
Expand All @@ -142,12 +142,12 @@ py::object getCategories(BaseObject *self)
return std::move(l);
}

std::string getAsACreateObjectParameter(BaseObject *self)
std::string getAsACreateObjectParameter(BaseComponent *self)
{
return getLinkPath(self);
}

void setSrc(BaseObject &self, char *valueString, BaseObject *loader)
void setSrc(BaseComponent &self, char *valueString, BaseComponent *loader)
{
self.setSrc(valueString,loader);
}
Expand All @@ -166,7 +166,7 @@ void setSrc(BaseObject &self, char *valueString, BaseObject *loader)
/// In the example above, node1 and node2 can be inferred as being nodes without performing any checks.
/// object1 can be a node or an object, but cannot be a datafield nor a link
/// value can be a node or an object (if object1 is a node), or must be a data (if object1 is an object)
py::object __getitem__(BaseObject &self, std::string s)
py::object __getitem__(BaseComponent &self, std::string s)
{
if (s[0] == '.')
s.erase(s.begin());
Expand All @@ -189,49 +189,49 @@ py::object __getitem__(BaseObject &self, std::string s)
return getItem(self, s);
}

auto getBaseObjectBinding(py::module& m)
auto getBaseComponentBinding(py::module& m)
{
/// Register the BaseObject binding into the pybind11 typing system
static py::class_<BaseObject, Base, py_shared_ptr<BaseObject>>p(m, "Object", sofapython3::doc::baseObject::Class);
/// Register the BaseComponent binding into the pybind11 typing system
static py::class_<BaseComponent, Base, py_shared_ptr<BaseComponent>>p(m, "Object", sofapython3::doc::BaseComponent::Class);
return p;
}

void moduleForwardAddBaseObject(py::module& m)
void moduleForwardAddBaseComponent(py::module& m)
{
getBaseObjectBinding(m);
getBaseComponentBinding(m);
}

void moduleAddBaseObject(py::module& m)
void moduleAddBaseComponent(py::module& m)
{
auto p = getBaseObjectBinding(m);
auto p = getBaseComponentBinding(m);

/// Register the BaseObject binding into the downcasting subsystem
PythonFactory::registerType<sofa::core::objectmodel::BaseObject>(
/// Register the BaseComponent binding into the downcasting subsystem
PythonFactory::registerType<sofa::core::objectmodel::BaseComponent>(
[](sofa::core::objectmodel::Base* object)
{
return py::cast(py_shared_ptr<sofa::core::objectmodel::BaseObject>(object->toBaseObject()));
return py::cast(py_shared_ptr<sofa::core::objectmodel::BaseComponent>(object->toBaseComponent()));
});

p.def("init", &BaseObject::init, sofapython3::doc::baseObject::init);
p.def("reinit", &BaseObject::reinit, sofapython3::doc::baseObject::reinit);
p.def("getPathName", &BaseObject::getPathName, sofapython3::doc::baseObject::getPathName);
p.def("getLinkPath", [](const BaseObject &self){ return std::string("@") + self.getPathName(); }, sofapython3::doc::baseObject::getLink);
p.def("getSlaves", getSlaves, sofapython3::doc::baseObject::getSlaves);
p.def("getContext", getContext, sofapython3::doc::baseObject::getContext);
p.def("getMaster", getMaster, sofapython3::doc::baseObject::getMaster);
p.def("addSlave", &BaseObject::addSlave, sofapython3::doc::baseObject::addSlave);
p.def("storeResetState", &BaseObject::storeResetState, sofapython3::doc::baseObject::storeResetState);
p.def("reset", &BaseObject::reset, sofapython3::doc::baseObject::reset);
p.def("getTarget", getTarget, sofapython3::doc::baseObject::getTarget);
p.def("getCategories", getCategories, sofapython3::doc::baseObject::getCategories);
p.def("bwdInit", &BaseObject::bwdInit, sofapython3::doc::baseObject::bwdInit);
p.def("cleanup", &BaseObject::cleanup, sofapython3::doc::baseObject::cleanup);
p.def("computeBBox", &computeBBox, sofapython3::doc::baseObject::computeBBox);
p.def("getLinkPath", &getLinkPath, sofapython3::doc::baseObject::getLinkPath);
p.def("getAsACreateObjectParameter", getAsACreateObjectParameter, sofapython3::doc::baseObject::getAsACreateObjectParameter);
p.def("setSrc", setSrc, sofapython3::doc::baseObject::setSrc);
p.def("computeBBox", &BaseObject::computeBBox, sofapython3::doc::baseObject::computeBBox);
p.def("__getitem__", __getitem__, sofapython3::doc::baseObject::__getitem__);
p.def("init", &BaseComponent::init, sofapython3::doc::BaseComponent::init);
p.def("reinit", &BaseComponent::reinit, sofapython3::doc::BaseComponent::reinit);
p.def("getPathName", &BaseComponent::getPathName, sofapython3::doc::BaseComponent::getPathName);
p.def("getLinkPath", [](const BaseComponent &self){ return std::string("@") + self.getPathName(); }, sofapython3::doc::BaseComponent::getLink);
p.def("getSlaves", getSlaves, sofapython3::doc::BaseComponent::getSlaves);
p.def("getContext", getContext, sofapython3::doc::BaseComponent::getContext);
p.def("getMaster", getMaster, sofapython3::doc::BaseComponent::getMaster);
p.def("addSlave", &BaseComponent::addSlave, sofapython3::doc::BaseComponent::addSlave);
p.def("storeResetState", &BaseComponent::storeResetState, sofapython3::doc::BaseComponent::storeResetState);
p.def("reset", &BaseComponent::reset, sofapython3::doc::BaseComponent::reset);
p.def("getTarget", getTarget, sofapython3::doc::BaseComponent::getTarget);
p.def("getCategories", getCategories, sofapython3::doc::BaseComponent::getCategories);
p.def("bwdInit", &BaseComponent::bwdInit, sofapython3::doc::BaseComponent::bwdInit);
p.def("cleanup", &BaseComponent::cleanup, sofapython3::doc::BaseComponent::cleanup);
p.def("computeBBox", &computeBBox, sofapython3::doc::BaseComponent::computeBBox);
p.def("getLinkPath", &getLinkPath, sofapython3::doc::BaseComponent::getLinkPath);
p.def("getAsACreateObjectParameter", getAsACreateObjectParameter, sofapython3::doc::BaseComponent::getAsACreateObjectParameter);
p.def("setSrc", setSrc, sofapython3::doc::BaseComponent::setSrc);
p.def("computeBBox", &BaseComponent::computeBBox, sofapython3::doc::BaseComponent::computeBBox);
p.def("__getitem__", __getitem__, sofapython3::doc::BaseComponent::__getitem__);
}

} /// namespace sofapython3
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
namespace sofapython3
{

pybind11::object getItem(const sofa::core::objectmodel::BaseObject & self, const std::string& path);
pybind11::object getItem(const sofa::core::objectmodel::BaseComponent & self, const std::string& path);

void moduleForwardAddBaseObject(pybind11::module &m);
void moduleAddBaseObject(pybind11::module &m);
void moduleForwardAddBaseComponent(pybind11::module &m);
void moduleAddBaseComponent(pybind11::module &m);

} /// namespace sofapython

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#pragma once

namespace sofapython3::doc::baseObject
namespace sofapython3::doc::BaseComponent
{
static auto Class =
R"(
Expand Down Expand Up @@ -61,41 +61,41 @@ static auto reinit =
)";
static auto getPathName =
R"(
Return the full path name of this baseObject
Return the full path name of this BaseComponent
:rtype: string
)";

static auto getLink =
R"(
Return the link of the baseObject
:param self: the baseObject itself
:type self: baseObject
Return the link of the BaseComponent
:param self: the BaseComponent itself
:type self: BaseComponent
:rtype: string
)";

static auto getSlaves =
R"(
Return the slaves of the baseObject.
Return the slaves of the BaseComponent.
:rtype: list
)";

static auto getContext =
R"(
Return the conext of the baseObject.
Return the conext of the BaseComponent.
:rtype: BaseContext
)";

static auto getMaster =
R"(
Return the master of the baseObject.
:rtype: BaseObject
Return the master of the BaseComponent.
:rtype: BaseComponent
)";

static auto addSlave =
R"(
Add a slave to the master BaseObject.
Add a slave to the master BaseComponent.
:param slave: the slave to be added.
:type slave: BaseObject
:type slave: BaseComponent
)";

static auto storeResetState =
Expand All @@ -111,13 +111,13 @@ static auto reset =

static auto getName =
R"(
Accessor to the baseObject name.
Accessor to the BaseComponent name.
:rtype: string
)";

static auto getTarget =
R"(
Return the target (plugin) that contains the current baseObject.
Return the target (plugin) that contains the current BaseComponent.
:rtype: string
)";

Expand Down Expand Up @@ -151,13 +151,13 @@ static auto computeBBox =

static auto getLinkPath =
R"(
Return the full path name of this baseObject with an extra prefix '@'
Return the full path name of this BaseComponent with an extra prefix '@'
:rtype: string
)";

static auto getAsACreateObjectParameter =
R"(
Return the full path name of this baseObject with an extra prefix '@'
Return the full path name of this BaseComponent with an extra prefix '@'
:rtype: string
)";

Expand Down
25 changes: 14 additions & 11 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include <sofa/simulation/mechanicalvisitor/MechanicalComputeEnergyVisitor.h>
#include <sofa/core/ComponentNameHelper.h>

#include <sofa/core/objectmodel/BaseComponent.h>
using sofa::core::objectmodel::BaseComponent;

#include <sofa/core/objectmodel/BaseData.h>
using sofa::core::objectmodel::BaseData;

Expand All @@ -44,7 +47,7 @@ using sofa::core::ExecParams;
using sofapython3::LinkPath;

#include <SofaPython3/Sofa/Core/Binding_Base.h>
#include <SofaPython3/Sofa/Core/Binding_BaseObject.h>
#include <SofaPython3/Sofa/Core/Binding_BaseComponent.h>
#include <SofaPython3/DataHelper.h>

#include <sofa/core/ObjectFactory.h>
Expand Down Expand Up @@ -138,7 +141,7 @@ py::object getItem(Node& self, std::list<std::string>& path)
if (path.empty())
return py::cast(self);
Node* child = self.getChild(path.front());
BaseObject* obj = self.getObject(path.front());
BaseComponent* obj = self.getObject(path.front());
BaseData* data = self.findData(path.front());
if (child)
{
Expand Down Expand Up @@ -182,25 +185,25 @@ py_shared_ptr<Node> __init_kwarged__(const std::string& name, const py::kwargs&
/// Method: init (beware this is not the python __init__, this is sofa's init())
void init(Node& self) { self.init(sofa::core::execparams::defaultInstance()); }

py::object addObject(Node& self, BaseObject * object)
py::object addObject(Node& self, BaseComponent * object)
{
try {
if (self.addObject(object))
return PythonFactory::toPython(object);
} catch (...) {
throw py::type_error("Trying to add an object that isn't derived from sofa::core::objectmodel::BaseObject.");
throw py::type_error("Trying to add an object that isn't derived from sofa::core::objectmodel::BaseComponent.");
}
return py::none();
}

void removeObject(Node& self, BaseObject* object)
void removeObject(Node& self, BaseComponent* object)
{
self.removeObject(object);
}

py::object hasObject(Node &n, const std::string &name)
{
BaseObject *object = n.getObject(name);
BaseComponent *object = n.getObject(name);
if (object)
return py::cast(true);
return py::cast(false);
Expand All @@ -216,7 +219,7 @@ py::object getObject(Node &n, const std::string &name, const py::kwargs& kwargs)
<< PythonEnvironment::getPythonCallingPointString() ;
}

BaseObject *object = n.getObject(name);
BaseComponent *object = n.getObject(name);
if (object)
return PythonFactory::toPython(object);
return py::none();
Expand Down Expand Up @@ -344,9 +347,9 @@ py::object addObjectKwargs(Node* self, const std::string& type, const py::kwargs
/// Implement the addObject function.
py::object addKwargs(Node* self, const py::object& callable, const py::kwargs& kwargs)
{
if(py::isinstance<BaseObject*>(callable))
if(py::isinstance<BaseComponent*>(callable))
{
BaseObject* obj = py::cast<BaseObject*>(callable);
BaseComponent* obj = py::cast<BaseComponent*>(callable);
self->addObject(obj);
return py::cast(obj);
}
Expand Down Expand Up @@ -375,7 +378,7 @@ py::object addKwargs(Node* self, const py::object& callable, const py::kwargs& k
Base* base = py::cast<Base*>(c);
if(!py::isinstance<Base*>(c))
{
throw py::value_error("add: the function passed as first argument can only return a Sofa.BaseObject or Sofa.Node object");
throw py::value_error("add: the function passed as first argument can only return a Sofa.BaseComponent or Sofa.Node object");
}

// Set the creation point
Expand Down Expand Up @@ -516,7 +519,7 @@ py::object __getattr__(py::object pyself, const std::string& name)
{
Node* selfnode = py::cast<Node*>(pyself);
/// Search in the object lists
BaseObject *object = selfnode->getObject(name);
BaseComponent *object = selfnode->getObject(name);
if (object)
return PythonFactory::toPython(object);

Expand Down
Loading
Loading