UuuNyaa / blender_mmd_tools

MMD Tools is a blender addon for importing/exporting Models and Motions of MikuMikuDance.
GNU General Public License v3.0
1.94k stars 92 forks source link

Some new features #78

Open One-sixth opened 1 year ago

One-sixth commented 1 year ago

About the new features

  1. Version number There is already quite a bit of migration code out there. I think the version number record can be added, which may be helpful for later mmd_tool upgrade. It also reminds the user to update the mmd_tool.

  2. When using object material, do not modify material deformation data. Build the material morph. Find the material slot where the material is located. Check the link settings for the material slot. If the mat_slot's link is set to 'OBJECT', the material slot's material will be used instead.

The material slot search function will return like this [(mesh_obj_name, mat_slot_idx, data_mat_index_in_this_mmd_model_all_data_mat), (mesh_obj_name, mat_slot_idx, data_mat_index_in_this_mmd_model_all_data_mat),... ]

  1. MaterialLibrary, which allows mmd models to have multiple sets of materials. Under mmd_root, add a child empty node named mat_lib as the root of the material library. In the material library root child, add some mesh_obj that do not contain any vertices. Each mat_lib_mesh_obj will stores a set of materials.

In this state. We will use data_mat to indexing. Use the obj_mat to rendering. The main_model and mat_lib_mesh_obj will always have the same data_material order and quantity.

  1. Material Library Panel (located in 3D viewport)

Button 1: Synchronize the material slot Create, delete, modify, and move material slots in the main model. Use this button to quickly align the order and number of material slots in the material library with the main model.

