Giter VIP home page Giter VIP logo

obj2mjcf's Introduction

obj2mjcf

PyPI Python Version PyPI version

obj2mjcf is a CLI for proccessing composite Wavefront OBJ files for use in MuJoCo. It automatically:

  • Splits an OBJ file into sub-meshes that are grouped by the materials referenced in the OBJ's MTL file
  • Generates an MJCF XML file that is pre-filled with materials, meshes and geom elements referencing these OBJ files
  • Optionally generates a collision mesh by performing a convex decomposition of the OBJ using CoACD

obj2mjcf was used to process model meshes for MuJoCo Menagerie:

Motivation

As of June 2022, MuJoCo does not support composite OBJ files consisting of groups or objects (o or g OBJ tags) and only 1 material can be assigned per mesh. This means that you have to manually split your OBJ file into sub-meshes, a process that is tedious and error-prone. This tool is meant to automate this process.

Installation

Important. MuJoCo support for OBJ files is only available in versions 2.1.2 and above. Make sure you upgrade to the latest version via the Releases page.

The recommended way to install this package is via PyPI:

pip install --upgrade obj2mjcf

Usage

Type the following at the command line for a detailed description of available options:

obj2mjcf --help

obj2mjcf's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

obj2mjcf's Issues

Poor results on some objects

Hi, I am using your codes to convert some .obj files to the mujoco simulator. However, I find results from using the V-HACD method as an approximation are very poor for some objects.

For example, I am using HOPE Dataset, and for one of the object AlphabetSoup, the results are very poor even when I set vhacd-args.max-output-convex-hulls=30:

image

Do you have any suggestions for these objects? Thank you!

How to display color in obj model

Thank you for this great tool! I'm opening this issue because I am importing an obj model with complex colors. The obj model generated by the program has colors, but the xml in the simulation (mujuco 230) does not display the color of the model. I want to ask how to realize the different color parts of the robot in the example? Only can use map or rgba method to achieve this? Thank you for your answer.

At the same time, I hope that the project will write down the requirements for the mujoco version. The 210 version cannot be displayed. I changed 230 to display it. Thank you again for your toolkit!

Installer fails in Windows 10

A first install fails due to missing definition of environment variables MUJOCO_PATH and MUJOCO_PLUGIN_PATH.
After setting them to MUJOCO_PATH =C:\Users_conc\Downloads\mujoco and MUJOCO_PLUGIN_PATH=C:\Users_conc\Downloads\mujoco\bin\mujoco_plugin, installation still fails due to "missing file" (" Impossibile trovare il file specificato"), but which file?

Final output:

 running install
  running build
  running build_py
  creating build
  creating build\lib.win32-3.10
  creating build\lib.win32-3.10\mujoco
  copying mujoco\bindings_test.py -> build\lib.win32-3.10\mujoco
  copying mujoco\gl_context.py -> build\lib.win32-3.10\mujoco
  copying mujoco\renderer.py -> build\lib.win32-3.10\mujoco
  copying mujoco\renderer_test.py -> build\lib.win32-3.10\mujoco
  copying mujoco\render_test.py -> build\lib.win32-3.10\mujoco
  copying mujoco\rollout.py -> build\lib.win32-3.10\mujoco
  copying mujoco\rollout_test.py -> build\lib.win32-3.10\mujoco
  copying mujoco\viewer.py -> build\lib.win32-3.10\mujoco
  copying mujoco\viewer_test.py -> build\lib.win32-3.10\mujoco
  copying mujoco\__init__.py -> build\lib.win32-3.10\mujoco
  creating build\lib.win32-3.10\mujoco\egl
  copying mujoco\egl\egl_ext.py -> build\lib.win32-3.10\mujoco\egl
  copying mujoco\egl\__init__.py -> build\lib.win32-3.10\mujoco\egl
  creating build\lib.win32-3.10\mujoco\glfw
  copying mujoco\glfw\__init__.py -> build\lib.win32-3.10\mujoco\glfw
  creating build\lib.win32-3.10\mujoco\osmesa
  copying mujoco\osmesa\__init__.py -> build\lib.win32-3.10\mujoco\osmesa
  running build_ext
  error: [WinError 2] Impossibile trovare il file specificato
  Configuring CMake with the following arguments:
      -DPython3_ROOT_DIR:PATH=C:/Users/_conc/AppData/Local/Programs/Python/Python310-32
      -DPython3_EXECUTABLE:STRING=C:/Users/_conc/AppData/Local/Programs/Python/Python310-32/python.exe
      -DCMAKE_MODULE_PATH:PATH=C:/Users/_conc/AppData/Local/Temp/pip-install-r1cxrofn/mujoco_8a926e14192e48e69dac3593e46ed701/cmake
      -DCMAKE_BUILD_TYPE:STRING=Release
      -DCMAKE_LIBRARY_OUTPUT_DIRECTORY:PATH=build/temp.win32-3.10/Release
      -DCMAKE_INTERPROCEDURAL_OPTIMIZATION:BOOL=ON
      -DCMAKE_Fortran_COMPILER:STRING=
      -DBUILD_TESTING:BOOL=OFF
      -DMUJOCO_LIBRARY_DIR:PATH=C:/Users/_conc/Downloads/mujoco/lib
      -DMUJOCO_INCLUDE_DIR:PATH=C:/Users/_conc/Downloads/mujoco/include

