Skip to content
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

Adds bindings for the model convenience class #2036

Merged
merged 24 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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 python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pybind11_add_module(${BINDINGS_MODULE_NAME} MODULE
src/gz/sim/_gz_sim_pybind11.cc
src/gz/sim/EntityComponentManager.cc
src/gz/sim/EventManager.cc
src/gz/sim/Model.cc
src/gz/sim/TestFixture.cc
src/gz/sim/Server.cc
src/gz/sim/ServerConfig.cc
Expand Down Expand Up @@ -86,6 +87,7 @@ endif()

if (BUILD_TESTING)
set(python_tests
model_TEST
testFixture_TEST
world_TEST
)
Expand Down
106 changes: 106 additions & 0 deletions python/src/gz/sim/Model.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (C) 2023 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "Model.hh"

namespace py = pybind11;

namespace gz
{
namespace sim
{
namespace python
{
void defineSimModel(py::object module)
{
py::class_<gz::sim::Model>(module, "Model")
.def(py::init<gz::sim::Entity>())
azeey marked this conversation as resolved.
Show resolved Hide resolved
.def(py::init<gz::sim::Model>())
.def("entity", &gz::sim::Model::Entity,
"Get the entity which this Model is related to.")
.def("valid", &gz::sim::Model::Valid,
py::arg("ecm"),

Check warning on line 39 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L39

Added line #L39 was not covered by tests
"Check whether this model correctly refers to an entity that "
"has a components::Model.")
.def("name", &gz::sim::Model::Name,
py::arg("ecm"),

Check warning on line 43 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L43

Added line #L43 was not covered by tests
"Get the model's unscoped name.")
.def("static", &gz::sim::Model::Static,
py::arg("ecm"),

Check warning on line 46 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L46

Added line #L46 was not covered by tests
"Get whether this model is static.")
.def("self_collide", &gz::sim::Model::SelfCollide,
py::arg("ecm"),

Check warning on line 49 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L49

Added line #L49 was not covered by tests
"Get whether this model has self-collide enabled.")
.def("wind_mode", &gz::sim::Model::WindMode,
py::arg("ecm"),

Check warning on line 52 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L52

Added line #L52 was not covered by tests
"Get whether this model has wind enabled.")
.def("source_file_path", &gz::sim::Model::SourceFilePath,
py::arg("ecm"),

Check warning on line 55 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L55

Added line #L55 was not covered by tests
"Get the source file where this model came from. If empty,"
"the model wasn't loaded directly from a file, probably from an SDF "
"string.")
.def("joint_by_name", &gz::sim::Model::JointByName,
py::arg("ecm"),
py::arg("name"),

Check warning on line 61 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L60-L61

Added lines #L60 - L61 were not covered by tests
"Get the ID of a joint entity which is an immediate child of "
"this model.")
.def("link_by_name", &gz::sim::Model::LinkByName,
py::arg("ecm"),
py::arg("name"),

Check warning on line 66 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L65-L66

Added lines #L65 - L66 were not covered by tests
"Get the ID of a link entity which is an immediate child of "
"this model.")
.def("joints", &gz::sim::Model::Joints,
py::arg("ecm"),

Check warning on line 70 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L70

Added line #L70 was not covered by tests
"Get all joints which are immediate children of this model.")
.def("links", &gz::sim::Model::Links,
py::arg("ecm"),

Check warning on line 73 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L73

Added line #L73 was not covered by tests
"Get all links which are immediate children of this model.")
.def("models", &gz::sim::Model::Models,
py::arg("ecm"),

Check warning on line 76 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L76

Added line #L76 was not covered by tests
"Get all models which are immediate children of this model.")
.def("joint_count", &gz::sim::Model::JointCount,
py::arg("ecm"),

Check warning on line 79 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L79

Added line #L79 was not covered by tests
"Get the number of joints which are immediate children of this "
"model.")
.def("link_count", &gz::sim::Model::LinkCount,
py::arg("ecm"),

Check warning on line 83 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L83

Added line #L83 was not covered by tests
"Get the number of links which are immediate children of this "
"model.")
.def("set_world_pose_cmd", &gz::sim::Model::SetWorldPoseCmd,
py::arg("ecm"),
py::arg("pose"),

Check warning on line 88 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L87-L88

Added lines #L87 - L88 were not covered by tests
"Set a command to change the model's pose.")
.def("canonical_link", &gz::sim::Model::CanonicalLink,
py::arg("ecm"),

Check warning on line 91 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L91

Added line #L91 was not covered by tests
"Get the model's canonical link entity.")
.def("__copy__",
[](const gz::sim::Model &self)

Check warning on line 94 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L94

Added line #L94 was not covered by tests
{
return gz::sim::Model(self);

Check warning on line 96 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L96

Added line #L96 was not covered by tests
})
.def("__deepcopy__",
[](const gz::sim::Model &self, pybind11::dict)

Check warning on line 99 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L99

Added line #L99 was not covered by tests
{
return gz::sim::Model(self);

Check warning on line 101 in python/src/gz/sim/Model.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Model.cc#L101

Added line #L101 was not covered by tests
});
}
} // namespace python
} // namespace sim
} // namespace gz
40 changes: 40 additions & 0 deletions python/src/gz/sim/Model.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2023 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef GZ_SIM_PYTHON__MODEL_HH_
#define GZ_SIM_PYTHON__MODEL_HH_

#include <pybind11/pybind11.h>

#include <gz/sim/Model.hh>

namespace gz
{
namespace sim
{
namespace python
{
/// Define a pybind11 wrapper for a gz::sim::Model
/**
* \param[in] module a pybind11 module to add the definition to
*/
void
defineSimModel(pybind11::object module);
} // namespace python
} // namespace sim
} // namespace gz

#endif // GZ_SIM_PYTHON__MODEL_HH_
13 changes: 12 additions & 1 deletion python/src/gz/sim/World.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
{
py::class_<gz::sim::World>(module, "World")
.def(py::init<gz::sim::Entity>())
.def(py::init<gz::sim::World>())
.def("entity", &gz::sim::World::Entity,
"Get the entity which this World is related to.")
.def("valid", &gz::sim::World::Valid,
Expand Down Expand Up @@ -92,7 +93,17 @@
.def("model_count", &gz::sim::World::ModelCount,
py::arg("ecm"),
"Get the number of models which are immediate children of this "
"world.");
"world.")
.def("__copy__",
[](const gz::sim::World &self)

Check warning on line 98 in python/src/gz/sim/World.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/World.cc#L98

Added line #L98 was not covered by tests
{
return gz::sim::World(self);

Check warning on line 100 in python/src/gz/sim/World.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/World.cc#L100

Added line #L100 was not covered by tests
})
.def("__deepcopy__",
[](const gz::sim::World &self, pybind11::dict)

Check warning on line 103 in python/src/gz/sim/World.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/World.cc#L103

Added line #L103 was not covered by tests
{
return gz::sim::World(self);

Check warning on line 105 in python/src/gz/sim/World.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/World.cc#L105

Added line #L105 was not covered by tests
});
}
} // namespace python
} // namespace sim
Expand Down
6 changes: 5 additions & 1 deletion python/src/gz/sim/_gz_sim_pybind11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

