powroupi / blender_mmd_tools

mmd_tools is a blender addon for importing Models and Motions of MikuMikuDance.
GNU General Public License v3.0
1.79k stars 278 forks source link

character conversion to pmx with mmd_tools? #80

Open Hogarth-MMD opened 6 years ago

Hogarth-MMD commented 6 years ago

Is it possible to convert a character to pmx using mmd_tools? Someone could import a character into Blender from Blender or Sims or Poser or XNALara or whatever, rename the bones to MMD Japanese bone names and then try to export it to pmx with mmd_tools. Would this be successful? Or will someone need to write a python script which adds all of the necessary pmx custom properties to the model before this could be successful?

nathanvasil commented 6 years ago

The necessary .pmx custom properties are contained in New MMD Model. Nothing else is really necessary. Any mesh you can import, you can parent to a new MMD model armature and walk away with a .pmx character.

It certainly is possible to convert characters to .pmx using MMD Tools. It happens a lot. The actual amount of manual labor depends on the input format and how much you can get out of it. Are you trying to turn a character .obj into a character .pmx? There's a lot of manual work. Are you try to turn an imported Metasequoia or native Blender file into a .pmx? There's very little manual work. Note that there are a million different ways to control bones-- I believe MMD's IK solver is non-standard, and keep in mind that many games do some IK-like behavior in the code rather than in the model. And there are even more ways to define materials. Just because you get all the bones and weights out, doesn't mean it will move like it does in the original application, and just because you get some kind of material settings out, doesn't mean that it will look like it does in the original application.

Hogarth-MMD commented 6 years ago

http://www.manuelbastioni.com/download.php

http://www.makehuman.org/index.php

Manuel Bastioni Lab (Blender add-on) and Makehuman might be logical sources of free character models for MMD. I haven't tried to convert these for MMD, though. I don't know how difficult this would be.

nathanvasil commented 6 years ago

I expect that the most work would be in translating the bone names. And a relatively quick edit of the dictionary (reversing it, essentially) could probably deal with that. But I haven't used either of those addons.

Hogarth-MMD commented 6 years ago

So, how can someone convert a character model from other software to .pmx using mmd_tools? I tried to do this but I was not successful. Many people are wanting to know how to do this. I don't see any button which says "New MMD model". There is a button which says "Create Model", but this creates a separate model. It does not help convert an existing model to pmx.

nathanvasil commented 6 years ago

"From other software"-- depends on what software.

Let's say you just made a new character model in Blender, and you want to export it for use in MMD (for example, you followed https://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/Bones ). It has an armature, it is weighted, etc.

Start by creating a new MMD model, via the "Create Model" button. This gives you an armature parented to an empty, but of course you already have an armature you want to use instead. Select the armature of the New MMD Model and delete it. Then, select your armature, shift-select your New MMD Model empty, and ctrl-P->Object to parent the model's armature to the empty. Now, when you export the model, you'll see that you have your model's mesh and your model's armature.

There are a lot more styles of models than just this simplest one. I've tried to export models using Blenrig and couldn't wrap my head around how to do it, because they have some weird, Blender-specific abstractions for handling deformation. So the specifics of what you're exporting matter. There isn't always a simple, obvious way to translate a model into a format understandable by MMD.

MMD Tools uses various MMD characteristics to store things the way MMD wants to see them. For example, you have a MMD Materials subsection of your materials. MMD Tools tends to handle this relatively one-way: if you update your MMD Materials, it will update your default, Blender materials; but if you update your default Blender materials, it won't update your MMD materials. Because of this, you may have to specify a large number of characteristics. Some of these will be autogenerated on export if you don't specify-- for example, if a bone has no MMD name, it will use the Blender name. For others, it wouldn't really make a lot of sense to copy over values: MMD renders differently than Blender, and it would be hard to say exactly what is the best way to generate MMD Ambient/Diffuse values.

Some of this could probably be automated by MMD Tools more effectively than it currently does.

Hogarth-MMD commented 6 years ago

Feature request: Someone can select an armature or a mesh object which has an armature modifier, and press a button, and mmd_tools will automatically add an mmd_tools root/empty object and all needed custom properties for exporting this model from Blender with mmd_tools. Many people are wanting to be able to do this, but the method suggested by @nathanvasil is complicated for some people.

Hogarth-MMD commented 6 years ago

