Skip to content

Commit 3cf91c5

Browse files
committed
Add PythonAPI module
1 parent 084fe6c commit 3cf91c5

15 files changed

+541
-0
lines changed

PythonClient/.pep8 .pep8

File renamed without changes.

PythonAPI/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.egg-info
2+
build
3+
dependencies
4+
dist

PythonAPI/setup.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma de
2+
# Barcelona (UAB).
3+
#
4+
# This work is licensed under the terms of the MIT license.
5+
# For a copy, see <https://opensource.org/licenses/MIT>.
6+
7+
from setuptools import setup, Extension
8+
9+
import fnmatch
10+
import glob
11+
import os
12+
import platform
13+
import sys
14+
15+
16+
def get_libcarla_extensions():
17+
libraries = ['carla_client', 'rpc']
18+
19+
if os.name == "posix":
20+
if platform.dist()[0] == "Ubuntu":
21+
libraries += ["boost_python-py%d%d" % (sys.version_info.major,
22+
sys.version_info.minor)]
23+
else:
24+
libraries += ["boost_python"]
25+
else:
26+
raise NotImplementedError
27+
28+
def walk(folder, file_filter='*'):
29+
for root, _, filenames in os.walk(folder):
30+
for filename in fnmatch.filter(filenames, file_filter):
31+
yield os.path.join(root, filename)
32+
33+
depends = [x for x in walk('dependencies')]
34+
35+
def make_extension(name, sources):
36+
return Extension(
37+
name,
38+
sources=sources,
39+
include_dirs=[
40+
'/usr/local/include',
41+
'dependencies/include'],
42+
library_dirs=[
43+
'/usr/local/lib/boost',
44+
'dependencies/lib'],
45+
runtime_library_dirs=['/usr/local/lib/boost'],
46+
libraries=libraries,
47+
extra_compile_args=['-fPIC', '-std=c++17'],
48+
language='c++17',
49+
depends=depends)
50+
51+
return [make_extension('carla.libcarla', glob.glob('source/libcarla/*.cpp'))]
52+
53+
54+
setup(
55+
name='carla',
56+
version='0.9.0',
57+
package_dir={'': 'source'},
58+
packages=['carla'],
59+
ext_modules=get_libcarla_extensions(),
60+
license='MIT License',
61+
description='Python API for communicating with the CARLA server.',
62+
url='https://github.com/carla-simulator/carla',
63+
author='The CARLA team',
64+
author_email='[email protected]',
65+
include_package_data=True)

PythonAPI/source/carla/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma de
2+
# Barcelona (UAB).
3+
#
4+
# This work is licensed under the terms of the MIT license.
5+
# For a copy, see <https://opensource.org/licenses/MIT>.
6+
7+
from .libcarla import *

PythonAPI/source/libcarla/Actor.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2+
// de Barcelona (UAB).
3+
//
4+
// This work is licensed under the terms of the MIT license.
5+
// For a copy, see <https://opensource.org/licenses/MIT>.
6+
7+
#include <carla/client/Actor.h>
8+
9+
#include <boost/python.hpp>
10+
11+
#include <ostream>
12+
13+
namespace carla {
14+
namespace client {
15+
16+
std::ostream &operator<<(std::ostream &out, const Actor &actor) {
17+
out << "Actor(id=" << actor.GetId() << ", type_id=" << actor.GetTypeId() << ')';
18+
return out;
19+
}
20+
21+
} // namespace client
22+
} // namespace carla
23+
24+
void export_actor() {
25+
using namespace boost::python;
26+
namespace cc = carla::client;
27+
28+
class_<cc::Actor, boost::noncopyable, boost::shared_ptr<cc::Actor>>("Actor", no_init)
29+
.add_property("id", &cc::Actor::GetId)
30+
.add_property("type_id", +[](const cc::Actor &self) -> std::string {
31+
// Needs to copy the string by value.
32+
return self.GetTypeId();
33+
})
34+
.add_property("blueprint", &cc::Actor::GetBlueprint)
35+
.def("get_world", &cc::Actor::GetWorld)
36+
.def("apply_control", &cc::Actor::ApplyControl)
37+
.def(self_ns::str(self_ns::self))
38+
;
39+
}
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2+
// de Barcelona (UAB).
3+
//
4+
// This work is licensed under the terms of the MIT license.
5+
// For a copy, see <https://opensource.org/licenses/MIT>.
6+
7+
#include <carla/client/BlueprintLibrary.h>
8+
#include <carla/client/ActorBlueprint.h>
9+
10+
#include <boost/python.hpp>
11+
12+
#include <ostream>
13+
14+
namespace carla {
15+
namespace client {
16+
17+
std::ostream &operator<<(std::ostream &out, const ActorBlueprint &bp) {
18+
out << bp.GetTypeId();
19+
return out;
20+
}
21+
22+
std::ostream &operator<<(std::ostream &out, const BlueprintLibrary &blueprints) {
23+
auto it = blueprints.begin();
24+
out << '[' << *it;
25+
for (++it; it != blueprints.end(); ++it) {
26+
out << ", " << *it;
27+
}
28+
out << ']';
29+
return out;
30+
}
31+
32+
} // namespace client
33+
} // namespace carla
34+
35+
void export_blueprint() {
36+
using namespace boost::python;
37+
namespace cc = carla::client;
38+
39+
class_<cc::ActorBlueprint>("ActorBlueprint", no_init)
40+
.add_property("type_id", +[](const cc::ActorBlueprint &self) -> std::string {
41+
return self.GetTypeId();
42+
})
43+
.def("startswith", &cc::ActorBlueprint::StartsWith)
44+
.def("match", &cc::ActorBlueprint::MatchWildcards)
45+
.def(self_ns::str(self_ns::self))
46+
;
47+
48+
class_<cc::BlueprintLibrary>("BlueprintLibrary", no_init)
49+
.def("filter", &cc::BlueprintLibrary::Filter)
50+
.def("__getitem__", +[](const cc::BlueprintLibrary &self, size_t pos) -> cc::ActorBlueprint {
51+
return self[pos];
52+
})
53+
.def("__len__", &cc::BlueprintLibrary::size)
54+
.def("__iter__", range(&cc::BlueprintLibrary::begin, &cc::BlueprintLibrary::end))
55+
.def(self_ns::str(self_ns::self))
56+
;
57+
}