unknown mesh file type 1_0.obj

unknown mesh file type 1_0.obj
after obj2mjcf --obj-dir /path/to/your/obj/files --save-mjcf --compile-model --verbose i use mujoco to open xml file but out put is unknown mesh file type 1_0.obj
image

Wrong colors for splitted meshes

Thank you for this great tool! I'm opening this issue because I obtained a weird result while decomposing some obj meshes:
When specifying the --save-mtl flag, the materials associated to each mesh are switched (eg if I split base.obj into base_0.obj and base_1.obj, the material for base_0.obj will be associated to base_1.obj and vice versa). Have you ever reproduced that behavior?

My guess is that the declaration order of the textures in the original mtl file will influence that?

Additional question #10 concerning the change to a concave model

Hi, @kevinzakka

I am very sorry for repeatedly pestering you.

However, I would like to ask an additional new question regarding #10 , as I did not understand your previous answer regarding the change to a concave model and it was closed.

You told me that I should probably want to tweak the parameters of V-HACD though since it might not generate a good approximation with the default args.,
but how exactly can the values be changed?


I would like to reiterate the situation just to be sure.

I would like to use Mujoco to simulate a ball in a cup and pour it.
So I have created an STL file of the cup and read it in as an xml file and have it output.

However, I found the following text at the URL site below.

" if you want to model a non-convex shape, you have to decompose it into a union of convex geoms and attach all of them to the same body. "
https://mujoco.readthedocs.io/en/latest/overview.html

In fact, using the STL file of the created cup (black object), the object (green sphere) did not fit into the cup as shown in the attached photo.
image

Regarding the above problem, I was taught how to decompose a non-convex shape model into a convex shape model here.
As a result, I was able to correctly decompose the model at the time you taught me and generated about 20~30 obj files named "XX_collision_YY" in the newly generated folder ("Cup0722v1"), as shown in the image below.

Cup0722v1.zip

image

However, when I run it now, the collision obj file is not generated and only the original obj file and xml file exist in the newly generated folder ("Cup0722v1").

Cup0722v1_fail.zip

image

I have written this at length, but to summarize the current problem,
I am having trouble with the nonconvex shape model not being properly decomposed when I run thebelow program.

obj2mjcf --obj-dir . --obj-filter cup --save-mjcf --compile-model --vhacd-args.enable

I am truly sorry for the many times I have bothered you in your busy schedule, but I would appreciate your advice.

Thank you in advance.

Error when running obj2mjcf

The following error pops up when I run

obj2mjcf --obj-dir . --save-mjcf --compile-model --verbose

in the directory containing my .obj files, although after running, it's generating a folder containing my processed obj file.

Traceback (most recent call last):
  File "/opt/homebrew/bin/obj2mjcf", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/obj2mjcf/_cli.py", line 560, in main
    process_obj(obj_file, args)
  File "/opt/homebrew/lib/python3.11/site-packages/obj2mjcf/_cli.py", line 473, in process_obj
    obj_body, "geom", material=material.name, mesh=str(meshname.stem)
                               ^^^^^^^^
UnboundLocalError: cannot access local variable 'material' where it is not associated with a value

Add Transparency

Hello, thanks so much for making this! Could you update the code to also take into consideration the texture / color transparency (aka alpha value).

I think the change should be somewhere around here. Ilya has successfully done this as well in case you get stuck :)

rgba=material.diffuse + " 1.0",

collision_model assembly test

