Skip to content

Commit

Permalink
Update after fixing all "make format" issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kwesiRutledge committed Feb 12, 2024
1 parent 7ffda13 commit b39b59c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 40 deletions.
11 changes: 7 additions & 4 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
[flake8]
exclude = .git
exclude = .git, ./venv-obj2mjcf
max-line-length = 80
ignore =
E203, # whitespace before colon (black default)
E501, # line too long (<n> characters)
W503, # line break before binary operator
# whitespace before colon (black default)
E203,
# line too long (<n> characters)
E501,
# line break before binary operator
W503,

per-file-ignores =
*/__init__.py: F401
10 changes: 4 additions & 6 deletions obj2mjcf/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@
import tempfile
from dataclasses import dataclass, field
from pathlib import Path
from typing import List, Optional, Sequence
from typing import List, Optional

import mujoco
import trimesh
import tyro
from lxml import etree
from PIL import Image
from termcolor import cprint

from obj2mjcf.Material import _MTL_COMMENT_CHAR, Material
from obj2mjcf.MJCFBuilder import MJCFBuilder
from obj2mjcf.Material import Material

# Find the V-HACD v4.0 executable in the system path.
# Note trimesh has not updated their code to work with v4.0 which is why we do not use
Expand Down Expand Up @@ -340,8 +338,8 @@ def process_obj(filename: Path, args: Args) -> None:
f.write("".join(lines))

# Build an MJCF.
builder = MJCFBuilder(filename, mesh, mtls)
tree = builder.build()
builder = MJCFBuilder(filename, mesh, mtls, decomp_success=decomp_success)
builder.build()

# Compile and step the physics to check for any errors.
if args.compile_model:
Expand Down
8 changes: 5 additions & 3 deletions obj2mjcf/Material.py → obj2mjcf/material.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional, Sequence
from dataclasses import dataclass, field
"""A class for handling MuJoCo material properties."""

from dataclasses import dataclass
from typing import Optional, Sequence

# MTL fields relevant to MuJoCo.
_MTL_FIELDS = (
Expand All @@ -20,6 +21,7 @@
# Character used to denote a comment in an MTL file.
_MTL_COMMENT_CHAR = "#"


@dataclass
class Material:
name: str
Expand Down Expand Up @@ -69,4 +71,4 @@ def mjcf_specular(self) -> str:
Ks = sum(list(map(float, self.Ks.split(" ")))) / 3
else:
Ks = 0.5
return f"{Ks}"
return f"{Ks}"
58 changes: 31 additions & 27 deletions obj2mjcf/MJCFBuilder.py → obj2mjcf/mjcf_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,46 @@
Description:
This file contains the class that is used to build MJCF files.
"""

import logging
import os
from lxml import etree
from pathlib import Path

from typing import List, Tuple, Union, Any
from typing import Any, List, Union

import mujoco
import trimesh
from lxml import etree
from termcolor import cprint

from obj2mjcf.Material import Material


# 2-space indentation for the generated XML.
_XML_INDENTATION = " "


class MJCFBuilder:
def __init__(
self,
filename: Path,
mesh: Union[trimesh.base.Trimesh,Any],
materials: List[Material],
work_dir: Path = None,
self,
filename: Path,
mesh: Union[trimesh.base.Trimesh, Any],
materials: List[Material],
work_dir: Path = Path(),
decomp_success: bool = False,
):
self.filename = filename
self.mesh = mesh
self.materials = materials
self.decomp_success = decomp_success

self.work_dir = work_dir
if self.work_dir is None:
if self.work_dir == Path():
self.work_dir = filename.parent / filename.stem

# Define variables that will be defined later
self.tree = None

def add_visual_and_collision_default_classes(
self,
root: etree.Element,
self,
root: etree.Element,
):
# Define the default element
default_elem = etree.SubElement(root, "default")
Expand Down Expand Up @@ -101,9 +101,9 @@ def add_assets(self, root: etree.Element, mtls: List[Material]) -> etree.Element
return asset_elem

def add_visual_geometries(
self,
obj_body: etree.Element,
asset_elem: etree.Element,
self,
obj_body: etree.Element,
asset_elem: etree.Element,
):
# Constants
filename = self.filename
Expand All @@ -120,7 +120,10 @@ def add_visual_geometries(
# Add the geom to the worldbody.
if process_mtl:
e_ = etree.SubElement(
obj_body, "geom", material=materials[0].name, mesh=str(meshname.stem)
obj_body,
"geom",
material=materials[0].name,
mesh=str(meshname.stem),
)
e_.attrib["class"] = "visual"
else:
Expand All @@ -142,21 +145,23 @@ def add_visual_geometries(
e_.attrib["class"] = "visual"

def add_collision_geometries(
self,
obj_body: etree.Element,
asset_elem: etree.Element,
decomp_success: bool = False,
self,
obj_body: etree.Element,
asset_elem: etree.Element,
):
# Constants
filename = self.filename
mesh = self.mesh
decomp_success = self.decomp_success

work_dir = self.work_dir

if decomp_success:
# Find collision files from the decomposed convex hulls.
collisions = [
x for x in work_dir.glob("**/*") if x.is_file() and "collision" in x.name
x
for x in work_dir.glob("**/*")
if x.is_file() and "collision" in x.name
]
collisions.sort(key=lambda x: int(x.stem.split("_")[-1]))

Expand All @@ -178,12 +183,11 @@ def add_collision_geometries(
e_.attrib["class"] = "collision"

def build(
self,
add_free_joint: bool = False,
):
self,
add_free_joint: bool = False,
) -> None:
# Constants
filename = self.filename
mesh = self.mesh
mtls = self.materials

# Start assembling xml tree
Expand Down Expand Up @@ -250,4 +254,4 @@ def save_mjcf(
# Save the MJCF file.
xml_path = str(work_dir / f"{filename.stem}.xml")
tree.write(xml_path, encoding="utf-8")
logging.info(f"Saved MJCF to {xml_path}")
logging.info(f"Saved MJCF to {xml_path}")

0 comments on commit b39b59c

Please sign in to comment.