PythonAPI/source/libcarla/Client.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2+
// de Barcelona (UAB).
3+
//
4+
// This work is licensed under the terms of the MIT license.
5+
// For a copy, see <https://opensource.org/licenses/MIT>.
6+
7+
#include <carla/client/Client.h>
8+
#include <carla/client/World.h>
9+
10+
#include <boost/python.hpp>
11+
12+
void export_client() {
13+
using namespace boost::python;
14+
namespace cc = carla::client;
15+
16+
class_<cc::Client, boost::noncopyable, boost::shared_ptr<cc::Client>>("Client", init<std::string, uint16_t>())
17+
.def("set_timeout", &cc::Client::SetTimeout)
18+
.def("get_client_version", &cc::Client::GetClientVersion)
19+
.def("get_server_version", &cc::Client::GetServerVersion)
20+
.def("ping", &cc::Client::Ping)
21+
.def("get_world", &cc::Client::GetWorld)
22+
;
23+
}

PythonAPI/source/libcarla/Control.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2+
// de Barcelona (UAB).
3+
//
4+
// This work is licensed under the terms of the MIT license.
5+
// For a copy, see <https://opensource.org/licenses/MIT>.
6+
7+
#include <carla/client/Control.h>
8+
9+
#include <boost/python.hpp>
10+
11+
#include <ostream>
12+
13+
namespace carla {
14+
namespace rpc {
15+
16+
std::ostream &operator<<(std::ostream &out, const VehicleControl &control) {
17+
auto boolalpha = [](bool b) { return b ? "True" : "False"; };
18+
out << "VehicleControl(throttle=" << control.throttle
19+
<< ", steer=" << control.steer
20+
<< ", brake=" << control.brake
21+
<< ", hand_brake=" << boolalpha(control.hand_brake)
22+
<< ", reverse=" << boolalpha(control.reverse) << ')';
23+
return out;
24+
}
25+
26+
} // namespace rpc
27+
} // namespace carla
28+
29+
void export_control() {
30+
using namespace boost::python;
31+
namespace cc = carla::client;
32+
33+
class_<cc::VehicleControl>("VehicleControl")
34+
.def(init<float, float, float, bool, bool>(
35+
(arg("throttle")=0.0f,
36+
arg("steer")=0.0f,
37+
arg("brake")=0.0f,
38+
arg("hand_brake")=false,
39+
arg("reverse")=false)))
40+
.def_readwrite("throttle", &cc::VehicleControl::throttle)
41+
.def_readwrite("steer", &cc::VehicleControl::steer)
42+
.def_readwrite("brake", &cc::VehicleControl::brake)
43+
.def_readwrite("hand_brake", &cc::VehicleControl::hand_brake)
44+
.def_readwrite("reverse", &cc::VehicleControl::reverse)
45+
.def(self_ns::str(self_ns::self))
46+
;
47+
}
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2+
// de Barcelona (UAB).
3+
//
4+
// This work is licensed under the terms of the MIT license.
5+
// For a copy, see <https://opensource.org/licenses/MIT>.
6+
7+
#include <carla/client/Transform.h>
8+
9+
#include <boost/python.hpp>
10+
11+
#include <ostream>
12+
13+
namespace carla {
14+
namespace rpc {
15+
16+
std::ostream &operator<<(std::ostream &out, const Location &location) {
17+
out << "Location(x=" << location.x
18+
<< ", y=" << location.y
19+
<< ", z=" << location.z << ')';
20+
return out;
21+
}
22+
23+
std::ostream &operator<<(std::ostream &out, const Rotation &rotation) {
24+
out << "Rotation(pitch=" << rotation.pitch
25+
<< ", yaw=" << rotation.yaw
26+
<< ", roll=" << rotation.roll << ')';
27+
return out;
28+
}
29+
30+
std::ostream &operator<<(std::ostream &out, const Transform &transform) {
31+
out << "Transform(" << transform.location << ", " << transform.rotation << ')';
32+
return out;
33+
}
34+
35+
} // namespace rpc
36+
} // namespace carla
37+
38+
void export_transform() {
39+
using namespace boost::python;
40+
namespace cc = carla::client;
41+
42+
class_<cc::Location>("Location")
43+
.def(init<float, float, float>((arg("x")=0.0f, arg("y")=0.0f, arg("z")=0.0f)))
44+
.def_readwrite("x", &cc::Location::x)
45+
.def_readwrite("y", &cc::Location::y)
46+
.def_readwrite("z", &cc::Location::z)
47+
.def(self_ns::str(self_ns::self))
48+
;
49+
50+
class_<cc::Rotation>("Rotation")
51+
.def(init<float, float, float>((arg("pitch")=0.0f, arg("yaw")=0.0f, arg("roll")=0.0f)))
52+
.def_readwrite("pitch", &cc::Rotation::pitch)
53+
.def_readwrite("yaw", &cc::Rotation::yaw)
54+
.def_readwrite("roll", &cc::Rotation::roll)
55+
.def(self_ns::str(self_ns::self))
56+
;
57+
58+
class_<cc::Transform>("Transform")
59+
.def(init<cc::Location, cc::Rotation>(
60+
(arg("location")=cc::Location(), arg("rotation")=cc::Rotation())))
61+
.def_readwrite("location", &cc::Transform::location)
62+
.def_readwrite("rotation", &cc::Transform::rotation)
63+
.def(self_ns::str(self_ns::self))
64+
;
65+
}

