BramStoutProductions / MiEx

A modern Minecraft Exporter
BSD 3-Clause "New" or "Revised" License
47 stars 6 forks source link

Updated Blender Addon #76

Open BramStout opened 1 month ago

BramStout commented 1 month ago

With the latest version of MiEx (1.6), there is better support for importing materials into various DCCs, including Blender. MiEx can export out the materials as a JSON file, which an import script can then read and recreate the materials in the DCC. This helps when the USD importer included in the DCC doesn't fully support importing in materials, which is the case with Blender. The current Blender addon does not yet use this system, and so it would be nice to update it.

I have already created a Maya import script which uses this system. It imports in the export via USD like normal, but then it checks if the material JSON file exists and if so, it reads it in and sets up all of the materials. https://github.com/BramStoutProductions/MiEx/blob/main/extras/Maya/Maya_MiEx_Importer.py#L272

This does require a new resource pack specifically for Blender, that tells MiEx to export out the materials into the JSON file. You can see an example of this in the ForMaya resource packs. It starts off by creating a placeholder material that is just a UsdPreviewSurface node that gets written out into the USD file. This causes the DCC's USD importer to create a new material and assign it to all of the geometry. Then all of the actual materials have JSON: at the start of their types. That tells MiEx to export out those nodes to the JSON file. The actual types of those nodes would then be the DCC's native types. In the StandardSurfaceForMaya resource pack, you can see that the placholder node uses inputs: and outputs: prefixes, while the other nodes don't. That's because in Maya attributes don't have those prefixes and the import script just directly looks up an attribute with that name, therefore the actual shading nodes don't use these prefixes.

The material JSON file exported looks pretty similar to that of the material templates, but it can contain keyframes. Keyframes are stored as a single list of [frame,value, frame,value, frame,value, frame,value]. Value could be a number or an array of numbers if the type is a compound type. The keyframes are used for animated textures.

Next to updating the addon, a new Blender resource pack should be created with the new material templates specifically for Blender. The ForMaya resource packs can be used as an example for this.

Another things to note, is that Blender by default renders out all meshes as double sided (a.k.a. without backface culling). However, Minecraft renders out meshes as single sided (a.k.a. with backface culling). MiEx also exports out meshes assuming that it's single sided. In some cases they can be set to be double sided, which is specified in the miex_config.json file. MiEx writes out for each mesh whether it's single sided or double sided, but Blender does not read this property. That's because, unlike pretty much every other application, Blender has the flag on whether a mesh is single sided or double sided (backface culling) on the material rather than the mesh.

The easiest way to fix this would be to have the addon simply turn on backface culling (make everything single sided) on every single material. There are a couple of blocks that MiEx will export out as double sided, so we can fix that by overriding the doubleSided attribute in the miex_config.json file in the Blender resource pack, and set it to be an empty array.

I don't have enough experience with Blender to update this addon myself, so I am opening it up as an issue so that somebody else can take on this task. @BlueEvilGFX created the original Blender addon, so would you be able to do this update of it?

BlueEvilGFX commented 1 month ago

I am not sure if I understood this correctly. So if I export a world a new ..._materials.json file is created in the same directory as the export? Or must the .json file be manually created/ set up?I I could not find an option for this. So might do something wrong there.

BramStout commented 1 month ago

I am not sure if I understood this correctly. So if I export a world a new ..._materials.json file is created in the same directory as the export? Or must the .json file be manually created/ set up?I I could not find an option for this. So might do something wrong there.

If the material templates used, specify nodes whose type has the JSON: prefix, then MiEx will generate the ..._materials.json file in the same directory as the export. This does mean that there needs to be a Blender resource pack with material templates that use the JSON: prefix so that the ..._materials.json file gets created.

You can do an export with the StandardSurfaceForMaya resource pack to see the ...materials.json file get generated https://github.com/BramStoutProductions/MiEx/tree/main/extras/example_resource_packs/StandardSurfaceForMaya

BramStout commented 1 hour ago

MiEx now has a new Blender addon! You can read more about the new Blender addon here: https://github.com/BramStoutProductions/MiEx/wiki/8.-Importing-into-DCCs#blender