kscale 0.0.1__tar.gz → 0.0.3__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- kscale-0.0.3/PKG-INFO +52 -0
- kscale-0.0.3/README.md +32 -0
- kscale-0.0.3/kscale/__init__.py +1 -0
- {kscale-0.0.1 → kscale-0.0.3}/kscale/conf.py +3 -11
- kscale-0.0.3/kscale/formats/mjcf.py +509 -0
- kscale-0.0.3/kscale/requirements.txt +8 -0
- kscale-0.0.3/kscale/store/bullet/MANIFEST.in +1 -0
- kscale-0.0.3/kscale/store/cli.py +35 -0
- kscale-0.0.3/kscale/store/pybullet.py +179 -0
- kscale-0.0.3/kscale/store/urdf.py +213 -0
- kscale-0.0.3/kscale.egg-info/PKG-INFO +52 -0
- {kscale-0.0.1 → kscale-0.0.3}/kscale.egg-info/SOURCES.txt +5 -1
- kscale-0.0.3/kscale.egg-info/entry_points.txt +2 -0
- {kscale-0.0.1 → kscale-0.0.3}/kscale.egg-info/requires.txt +2 -0
- {kscale-0.0.1 → kscale-0.0.3}/setup.cfg +4 -0
- {kscale-0.0.1 → kscale-0.0.3}/setup.py +2 -1
- kscale-0.0.1/PKG-INFO +0 -29
- kscale-0.0.1/README.md +0 -11
- kscale-0.0.1/kscale/requirements.txt +0 -3
- kscale-0.0.1/kscale/store/auth.py +0 -13
- kscale-0.0.1/kscale/store/gen/__init__.py +0 -0
- kscale-0.0.1/kscale/store/urdf.py +0 -1
- kscale-0.0.1/kscale.egg-info/PKG-INFO +0 -29
- {kscale-0.0.1 → kscale-0.0.3}/LICENSE +0 -0
- {kscale-0.0.1 → kscale-0.0.3}/MANIFEST.in +0 -0
- {kscale-0.0.1 → kscale-0.0.3}/kscale/py.typed +0 -0
- {kscale-0.0.1 → kscale-0.0.3}/kscale/requirements-dev.txt +0 -0
- {kscale-0.0.1/kscale → kscale-0.0.3/kscale/store}/__init__.py +0 -0
- {kscale-0.0.1/kscale/store → kscale-0.0.3/kscale/store/gen}/__init__.py +0 -0
- {kscale-0.0.1 → kscale-0.0.3}/kscale/store/gen/api.py +0 -0
- {kscale-0.0.1 → kscale-0.0.3}/kscale.egg-info/dependency_links.txt +0 -0
- {kscale-0.0.1 → kscale-0.0.3}/kscale.egg-info/top_level.txt +0 -0
- {kscale-0.0.1 → kscale-0.0.3}/pyproject.toml +0 -0
- {kscale-0.0.1 → kscale-0.0.3}/tests/test_dummy.py +0 -0
kscale-0.0.3/PKG-INFO
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: kscale
|
3
|
+
Version: 0.0.3
|
4
|
+
Summary: The kscale project
|
5
|
+
Home-page: https://github.com/kscalelabs/kscale
|
6
|
+
Author: Benjamin Bolte
|
7
|
+
Requires-Python: >=3.11
|
8
|
+
Description-Content-Type: text/markdown
|
9
|
+
License-File: LICENSE
|
10
|
+
Requires-Dist: omegaconf
|
11
|
+
Requires-Dist: httpx
|
12
|
+
Requires-Dist: requests
|
13
|
+
Provides-Extra: dev
|
14
|
+
Requires-Dist: black; extra == "dev"
|
15
|
+
Requires-Dist: darglint; extra == "dev"
|
16
|
+
Requires-Dist: mypy; extra == "dev"
|
17
|
+
Requires-Dist: pytest; extra == "dev"
|
18
|
+
Requires-Dist: ruff; extra == "dev"
|
19
|
+
Requires-Dist: datamodel-code-generator; extra == "dev"
|
20
|
+
|
21
|
+
<p align="center">
|
22
|
+
<picture>
|
23
|
+
<img alt="K-Scale Open Source Robotics" src="https://media.kscale.dev/kscale-open-source-header.png" style="max-width: 100%;">
|
24
|
+
</picture>
|
25
|
+
</p>
|
26
|
+
|
27
|
+
<div align="center">
|
28
|
+
|
29
|
+
[![License](https://img.shields.io/badge/license-MIT-green)](https://github.com/kscalelabs/ksim/blob/main/LICENSE)
|
30
|
+
[![Discord](https://img.shields.io/discord/1224056091017478166)](https://discord.gg/k5mSvCkYQh)
|
31
|
+
[![Wiki](https://img.shields.io/badge/wiki-humanoids-black)](https://humanoids.wiki)
|
32
|
+
<br />
|
33
|
+
[![python](https://img.shields.io/badge/-Python_3.11-blue?logo=python&logoColor=white)](https://github.com/pre-commit/pre-commit)
|
34
|
+
[![black](https://img.shields.io/badge/Code%20Style-Black-black.svg?labelColor=gray)](https://black.readthedocs.io/en/stable/)
|
35
|
+
[![ruff](https://img.shields.io/badge/Linter-Ruff-red.svg?labelColor=gray)](https://github.com/charliermarsh/ruff)
|
36
|
+
<br />
|
37
|
+
[![Python Checks](https://github.com/kscalelabs/kscale/actions/workflows/test.yml/badge.svg)](https://github.com/kscalelabs/kscale/actions/workflows/test.yml)
|
38
|
+
[![Publish Python Package](https://github.com/kscalelabs/kscale/actions/workflows/publish.yml/badge.svg)](https://github.com/kscalelabs/kscale/actions/workflows/publish.yml)
|
39
|
+
|
40
|
+
</div>
|
41
|
+
|
42
|
+
# K-Scale Command Line Interface
|
43
|
+
|
44
|
+
This is a command line tool for interacting with various services provided by K-Scale Labs, such as:
|
45
|
+
|
46
|
+
- [K-Scale Store](https://kscale.store/)
|
47
|
+
|
48
|
+
## Installation
|
49
|
+
|
50
|
+
```bash
|
51
|
+
pip install kscale
|
52
|
+
```
|
kscale-0.0.3/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
<p align="center">
|
2
|
+
<picture>
|
3
|
+
<img alt="K-Scale Open Source Robotics" src="https://media.kscale.dev/kscale-open-source-header.png" style="max-width: 100%;">
|
4
|
+
</picture>
|
5
|
+
</p>
|
6
|
+
|
7
|
+
<div align="center">
|
8
|
+
|
9
|
+
[![License](https://img.shields.io/badge/license-MIT-green)](https://github.com/kscalelabs/ksim/blob/main/LICENSE)
|
10
|
+
[![Discord](https://img.shields.io/discord/1224056091017478166)](https://discord.gg/k5mSvCkYQh)
|
11
|
+
[![Wiki](https://img.shields.io/badge/wiki-humanoids-black)](https://humanoids.wiki)
|
12
|
+
<br />
|
13
|
+
[![python](https://img.shields.io/badge/-Python_3.11-blue?logo=python&logoColor=white)](https://github.com/pre-commit/pre-commit)
|
14
|
+
[![black](https://img.shields.io/badge/Code%20Style-Black-black.svg?labelColor=gray)](https://black.readthedocs.io/en/stable/)
|
15
|
+
[![ruff](https://img.shields.io/badge/Linter-Ruff-red.svg?labelColor=gray)](https://github.com/charliermarsh/ruff)
|
16
|
+
<br />
|
17
|
+
[![Python Checks](https://github.com/kscalelabs/kscale/actions/workflows/test.yml/badge.svg)](https://github.com/kscalelabs/kscale/actions/workflows/test.yml)
|
18
|
+
[![Publish Python Package](https://github.com/kscalelabs/kscale/actions/workflows/publish.yml/badge.svg)](https://github.com/kscalelabs/kscale/actions/workflows/publish.yml)
|
19
|
+
|
20
|
+
</div>
|
21
|
+
|
22
|
+
# K-Scale Command Line Interface
|
23
|
+
|
24
|
+
This is a command line tool for interacting with various services provided by K-Scale Labs, such as:
|
25
|
+
|
26
|
+
- [K-Scale Store](https://kscale.store/)
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
|
30
|
+
```bash
|
31
|
+
pip install kscale
|
32
|
+
```
|
@@ -0,0 +1 @@
|
|
1
|
+
__version__ = "0.0.3"
|
@@ -17,21 +17,13 @@ def get_path() -> Path:
|
|
17
17
|
|
18
18
|
@dataclass
|
19
19
|
class StoreSettings:
|
20
|
-
api_key: str = field(default=II("oc.env:KSCALE_API_KEY"))
|
21
|
-
|
22
|
-
def get_api_key(self) -> str:
|
23
|
-
try:
|
24
|
-
return self.api_key
|
25
|
-
except AttributeError:
|
26
|
-
raise ValueError(
|
27
|
-
"API key not found! Get one here and set it as the `KSCALE_API_KEY` "
|
28
|
-
"environment variable: https://kscale.store/keys"
|
29
|
-
)
|
20
|
+
api_key: str = field(default=II("oc.env:KSCALE_API_KEY,"))
|
21
|
+
cache_dir: str = field(default=II("oc.env:KSCALE_CACHE_DIR,'~/.kscale/cache/'"))
|
30
22
|
|
31
23
|
|
32
24
|
@dataclass
|
33
25
|
class Settings:
|
34
|
-
store: StoreSettings = StoreSettings
|
26
|
+
store: StoreSettings = field(default_factory=StoreSettings)
|
35
27
|
|
36
28
|
def save(self) -> None:
|
37
29
|
(dir_path := get_path()).mkdir(parents=True, exist_ok=True)
|
@@ -0,0 +1,509 @@
|
|
1
|
+
"""Defines common types and functions for exporting MJCF files.
|
2
|
+
|
3
|
+
API reference:
|
4
|
+
https://github.com/google-deepmind/mujoco/blob/main/src/xml/xml_native_writer.cc#L780
|
5
|
+
|
6
|
+
Todo:
|
7
|
+
1. Clean up the inertia config
|
8
|
+
2. Add visual and imu support
|
9
|
+
"""
|
10
|
+
|
11
|
+
import glob
|
12
|
+
import os
|
13
|
+
import shutil
|
14
|
+
import xml.etree.ElementTree as ET
|
15
|
+
from dataclasses import dataclass, field
|
16
|
+
from pathlib import Path
|
17
|
+
from typing import Literal
|
18
|
+
|
19
|
+
|
20
|
+
@dataclass
|
21
|
+
class Compiler:
|
22
|
+
coordinate: Literal["local", "global"] | None = None
|
23
|
+
angle: Literal["radian", "degree"] = "radian"
|
24
|
+
meshdir: str | None = None
|
25
|
+
eulerseq: Literal["xyz", "zxy", "zyx", "yxz", "yzx", "xzy"] | None = None
|
26
|
+
autolimits: bool | None = None
|
27
|
+
|
28
|
+
def to_xml(self, compiler: ET.Element | None = None) -> ET.Element:
|
29
|
+
if compiler is None:
|
30
|
+
compiler = ET.Element("compiler")
|
31
|
+
if self.coordinate is not None:
|
32
|
+
compiler.set("coordinate", self.coordinate)
|
33
|
+
compiler.set("angle", self.angle)
|
34
|
+
if self.meshdir is not None:
|
35
|
+
compiler.set("meshdir", self.meshdir)
|
36
|
+
if self.eulerseq is not None:
|
37
|
+
compiler.set("eulerseq", self.eulerseq)
|
38
|
+
if self.autolimits is not None:
|
39
|
+
compiler.set("autolimits", str(self.autolimits).lower())
|
40
|
+
return compiler
|
41
|
+
|
42
|
+
|
43
|
+
@dataclass
|
44
|
+
class Mesh:
|
45
|
+
name: str
|
46
|
+
file: str
|
47
|
+
scale: tuple[float, float, float] | None = None
|
48
|
+
|
49
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
50
|
+
mesh = ET.Element("mesh") if root is None else ET.SubElement(root, "mesh")
|
51
|
+
mesh.set("name", self.name)
|
52
|
+
mesh.set("file", self.file)
|
53
|
+
if self.scale is not None:
|
54
|
+
mesh.set("scale", " ".join(map(str, self.scale)))
|
55
|
+
return mesh
|
56
|
+
|
57
|
+
|
58
|
+
@dataclass
|
59
|
+
class Joint:
|
60
|
+
name: str | None = None
|
61
|
+
type: Literal["hinge", "slide", "ball", "free"] | None = None
|
62
|
+
pos: tuple[float, float, float] | None = None
|
63
|
+
axis: tuple[float, float, float] | None = None
|
64
|
+
limited: bool | None = None
|
65
|
+
range: tuple[float, float] | None = None
|
66
|
+
damping: float | None = None
|
67
|
+
stiffness: float | None = None
|
68
|
+
armature: float | None = None
|
69
|
+
frictionloss: float | None = None
|
70
|
+
|
71
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
72
|
+
joint = ET.Element("joint") if root is None else ET.SubElement(root, "joint")
|
73
|
+
if self.name is not None:
|
74
|
+
joint.set("name", self.name)
|
75
|
+
if self.type is not None:
|
76
|
+
joint.set("type", self.type)
|
77
|
+
if self.pos is not None:
|
78
|
+
joint.set("pos", " ".join(map(str, self.pos)))
|
79
|
+
if self.axis is not None:
|
80
|
+
joint.set("axis", " ".join(map(str, self.axis)))
|
81
|
+
if self.range is not None:
|
82
|
+
joint.set("range", " ".join(map(str, self.range)))
|
83
|
+
if self.limited is not None:
|
84
|
+
joint.set("limited", str(self.limited).lower())
|
85
|
+
if self.damping is not None:
|
86
|
+
joint.set("damping", str(self.damping))
|
87
|
+
if self.stiffness is not None:
|
88
|
+
joint.set("stiffness", str(self.stiffness))
|
89
|
+
if self.armature is not None:
|
90
|
+
joint.set("armature", str(self.armature))
|
91
|
+
if self.frictionloss is not None:
|
92
|
+
joint.set("frictionloss", str(self.frictionloss))
|
93
|
+
return joint
|
94
|
+
|
95
|
+
|
96
|
+
@dataclass
|
97
|
+
class Geom:
|
98
|
+
name: str | None = None
|
99
|
+
type: Literal["plane", "sphere", "cylinder", "box", "capsule", "ellipsoid", "mesh"] | None = None
|
100
|
+
plane: str | None = None
|
101
|
+
rgba: tuple[float, float, float, float] | None = None
|
102
|
+
pos: tuple[float, float, float] | None = None
|
103
|
+
quat: tuple[float, float, float, float] | None = None
|
104
|
+
matplane: str | None = None
|
105
|
+
material: str | None = None
|
106
|
+
condim: int | None = None
|
107
|
+
contype: int | None = None
|
108
|
+
conaffinity: int | None = None
|
109
|
+
size: tuple[float, float, float] | None = None
|
110
|
+
friction: tuple[float, float, float] | None = None
|
111
|
+
solref: tuple[float, float] | None = None
|
112
|
+
density: float | None = None
|
113
|
+
|
114
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
115
|
+
geom = ET.Element("geom") if root is None else ET.SubElement(root, "geom")
|
116
|
+
if self.name is not None:
|
117
|
+
geom.set("name", self.name)
|
118
|
+
if self.type is not None:
|
119
|
+
geom.set("type", self.type)
|
120
|
+
if self.rgba is not None:
|
121
|
+
geom.set("rgba", " ".join(map(str, self.rgba)))
|
122
|
+
if self.pos is not None:
|
123
|
+
geom.set("pos", " ".join(map(str, self.pos)))
|
124
|
+
if self.quat is not None:
|
125
|
+
geom.set("quat", " ".join(map(str, self.quat)))
|
126
|
+
if self.matplane is not None:
|
127
|
+
geom.set("matplane", self.matplane)
|
128
|
+
if self.material is not None:
|
129
|
+
geom.set("material", self.material)
|
130
|
+
if self.condim is not None:
|
131
|
+
geom.set("condim", str(self.condim))
|
132
|
+
if self.contype is not None:
|
133
|
+
geom.set("contype", str(self.contype))
|
134
|
+
if self.conaffinity is not None:
|
135
|
+
geom.set("conaffinity", str(self.conaffinity))
|
136
|
+
if self.plane is not None:
|
137
|
+
geom.set("plane", self.plane)
|
138
|
+
if self.size is not None:
|
139
|
+
geom.set("size", " ".join(map(str, self.size)))
|
140
|
+
if self.friction is not None:
|
141
|
+
geom.set("friction", " ".join(map(str, self.friction)))
|
142
|
+
if self.solref is not None:
|
143
|
+
geom.set("solref", " ".join(map(str, self.solref)))
|
144
|
+
if self.density is not None:
|
145
|
+
geom.set("density", str(self.density))
|
146
|
+
return geom
|
147
|
+
|
148
|
+
|
149
|
+
@dataclass
|
150
|
+
class Body:
|
151
|
+
name: str
|
152
|
+
pos: tuple[float, float, float] | None = field(default=None)
|
153
|
+
quat: tuple[float, float, float, float] | None = field(default=None)
|
154
|
+
geom: Geom | None = field(default=None)
|
155
|
+
joint: Joint | None = field(default=None)
|
156
|
+
|
157
|
+
# TODO - Fix inertia, until then rely on Mujoco's engine
|
158
|
+
# inertial: Inertial = None
|
159
|
+
|
160
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
161
|
+
body = ET.Element("body") if root is None else ET.SubElement(root, "body")
|
162
|
+
body.set("name", self.name)
|
163
|
+
if self.pos is not None:
|
164
|
+
body.set("pos", " ".join(map(str, self.pos)))
|
165
|
+
if self.quat is not None:
|
166
|
+
body.set("quat", " ".join(f"{x:.8g}" for x in self.quat))
|
167
|
+
if self.joint is not None:
|
168
|
+
self.joint.to_xml(body)
|
169
|
+
if self.geom is not None:
|
170
|
+
self.geom.to_xml(body)
|
171
|
+
return body
|
172
|
+
|
173
|
+
|
174
|
+
@dataclass
|
175
|
+
class Flag:
|
176
|
+
frictionloss: str | None = None
|
177
|
+
# removed at 3.1.4
|
178
|
+
# sensornoise: str | None = None
|
179
|
+
|
180
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
181
|
+
flag = ET.Element("flag") if root is None else ET.SubElement(root, "flag")
|
182
|
+
if self.frictionloss is not None:
|
183
|
+
flag.set("frictionloss", self.frictionloss)
|
184
|
+
return flag
|
185
|
+
|
186
|
+
|
187
|
+
@dataclass
|
188
|
+
class Option:
|
189
|
+
timestep: float | None = None
|
190
|
+
viscosity: float | None = None
|
191
|
+
iterations: int | None = None
|
192
|
+
solver: Literal["PGS", "CG", "Newton"] | None = None
|
193
|
+
gravity: tuple[float, float, float] | None = None
|
194
|
+
flag: Flag | None = None
|
195
|
+
|
196
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
197
|
+
if root is None:
|
198
|
+
option = ET.Element("option")
|
199
|
+
else:
|
200
|
+
option = ET.SubElement(root, "option")
|
201
|
+
if self.iterations is not None:
|
202
|
+
option.set("iterations", str(self.iterations))
|
203
|
+
if self.timestep is not None:
|
204
|
+
option.set("timestep", str(self.timestep))
|
205
|
+
if self.viscosity is not None:
|
206
|
+
option.set("viscosity", str(self.viscosity))
|
207
|
+
if self.solver is not None:
|
208
|
+
option.set("solver", self.solver)
|
209
|
+
if self.gravity is not None:
|
210
|
+
option.set("gravity", " ".join(map(str, self.gravity)))
|
211
|
+
if self.flag is not None:
|
212
|
+
self.flag.to_xml(option)
|
213
|
+
return option
|
214
|
+
|
215
|
+
|
216
|
+
@dataclass
|
217
|
+
class Motor:
|
218
|
+
name: str | None = None
|
219
|
+
joint: str | None = None
|
220
|
+
ctrlrange: tuple[float, float] | None = None
|
221
|
+
ctrllimited: bool | None = None
|
222
|
+
gear: float | None = None
|
223
|
+
|
224
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
225
|
+
if root is None:
|
226
|
+
motor = ET.Element("motor")
|
227
|
+
else:
|
228
|
+
motor = ET.SubElement(root, "motor")
|
229
|
+
if self.name is not None:
|
230
|
+
motor.set("name", self.name)
|
231
|
+
if self.joint is not None:
|
232
|
+
motor.set("joint", self.joint)
|
233
|
+
if self.ctrllimited is not None:
|
234
|
+
motor.set("ctrllimited", str(self.ctrllimited).lower())
|
235
|
+
if self.ctrlrange is not None:
|
236
|
+
motor.set("ctrlrange", " ".join(map(str, self.ctrlrange)))
|
237
|
+
if self.gear is not None:
|
238
|
+
motor.set("gear", str(self.gear))
|
239
|
+
return motor
|
240
|
+
|
241
|
+
|
242
|
+
@dataclass
|
243
|
+
class Light:
|
244
|
+
directional: bool = True
|
245
|
+
diffuse: tuple[float, float, float] | None = None
|
246
|
+
specular: tuple[float, float, float] | None = None
|
247
|
+
pos: tuple[float, float, float] | None = None
|
248
|
+
dir: tuple[float, float, float] | None = None
|
249
|
+
castshadow: bool | None = None
|
250
|
+
|
251
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
252
|
+
if root is None:
|
253
|
+
light = ET.Element("light")
|
254
|
+
else:
|
255
|
+
light = ET.SubElement(root, "light")
|
256
|
+
if self.directional is not None:
|
257
|
+
light.set("directional", str(self.directional).lower())
|
258
|
+
if self.diffuse is not None:
|
259
|
+
light.set("diffuse", " ".join(map(str, self.diffuse)))
|
260
|
+
if self.specular is not None:
|
261
|
+
light.set("specular", " ".join(map(str, self.specular)))
|
262
|
+
if self.pos is not None:
|
263
|
+
light.set("pos", " ".join(map(str, self.pos)))
|
264
|
+
if self.dir is not None:
|
265
|
+
light.set("dir", " ".join(map(str, self.dir)))
|
266
|
+
if self.castshadow is not None:
|
267
|
+
light.set("castshadow", str(self.castshadow).lower())
|
268
|
+
return light
|
269
|
+
|
270
|
+
|
271
|
+
@dataclass
|
272
|
+
class Equality:
|
273
|
+
solref: tuple[float, float]
|
274
|
+
|
275
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
276
|
+
equality = ET.Element("equality") if root is None else ET.SubElement(root, "equality")
|
277
|
+
equality.set("solref", " ".join(map(str, self.solref)))
|
278
|
+
return equality
|
279
|
+
|
280
|
+
|
281
|
+
@dataclass
|
282
|
+
class Site:
|
283
|
+
name: str | None = None
|
284
|
+
size: float | None = None
|
285
|
+
pos: tuple[float, float, float] | None = None
|
286
|
+
|
287
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
288
|
+
site = ET.Element("site") if root is None else ET.SubElement(root, "site")
|
289
|
+
if self.name is not None:
|
290
|
+
site.set("name", self.name)
|
291
|
+
if self.size is not None:
|
292
|
+
site.set("size", str(self.size))
|
293
|
+
if self.pos is not None:
|
294
|
+
site.set("pos", " ".join(map(str, self.pos)))
|
295
|
+
return site
|
296
|
+
|
297
|
+
|
298
|
+
@dataclass
|
299
|
+
class Default:
|
300
|
+
joint: Joint | None = None
|
301
|
+
geom: Geom | None = None
|
302
|
+
class_: str | None = None
|
303
|
+
motor: Motor | None = None
|
304
|
+
equality: Equality | None = None
|
305
|
+
visual_geom: ET.Element | None = None
|
306
|
+
|
307
|
+
def to_xml(self, default: ET.Element | None = None) -> ET.Element:
|
308
|
+
default = ET.Element("default") if default is None else ET.SubElement(default, "default")
|
309
|
+
if self.joint is not None:
|
310
|
+
self.joint.to_xml(default)
|
311
|
+
if self.geom is not None:
|
312
|
+
self.geom.to_xml(default)
|
313
|
+
if self.class_ is not None:
|
314
|
+
default.set("class", self.class_)
|
315
|
+
if self.motor is not None:
|
316
|
+
self.motor.to_xml(default)
|
317
|
+
if self.equality is not None:
|
318
|
+
self.equality.to_xml(default)
|
319
|
+
if self.visual_geom is not None:
|
320
|
+
default.append(self.visual_geom)
|
321
|
+
return default
|
322
|
+
|
323
|
+
|
324
|
+
@dataclass
|
325
|
+
class Actuator:
|
326
|
+
motors: list[Motor]
|
327
|
+
|
328
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
329
|
+
actuator = ET.Element("actuator") if root is None else ET.SubElement(root, "actuator")
|
330
|
+
for motor in self.motors:
|
331
|
+
motor.to_xml(actuator)
|
332
|
+
return actuator
|
333
|
+
|
334
|
+
|
335
|
+
@dataclass
|
336
|
+
class Actuatorpos:
|
337
|
+
name: str | None = None
|
338
|
+
actuator: str | None = None
|
339
|
+
user: str | None = None
|
340
|
+
|
341
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
342
|
+
actuatorpos = ET.Element("actuatorpos") if root is None else ET.SubElement(root, "actuatorpos")
|
343
|
+
if self.name is not None:
|
344
|
+
actuatorpos.set("name", self.name)
|
345
|
+
if self.actuator is not None:
|
346
|
+
actuatorpos.set("actuator", self.actuator)
|
347
|
+
if self.user is not None:
|
348
|
+
actuatorpos.set("user", self.user)
|
349
|
+
return actuatorpos
|
350
|
+
|
351
|
+
|
352
|
+
@dataclass
|
353
|
+
class Actuatorvel:
|
354
|
+
name: str | None = None
|
355
|
+
actuator: str | None = None
|
356
|
+
user: str | None = None
|
357
|
+
|
358
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
359
|
+
actuatorvel = ET.Element("actuatorvel") if root is None else ET.SubElement(root, "actuatorvel")
|
360
|
+
if self.name is not None:
|
361
|
+
actuatorvel.set("name", self.name)
|
362
|
+
if self.actuator is not None:
|
363
|
+
actuatorvel.set("actuator", self.actuator)
|
364
|
+
if self.user is not None:
|
365
|
+
actuatorvel.set("user", self.user)
|
366
|
+
return actuatorvel
|
367
|
+
|
368
|
+
|
369
|
+
@dataclass
|
370
|
+
class Actuatorfrc:
|
371
|
+
name: str | None = None
|
372
|
+
actuator: str | None = None
|
373
|
+
user: str | None = None
|
374
|
+
noise: float | None = None
|
375
|
+
|
376
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
377
|
+
actuatorfrc = ET.Element("actuatorfrc") if root is None else ET.SubElement(root, "actuatorfrc")
|
378
|
+
if self.name is not None:
|
379
|
+
actuatorfrc.set("name", self.name)
|
380
|
+
if self.actuator is not None:
|
381
|
+
actuatorfrc.set("actuator", self.actuator)
|
382
|
+
if self.user is not None:
|
383
|
+
actuatorfrc.set("user", self.user)
|
384
|
+
if self.noise is not None:
|
385
|
+
actuatorfrc.set("noise", str(self.noise))
|
386
|
+
return actuatorfrc
|
387
|
+
|
388
|
+
|
389
|
+
@dataclass
|
390
|
+
class Sensor:
|
391
|
+
actuatorpos: list[Actuatorpos] | None = None
|
392
|
+
actuatorvel: list[Actuatorvel] | None = None
|
393
|
+
actuatorfrc: list[Actuatorfrc] | None = None
|
394
|
+
|
395
|
+
def to_xml(self, root: ET.Element | None = None) -> ET.Element:
|
396
|
+
sensor = ET.Element("sensor") if root is None else ET.SubElement(root, "sensor")
|
397
|
+
if self.actuatorpos is not None:
|
398
|
+
for actuatorpos in self.actuatorpos:
|
399
|
+
actuatorpos.to_xml(sensor)
|
400
|
+
if self.actuatorvel is not None:
|
401
|
+
for actuatorvel in self.actuatorvel:
|
402
|
+
actuatorvel.to_xml(sensor)
|
403
|
+
if self.actuatorfrc is not None:
|
404
|
+
for actuatorfrc in self.actuatorfrc:
|
405
|
+
actuatorfrc.to_xml(sensor)
|
406
|
+
return sensor
|
407
|
+
|
408
|
+
|
409
|
+
def _copy_stl_files(source_directory: str | Path, destination_directory: str | Path) -> None:
|
410
|
+
# Ensure the destination directory exists, create if not
|
411
|
+
os.makedirs(destination_directory, exist_ok=True)
|
412
|
+
|
413
|
+
# Use glob to find all .stl files in the source directory
|
414
|
+
pattern = os.path.join(source_directory, "*.stl")
|
415
|
+
for file_path in glob.glob(pattern):
|
416
|
+
destination_path = os.path.join(destination_directory, os.path.basename(file_path))
|
417
|
+
shutil.copy(file_path, destination_path)
|
418
|
+
print(f"Copied {file_path} to {destination_path}")
|
419
|
+
|
420
|
+
|
421
|
+
def _remove_stl_files(source_directory: str | Path) -> None:
|
422
|
+
for filename in os.listdir(source_directory):
|
423
|
+
if filename.endswith(".stl"):
|
424
|
+
file_path = os.path.join(source_directory, filename)
|
425
|
+
os.remove(file_path)
|
426
|
+
|
427
|
+
|
428
|
+
class Robot:
|
429
|
+
"""A class to adapt the world in a Mujoco XML file."""
|
430
|
+
|
431
|
+
def __init__(
|
432
|
+
self,
|
433
|
+
robot_name: str,
|
434
|
+
output_dir: str | Path,
|
435
|
+
compiler: Compiler | None = None,
|
436
|
+
) -> None:
|
437
|
+
"""Initialize the robot.
|
438
|
+
|
439
|
+
Args:
|
440
|
+
robot_name (str): The name of the robot.
|
441
|
+
output_dir (str | Path): The output directory.
|
442
|
+
compiler (Compiler, optional): The compiler settings.
|
443
|
+
"""
|
444
|
+
self.robot_name = robot_name
|
445
|
+
self.output_dir = Path(output_dir)
|
446
|
+
self.compiler = compiler
|
447
|
+
self._set_clean_up()
|
448
|
+
self.tree = ET.parse(self.output_dir / f"{self.robot_name}.xml")
|
449
|
+
|
450
|
+
def _set_clean_up(self) -> None:
|
451
|
+
try:
|
452
|
+
import mujoco # type: ignore[import-not-found]
|
453
|
+
except ImportError as e:
|
454
|
+
raise ImportError(
|
455
|
+
"Please install the package with Mujoco as a dependency, using "
|
456
|
+
"`pip install kscale-onshape-library[mujoco]`"
|
457
|
+
) from e
|
458
|
+
|
459
|
+
# HACK
|
460
|
+
# mujoco has a hard time reading meshes
|
461
|
+
_copy_stl_files(self.output_dir / "meshes", self.output_dir)
|
462
|
+
|
463
|
+
urdf_tree = ET.parse(self.output_dir / f"{self.robot_name}.urdf")
|
464
|
+
root = urdf_tree.getroot()
|
465
|
+
|
466
|
+
tree = ET.ElementTree(root)
|
467
|
+
tree.write(self.output_dir / f"{self.robot_name}.urdf", encoding="utf-8")
|
468
|
+
model = mujoco.MjModel.from_xml_path((self.output_dir / f"{self.robot_name}.urdf").as_posix())
|
469
|
+
mujoco.mj_saveLastXML((self.output_dir / f"{self.robot_name}.xml").as_posix(), model)
|
470
|
+
|
471
|
+
# Removes all the existing files.
|
472
|
+
_remove_stl_files(self.output_dir)
|
473
|
+
|
474
|
+
def adapt_world(self) -> None:
|
475
|
+
root = self.tree.getroot()
|
476
|
+
|
477
|
+
# Turn off internal collisions
|
478
|
+
for element in root:
|
479
|
+
if element.tag == "geom":
|
480
|
+
element.attrib["contype"] = str(1)
|
481
|
+
element.attrib["conaffinity"] = str(0)
|
482
|
+
|
483
|
+
compiler = root.find("compiler")
|
484
|
+
if self.compiler is not None:
|
485
|
+
compiler = self.compiler.to_xml(compiler)
|
486
|
+
|
487
|
+
worldbody = root.find("worldbody")
|
488
|
+
if worldbody is None:
|
489
|
+
raise ValueError("No worldbody found in the XML file")
|
490
|
+
|
491
|
+
new_root_body = Body(name="root", pos=(0, 0, 0), quat=(1, 0, 0, 0)).to_xml()
|
492
|
+
|
493
|
+
# List to store items to be moved to the new root body
|
494
|
+
items_to_move = []
|
495
|
+
# Gather all children (geoms and bodies) that need to be moved under the new root body
|
496
|
+
for element in worldbody:
|
497
|
+
items_to_move.append(element)
|
498
|
+
|
499
|
+
# Move gathered elements to the new root body
|
500
|
+
for item in items_to_move:
|
501
|
+
worldbody.remove(item)
|
502
|
+
new_root_body.append(item)
|
503
|
+
|
504
|
+
# Add the new root body to the worldbody
|
505
|
+
worldbody.append(new_root_body)
|
506
|
+
self.tree = ET.ElementTree(root)
|
507
|
+
|
508
|
+
def save(self, path: str | Path) -> None:
|
509
|
+
self.tree.write(path, encoding="utf-8")
|
@@ -0,0 +1 @@
|
|
1
|
+
include . *.obj *.urdf
|
@@ -0,0 +1,35 @@
|
|
1
|
+
"""Defines the top-level KOL CLI."""
|
2
|
+
|
3
|
+
import argparse
|
4
|
+
import asyncio
|
5
|
+
from typing import Sequence
|
6
|
+
|
7
|
+
from kscale.store import pybullet, urdf
|
8
|
+
|
9
|
+
|
10
|
+
async def main(args: Sequence[str] | None = None) -> None:
|
11
|
+
parser = argparse.ArgumentParser(description="K-Scale OnShape Library", add_help=False)
|
12
|
+
parser.add_argument(
|
13
|
+
"subcommand",
|
14
|
+
choices=[
|
15
|
+
"urdf",
|
16
|
+
"pybullet",
|
17
|
+
],
|
18
|
+
help="The subcommand to run",
|
19
|
+
)
|
20
|
+
parsed_args, remaining_args = parser.parse_known_args(args)
|
21
|
+
|
22
|
+
match parsed_args.subcommand:
|
23
|
+
case "urdf":
|
24
|
+
await urdf.main(remaining_args)
|
25
|
+
case "pybullet":
|
26
|
+
await pybullet.main(remaining_args)
|
27
|
+
|
28
|
+
|
29
|
+
def sync_main(args: Sequence[str] | None = None) -> None:
|
30
|
+
asyncio.run(main(args))
|
31
|
+
|
32
|
+
|
33
|
+
if __name__ == "__main__":
|
34
|
+
# python3 -m kscale.store.cli
|
35
|
+
sync_main()
|
@@ -0,0 +1,179 @@
|
|
1
|
+
"""Simple script to interact with a URDF in PyBullet."""
|
2
|
+
|
3
|
+
import argparse
|
4
|
+
import asyncio
|
5
|
+
import itertools
|
6
|
+
import logging
|
7
|
+
import math
|
8
|
+
import time
|
9
|
+
from pathlib import Path
|
10
|
+
from typing import Sequence
|
11
|
+
|
12
|
+
from kscale.store.urdf import download_urdf
|
13
|
+
|
14
|
+
logger = logging.getLogger(__name__)
|
15
|
+
|
16
|
+
|
17
|
+
async def main(args: Sequence[str] | None = None) -> None:
|
18
|
+
parser = argparse.ArgumentParser(description="Show a URDF in PyBullet")
|
19
|
+
parser.add_argument("listing_id", help="Listing ID for the URDF")
|
20
|
+
parser.add_argument("--dt", type=float, default=0.01, help="Time step")
|
21
|
+
parser.add_argument("-n", "--hide-gui", action="store_true", help="Hide the GUI")
|
22
|
+
parser.add_argument("--no-merge", action="store_true", help="Do not merge fixed links")
|
23
|
+
parser.add_argument("--hide-origin", action="store_true", help="Do not show the origin")
|
24
|
+
parser.add_argument("--show-inertia", action="store_true", help="Visualizes the inertia frames")
|
25
|
+
parser.add_argument("--see-thru", action="store_true", help="Use see-through mode")
|
26
|
+
parser.add_argument("--show-collision", action="store_true", help="Show collision meshes")
|
27
|
+
parsed_args = parser.parse_args(args)
|
28
|
+
|
29
|
+
# Gets the URDF path.
|
30
|
+
urdf_path = await download_urdf(parsed_args.listing_id)
|
31
|
+
|
32
|
+
breakpoint()
|
33
|
+
|
34
|
+
try:
|
35
|
+
import pybullet as p # type: ignore[import-not-found]
|
36
|
+
except ImportError:
|
37
|
+
raise ImportError("pybullet is required to run this script")
|
38
|
+
|
39
|
+
# Connect to PyBullet.
|
40
|
+
p.connect(p.GUI)
|
41
|
+
p.setGravity(0, 0, -9.81)
|
42
|
+
p.setRealTimeSimulation(0)
|
43
|
+
|
44
|
+
# Turn off panels.
|
45
|
+
if parsed_args.hide_gui:
|
46
|
+
p.configureDebugVisualizer(p.COV_ENABLE_GUI, 0)
|
47
|
+
p.configureDebugVisualizer(p.COV_ENABLE_SEGMENTATION_MARK_PREVIEW, 0)
|
48
|
+
p.configureDebugVisualizer(p.COV_ENABLE_DEPTH_BUFFER_PREVIEW, 0)
|
49
|
+
p.configureDebugVisualizer(p.COV_ENABLE_RGB_BUFFER_PREVIEW, 0)
|
50
|
+
|
51
|
+
# Enable mouse picking.
|
52
|
+
p.configureDebugVisualizer(p.COV_ENABLE_MOUSE_PICKING, 1)
|
53
|
+
|
54
|
+
# Loads the floor plane.
|
55
|
+
floor = p.loadURDF(str((Path(__file__).parent / "bullet" / "plane.urdf").resolve()))
|
56
|
+
|
57
|
+
# Load the robot URDF.
|
58
|
+
start_position = [0.0, 0.0, 1.0]
|
59
|
+
start_orientation = p.getQuaternionFromEuler([0.0, 0.0, 0.0])
|
60
|
+
flags = p.URDF_USE_INERTIA_FROM_FILE
|
61
|
+
if not parsed_args.no_merge:
|
62
|
+
flags |= p.URDF_MERGE_FIXED_LINKS
|
63
|
+
robot = p.loadURDF(str(urdf_path), start_position, start_orientation, flags=flags, useFixedBase=0)
|
64
|
+
|
65
|
+
# Display collision meshes as separate object.
|
66
|
+
if parsed_args.show_collision:
|
67
|
+
collision_flags = p.URDF_USE_INERTIA_FROM_FILE | p.URDF_USE_SELF_COLLISION_EXCLUDE_ALL_PARENTS
|
68
|
+
collision = p.loadURDF(str(urdf_path), start_position, start_orientation, flags=collision_flags, useFixedBase=0)
|
69
|
+
|
70
|
+
# Make collision shapes semi-transparent.
|
71
|
+
joint_ids = [i for i in range(p.getNumJoints(collision))] + [-1]
|
72
|
+
for i in joint_ids:
|
73
|
+
p.changeVisualShape(collision, i, rgbaColor=[1, 0, 0, 0.5])
|
74
|
+
|
75
|
+
# Initializes physics parameters.
|
76
|
+
p.changeDynamics(floor, -1, lateralFriction=1, spinningFriction=-1, rollingFriction=-1)
|
77
|
+
p.setPhysicsEngineParameter(fixedTimeStep=parsed_args.dt, maxNumCmdPer1ms=1000)
|
78
|
+
|
79
|
+
# Shows the origin of the robot.
|
80
|
+
if not parsed_args.hide_origin:
|
81
|
+
p.addUserDebugLine([0, 0, 0], [0.1, 0, 0], [1, 0, 0], parentObjectUniqueId=robot, parentLinkIndex=-1)
|
82
|
+
p.addUserDebugLine([0, 0, 0], [0, 0.1, 0], [0, 1, 0], parentObjectUniqueId=robot, parentLinkIndex=-1)
|
83
|
+
p.addUserDebugLine([0, 0, 0], [0, 0, 0.1], [0, 0, 1], parentObjectUniqueId=robot, parentLinkIndex=-1)
|
84
|
+
|
85
|
+
# Make the robot see-through.
|
86
|
+
joint_ids = [i for i in range(p.getNumJoints(robot))] + [-1]
|
87
|
+
if parsed_args.see_thru:
|
88
|
+
for i in joint_ids:
|
89
|
+
p.changeVisualShape(robot, i, rgbaColor=[1, 1, 1, 0.5])
|
90
|
+
|
91
|
+
def draw_box(pt: list[list[float]], color: tuple[float, float, float], obj_id: int, link_id: int) -> None:
|
92
|
+
assert len(pt) == 8
|
93
|
+
assert all(len(p) == 3 for p in pt)
|
94
|
+
|
95
|
+
mapping = [1, 3, 0, 2]
|
96
|
+
for i in range(4):
|
97
|
+
p.addUserDebugLine(pt[i], pt[i + 4], color, 1, parentObjectUniqueId=obj_id, parentLinkIndex=link_id)
|
98
|
+
p.addUserDebugLine(pt[i], pt[mapping[i]], color, 1, parentObjectUniqueId=obj_id, parentLinkIndex=link_id)
|
99
|
+
p.addUserDebugLine(
|
100
|
+
pt[i + 4], pt[mapping[i] + 4], color, 1, parentObjectUniqueId=obj_id, parentLinkIndex=link_id
|
101
|
+
)
|
102
|
+
|
103
|
+
# Shows bounding boxes around each part of the robot representing the inertia frame.
|
104
|
+
if parsed_args.show_inertia:
|
105
|
+
for i in joint_ids:
|
106
|
+
dynamics_info = p.getDynamicsInfo(robot, i)
|
107
|
+
mass = dynamics_info[0]
|
108
|
+
if mass <= 0:
|
109
|
+
continue
|
110
|
+
inertia = dynamics_info[2]
|
111
|
+
ixx = inertia[0]
|
112
|
+
iyy = inertia[1]
|
113
|
+
izz = inertia[2]
|
114
|
+
box_scale_x = 0.5 * math.sqrt(6 * (izz + iyy - ixx) / mass)
|
115
|
+
box_scale_y = 0.5 * math.sqrt(6 * (izz + ixx - iyy) / mass)
|
116
|
+
box_scale_z = 0.5 * math.sqrt(6 * (ixx + iyy - izz) / mass)
|
117
|
+
|
118
|
+
half_extents = [box_scale_x, box_scale_y, box_scale_z]
|
119
|
+
pt = [
|
120
|
+
[x, y, z]
|
121
|
+
for x, y, z in itertools.product(
|
122
|
+
[-half_extents[0], half_extents[0]],
|
123
|
+
[-half_extents[1], half_extents[1]],
|
124
|
+
[-half_extents[2], half_extents[2]],
|
125
|
+
)
|
126
|
+
]
|
127
|
+
draw_box(pt, (1, 0, 0), robot, i)
|
128
|
+
|
129
|
+
# Show joint controller.
|
130
|
+
joints: dict[str, int] = {}
|
131
|
+
controls: dict[str, float] = {}
|
132
|
+
for i in range(p.getNumJoints(robot)):
|
133
|
+
joint_info = p.getJointInfo(robot, i)
|
134
|
+
name = joint_info[1].decode("utf-8")
|
135
|
+
joint_type = joint_info[2]
|
136
|
+
joints[name] = i
|
137
|
+
if joint_type == p.JOINT_PRISMATIC:
|
138
|
+
joint_min, joint_max = joint_info[8:10]
|
139
|
+
controls[name] = p.addUserDebugParameter(name, joint_min, joint_max, 0.0)
|
140
|
+
elif joint_type == p.JOINT_REVOLUTE:
|
141
|
+
joint_min, joint_max = joint_info[8:10]
|
142
|
+
controls[name] = p.addUserDebugParameter(name, joint_min, joint_max, 0.0)
|
143
|
+
|
144
|
+
# Run the simulation until the user closes the window.
|
145
|
+
last_time = time.time()
|
146
|
+
prev_control_values = {k: 0.0 for k in controls}
|
147
|
+
while p.isConnected():
|
148
|
+
# Reset the simulation if "r" was pressed.
|
149
|
+
keys = p.getKeyboardEvents()
|
150
|
+
if ord("r") in keys and keys[ord("r")] & p.KEY_WAS_TRIGGERED:
|
151
|
+
p.resetBasePositionAndOrientation(robot, start_position, start_orientation)
|
152
|
+
p.setJointMotorControlArray(
|
153
|
+
robot,
|
154
|
+
range(p.getNumJoints(robot)),
|
155
|
+
p.POSITION_CONTROL,
|
156
|
+
targetPositions=[0] * p.getNumJoints(robot),
|
157
|
+
)
|
158
|
+
|
159
|
+
# Set joint positions.
|
160
|
+
for k, v in controls.items():
|
161
|
+
try:
|
162
|
+
target_position = p.readUserDebugParameter(v)
|
163
|
+
if target_position != prev_control_values[k]:
|
164
|
+
prev_control_values[k] = target_position
|
165
|
+
p.setJointMotorControl2(robot, joints[k], p.POSITION_CONTROL, target_position)
|
166
|
+
except p.error:
|
167
|
+
logger.debug("Failed to set joint %s", k)
|
168
|
+
pass
|
169
|
+
|
170
|
+
# Step simulation.
|
171
|
+
p.stepSimulation()
|
172
|
+
cur_time = time.time()
|
173
|
+
time.sleep(max(0, parsed_args.dt - (cur_time - last_time)))
|
174
|
+
last_time = cur_time
|
175
|
+
|
176
|
+
|
177
|
+
if __name__ == "__main__":
|
178
|
+
# python -m kscale.store.pybullet
|
179
|
+
asyncio.run(main())
|
@@ -0,0 +1,213 @@
|
|
1
|
+
"""Utility functions for managing artifacts in the K-Scale store."""
|
2
|
+
|
3
|
+
import argparse
|
4
|
+
import asyncio
|
5
|
+
import logging
|
6
|
+
import os
|
7
|
+
import shutil
|
8
|
+
import sys
|
9
|
+
import tarfile
|
10
|
+
from pathlib import Path
|
11
|
+
from typing import Literal, Sequence, get_args
|
12
|
+
|
13
|
+
import httpx
|
14
|
+
import requests
|
15
|
+
|
16
|
+
from kscale.conf import Settings
|
17
|
+
from kscale.store.gen.api import UrdfResponse
|
18
|
+
|
19
|
+
# Set up logging
|
20
|
+
logging.basicConfig(level=logging.INFO)
|
21
|
+
logger = logging.getLogger(__name__)
|
22
|
+
|
23
|
+
|
24
|
+
def get_api_key() -> str:
|
25
|
+
api_key = Settings.load().store.api_key
|
26
|
+
if not api_key:
|
27
|
+
raise ValueError(
|
28
|
+
"API key not found! Get one here and set it as the `KSCALE_API_KEY` environment variable or in your "
|
29
|
+
"config file: https://kscale.store/keys"
|
30
|
+
)
|
31
|
+
return api_key
|
32
|
+
|
33
|
+
|
34
|
+
def get_cache_dir() -> Path:
|
35
|
+
return Path(Settings.load().store.cache_dir).expanduser().resolve()
|
36
|
+
|
37
|
+
|
38
|
+
def get_listing_dir(listing_id: str) -> Path:
|
39
|
+
(cache_dir := get_cache_dir() / listing_id).mkdir(parents=True, exist_ok=True)
|
40
|
+
return cache_dir
|
41
|
+
|
42
|
+
|
43
|
+
def fetch_urdf_info(listing_id: str) -> UrdfResponse:
|
44
|
+
url = f"https://api.kscale.store/urdf/info/{listing_id}"
|
45
|
+
headers = {
|
46
|
+
"Authorization": f"Bearer {get_api_key()}",
|
47
|
+
}
|
48
|
+
response = requests.get(url, headers=headers)
|
49
|
+
response.raise_for_status()
|
50
|
+
return UrdfResponse(**response.json())
|
51
|
+
|
52
|
+
|
53
|
+
async def download_artifact(artifact_url: str, cache_dir: Path) -> Path:
|
54
|
+
filename = os.path.join(cache_dir, artifact_url.split("/")[-1])
|
55
|
+
headers = {
|
56
|
+
"Authorization": f"Bearer {get_api_key()}",
|
57
|
+
}
|
58
|
+
|
59
|
+
if not os.path.exists(filename):
|
60
|
+
logger.info("Downloading artifact from %s", artifact_url)
|
61
|
+
|
62
|
+
async with httpx.AsyncClient() as client:
|
63
|
+
response = await client.get(artifact_url, headers=headers)
|
64
|
+
response.raise_for_status()
|
65
|
+
with open(filename, "wb") as f:
|
66
|
+
for chunk in response.iter_bytes(chunk_size=8192):
|
67
|
+
f.write(chunk)
|
68
|
+
logger.info("Artifact downloaded to %s", filename)
|
69
|
+
else:
|
70
|
+
logger.info("Artifact already cached at %s", filename)
|
71
|
+
|
72
|
+
# Extract the .tgz file
|
73
|
+
extract_dir = cache_dir / os.path.splitext(os.path.basename(filename))[0]
|
74
|
+
if not extract_dir.exists():
|
75
|
+
logger.info("Extracting %s to %s", filename, extract_dir)
|
76
|
+
with tarfile.open(filename, "r:gz") as tar:
|
77
|
+
tar.extractall(path=extract_dir)
|
78
|
+
else:
|
79
|
+
logger.info("Artifact already extracted at %s", extract_dir)
|
80
|
+
|
81
|
+
return extract_dir
|
82
|
+
|
83
|
+
|
84
|
+
def create_tarball(folder_path: str | Path, output_filename: str, cache_dir: Path) -> str:
|
85
|
+
tarball_path = os.path.join(cache_dir, output_filename)
|
86
|
+
with tarfile.open(tarball_path, "w:gz") as tar:
|
87
|
+
for root, _, files in os.walk(folder_path):
|
88
|
+
for file in files:
|
89
|
+
file_path = os.path.join(root, file)
|
90
|
+
arcname = os.path.relpath(file_path, start=folder_path)
|
91
|
+
tar.add(file_path, arcname=arcname)
|
92
|
+
logger.info("Added %s as %s", file_path, arcname)
|
93
|
+
logger.info("Created tarball %s", tarball_path)
|
94
|
+
return tarball_path
|
95
|
+
|
96
|
+
|
97
|
+
async def upload_artifact(tarball_path: str, listing_id: str) -> None:
|
98
|
+
url = f"https://api.kscale.store/urdf/upload/{listing_id}"
|
99
|
+
headers = {
|
100
|
+
"Authorization": f"Bearer {get_api_key()}",
|
101
|
+
}
|
102
|
+
|
103
|
+
async with httpx.AsyncClient() as client:
|
104
|
+
with open(tarball_path, "rb") as f:
|
105
|
+
files = {"file": (f.name, f, "application/gzip")}
|
106
|
+
response = await client.post(url, headers=headers, files=files)
|
107
|
+
|
108
|
+
response.raise_for_status()
|
109
|
+
|
110
|
+
logger.info("Uploaded artifact to %s", url)
|
111
|
+
|
112
|
+
|
113
|
+
async def download_urdf(listing_id: str) -> Path:
|
114
|
+
try:
|
115
|
+
urdf_info = fetch_urdf_info(listing_id)
|
116
|
+
|
117
|
+
if urdf_info.urdf is None:
|
118
|
+
breakpoint()
|
119
|
+
raise ValueError(f"No URDF found for listing {listing_id}")
|
120
|
+
|
121
|
+
artifact_url = urdf_info.urdf.url
|
122
|
+
return await download_artifact(artifact_url, get_listing_dir(listing_id))
|
123
|
+
|
124
|
+
except requests.RequestException:
|
125
|
+
logger.exception("Failed to fetch URDF info")
|
126
|
+
raise
|
127
|
+
|
128
|
+
|
129
|
+
async def show_urdf_info(listing_id: str) -> None:
|
130
|
+
try:
|
131
|
+
urdf_info = fetch_urdf_info(listing_id)
|
132
|
+
|
133
|
+
if urdf_info.urdf:
|
134
|
+
logger.info("URDF Artifact ID: %s", urdf_info.urdf.artifact_id)
|
135
|
+
logger.info("URDF URL: %s", urdf_info.urdf.url)
|
136
|
+
else:
|
137
|
+
logger.info("No URDF found for listing %s", listing_id)
|
138
|
+
except requests.RequestException:
|
139
|
+
logger.exception("Failed to fetch URDF info")
|
140
|
+
raise
|
141
|
+
|
142
|
+
|
143
|
+
async def upload_urdf(listing_id: str, args: Sequence[str] | None = None) -> None:
|
144
|
+
parser = argparse.ArgumentParser(description="Upload a URDF artifact to the K-Scale store")
|
145
|
+
parser.add_argument("folder_path", help="The path to the folder containing the URDF files")
|
146
|
+
parsed_args = parser.parse_args(args)
|
147
|
+
folder_path = Path(parsed_args.folder_path).expanduser().resolve()
|
148
|
+
|
149
|
+
output_filename = f"{listing_id}.tgz"
|
150
|
+
tarball_path = create_tarball(folder_path, output_filename, get_listing_dir(listing_id))
|
151
|
+
|
152
|
+
try:
|
153
|
+
fetch_urdf_info(listing_id)
|
154
|
+
await upload_artifact(tarball_path, listing_id)
|
155
|
+
except requests.RequestException:
|
156
|
+
logger.exception("Failed to upload artifact")
|
157
|
+
raise
|
158
|
+
|
159
|
+
|
160
|
+
async def remove_local_urdf(listing_id: str) -> None:
|
161
|
+
try:
|
162
|
+
if listing_id.lower() == "all":
|
163
|
+
cache_dir = get_cache_dir()
|
164
|
+
if cache_dir.exists():
|
165
|
+
logger.info("Removing all local caches at %s", cache_dir)
|
166
|
+
shutil.rmtree(cache_dir)
|
167
|
+
else:
|
168
|
+
logger.error("No local caches found")
|
169
|
+
else:
|
170
|
+
listing_dir = get_listing_dir(listing_id)
|
171
|
+
if listing_dir.exists():
|
172
|
+
logger.info("Removing local cache at %s", listing_dir)
|
173
|
+
shutil.rmtree(listing_dir)
|
174
|
+
else:
|
175
|
+
logger.error("No local cache found for listing %s", listing_id)
|
176
|
+
|
177
|
+
except Exception:
|
178
|
+
logger.error("Failed to remove local cache")
|
179
|
+
raise
|
180
|
+
|
181
|
+
|
182
|
+
Command = Literal["download", "info", "upload", "remove-local"]
|
183
|
+
|
184
|
+
|
185
|
+
async def main(args: Sequence[str] | None = None) -> None:
|
186
|
+
parser = argparse.ArgumentParser(description="K-Scale URDF Store", add_help=False)
|
187
|
+
parser.add_argument("command", choices=get_args(Command), help="The command to run")
|
188
|
+
parser.add_argument("listing_id", help="The listing ID to operate on")
|
189
|
+
parsed_args, remaining_args = parser.parse_known_args(args)
|
190
|
+
|
191
|
+
command: Command = parsed_args.command
|
192
|
+
listing_id: str = parsed_args.listing_id
|
193
|
+
|
194
|
+
match command:
|
195
|
+
case "download":
|
196
|
+
await download_urdf(listing_id)
|
197
|
+
|
198
|
+
case "info":
|
199
|
+
await show_urdf_info(listing_id)
|
200
|
+
|
201
|
+
case "upload":
|
202
|
+
await upload_urdf(listing_id, remaining_args)
|
203
|
+
|
204
|
+
case "remove-local":
|
205
|
+
await remove_local_urdf(listing_id)
|
206
|
+
|
207
|
+
case _:
|
208
|
+
logger.error("Invalid command")
|
209
|
+
sys.exit(1)
|
210
|
+
|
211
|
+
|
212
|
+
if __name__ == "__main__":
|
213
|
+
asyncio.run(main())
|
@@ -0,0 +1,52 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: kscale
|
3
|
+
Version: 0.0.3
|
4
|
+
Summary: The kscale project
|
5
|
+
Home-page: https://github.com/kscalelabs/kscale
|
6
|
+
Author: Benjamin Bolte
|
7
|
+
Requires-Python: >=3.11
|
8
|
+
Description-Content-Type: text/markdown
|
9
|
+
License-File: LICENSE
|
10
|
+
Requires-Dist: omegaconf
|
11
|
+
Requires-Dist: httpx
|
12
|
+
Requires-Dist: requests
|
13
|
+
Provides-Extra: dev
|
14
|
+
Requires-Dist: black; extra == "dev"
|
15
|
+
Requires-Dist: darglint; extra == "dev"
|
16
|
+
Requires-Dist: mypy; extra == "dev"
|
17
|
+
Requires-Dist: pytest; extra == "dev"
|
18
|
+
Requires-Dist: ruff; extra == "dev"
|
19
|
+
Requires-Dist: datamodel-code-generator; extra == "dev"
|
20
|
+
|
21
|
+
<p align="center">
|
22
|
+
<picture>
|
23
|
+
<img alt="K-Scale Open Source Robotics" src="https://media.kscale.dev/kscale-open-source-header.png" style="max-width: 100%;">
|
24
|
+
</picture>
|
25
|
+
</p>
|
26
|
+
|
27
|
+
<div align="center">
|
28
|
+
|
29
|
+
[![License](https://img.shields.io/badge/license-MIT-green)](https://github.com/kscalelabs/ksim/blob/main/LICENSE)
|
30
|
+
[![Discord](https://img.shields.io/discord/1224056091017478166)](https://discord.gg/k5mSvCkYQh)
|
31
|
+
[![Wiki](https://img.shields.io/badge/wiki-humanoids-black)](https://humanoids.wiki)
|
32
|
+
<br />
|
33
|
+
[![python](https://img.shields.io/badge/-Python_3.11-blue?logo=python&logoColor=white)](https://github.com/pre-commit/pre-commit)
|
34
|
+
[![black](https://img.shields.io/badge/Code%20Style-Black-black.svg?labelColor=gray)](https://black.readthedocs.io/en/stable/)
|
35
|
+
[![ruff](https://img.shields.io/badge/Linter-Ruff-red.svg?labelColor=gray)](https://github.com/charliermarsh/ruff)
|
36
|
+
<br />
|
37
|
+
[![Python Checks](https://github.com/kscalelabs/kscale/actions/workflows/test.yml/badge.svg)](https://github.com/kscalelabs/kscale/actions/workflows/test.yml)
|
38
|
+
[![Publish Python Package](https://github.com/kscalelabs/kscale/actions/workflows/publish.yml/badge.svg)](https://github.com/kscalelabs/kscale/actions/workflows/publish.yml)
|
39
|
+
|
40
|
+
</div>
|
41
|
+
|
42
|
+
# K-Scale Command Line Interface
|
43
|
+
|
44
|
+
This is a command line tool for interacting with various services provided by K-Scale Labs, such as:
|
45
|
+
|
46
|
+
- [K-Scale Store](https://kscale.store/)
|
47
|
+
|
48
|
+
## Installation
|
49
|
+
|
50
|
+
```bash
|
51
|
+
pip install kscale
|
52
|
+
```
|
@@ -12,11 +12,15 @@ kscale/requirements.txt
|
|
12
12
|
kscale.egg-info/PKG-INFO
|
13
13
|
kscale.egg-info/SOURCES.txt
|
14
14
|
kscale.egg-info/dependency_links.txt
|
15
|
+
kscale.egg-info/entry_points.txt
|
15
16
|
kscale.egg-info/requires.txt
|
16
17
|
kscale.egg-info/top_level.txt
|
18
|
+
kscale/formats/mjcf.py
|
17
19
|
kscale/store/__init__.py
|
18
|
-
kscale/store/
|
20
|
+
kscale/store/cli.py
|
21
|
+
kscale/store/pybullet.py
|
19
22
|
kscale/store/urdf.py
|
23
|
+
kscale/store/bullet/MANIFEST.in
|
20
24
|
kscale/store/gen/__init__.py
|
21
25
|
kscale/store/gen/api.py
|
22
26
|
tests/test_dummy.py
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# mypy: disable-error-code="import-untyped"
|
1
|
+
# mypy: disable-error-code="import-untyped, import-not-found"
|
2
2
|
#!/usr/bin/env python
|
3
3
|
"""Setup script for the project."""
|
4
4
|
|
@@ -29,6 +29,7 @@ setup(
|
|
29
29
|
version=version,
|
30
30
|
description="The kscale project",
|
31
31
|
author="Benjamin Bolte",
|
32
|
+
license_files=("LICENSE",),
|
32
33
|
url="https://github.com/kscalelabs/kscale",
|
33
34
|
long_description=long_description,
|
34
35
|
long_description_content_type="text/markdown",
|
kscale-0.0.1/PKG-INFO
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.1
|
2
|
-
Name: kscale
|
3
|
-
Version: 0.0.1
|
4
|
-
Summary: The kscale project
|
5
|
-
Home-page: https://github.com/kscalelabs/kscale
|
6
|
-
Author: Benjamin Bolte
|
7
|
-
Requires-Python: >=3.11
|
8
|
-
Description-Content-Type: text/markdown
|
9
|
-
License-File: LICENSE
|
10
|
-
Requires-Dist: omegaconf
|
11
|
-
Provides-Extra: dev
|
12
|
-
Requires-Dist: black; extra == "dev"
|
13
|
-
Requires-Dist: darglint; extra == "dev"
|
14
|
-
Requires-Dist: mypy; extra == "dev"
|
15
|
-
Requires-Dist: pytest; extra == "dev"
|
16
|
-
Requires-Dist: ruff; extra == "dev"
|
17
|
-
Requires-Dist: datamodel-code-generator; extra == "dev"
|
18
|
-
|
19
|
-
# K-Scale Command Line Interface
|
20
|
-
|
21
|
-
This is a command line tool for interacting with various services provided by K-Scale Labs, such as:
|
22
|
-
|
23
|
-
- [K-Scale Store](https://kscale.store/)
|
24
|
-
|
25
|
-
## Installation
|
26
|
-
|
27
|
-
```bash
|
28
|
-
pip install kscale
|
29
|
-
```
|
kscale-0.0.1/README.md
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
"""Defines utility functions for authenticating the K-Scale Store API."""
|
2
|
-
|
3
|
-
from kscale.conf import Settings
|
4
|
-
|
5
|
-
|
6
|
-
def get_api_key() -> str:
|
7
|
-
try:
|
8
|
-
return Settings.load().store.api_key
|
9
|
-
except AttributeError:
|
10
|
-
raise ValueError(
|
11
|
-
"API key not found! Get one here and set it as the `KSCALE_API_KEY` "
|
12
|
-
"environment variable: https://kscale.store/keys"
|
13
|
-
)
|
File without changes
|
@@ -1 +0,0 @@
|
|
1
|
-
"""Utility functions for managing artifacts in the K-Scale store."""
|
@@ -1,29 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.1
|
2
|
-
Name: kscale
|
3
|
-
Version: 0.0.1
|
4
|
-
Summary: The kscale project
|
5
|
-
Home-page: https://github.com/kscalelabs/kscale
|
6
|
-
Author: Benjamin Bolte
|
7
|
-
Requires-Python: >=3.11
|
8
|
-
Description-Content-Type: text/markdown
|
9
|
-
License-File: LICENSE
|
10
|
-
Requires-Dist: omegaconf
|
11
|
-
Provides-Extra: dev
|
12
|
-
Requires-Dist: black; extra == "dev"
|
13
|
-
Requires-Dist: darglint; extra == "dev"
|
14
|
-
Requires-Dist: mypy; extra == "dev"
|
15
|
-
Requires-Dist: pytest; extra == "dev"
|
16
|
-
Requires-Dist: ruff; extra == "dev"
|
17
|
-
Requires-Dist: datamodel-code-generator; extra == "dev"
|
18
|
-
|
19
|
-
# K-Scale Command Line Interface
|
20
|
-
|
21
|
-
This is a command line tool for interacting with various services provided by K-Scale Labs, such as:
|
22
|
-
|
23
|
-
- [K-Scale Store](https://kscale.store/)
|
24
|
-
|
25
|
-
## Installation
|
26
|
-
|
27
|
-
```bash
|
28
|
-
pip install kscale
|
29
|
-
```
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|