Skip to content

Commit

Permalink
Major bug fix: Joint and Link keys dont expect unique mate or part na…
Browse files Browse the repository at this point in the history
…mes anymore for generating URDF components.
  • Loading branch information
senthurayyappan committed Dec 10, 2024
1 parent bd5f99c commit 584bdd0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
14 changes: 11 additions & 3 deletions onshape_api/urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
VisualLink,
)
from onshape_api.parse import MATE_JOINER, PARENT, RELATION_PARENT
from onshape_api.utilities.helpers import get_sanitized_name
from onshape_api.utilities.helpers import get_sanitized_name, make_unique_keys

SCRIPT_DIR = os.path.dirname(__file__)

Expand Down Expand Up @@ -486,7 +486,15 @@ def get_urdf_components(
assets_map[child] = downloadable_link
links.append(link)

links_map = {link.name: link for link in links}
joints_map = {joint.name: joint for joint in joints}
unique_joint_key_map = make_unique_keys([joint.name for joint in joints])
unique_link_key_map = make_unique_keys([link.name for link in links])

for joint_key in unique_joint_key_map:
joints[unique_joint_key_map[joint_key]].name = joint_key
joints_map[joint_key] = joints[unique_joint_key_map[joint_key]]

for link_key in unique_link_key_map:
links[unique_link_key_map[link_key]].name = link_key
links_map[link_key] = links[unique_link_key_map[link_key]]

return links_map, joints_map, assets_map
31 changes: 31 additions & 0 deletions onshape_api/utilities/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,37 @@ def get_random_names(directory: str, count: int, filename: str = "words.txt") ->
return random.sample(words, count)


def make_unique_keys(keys: list[str]) -> dict[str, int]:
"""
Make a list of keys unique by appending a number to duplicate keys and
return a mapping of unique keys to their original indices.
Args:
keys: List of keys.
Returns:
A dictionary mapping unique keys to their original indices.
Examples:
>>> make_unique_keys(["a", "b", "a", "a"])
{"a": 0, "b": 1, "a-1": 2, "a-2": 3}
"""
unique_key_map = {}
key_count = {}

for index, key in enumerate(keys):
if key in key_count:
key_count[key] += 1
unique_key = f"{key}-{key_count[key]}"
else:
key_count[key] = 0
unique_key = key

unique_key_map[unique_key] = index

return unique_key_map


def get_sanitized_name(name: str, replace_with: str = "-") -> str:
"""
Sanitize a name by removing special characters, preserving "-" and "_", and
Expand Down

0 comments on commit 584bdd0

Please sign in to comment.