Skip to content

[SofaPython3] Add a LinkPath object in both the binding and plugin. #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 16, 2022
Merged
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
2 changes: 2 additions & 0 deletions Plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(HEADER_FILES

${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/DataCache.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/DataHelper.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/LinkPath.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/PythonFactory.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Prefab.h
)
Expand All @@ -19,6 +20,7 @@ set(SOURCE_FILES

${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/DataCache.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/DataHelper.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/LinkPath.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/PythonFactory.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Prefab.cpp
)
Expand Down
10 changes: 9 additions & 1 deletion Plugin/src/SofaPython3/DataHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
#include <sofa/simulation/Node.h>

#include <SofaPython3/DataHelper.h>
#include <SofaPython3/LinkPath.h>
#include <SofaPython3/DataCache.h>
#include <SofaPython3/PythonFactory.h>


using sofa::core::objectmodel::BaseLink;

namespace sofapython3
Expand Down Expand Up @@ -62,6 +64,12 @@ std::string toSofaParsableString(const py::handle& p)
return toSofaParsableString(o);
}

// if the object is a link path we set it.
if(py::isinstance<sofapython3::LinkPath>(p))
{
return py::str(p);
}

return py::repr(p);
}

Expand Down Expand Up @@ -463,7 +471,7 @@ BaseData* deriveTypeFromParent(sofa::core::objectmodel::BaseContext* ctx, const
bool isProtectedKeyword(const std::string& name)
{
if (name == "children" || name == "objects" || name == "parents" ||
name == "data" || name == "links")
name == "data" || name == "links" || name == "linkpath")
{
return true;
}
Expand Down
43 changes: 43 additions & 0 deletions Plugin/src/SofaPython3/LinkPath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2021 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/>. *
*******************************************************************************
* Contact information: [email protected] *
******************************************************************************/

#include <SofaPython3/LinkPath.h>

namespace sofapython3
{

LinkPath::LinkPath(sofa::core::sptr<sofa::core::objectmodel::Base> target)
{
targetBase = target;
targetData = nullptr;
}

LinkPath::LinkPath(sofa::core::objectmodel::BaseData* target)
{
targetBase = target->getOwner();
targetData = target;
}

bool LinkPath::isPointingToData() const
{
return targetData != nullptr;
}

}/// namespace sofapython3
46 changes: 46 additions & 0 deletions Plugin/src/SofaPython3/LinkPath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2021 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/>. *
*******************************************************************************
* Contact information: [email protected] *
******************************************************************************/

#pragma once

#include <sofa/core/objectmodel/Base.h>
#include <sofa/core/objectmodel/BaseData.h>

namespace sofapython3
{

/// A placeholder class storing a link to a data or a base
///
/// LinkPath can be used to indicate that a link is needed to be made in place of a string
/// like "@/usr/myobj.position" or "@/usr/myobj"
///
class LinkPath
{
public:
sofa::core::sptr<sofa::core::objectmodel::Base> targetBase;
sofa::core::objectmodel::BaseData* targetData;

LinkPath(sofa::core::sptr<sofa::core::objectmodel::Base>);
LinkPath(sofa::core::objectmodel::BaseData*);

bool isPointingToData() const;
};

} /// sofapython3
7 changes: 7 additions & 0 deletions Plugin/src/SofaPython3/PythonFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ using sofa::core::objectmodel::ScriptEvent;
using sofa::core::objectmodel::Event;

#include <SofaPython3/PythonEnvironment.h>
#include <SofaPython3/LinkPath.h>

#include <sofa/core/topology/Topology.h>

Expand Down Expand Up @@ -293,6 +294,12 @@ void copyFromListOf<std::string>(BaseData& d, const AbstractTypeInfo& nfo, const

void PythonFactory::fromPython(BaseData* d, const py::object& o)
{
if(py::isinstance<sofapython3::LinkPath>(o))
{
d->setParent(py::cast<LinkPath&>(o).targetData);
return;
}

const AbstractTypeInfo& nfo{ *(d->getValueTypeInfo()) };

// Is this data field a container ?
Expand Down
38 changes: 26 additions & 12 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ using sofa::helper::WriteOnlyAccessor;

#include <SofaPython3/PythonFactory.h>

#include <SofaPython3/Sofa/Core/Binding_LinkPath.h>
#include <SofaPython3/Sofa/Core/Binding_Base.h>
#include <SofaPython3/Sofa/Core/Binding_Base_doc.h>
#include <SofaPython3/Sofa/Core/Binding_DataDict.h>
Expand Down Expand Up @@ -90,6 +91,10 @@ py::object BindingBase::GetAttr(Base* self, const std::string& s, bool doThrowEx
if( s == "__data__")
return py::cast( DataDict(self) );

/// Returns the linkpath to the self object.
if(s == "linkpath")
return py::cast(sofapython3::LinkPath(self));

if(doThrowException)
throw py::attribute_error("Missing attribute: "+s);

Expand All @@ -101,18 +106,34 @@ bool BindingBase::SetData(BaseData* d, py::object value)
if(d==nullptr)
return false;

const AbstractTypeInfo& nfo{ *(d->getValueTypeInfo()) };

PythonFactory::fromPython(d, value);
return true;
}

bool BindingBase::SetLink(BaseLink* link, py::object value)
{
if(link==nullptr)
return false;

if(py::isinstance<py::str>(value))
return link->read(py::cast<py::str>(value));

if(py::isinstance<LinkPath>(value))
{
auto& target = py::cast<LinkPath&>(value);
if(target.isPointingToData())
throw std::runtime_error("Passing a link to a data field instead of an object.");
link->setLinkedBase(target.targetBase.get());
}
return true;
}


void BindingBase::SetAttr(py::object self, const std::string& s, py::object value)
{
Base* self_d = py::cast<Base*>(self);
BaseData* d = self_d->findData(s);

BaseData* d = self_d->findData(s);
if(d!=nullptr)
{
SetData(d, value);
Expand All @@ -122,6 +143,7 @@ void BindingBase::SetAttr(py::object self, const std::string& s, py::object valu
BaseLink* l = self_d->findLink(s);
if(l!=nullptr)
{
SetLink(l, value);
return;
}

Expand All @@ -132,24 +154,16 @@ void BindingBase::SetAttr(py::object self, const std::string& s, py::object valu
void BindingBase::SetAttr(Base& self, const std::string& s, py::object value)
{
BaseData* d = self.findData(s);

if(d!=nullptr)
{
const AbstractTypeInfo& nfo{ *(d->getValueTypeInfo()) };

/// We go for the container path.
if(nfo.Container())
{
PythonFactory::fromPython(d,value);
return;
}
PythonFactory::fromPython(d, value);
return;
}

BaseLink* l = self.findLink(s);
if(l!=nullptr)
{
SetLink(l, value);
return;
}

Expand Down
1 change: 1 addition & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class BindingBase
/// Set the data field value from the array.
static void SetDataFromArray(sofa::core::objectmodel::BaseData* data, const pybind11::array& value);
static bool SetData(sofa::core::objectmodel::BaseData* data, pybind11::object value);
static bool SetLink(sofa::core::objectmodel::BaseLink* link, pybind11::object value);
static pybind11::object setDataValues(sofa::core::objectmodel::Base& self, pybind11::kwargs kwargs);

static pybind11::list getDataFields(sofa::core::objectmodel::Base& self);
Expand Down
13 changes: 11 additions & 2 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_BaseData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <SofaPython3/Sofa/Core/Binding_Base.h>
#include <SofaPython3/Sofa/Core/Binding_BaseData.h>
#include <SofaPython3/Sofa/Core/Binding_BaseData_doc.h>
#include <SofaPython3/Sofa/Core/Binding_LinkPath.h>
#include <SofaPython3/Sofa/Core/Data/Binding_DataContainer.h>
#include <SofaPython3/DataHelper.h>
#include <SofaPython3/PythonFactory.h>
Expand Down Expand Up @@ -139,7 +140,7 @@ py::object __getattr__(py::object self, const std::string& s)
return PythonFactory::valueToPython_ro(py::cast<BaseData*>(self));

if(s == "linkpath")
return py::cast((py::cast<BaseData*>(self))->getLinkPath());
return py::cast(sofapython3::LinkPath(py::cast<BaseData*>(self)));

/// BaseData does not support dynamic attributes, if you think this is an important feature
/// please request for its integration.
Expand All @@ -151,11 +152,18 @@ void setParent(BaseData* self, BaseData* parent)
self->setParent(parent);
}

void setParentFromLinkPath(BaseData* self, const std::string& parent)
void setParentFromLinkPathStr(BaseData* self, const std::string& parent)
{
self->setParent(parent);
}

void setParentFromLinkPath(BaseData* self, const LinkPath& parent)
{
if(!parent.isPointingToData())
throw std::runtime_error("The provided linkpath is not containing a linkable data");
self->setParent(parent.targetData);
}

bool hasParent(BaseData *self)
{
return (self->getParent() != nullptr);
Expand Down Expand Up @@ -205,6 +213,7 @@ void moduleAddBaseData(py::module& m)
data.def("isPersistent", &BaseData::isPersistent, sofapython3::doc::baseData::isPersistent);
data.def("setPersistent", &BaseData::setPersistent, sofapython3::doc::baseData::setPersistent);
data.def("setParent", setParent, sofapython3::doc::baseData::setParent);
data.def("setParent", setParentFromLinkPathStr, sofapython3::doc::baseData::setParent);
data.def("setParent", setParentFromLinkPath, sofapython3::doc::baseData::setParent);
data.def("hasParent", hasParent, sofapython3::doc::baseData::hasParent);
data.def("read", &BaseData::read, sofapython3::doc::baseData::read);
Expand Down
84 changes: 84 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_LinkPath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2021 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/>. *
*******************************************************************************
* Contact information: [email protected] *
******************************************************************************/

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

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

#include <SofaPython3/Sofa/Core/Binding_LinkPath.h>
#include <SofaPython3/Sofa/Core/Binding_LinkPath_doc.h>

/// Makes an alias for the pybind11 namespace to increase readability.
namespace py { using namespace pybind11; }

// To bring in the `_a` literal
using namespace pybind11::literals;

namespace sofapython3
{

LinkPath::LinkPath(sofa::core::sptr<sofa::core::objectmodel::Base> target)
{
targetBase = target;
targetData = nullptr;
}

LinkPath::LinkPath(sofa::core::objectmodel::BaseData* target)
{
// If the data is attached to
if(target->getOwner())
{
targetBase = target->getOwner();
}
targetData = target;
}

bool LinkPath::isPointingToData() const
{
return targetData != nullptr;
}

std::string __str__(const LinkPath& entry)
{
std::ostringstream s;
if(entry.targetData != nullptr)
return entry.targetData->getLinkPath();
else if(entry.targetBase.get() != nullptr)
return "@" + entry.targetBase->getPathName();
throw std::runtime_error("Empty LinkPath");
}

std::string __repr__(const LinkPath& entry)
{
std::ostringstream s;
s << "LinkPath(\"" << __str__(entry) << "\")";
return s.str();
}

void moduleAddLinkPath(py::module& m)
{
py::class_<LinkPath> link(m, "LinkPath", sofapython3::doc::linkpath::linkpath);
link.def("__str__", &__str__);
link.def("__repr__", &__repr__);
}

}/// namespace sofapython3
Loading