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

fix: dont add materials randomly #9

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions tests/extender.urdf
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<robot name="vgc10_extender_stick">
<material name="Silver">
<color rgba="0.753 0.753 0.753 1.0" />
</material>
<link name="INPUT-extender_stick">
<inertial>
<origin xyz="-1.14523654741374E-09 -2.79672501268441E-08 0.0228788479998501" rpy="0 0 0" />
Expand Down
3 changes: 3 additions & 0 deletions tests/extender2.urdf
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<robot name="vgc10_extender_stick">
<material name="Silver">
<color rgba="0.753 0.753 0.753 1.0" />
</material>
<link name="INPUT-extender_stick-2">
<inertial>
<origin xyz="-1.14523654741374E-09 -2.79672501268441E-08 0.0228788479998501" rpy="0 0 0" />
Expand Down
8 changes: 4 additions & 4 deletions tests/output.urdf
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version='1.0' encoding='UTF-8'?>
<robot name="vgc10_extender_stick">
<material name="Silver">
<color rgba="0.753 0.753 0.753 1.0" />
</material>
<link name="INPUT-extender_stick">
<inertial>
<origin xyz="-1.14523654741374E-09 -2.79672501268441E-08 0.0228788479998501" rpy="0 0 0" />
Expand Down Expand Up @@ -27,10 +30,7 @@
<child link="CONNECTED:OUTPUT-vgc10_extender_stick" />
<axis xyz="0 0 0" />
</joint>
<material name="Silver">
<color rgba="0.753 0.753 0.753 1.0" />
</material>
<joint name="GENERATED_CONNECTION(1)" type="fixed">
<joint name="GENERATED_CONNECTION(1)" type="fixed">
<origin xyz="0 0 0" rpy="0 0 0" />
<parent link="CONNECTED:OUTPUT-vgc10_extender_stick" />
<child link="CONNECTED:INPUT-extender_stick(1)" />
Expand Down
64 changes: 3 additions & 61 deletions urdf_compose/urdf_obj.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,12 @@
import copy
import os
import subprocess
import xml.etree.ElementTree as ET
from pathlib import Path
from uuid import uuid1

from urdf_compose.xml_utils import elements_equal

# URDFObj should not be specific to us as Tutor

std_materials = ET.fromstring(
"""

<!-- Materials -->
<robot>
<material name="Black">
<color rgba="0.0 0.0 0.0 1.0" />
</material>
<material name="Red">
<color rgba="0.85 0.19 0.21 1.0" />
</material>
<material name="Blue">
<color rgba="0.28 0.52 0.92 1.0" />
</material>
<material name="Green">
<color rgba="0.23 0.72 0.32 1.0" />
</material>
<material name="Yellow">
<color rgba="0.95 0.76 0.05 1.0" />
</material>
<material name="White">
<color rgba="1.0 1.0 1.0 1.0" />
</material>
<material name="Silver">
<color rgba="0.753 0.753 0.753 1.0" />
</material>
<material name="DarkGrey">
<color rgba="0.2 0.2 0.2 1.0" />
</material>
</robot>
"""
)


class CheckURDFFailure(Exception):
pass
Expand Down Expand Up @@ -103,20 +68,6 @@ def write_xml(self, dest: Path) -> None:
dest.touch(exist_ok=True)
self.tree.write(str(dest), xml_declaration=True, encoding="UTF-8")

def _add_colors(self) -> None:
materials_used = set()
materials_defined = set()
for material in self.getroot().iter("material"):
name = material.attrib["name"]
if len(material) == 0:
materials_used.add(name)
else:
materials_defined.add(name)

for mat in std_materials:
if mat.attrib["name"] in materials_used and mat.attrib["name"] not in materials_defined:
self.getroot().append(copy.deepcopy(mat))

def __hash__(self) -> int:
return hash(id(self))

Expand Down Expand Up @@ -150,20 +101,11 @@ def __init__(self, path: Path, check: bool = True):
tree = ET.ElementTree()
tree.parse(str(self.path))
super().__init__(tree)
self._add_colors()

if check:
check_file = self.path.parent / f"{self.path.name}_check_urdfOBJ{uuid1()}.urdf"
self.write_xml(check_file)
check_urdf_result = check_urdf(check_file)

if check_file.exists() and check_urdf_result is None:
check_file.unlink()
else:
raise RuntimeError(
f"Attempted to create URDFObj, but given invalid urdf file {path}. You can"
f"check adjusted file at {check_file}. {check_urdf_result=}",
)
check_urdf_result = check_urdf(path)
if check_urdf_result is not None:
raise check_urdf_result

def __repr__(self) -> str:
return f"ExplicitURDFObj from {self.path.name}"
Loading