PythonAPI/source/libcarla/World.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2+
// de Barcelona (UAB).
3+
//
4+
// This work is licensed under the terms of the MIT license.
5+
// For a copy, see <https://opensource.org/licenses/MIT>.
6+
7+
#include <carla/client/Actor.h>
8+
#include <carla/client/World.h>
9+
10+
#include <boost/python.hpp>
11+
12+
void export_world() {
13+
using namespace boost::python;
14+
namespace cc = carla::client;
15+
16+
class_<cc::World, boost::noncopyable, boost::shared_ptr<cc::World>>("World", no_init)
17+
.def("get_blueprint_library", &cc::World::GetBlueprintLibrary)
18+
.def("try_spawn_actor", &cc::World::TrySpawnActor)
19+
.def("spawn_actor", &cc::World::SpawnActor)
20+
;
21+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2+
// de Barcelona (UAB).
3+
//
4+
// This work is licensed under the terms of the MIT license.
5+
// For a copy, see <https://opensource.org/licenses/MIT>.
6+
7+
#include <boost/python.hpp>
8+
9+
void export_actor();
10+
void export_blueprint();
11+
void export_client();
12+
void export_control();
13+
void export_transform();
14+
void export_world();
15+
16+
BOOST_PYTHON_MODULE(libcarla) {
17+
using namespace boost::python;
18+
scope().attr("__path__") = "libcarla";
19+
export_transform();
20+
export_control();
21+
export_blueprint();
22+
export_actor();
23+
export_world();
24+
export_client();
25+
}

PythonAPI/test/__init__.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma de
2+
# Barcelona (UAB).
3+
#
4+
# This work is licensed under the terms of the MIT license.
5+
# For a copy, see <https://opensource.org/licenses/MIT>.
6+
7+
import sys
8+
9+
sys.path.append(
10+
'../dist/carla-0.9.0-py%d.%d-linux-x86_64.egg' % (sys.version_info.major,
11+
sys.version_info.minor))

PythonAPI/test/test_client.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma de
2+
# Barcelona (UAB).
3+
#
4+
# This work is licensed under the terms of the MIT license.
5+
# For a copy, see <https://opensource.org/licenses/MIT>.
6+
7+
import carla
8+
9+
import sys
10+
import unittest
11+
12+
from subprocess import check_output
13+
14+
15+
class testClient(unittest.TestCase):
16+
def test_client_version(self):
17+
c = carla.Client('localhost', 8080)
18+
v = c.get_client_version()
19+
out = check_output(['git', 'describe', '--tags', '--dirty', '--always', ])
20+
if sys.version_info > (3, 0):
21+
out = out.decode('utf8')
22+
self.assertEqual(str(v), str(out.strip()))

0 commit comments

Comments
 (0)