#include <pybind11/pybind11.h>

#include "gz/sim/Entity.hh"

#include "EntityComponentManager.hh"
#include "EventManager.hh"
#include "Model.hh"
#include "Server.hh"
#include "ServerConfig.hh"
#include "TestFixture.hh"
Expand All @@ -27,9 +30,10 @@

PYBIND11_MODULE(BINDINGS_MODULE_NAME, m) {
m.doc() = "Gazebo Sim Python Library.";

m.attr("K_NULL_ENTITY") = gz::sim::kNullEntity;
gz::sim::python::defineSimEntityComponentManager(m);
gz::sim::python::defineSimEventManager(m);
gz::sim::python::defineSimModel(m);
gz::sim::python::defineSimServer(m);
gz::sim::python::defineSimServerConfig(m);
gz::sim::python::defineSimTestFixture(m);
Expand Down
78 changes: 78 additions & 0 deletions python/test/model_TEST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python3
# Copyright (C) 2023 Open Source Robotics Foundation

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import unittest

from gz.common import set_verbosity
from gz_test_deps.sim import K_NULL_ENTITY, TestFixture, Model, World, world_entity

class TestModel(unittest.TestCase):
post_iterations = 0
iterations = 0
pre_iterations = 0

def test_model(self):
set_verbosity(4)

file_path = os.path.dirname(os.path.realpath(__file__))
fixture = TestFixture(os.path.join(file_path, 'model_test.sdf'))

def on_post_udpate_cb(_info, _ecm):
self.post_iterations += 1

def on_pre_udpate_cb(_info, _ecm):
self.pre_iterations += 1
world_e = world_entity(_ecm)
self.assertNotEqual(K_NULL_ENTITY, world_e)
w = World(world_e)
model = Model(w.model_by_name(_ecm, 'model_test'))
# Entity Test
self.assertNotEqual(K_NULL_ENTITY, model.entity())
# Valid Test
self.assertTrue(model.valid(_ecm))
# Name Test
self.assertEqual('model_test', model.name(_ecm))
# Static Test
self.assertTrue(model.static(_ecm))
# Self Collide Test
self.assertTrue(model.self_collide(_ecm))
# Wind Mode Test
self.assertTrue(model.wind_mode(_ecm))
# Get Joints Test
self.assertNotEqual(K_NULL_ENTITY, model.joint_by_name(_ecm, 'model_joint_test'))
self.assertEqual(1, model.joint_count(_ecm))
# Get Links Test
self.assertNotEqual(K_NULL_ENTITY, model.link_by_name(_ecm, 'model_link_test_1'))
self.assertNotEqual(K_NULL_ENTITY, model.link_by_name(_ecm, 'model_link_test_2'))
self.assertEqual(2, model.link_count(_ecm))

def on_udpate_cb(_info, _ecm):
self.iterations += 1

fixture.on_post_update(on_post_udpate_cb)
fixture.on_update(on_udpate_cb)
fixture.on_pre_update(on_pre_udpate_cb)
fixture.finalize()

server = fixture.server()
server.run(True, 1000, False)

self.assertEqual(1000, self.pre_iterations)
self.assertEqual(1000, self.iterations)
self.assertEqual(1000, self.post_iterations)

if __name__ == '__main__':
unittest.main()
25 changes: 25 additions & 0 deletions python/test/model_test.sdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" ?>
<sdf version="1.6">
<world name="world_test">
<model name="model_test">
<pose>0.2 0.1 0.5 0.1 0.3 0.4</pose>
<static>true</static>
<self_collide>true</self_collide>
<enable_wind>true</enable_wind>

<link name="model_link_test_1">
<pose>0 0 0 0 0 0</pose>
</link>

<link name="model_link_test_2">
<pose>0 0 0 0 0 0</pose>
</link>

<joint name="model_joint_test" type="fixed">
<parent>model_link_test_1</parent>
<child>model_link_test_2</child>
azeey marked this conversation as resolved.
Show resolved Hide resolved
</joint>

</model>
</world>
</sdf>
Loading