Hi Kevin, you did a great job! I have a question when I try to do the peg-in-hole experiment. I firstly use your code to transform the peg and hole object into the collision models separately (circle.xml and peg_circle.xml). Then I try to let the peg fall into the hole, but it always fails(assembly.xml). I am not familiar with mujoco, can you help me explain the reason?
assembly_test.zip

Where is the output files?

Hi, I am using your code by running a python file that contains:

import pathlib
import subprocess

# Path to the directory containing this file.
_THIS_DIR = pathlib.Path(__file__).parent.absolute()


def test_runs_without_error() -> None:
    retcode = subprocess.call(
        [
            "obj2mjcf",
            "--obj-dir",
            "./test,
            "--save-mtl",
            "--save-mjcf",
            "--compile-model",
            "--verbose",
        ]
    )
    assert retcode == 0

When I ran it, I didn't get anything output in the terminal nor in the folder. Does this represent a successful or unsuccessful run? If it is successful, where are the output files? Thank you!

mismatch between visual and collision

Hi, thank you for your nice tool! I tried the new version to generate a better obj model. It works well, but there is mismatch between visual obj and collision obj. The command is like this:

obj2mjcf --obj-dir . --save-mjcf --compile-model --verbose --decompose --overwrite --add-free-joint --coacd-args.pca

Is there any problems? Thank you for your help!
image

a question

I still have a question about the xml file generated in this way. The origin obj file is a robot with about 6 to 10 joints and links, is the function of the robot links still work or not? Since i cant see joint information directly on the right board "joint"
image

Failed to Compile Model

Hello Kevin! Great tool!

I have the whole model of the robot in the blender, which I exported as .obj with the corresponding .mtl.
Once I convert using the obj2mjcf using the following command:

obj2mjcf --obj-dir "/home/dhruv/Documents/Model" --save-mjcf --compile-model --verbose

I get following error:

Error compiling model: File or filename expected, got 'PosixPath'
INFO:root:Saved MJCF to /home/dhruv/Documents/Model/robot/robot.xml

and when I compile the model using mujoco, I get following error:

Traceback (most recent call last):
  File "/home/dhruv/Documents/Model/test.py", line 4, in <module>
    model = mj.MjModel.from_xml_path(xml_path)
ValueError: Error: faces have inconsistent orientation: robot-v3_6
Object name = robot-v3_6, id = 6, line = 26, column = -1

I am not sure what I am doing wrong. Would appreciate all the help!
Thank you!

Modified for using OBJ or STL files only?

Hi, is it possible to modify the code to use the OBJ file only? The MTL file is only needed for the material info, so I wonder if it is possible to use a default material. It might be helpful because I think in Solidworks and Fusion 360 (Blender supports I think), exporting MTL files is not supported. Also, would it be hard to modify the code to support the STL format? Thank you.

Mesh collision not working properly

Hi,

Thanks for the great package! I am a student and I was trying to use obj2mjcf to convert a beaker.obj model into sub-meshes to use as a hollow object in mujoco.

I managed it separate it into 28 convex hulls, however, when I load the mesh into the env I have with a table and robotic arm, it penetrates the table and has its COM on the side.

I have attached the initial setup:
screenshot

and after free falling from a height:
screenshot

As a reference, the box and the mug model from MuJoCo are behaving fine.

Please see here for the xml code:
https://github.com/cherylwang20/mj_envs/blob/3e73f238eea48cffed0423d7fcd604b467838b4a/robohive/envs/arms/ur10e/scene_gripper.xml

Any help is greatly appreciated! Thank you.

Issue with Meshes Using Boolean Modifier in OBJ to MJCF Conversion

Hello,

Apologies for opening another issue. I'm experiencing a problem when converting OBJ models to MJCF, specifically with meshes modified by a Boolean modifier in Blender.

Context: I am designing a rack intended to store beakers, created by using the shapes of the beakers to cut out corresponding holes within the rack model. This issue is somewhat similar to a previous problem I faced while designing a cylindrical base for a round-bottom flask. In that case, I was able to get around the problem by avoiding the use of Boolean operations, but for this rack model, alternatives seem limited.

Issue: When I compile the model without applying the Boolean cuts, it compiles successfully. Here's the model without the cuts:
Model without Boolean cuts

However, after applying the Boolean modifier and recompiling, I encounter the following error:

Error compiling model: Error: faces of mesh 'rack' have inconsistent orientation. Please check the faces containing the vertices 88 and 89.
Object name = rack, id = 0, line = 11, column = -1

Here is an image of the rack with a test hole cut and compiled:
Model with Boolean cut

