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 22 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 whether this model correctly refers to an entity that "
"has a components::Model.")
.def("name", &gz::sim::Model::Name,
py::arg("ecm"),
"Get the model's unscoped name.")
.def("static", &gz::sim::Model::Static,
py::arg("ecm"),
"Get whether this model is static.")
.def("self_collide", &gz::sim::Model::SelfCollide,
py::arg("ecm"),
"Get whether this model has self-collide enabled.")
.def("wind_mode", &gz::sim::Model::WindMode,
py::arg("ecm"),
"Get whether this model has wind enabled.")
.def("source_file_path", &gz::sim::Model::SourceFilePath,
py::arg("ecm"),
"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"),
"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"),
"Get the ID of a link entity which is an immediate child of "
"this model.")
.def("joints", &gz::sim::Model::Joints,
py::arg("ecm"),
"Get all joints which are immediate children of this model.")
.def("links", &gz::sim::Model::Links,
py::arg("ecm"),
"Get all links which are immediate children of this model.")
.def("models", &gz::sim::Model::Models,
py::arg("ecm"),
"Get all models which are immediate children of this model.")
.def("joint_count", &gz::sim::Model::JointCount,
py::arg("ecm"),
"Get the number of joints which are immediate children of this "
"model.")
.def("link_count", &gz::sim::Model::LinkCount,
py::arg("ecm"),
"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"),
"Set a command to change the model's pose.")
.def("canonical_link", &gz::sim::Model::CanonicalLink,
py::arg("ecm"),
"Get the model's canonical link entity.")
.def("__copy__",
[](const gz::sim::Model &self)
{
return gz::sim::Model(self);
})
.def("__deepcopy__",
[](const gz::sim::Model &self, pybind11::dict)
{
return gz::sim::Model(self);
});
}
} // 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 @@ void defineSimWorld(py::object module)
{
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 @@ void defineSimWorld(py::object module)
.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)
{
return gz::sim::World(self);
})
.def("__deepcopy__",
[](const gz::sim::World &self, pybind11::dict)
{
return gz::sim::World(self);
});
}
} // 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;
azeey marked this conversation as resolved.
Show resolved Hide resolved
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