Implementation: Use data_mat to index and sort (Another implementation is to use a main_model_instance. But that's not easy to deal with multiple main model.)

Button 2: Synchronize material properties This can be used to quickly synchronize obj_mat and data_mat properties. For example, material_jp_name and texture path. Only data_mat's properties -> obj_mat's properties. The obj_mat's properties -> data_mat's properties function not in this panel.

Button 3: Check and repair the Material Library Includes some misc function to make the material library usable again. Such as (Option. Allow manually ban some fix function):

  1. Make the data_mat of the main model unique so that the material slot location can be correctly indexed.
  2. Always set mat_lib_mesh_obj 's material link in 'OBJECT' mode.

(Another approach is allow a data_mat to match multiple obj_mat. This will get a list of duplicate data_materials, but each material slot can be set independently. And it have difficult problems when merging multi main models.) (There are multiple approaches to detail, not sure which is better)

  1. Material Library Panel 2 (located in the Material Properties Panel)

Function 1 Repeat material hit: Displays a hint and a button for material uniqueness. Function 2 Material reset: Reset the current obj_mat same as data_mat. Function 3 Material property synchronization: Copy some of the current obj_mat properties to data_mat, or reverse copy.

  1. Customize the material shader node New material node feature for mmd_uuunya_tool, support simple custom material nodes. Such as: A custom material node will be treated as a custom material node if its name starts with "uya_mat." . If the input port name of the custom node has "color", it can be automatically link to the MMDShaderDev's color output. The first shader output port of the custom node, which can be automatically linked to the material output node.

This makes the mmd_uuunya_tool very easy to add and reuse existing materials.

6. The material library feature is an additional feature, not a requirement for mmd_tools, and might be added to mmd_uuunya_tool as well.

7. When the version number record is added for a period of time. And the material library function is nearing completion. Maybe we can move on to the next step. 7.1 Change the definition of material_morph's related_mesh to point to related_mesh_obj. Not related_mesh_data. 7.2 No longer use the material_id, no longer compatible with the old mmd_tool.

UuuNyaa commented 1 year ago

Thank you for the ideas! πŸ˜ƒ

I did not know the difference between obj_mats and data_mats. I have found that using obj_mats and data_mats on the same mesh data at the same time can be unstable.

https://user-images.githubusercontent.com/70152495/197435247-31062204-2bb3-454d-9cda-f17c9742a314.mp4

object-data_materials.zip

So we may need to devise UI that does not allow users to easily change material slots.

  1. Version number

I added it.

1 ... 7

I will try to figure out how to implement it.

One-sixth commented 1 year ago

@UuuNyaa We can divide our work to make us more efficient.. For the material library, I have a relatively clear implementation idea, and in the next few days I can make a simple framework that can help you understand quickly. Then continue to discuss and improve will more easy.

You can continue to refine the version number-related situation, which is usefull for the upgrade and help more users proactively upgrade their mmd_tools. The 5th idea custom shader node, this one is also a standalone idea. It will makes uya_mat very easy to extend.


About the version. I think it can add 4 features.

Panel disable. If the current mmd_tool version is less than the version carried by the file, the mmd_tool panel will be disabled. A warning message will be displayed in the panel to remind the user to update to the mmd_tool website.

Force write. (This need to delay or ignore the migrated code) Under the warning message, add a check box to whether to force enable mmd_tool function. If checked, the migration code will run, the force_write flag will be set, the check box wiil be disappear.

If force_write was enabled. When saving a blend file, it will overwrite the mmd_tools version and set the force_write flag to false. Otherwise will keep the origin version.

If the mmd_tool version is greater than or equal to the existing version of the file. The version will be always refreshed and force_write will always be false.



About the material lib features. Difference between data_mat and obj_mat. The point is that obj_mat is bing with mesh_obj and data_mat is bing with mesh_data.

You can select a mesh_obj, switch some material slots to obj_mat mode, and change its mesh_data to another mesh_data. Then you'll notice that obj_mat hasn't changed, but data_mat has.


In the Python API, If we need to access obj_mat that the material slot must be switched to obj_mat mode (I have not found another way to access it). But data_mat we can always access it through mesh_obj.data.materials.

That's why we need to keep the mat_lib_obj's material_slot always in obj_mat mode.

Because material_slot mode is very easy to modify by the outliner with mistake. Therefore, we need a repair button on MaterialPanel to proactively fix its state.

material_slot.link = 'OBJECT' will be set to obj_mat mode. material_slot.link = 'DATA' will be set to data_mat mode. Snipaste_2022-10-26_00-12-05


If we choose this method: main_mesh_obj and mat_lib_obj has same mesh_data. One problem is that after using the detach model function, We need to keep a mesh_obj with no vertices, containing all data_mat, at the top of the mesh_obj_list. At the same time, users are required not to merge mesh_obj arbitrarily to avoid mesh_data reference loss.

My preference is that main_mesh_obj and mat_lib_obj don't share mesh_data. The mat_lib_obj contains an empty mesh_data. After modifying the number or order of main_mesh_obj mat_slots, manually use the Sync button to synchronize the mat_lib_obj.

nagadomi commented 1 year ago

I think that if MaterialLibrary feature is not related to PMX or MMD modeling in general, it is better to be a stand-alone add-on.

This is just my personal opinion, The feature seems to be a preset function of the material for the entire model. Generally, a model is composed of multiple objects. While working on a project, new clothing models are added, objects are separated or joined, and materials are deleted or added. So I think it would be very costly to manage presets for the entire model. Also, materials cannot be shared with other objects because they depend on the object's UV Map. So it would be practical to implement a switch node within each material and switch connections from the driver or add-on. e.g. https://github.com/powroupi/blender_mmd_tools/issues/300#issuecomment-659653836

One-sixth commented 1 year ago

@nagadomi I think the material library properties can be placed in mmd_tools or mmd_uuunya_tool. But I don't think it should be used as a standalone plugin, which I think might be too trivial. This material library feature is mainly used to solve the problem of multiple material management of MMD models in Blender, and does not currently serve non-MMD models.

Detach or join new models, delete or add material slots. This is not a problem, I will consider it as well. It contains a sync button that automatically synchronizes these changes.

No.5 idea solves the problem that materials cannot be shared directly. We can share custom material nodes directly without share the original material. Snipaste_2022-10-27_00-26-39 Snipaste_2022-10-27_00-36-33

One-sixth commented 1 year ago

@UuuNyaa @nagadomi Hello, I have preliminarily finished the material library base feature. Also added material_morph support for obj_mat. The code is here: https://github.com/One-sixth/blender_mmd_tools/tree/add_material_library_feature

Some idea has changed. It is now supported that data_mat can be mapped to multiple obj_mats in material_morph.

UuuNyaa commented 1 year ago

@One-sixth Thank you for your effort πŸ˜ƒ I will check the commit.

One-sixth commented 1 year ago

@UuuNyaa Hello, I have completed the custom_node features. Here's the code. https://github.com/One-sixth/blender_mmd_tools/tree/add_custom_node_features

I have organized the code as mmd_tools, which should be easy to modify and move elsewhere. This is the keyword of custom node I used in the current design.

Naming Recognition

A node_group or node_tree 's name start with "nya_node."

--------------------------------------------

Define

Custon Node Input (Standard)
----------------------
mmd_shader_exist        :type=float
mmd_shader_output       :type=shader
mmd_shader_color        :type=color
mmd_shader_alpha        :type=float

mmd_base_tex_exist      :type=float
mmd_base_tex_uv         :type=vector
mmd_base_tex_color      :type=color
mmd_base_tex_alpha      :type=float

mmd_sphere_tex_exist    :type=float
mmd_sphere_tex_uv       :type=vector
mmd_sphere_tex_color    :type=color
mmd_sphere_tex_alpha    :type=float

mmd_toon_tex_exist      :type=float
mmd_toon_tex_uv         :type=vector
mmd_toon_tex_color      :type=color
mmd_toon_tex_alpha      :type=float

mmd_sub_tex_exist       (No support yet)
mmd_sub_tex_uv          :type=vector
mmd_sub_tex_color       (No support yet)
mmd_sub_tex_alpha       (No support yet)

Custon Node Output (Standard)
----------------------
shader_surface          :type=shader
shader_volume           :type=shader
shader_displacement     :type=vector

Custon Node Other Link (Option)
----------------------
(Auto find and link same sockets)

Here's the demo video:

https://user-images.githubusercontent.com/23693633/200168521-2338ad16-8c56-4650-a273-ea2fd874a899.mp4

Here is the demo file: test1.zip

UuuNyaa commented 1 year ago

@One-sixth Wow! πŸ˜ƒ Many features seem to be added. I will look at them when I have time.

And thank you for your effort.

UuuNyaa commented 1 year ago

@One-sixth I have tried the features.

And I have 3 questions.

  1. What is the intended use for Material Library?
  2. Custom Nodes is a feature independent of Material Library, right?
  3. Do you think these features can be implemented without modifying the MMD Tools itself?
One-sixth commented 1 year ago

@UuuNyaa

1. The main purpose of Material Library is to synchronize the material settings across multiple sets of materials. In my use case. I add multiple sets of rendering materials in one model.

Different rendering-style, such as PRB, NPR, Genshin-like. Different environments, such as indoor and outdoor. They use different presets.

Sometimes, the model needs to modify, add or delete materials, adjust the order of materials, adjust material settings, change the material texture. Manual property synchronization will take a lot of time

This feature also can achieved through library overrides, but library overrides have critical bugs and don't know when they will be fixed, so I came up with the Material Library feature.

2. Yes

3. The mmd_tools need to modify the implementation of material morph. Make it support dynamic object material selection. Then can support material library features.

I think the new features also can moved to a tool with high popularity and high mmd_tools related. The blender_mmd_uuunyaa_tools is a good choice too.

I think the new features provide a way to solve mmd_tools long-standing material management challenges.

UuuNyaa commented 1 year ago

@One-sixth Thanks for the answer!

  1. I understood your use case

  2. I would try to implement this feature in the following steps
    1. Improve mmd_tools to support dynamic object material selection
    2. Add material library feature to mmd_uuunyaa_tools
    3. Add custom nodes feature to mmd_uuunyaa_tools

I would prefer that mmd_tools not have the material library feature. Because it is not possible to export to MMD(.pmx) What do you think?

One-sixth commented 1 year ago

@UuuNyaa Great! I agree with you. Move the new feature to mmd_uuunyaa_tools. But I think material_library features should affect mmd model export. Because of the influence of material_slot.material. When a preset materials is enabled, the exported material uses the current object material.