Could you provide guidance on resolving this error? Any suggestions to ensure the Boolean modifier does not affect the mesh's consistency would be greatly appreciated.

Thank you for your assistance!

Please see here for my blender model and obj file:
rack.zip

Incompatibility with Python 3.7

Hey, thanks for making this tool! Not a big deal, but one of the function calls isn't compatible with Python 3.7 and requires 3.8+

Steps to reproduce:
Running obj2mjcf --obj-dir meshes/ --verbose --vhacd-args.enable, results in the following error:

Processing meshes/model.obj
INFO:root:Saving processed meshes to meshes/model
INFO:root:Decomposing /home/smnair/work/nutrition/Room_Essentials_Kitchen_Towels_16_x_26_2_count/meshes/model.obj
Traceback (most recent call last):
  File "/home/smnair/anaconda3/envs/nutrition/bin/obj2mjcf", line 8, in <module>
    sys.exit(main())
  File "/home/smnair/anaconda3/envs/nutrition/lib/python3.7/site-packages/obj2mjcf/_cli.py", line 508, in main
    process_obj(obj_file, args)
  File "/home/smnair/anaconda3/envs/nutrition/lib/python3.7/site-packages/obj2mjcf/_cli.py", line 239, in process_obj
    decomp_success = decompose_convex(filename, work_dir, args.vhacd_args)
  File "/home/smnair/anaconda3/envs/nutrition/lib/python3.7/site-packages/obj2mjcf/_cli.py", line 208, in decompose_convex
    file_to_delete.unlink(missing_ok=True)
TypeError: unlink() got an unexpected keyword argument 'missing_ok'

Based on this Stackoverflow post, it looks like the missing_ok parameter was only added to Pathlib in Python 3.8.

On the problem of not being able to decompose "nonconvex shapes" into associations of "convex shapes"

Hello,
I'd like to simulate pouring objects into a cup.
So, I created the STL file of the cup in CAD.

However, since the cup stl file is a non-convex geometry, it must be broken down into convex geom unions and they must all be attached to the same body in order to simulate in Mujoco.

So I was taught here before how to convert from stl files to obj files.

It worked that time, but when I tried again, when I ran the code A, only one file was generated and it was still a non-convex geometry.

What is the problem?
I would like to know ....
Thank you in advance.
Cup (2).zip

Feature Discussion

Hi Kevin,

in my pull request #12 you asked about other features I could find useful for this library.
I thought about creating kind of a feature-request or issue in here where others can more easily add their comments.

A few thoughts on obj2mjcf I would have are following:

  • absolute paths: Add the ability to create the Mujoco XML-Files with absolute paths to the actual object files, I found that they usually work better than relative paths, even if one has to recreate the xml files each time you move the files.

  • Creating URDF-Files: I know obj2mjcf's main purpose is for Mujoco, one could, however, easily add methods to extend it for URDF-files compatible with pybullet for example

  • Configurable node names in the XML Files: right now with the YCB Object files all the obj-files are called textured.obj. obj2mjcf then creates a folder textured with an textured.xml and names the body, model, materials and similar all something with textured. This causes problems when using the XML-Files since multiple nodes, for example mesh-nodes, with the same name is not allowed. (well, that would not be a problem if all the textured.obj files were named after their YCB Object, but they aren't). So it would be interesting to look into how to make these names configurable. Even more so if one would like multiple instances of one YCB-Object in a running Mujoco Simulation

  • Configurable Mass, Solimp and Solref for the XML File, I saw that you added the free joint and some default values for physics, but depending on the context one might require specific values here. The mass could then possibly even be applied per subcomponent after the decomposition

  • Alternative Decompositions: Of course you can not integrate every single Decomposition Library there is into this Library, but I found, in particular with the YCB Objects, a lot of the VHACD Decomposition does not make too much sense. I had better success with using the PyBullet VHACD Version. In terms of metrics/volume error the PyBullet Version might not be much better, but the chosen convex subcomponents make much more sense than the VHACD 4.0. This could be a matter of configuration but I attempted optimizing the VHACD 4.0 configuration to achieve as good results as with the pybullet version but I was unsuccessful

Just some examples here:
First the original Mustard Bottle, then PyBullet VHACD with default parameters, and VHACD 4.0 with default parameters:

Original Pybullet VHACD VHACD 4.0
image image image

I would be very happy to work with you on these tasks since I now have some experience with the topic, especially with the YCB Objects, and it would be helpful for my experiments too ^^

Edit: Put the Images into a table to declutter

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.