Skip to content

Commit 767a713

Browse files
authored
[Sofa.Core] A helper class for consistent component naming (#228)
* [Sofa.Core] A helper class for consistent component naming * Fix unit test * A NameHelper for each Node * More explicit class name * Remove dependency to ObjectFactory
1 parent 4643978 commit 767a713

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
#include <pybind11/stl.h>
2424
#include <pybind11/eval.h>
2525

26-
#include <sofa/core/objectmodel/BaseData.h>
2726
#include <sofa/simulation/Simulation.h>
27+
#include <sofa/core/ComponentNameHelper.h>
28+
29+
#include <sofa/core/objectmodel/BaseData.h>
2830
using sofa::core::objectmodel::BaseData;
2931

3032
#include <SofaSimulationGraph/SimpleApi.h>
@@ -186,15 +188,16 @@ py::object getObject(Node &n, const std::string &name, const py::kwargs& kwargs)
186188
/// Implement the addObject function.
187189
py::object addObjectKwargs(Node* self, const std::string& type, const py::kwargs& kwargs)
188190
{
191+
std::string name {};
189192
if (kwargs.contains("name"))
190193
{
191-
std::string name = py::str(kwargs["name"]);
194+
name = py::str(kwargs["name"]);
192195
if (sofapython3::isProtectedKeyword(name))
193196
throw py::value_error("Cannot call addObject with name " + name + ": Protected keyword");
194197
}
195198
/// Prepare the description to hold the different python attributes as data field's
196199
/// arguments then create the object.
197-
BaseObjectDescription desc {type.c_str(), type.c_str()};
200+
BaseObjectDescription desc {nullptr, type.c_str()};
198201
fillBaseObjectdescription(desc, kwargs);
199202
auto object = ObjectFactory::getInstance()->createObject(self, &desc);
200203

@@ -209,6 +212,12 @@ py::object addObjectKwargs(Node* self, const std::string& type, const py::kwargs
209212
throw py::value_error(tmp.str());
210213
}
211214

215+
if (name.empty())
216+
{
217+
const auto resolvedName = self->getNameHelper().resolveName(object->getClassName(), name);
218+
object->setName(resolvedName);
219+
}
220+
212221
checkParamUsage(desc);
213222

214223
/// Convert the logged messages in the object's internal logging into python exception.

bindings/Sofa/tests/Core/BaseLink.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ def test_getLinked_and_Owner(self):
9393
self.assertEqual(link_input.getLinkedBase(0).getName(),"t1")
9494
link_output = mm.findLink("output")
9595
self.assertEqual(link_output.getLinkedBase(0).getName(),"t2")
96-
self.assertEqual(link_input.getOwnerBase().getName(), "BarycentricMapping")
97-
self.assertEqual(link_output.getOwnerBase().getName(), "BarycentricMapping")
96+
self.assertEqual(link_input.getOwnerBase().getName(), mm.getName())
97+
self.assertEqual(link_output.getOwnerBase().getName(), mm.getName())
9898

9999
def test_baselink_attributes_forwarding(self):
100100
root = create_scene("root")

0 commit comments

Comments
 (0)