I have a .obj, .mtl files with multiple materials
for a specified material, I want to print the vertices and their corresponding uv coords.
Is there a way to e.g. parse a .obj, and .mtl file and export them to json?
When using
wavefront-obj-parser
PyWavefront
meshlab
All these tools generate a json file I think that the data is partial - I don't see the materials in there.
EDIT:
I tested with an example 3d model
step1: converted the .obj and .mtl with multiple materials into a .json file
Added the addon to Blender (version 2.76b) as instructed in here
In Blender, imported the .obj, .mtl (the model shows up but not the material textures)
In Blender exported to Three.json (see here)
step2: loaded the generated .json file (foo1.json) within a threejs example
Used the example webgl_loader_json_objconverter.html from here
Replaced Female02_slim.js with foo1.json
The model Male02_dds.js is loaded but the foo1.json is not and I'm getting an error message: TypeError: vertices is undefined
Some links indicate that Blender Exporter and Json File Loader are not so reliable (see here, here, here, and here in general)
Is there any other converter from .obj, .mtl to .json that anybody can recommend? (not necessarily in relation to three.js)
Assuming that I can convert to glTF format, is there a tool, (e.g. a command line utility in python) that will let me specify a material index and get the vertices and corresponding uv coords for this material?
Solved:
I ended up using meshlab plugin export to json. It works (almost) fine for me
Sure, you can use Blender and THREE JS plugin (exporter):
https://www.blender.org/
https://github.com/mrdoob/three.js/tree/dev/utils/exporters/blender
Exported file have materials as array if you use JSON Loader function
https://threejs.org/docs/#api/loaders/JSONLoader
Related
I am trying to convert an .fbx file to a .dae (Collada) file. I have tried using an application for this called FbxConverterUI, however when I drag the Collada file into Xcode it doesn't load all of the animations, meshes and textures from the .fbx file. I use Blender to create the .fbx file. I did try to export it in Collada format from Blender however only a single animation loaded with multiple copies of that one, the animation was also different than how I keyframed it so I bumped into exporting it in .fbx which was loaded correctly when I imported it back into Blender to check that the .fbx format would work.
I lastly tried to export it in usdc and obj but I wasn't bothered with obj because it filled my desktop with like 100 files, the usdc didn't work either because there was not textures or colour and no animations, just a white mesh.
If you could explain how I could convert .fbx to Collada with all of the animations, textures and mesh stays the same that would be great. Or if there is another way to export it in Collada (from Blender) with it working properly. Any other methods that work which you suggest me do would be amazing.
Thanks.
In case you need some kind of guidance, this are my "prefered" export settings from Blender (v.3.0) when it comes to animated models - and I usually had success with it. I am using a separate DAE File for the Model, including the rig - and for the animations I create separate DAE files, containing only the animation, that can be applyed to the node containing the rigged model using SCNAnimationPlayer. (do not convert the those type of DAE files to SCN files using XCode - this may break the file in some cases)
There are many more settings to tweak. (in my example picture anything else is left default)
Keep in mind: This is for SceneKit. For other rendering engines, things may vary.
I have been experimenting with different 3D model file formats in Xcode, primarily .obj and .dae.
However, after I export these files from Blender, they either don't have any lighting associated with them or all of the values (especially their metalness and roughness) are not what they were initially setup to be.
.obj export,
.dae export,
then the blender version.
Is the issue how I'm exporting the models from Blender, the models themselves, or something else?
obj
If you create a model in Blender (or in Maya), remember, every app has its own internal shaders that may differ from SceneKit's shaders. When you export any model as .obj you also get a corresponding uv-mapped .mtl texture file. There are no lights inside .obj, because this format does support only geometry (with smoothness, normals and UV IDs). And one more thing – .obj doesn't support animation.
dae
.dae format allows you export not only 3D geometry with animations but also its corresponding shaders (like Phong, Blinn or Lambert) with colours and textures (however, Blender or Maya's procedural textures are not supported). And all the lights that were in 3D scene will be also exported.
usdz
The best candidate for export is .usdz file. Not all 3D authoring tools have export usdz option, but Autodesk Maya 2022, thanks to USD plugin, has.
.usdz format has all the features that .dae format has, but in addition to that I can say it's native to RealityKit, SceneKit, Reality Composer and QuickLook, its hierarchical system was adapted for larger scenes and it supports Python USDZ schemas.
I noticed the .mtl file that gets exported along with my .obj exports isn't something I'm required to directly load when using Three.js, but I was wondering if the library loads it silently if it can find it in the same directory that it pulls the .obj from. I noticed in a directory where I didnt include the .mtls, the textures didn't map to my models, and I'm trying to understand what importance they have to Three.js if any. Does three.js load the .mtl file silently? Are they necessary at all for mapping textures?
Does three.js load the .mtl file silently?
No. If you just use OBJLoader, any related MTL files are ignored and default materials are applied. You have to use MTLLoader like demonstrated in the following example to load any material definitions. As you can see in the source code, you essentially perform two request. You first load the MTL and then the OBJ file.
https://threejs.org/examples/webgl_loader_obj_mtl.html
Are they necessary at all for mapping textures?
No, texture coordinates are defined in the OBJ file. So you can load the OBJ and apply a texture manually after loading like shown here:
https://threejs.org/examples/webgl_loader_obj.html
So I have two issues right now.
it seems objectloader never load the texture of 3d model from json files.
I don't know somehow, the positions of 3d models change a little bit after THREE.js loader load them.
Here is what it should look like
Here is what it looks like on my browser
Here is THREE.JS code:
var loader = new THREE.JSONLoader();
loader.load("./script/treehouse.json",function ( geometry) {
var mesh = new THREE.Mesh( geometry);
$scene.add( mesh );
});
Export setting:
And here is how the json file looks like:
Update: Now since I chose dae format, it looks exactly the same from blender, however, textures are still not there, and the color of models change every time i open the dae files or refresh the page.
I suggest using a different format than THREE.js internal format.
It's been known to change a bit between revisions and will end up requiring you to re-export assets later on. You're better off using a format that is standard, like GLTF, Collada, OBJ, or FBX. Additionally, the THREE json format is pretty bloated and results in files that are pretty easy to read, but can be quite large.
I recommend GLTF 2.0 (gltf) or Collada (.dae) format. You will get smaller files and the format should remain more stable. If you use Gltf, you can also use Don McCurdys nice GLTF previewing tool to sanity check your files if you are having problems.
https://gltf-viewer.donmccurdy.com/
There is also the three.js editor that can be helpful for sanity checking files.. try dragging your json on here:
https://threejs.org/editor/
I decided to try out gltf format for rendering models in Three.js and I'm using this converter (http://cesiumjs.org/convertmodel.html) to convert the model from Collada to GlTF. It spits out a *.gltf file, however in the examples in Three.js they pass a *.json file. How can I load the *.gltf file or how can I get the proper set of files to load? Thanks
You can use THREE.glTFLoader which can be found in the examples/js/loaders directory.
For a demo, see http://threejs.org/examples/webgl_loader_gltf.html.
three.js r.95
The gltf file is a valid json file as far as I understand it.
I opened a file converted from collada to gltf and it is just plain json.
I put it also through the validator at JSONLint.
I would suggest to change the file ending from .gltf to .json .