-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Separate Builder (+ Moved Material files) (#25)
* Introduce Separate Builder (+ Moved Material files) * Update after fixing all "make format" issues * Changed the case of Material and MJCFBuilder in imports
- Loading branch information
1 parent
93a7235
commit a3802b3
Showing
4 changed files
with
346 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"""A class for handling MuJoCo material properties.""" | ||
|
||
from dataclasses import dataclass | ||
from typing import Optional, Sequence | ||
|
||
# MTL fields relevant to MuJoCo. | ||
_MTL_FIELDS = ( | ||
# Ambient, diffuse and specular colors. | ||
"Ka", | ||
"Kd", | ||
"Ks", | ||
# d or Tr are used for the rgba transparency. | ||
"d", | ||
"Tr", | ||
# Shininess. | ||
"Ns", | ||
# References a texture file. | ||
"map_Kd", | ||
) | ||
|
||
# Character used to denote a comment in an MTL file. | ||
_MTL_COMMENT_CHAR = "#" | ||
|
||
|
||
@dataclass | ||
class Material: | ||
name: str | ||
Ka: Optional[str] = None | ||
Kd: Optional[str] = None | ||
Ks: Optional[str] = None | ||
d: Optional[str] = None | ||
Tr: Optional[str] = None | ||
Ns: Optional[str] = None | ||
map_Kd: Optional[str] = None | ||
|
||
@staticmethod | ||
def from_string(lines: Sequence[str]) -> "Material": | ||
"""Construct a Material object from a string.""" | ||
attrs = {"name": lines[0].split(" ")[1].strip()} | ||
for line in lines[1:]: | ||
for attr in _MTL_FIELDS: | ||
if line.startswith(attr): | ||
elems = line.split(" ")[1:] | ||
elems = [elem for elem in elems if elem != ""] | ||
attrs[attr] = " ".join(elems) | ||
break | ||
return Material(**attrs) | ||
|
||
def mjcf_rgba(self) -> str: | ||
Kd = self.Kd or "1.0 1.0 1.0" | ||
if self.d is not None: # alpha | ||
alpha = self.d | ||
elif self.Tr is not None: # 1 - alpha | ||
alpha = str(1.0 - float(self.Tr)) | ||
else: | ||
alpha = "1.0" | ||
# TODO(kevin): Figure out how to use Ka for computing rgba. | ||
return f"{Kd} {alpha}" | ||
|
||
def mjcf_shininess(self) -> str: | ||
if self.Ns is not None: | ||
# Normalize Ns value to [0, 1]. Ns values normally range from 0 to 1000. | ||
Ns = float(self.Ns) / 1_000 | ||
else: | ||
Ns = 0.5 | ||
return f"{Ns}" | ||
|
||
def mjcf_specular(self) -> str: | ||
if self.Ks is not None: | ||
# Take the average of the specular RGB values. | ||
Ks = sum(list(map(float, self.Ks.split(" ")))) / 3 | ||
else: | ||
Ks = 0.5 | ||
return f"{Ks}" |
Oops, something went wrong.