Creating the mmd_tools root/empty object and making it the parent of the armature can be done quite easily using Python code. But additionally all of the mmd_tools custom properties must be assigned. @powroupi , can you write a python script which prints out all of the mmd_tools custom properties from a scene? A script which can assign values to all of these custom properties? This is a fundamental function which is missing from mmd_tools. If someone tries to do a character conversion to MMD with mmd_tools, this cannot be done, and this is an evil, joyless waste of time, frustration, and futility, for someone who tries to do a character conversion to MMD with mmd_tools.

powroupi commented 6 years ago

The basic method is just like @nathanvasil said: "Create Model", replacing the armature with your armature, parenting all mesh objects to your armature, then you are ready to export.

Some basic material settings can be converted via python script, however, material nodes / cycles / bone constraints are almost impossible, you have to create those for MMD by yourself.

If you want to convert materials, the following script might help which converts the materials of selected mesh objects :smile:

import bpy
def convert_to_mmd(mat):
    mmd_material = mat.mmd_material

    diffuse = mat.diffuse_color[:]

    mmd_material.diffuse_color = diffuse
    mmd_material.ambient_color = [0.5*c for c in diffuse]

    mmd_material.alpha = mat.alpha
    mmd_material.specular = mat.specular_color
    mmd_material.shininess = mat.specular_hardness
    mmd_material.is_double_sided = mat.game_settings.use_backface_culling
    mmd_material.enabled_self_shadow_map = mat.use_cast_buffer_shadows
    mmd_material.enabled_self_shadow = mat.use_shadows
    mmd_material.edge_color = mat.line_color

for obj in bpy.context.selected_objects:
    if obj.type == 'MESH':
        print(obj.name)
        for mat in obj.data.materials:
            if mat:
                print('    -', mat.name)
                convert_to_mmd(mat)
Hogarth-MMD commented 6 years ago

Thank you, @powroupi ! Searching for the mmd_tools custom properties, I have found this list. Are there any others which are not in this list? But if I use auto-complete with these properties in the Blender python console, there is no clean separation of the mmd_tools custom properties from the built-in Blender custom properties. Do you know any solution to this problem?

bpy.context.active_object.mmd_root.vertex_morphs bpy.context.active_object.mmd_root.bone_morphs bpy.context.active_object.mmd_root.group_morphs bpy.context.active_object.mmd_root.material_morphs bpy.context.active_object.mmd_root.uv_morphs bpy.context.active_object.mmd_root.comment_text bpy.context.active_object.mmd_root.comment_e_text bpy.context.active_object.mmd_root.name bpy.context.active_object.mmd_root.name_e bpy.context.active_object.mmd_root.display_item_frames bpy.context.active_object.mmd_root.display_item_frames[3].items.keys() bpy.context.active_object.mmd_root.display_item_frames[3].items.values()

bpy.context.active_object.mmd_type bpy.context.active_object.mmd_camera bpy.context.active_object.data.materials[0].mmd_material bpy.context.active_object.pose.bones[0].mmd_bone bpy.context.active_object.pose.bones[0].mmd_shadow_bone_type bpy.context.active_object.mmd_rigid bpy.context.active_object.rigid_body bpy.context.active_object.mmd_joint bpy.context.active_object.rigid_body_constraint

powroupi commented 6 years ago

All custom properties are in mmd_tools/properties. A object (bpy.types.Object) has mmd_type, mmd_root..., a material (bpy.types.Material) has mmd_material, a pose bone (bpy.types.PoseBone) has mmd_bone..., etc.

bpy.context.active_object.rigid_body and bpy.context.active_object.rigid_body_constraint are provided by Blender.

And please note, mmd_root, mmd_camera, mmd_rigid, mmd_joint are used according to mmd_type (a object would not use all of them):

mmd_type properties class reference
ROOT mmd_root MMDRoot properties/root.py
CAMERA mmd_camera MMDCamera properties/camera.py
RIGID_BODY mmd_rigid MMDRigidBody properties/rigid_body.py
JOINT mmd_joint MMDJoint properties/rigid_body.py
Hogarth-MMD commented 6 years ago

Thank you @powroupi for the materials conversion script. :smile: However, the exported model crashes MMD. This happens because there are no bones and no morphs in display panel groups. Additional python scripting is needed. Also the model is exported with no model name. Also a bone is exported as a rotate-move bone when it should be exported as a rotate bone.

powroupi commented 6 years ago

Okay, I'll see what I can do about the exporter, hopefully some basic properties can be added automatically. :smile:

Hogarth-MMD commented 6 years ago

To solve this problem of model naming:

bpy.context.active_object.mmd_root.name = "powroupi"

Now all converted models will be named "powroupi" , in honor of @powroupi . :smile:

powroupi commented 6 years ago

Also the model is exported with no model name.

Did you use [Create Model] button to create basic MMD model? The basic MMD model should have default name New MMD Model and 2 default display panel groups Root, 表情, and it is not crashing MMD. :confused:

Also a bone is exported as a rotate-move bone when it should be exported as a rotate bone.

Set lock_location property of a pose bone to [True, True, True] explicitly (bpy.context.active_pose_bone.lock_location = [True, True, True]) if you don't want it to be movable in MMD. It is a ability to create a connecting bone which can also be movable in MMD ~and the ability to keep exported bone data the same as imported bone data as possible~. :)

EDIT: bone's link to (display connection) property is still inconsistent in some case which is a minor issue. :sweat_smile: Maybe treat a connected bone as non-movable is a better idea, and it's more convenient to convert a model. :smile:

Hogarth-MMD commented 6 years ago

I have been making a new add-on to do a mass renaming of bones for 10 different armature types. I have also been making a script to automatically add leg IK to an MMD model. These 2 new scripts would be very helpful to convert models to MMD. But I have a very low level of motivation to finish those add-ons and to upload them. I am waiting for mmd_tools to have the ability to conveniently convert a model to MMD from other software. mmd_tools cannot do that yet.

(BTW, The most boring task in the history of mankind is to manually rename finger bones. The second most boring task is to write a python script to do a mass renaming of finger bones. LOL. :smile: )

powroupi commented 6 years ago

I am waiting for mmd_tools to have the ability to conveniently convert a model to MMD from other software. mmd_tools cannot do that yet.

You may add some links of sample models so that I can see if there is any way of automation, otherwise, I can only focus on tools for editing MMD models in Blender. :expressionless:

Hogarth-MMD commented 6 years ago

https://xnalara.deviantart.com/ https://xnalara-customized.deviantart.com/

Blender add-on for import export of XNALara models: https://www.mediafire.com/folder/76g7rvt4ec661/XPS

Hogarth-MMD commented 6 years ago

I was thinking about the problem of converting Sintel for MMD. The Sintel facial animations are made with shape key drivers. Is there any way to convert a shape key driver animation to a shape key animation? I don't know how to do that.

powroupi commented 6 years ago

In Blender, shape keys of some advanced models are driven by bones, also the bones have custom shapes, so animators can pose a model much easier. I don't know how to do that in MMD. :cry: You can simply ignore that while converting to MMD, I think.

Hogarth-MMD commented 6 years ago

In mmd_tools, the textures are diffuse, toon, sphere. In an XNALara model, the textures seem to be diffuse, specular/lightmap, normal map. And there may be other differences with other 3D model formats. So this is an issue with converting and exporting models to .pmx.

Hogarth-MMD commented 6 years ago

Drivers and driven shape keys are not possible in a .pmd or .pmx model outside of Blender. Are you telling me that your mmd_tools .vmd exporter will automatically convert a shape key driver animation to a shape key animation?

powroupi commented 6 years ago

So this is an issue with converting and exporting models to .pmx.

Yes, it is an issue, but you can Disable SPH/SPA on export. I think it is only possible to export the base textures correctly.

mmd_tools .vmd exporter will automatically convert a shape key driver animation to a shape key animation?

No, I'm not sure how to do that yet. :cry: Bone animation can be baked in Blender, so export bone animation may have less issue. I'm not sure if there is a way to bake shape key animation in Blender except writing a python script. If shape key animation can be baked, I think the only problem is just the VMD file size. :yum:

Hogarth-MMD commented 6 years ago

Blender's .obj importer and .obj exporter are using a material's mirror color to store the ambient color. So the mmd_tools exporter should have an option: "Use mirror color as ambient color".

I am eagerly anticipating the complete implementation of model conversion and export with mmd_tools. I wish for @powroupi happiness and success in this and in everything else. :smile:

Hogarth-MMD commented 6 years ago

Hello @powroupi ! I have been working on an armature conversion python script for Sintel. The mesh objects of Sintel have modifiers on them. This is possibly an issue for model conversion with mmd_tools. Please tell me what does the mmd_tools exporter do with modifiers.? When exporting, are modifiers automatically applied? Or are modifiers ignored? Possibly there should be an export option to apply or ignore mesh modifiers. There might be an issue with modifiers which cannot be applied to a mesh object which has shape keys.

powroupi commented 6 years ago

mmd_tools exporter ought to export the model the same as previewed, you can disable particular modifiers as you like. And some modifiers don't like shape keys, I don't have the list. When you get some weird vertex morphs, it probably is caused by those modifiers.

EDIT: You can get more close result by enabling Visible Meshes Only on export. :)

Hogarth-MMD commented 6 years ago

Hello @powroupi ! The textures of Sintel are completely failing to be exported with mmd_tools. In the pmx file the textures are being referenced strangely and incorrectly, for example:

..\..\..\..\Sintel\textures\sintel_sweater_painted.jpg

I ran the materials correcting script by @powroupi but this was ineffective. Do you have any idea why this is happening and how to fix it?

powroupi commented 6 years ago

Enable Copy Textures or export pmx file to the Sintel folder may solve texture path issue.

Some materials using material nodes would not be exported correctly, you have to manually configure those materials in Blender (disable material.use_nodes and assign base texture to material's 1st texture slot) or in PMDE/PMXE.

By the way, non-mesh objects (curve objects, particle hairs, etc) are not supported, convert those to mesh objects manually if possible.

Hogarth-MMD commented 6 years ago

Download of Sintel Lite at blendswap:

https://www.blendswap.com/blends/view/7093

Hogarth-MMD commented 6 years ago

Of course I enabled Copy Textures. CopyTextures does not work.

powroupi commented 6 years ago

Make sure the textures are unpacked to a proper location.

When exporting, you may enable Create a log file (set Log Level to 4.DEBUG) and check the log file to see if there is any texture related error messages.

Hogarth-MMD commented 6 years ago

Thank you @powroupi . I solved this problem by saving a log file. The log file told me in exactly which folder mmd_tools was searching for textures. This folder did not exist on my computer. So I created this folder and then I copied all of the texture images of Sintel into it. I think that this problem happened because I packed the image files into the .blend file and then moved the original images away from their previous location (or something like that).

Hogarth-MMD commented 6 years ago

So I now I have Sintel Lite.pmx in MMD, which is a very nice accomplishment. This is not a flawless export, though. I have not yet renamed bones and morphs or edited the rig to be compatible with VMD files. The bones are still Rotation-Move when they should be Rotation. Sintel has a weird issue of not casting a shadow in MMD which I don't understand. Also when her thigh bone is rotated her pants are separated from her torso. In Blender this was prevented by a mesh deform modifier, but of course MMD has no mesh deform modifier.

Hogarth-MMD commented 6 years ago

@powroupi - how can I fix this rotation-move bone issue? Of course this needs a bug fix from you. In the meantime - what? What python script do I need to fix this problem?

Hogarth-MMD commented 6 years ago

Bones which have no parent bone are always rotation-move bones.
Bones which are connected to a parent bone are always rotation bones. Bones which are disconnected from a parent bone, and which have no vertex group, are rotation-move bones.
Bones which are disconnected from a parent bone, but which have a vertex group, are rotation bones.

So we lock location on all rotation bones?

powroupi commented 6 years ago

how can I fix this rotation-move bone issue? Of course this needs a bug fix from you. In the meantime - what? What python script do I need to fix this problem?

You may try follwing script (function have_vertex_group() is not implemented yet), maybe I'll implement this conversion to Convert Model button. :)

import bpy

def have_vertex_group(pose_bone):
    # add code to check vertex_groups
    return False

for pose_bone in bpy.context.active_object.pose.bones:
    if pose_bone.parent is None:
        continue
    if not pose_bone.bone.use_connect and not have_vertex_group(pose_bone):
        continue
    # pose_bone is rotation bone
    pose_bone.lock_location = [True, True, True]

As far as I know, in MMD, the bone still can move via key frames even if the bone is set to be not movable, it is only functional on user interface. :expressionless:

Hogarth-MMD commented 6 years ago

https://github.com/JanOtt/ShapeKey-Helpers

Shapekey-Helpers add-on has python code to apply a modifier which normally cannot be applied to an object which has shape keys.

Hogarth-MMD commented 6 years ago

FYI, In the Sintel armature and other rigify armatures:

DEF- means deform bone MCH- means mechanism bone ORG- means original bone

The DEF- bones have vertex groups. The ORG- bones are the "glue" which connects the DEF- bones to the other bones.

Hogarth-MMD commented 6 years ago

sintel

Hogarth-MMD commented 6 years ago

I have discovered that the correct posing and animating of Sintel is EXTREMELY dependent on her mesh deform modifiers and on the mesh object which is the target of these mesh deform modifiers. In the image above, I have changed only one thing in the Sintel model. I have deleted the mesh object which is the target of her mesh deform modifiers (before trying to give her a deep knee bend pose). If we cannot solve this problem, we cannot export Sintel to MMD. If we can solve this problem, then I don't see any reason why we won't succeed in exporting Sintel to MMD. In other aspects we have been making excellent progress.

Hogarth-MMD commented 6 years ago

https://sta.sh/0mzvv3fw41n Download link for Sintel Lite MMD !!!

Her knee bending seems to be a little off, and I deleted part of her skin that wasn't moving when she danced in MMD. Other than that, it looks like a successful conversion. My method of conversion isn't very user friendly yet, though. I used Blender's data transfer modifier to correct the mesh deform issue.

Hogarth-MMD commented 6 years ago

sintel in mmd

Hogarth-MMD commented 6 years ago

Thankyou @powroupi , for your excellent, recent addition of the Convert Model button to mmd_tools, and the Visible Meshes Only export option. These are important and helpful new capabilities of mmd_tools. :smile:

Hogarth-MMD commented 6 years ago

In the meshes of Sintel Lite, there is a mesh which has her head and skin. Unlike the other meshes of Sintel Lite, this mesh has BOTH a mesh deform modifier and an armature modifier. In the armature modifier, a vertex group called mesh_deform is assigned and an option called multi-modifier is enabled. If even one of these modifiers or modifier options is tampered with, the rigging of Sintel becomes a total mess. All of these modifier options are incompatible with MMD and mmd_tools. Does anyone have any idea how I can solve this issue? The crazy riggers of Sintel are making me crazy and I am in danger of going to an insane asylum in this situation, and maybe I need a little sympathy, in addition to psychiatric help.

Hogarth-MMD commented 6 years ago

The mesh deform modifier is not moving Sintel's head. The armature modifier is not moving Sintel's head. But the mesh deform modifier and armature modifier both combined are moving Sintel's head. Can anyone explain to me the logic of this? This is confusing me.

Hogarth-MMD commented 6 years ago

Hello @powroupi! I made a python script to mass add display panel groups, bones names, and shape key names to the MMD display panel. The works fine except that the shape key names are not being exported by mmd_tools. Why is this happening and how do I fix it? The shape key names appear in the mmd_tools display panel in the correct display panel group, but each shape key name has an error icon beside it and these shape key names are not exported by mmd_tools. (The names of the Basis shape key and sdef shape keys are not added by my script to the display panel. This is not the cause of this issue.)

powroupi commented 6 years ago

You may reference the following code to add items to display panel (maybe you forgot to assign morph_type):

item = mmd_root.display_item_frames[u'表情'].items.add()
item.morph_type = 'vertex_morphs'
item.name = 'shape key name'
Hogarth-MMD commented 6 years ago

Thanks @powroupi , but this bug fix was not successful. There must be another problem happening.

Hogarth-MMD commented 6 years ago
item = mmd_root.display_item_frames[u'表情'].items.add()
item.type = "MORPH"
item.morph_type = 'vertex_morphs'
item.name = 'shape key name'

This was successful. Thank you @powroupi for sending this clue to me. :smile:

Hogarth-MMD commented 6 years ago

The fundamental problem with converting Sintel is that the mesh objects do not have correct vertex weights. Their rigging in Blender is depending on mesh deform modifiers. This makes my task very challenging. I am making excellent progress with every aspect of converting Sintel to MMD, but this one issue is preventing success. Otherwise, I see no insurmountable problems. I have made a python script to mass add display panel groups, a python script to automatically add leg and foot IK to an MMD model. If anyone is anxious to have these, or if anyone wants me to upload my WIP Sintel conversion script, let me know.

Hogarth-MMD commented 6 years ago

Hello @powroupi ! Do you have the .blend and other files from the Sintel DVDs? I tried to export the Sintel adult dragon with mmd_tools. This adult dragon has 9 materials, but only 3 materials were exported. This exported dragon has an invisible body. This is a mysterious error. Can you please explain and correct this error?