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 277 forks source link

A question and an idea #43

Closed nathanvasil closed 7 years ago

nathanvasil commented 7 years ago

So this isn't an issue, so I'm not sure if there's some better way to express it. Please let me know if it's inappropriate to create an issue for this.

First of all, I'm wondering if there's a way to separate meshes into new MMD materials using mmd_tools. This is now the most important function that I need to use PMXE for.

The main reason I dislike unnecessary import/export cycles is because I lose quad structure. But...

I also have an idea. I don't know if it excites anybody, but in case it does: I've recently discovered I can use the way mmd_tools rips UV seamed vertices to restore these quads, just by marking everything as a seam and UV unwrapping. That way, when I re-import, there's only one possible quad for every original quad, and I can use tris-to-quads with 180 degree tolerances and get my original quad structure back, then remove doubles to restore mesh continuity.

But there are a number of reasons why this isn't ideal. Yet it could be managed automatically with import and export scripts. With an export option to rip quad edges on export, existing UV mapping could be maintained and there would be no need to sit through UV unwrapping, which takes far longer than an edge rip does when everything is a seam. With an import option to automatically restore quads when there was only one possible quad to generate, and possibly even merge the vertices of adjacent quads, this would be even faster and easier.

Of course, since this would quadruple your vertex count in MMD, it would need to be optional, and disabled by default. However I think it could be handy.

powroupi commented 7 years ago

Thank you for reporting this issue, and feel free to open issues when you have any questions, ideas, or feature requests. :smile:

Ideally, we may only need an import option (or a tool button / operator) for that. :) I've been thinking about similar feature for a long time, but it is difficult to me to implement it, sorry. :cry: Maybe other developers could help this. :blush:

nathanvasil commented 7 years ago

How would you do it just on import? I was thinking you might do it also by looking at normals and using those to recreate quads, but it struck me as complicated. There must be a reason why nobody's bothered adding that to the Tri-to-Quads operator already.

But still curious-- can I create a new MMD material in an imported MMD model?

One of these days, I might have to learn some Python. I heard it's a lot like Lua, and I liked Lua a lot. Just hard to justify it with how learning modelling and HLSL are such endless time sinks.

powroupi commented 7 years ago

How would you do it just on import?

I don't know, i always use tris-to-quads (Alt+j) manually to restore quad structure of an imported model when need. Your idea is good, i'll try it when i have more time. :)

can I create a new MMD material in an imported MMD model?

Sorry, i'm not sure if i understand it. To create a new MMD material:

One of these days, I might have to learn some Python.

Learning some Python / commands are useful in blender, you can write your own simple scripts in Text Editor, or type commands in Python Console to ease your work. :smile: https://docs.python.org/3/ https://www.blender.org/api/blender_python_api_current/

lordscales91 commented 7 years ago

I'm not sure what is the point of your idea. The PMX format only accepts triangle faces, so doesn't matter whether you have quads or not because every face should be split into triangles for export. When importing, if you want to restore the original quads it should be enough to just hit Alt+j to join the triangles. What do you want to accomplish that the tris-to-quads option can't?

nathanvasil commented 7 years ago

@powroupi Thanks, I didn't realize I needed to assign to the faces. There's a lot of basic Blender stuff that I don't know.

@lordscales91 Alt-J won't restore your original structure following import/export, not without a lot of manual tweaking, not on any reasonably complicated mesh. Automated triangulation + quadrangulation can leave you with normals and edges that look very unlike your original mesh and is unlikely to give you useful edge loops. Try making a torus and doing just two or three good, dramatic transformations, including a good twist, then triangulating and quadrangulating, and see what it does to your edges.

lordscales91 commented 7 years ago

@nathanvasil I haven't tested personally whether the original quads are lost or not, but I will trust your words 😉 . Now, how do you pretend to restore them? The PMX spec doesn't allow storing that information. You said something about using the UV coodinates. But, as far as I know, those values are just used to position the texture over the mesh. Can you explain it with an example?

nathanvasil commented 7 years ago

@lordscales91 Check out http://www.mediafire.com/file/f0nnqd8jc7g7knc/quaddemo1.blend . It contains four deformed toruses, all with the same vertex locations. The toruses in the rear are the original, unexported toruses. The toruses in the front have been exported to .pmx and then imported back in the file to get export triangulation. The toruses on the right have a a standard UV mapping. The toruses on the left have had every quad edge marked as a seam and were then uwrapped.

Try alt-J tris-to-quads on both front toruses. Because the front right torus has been deformed and then triangulated, you can't get the original quad structure back. The front left torus, however, has had all of its edges ripped while in its original quad form. If you alt-J it, and set the max angles to 180 degrees in the operator panel, you restore the original quad form. (To get the fully original torus, you will also have to remove doubles and discard custom normal data.)

The reason that this works is that by ripping every single quad edge of the original torus, there is only one possible way for each pair of triangles to be quadrangulated.

This comes at the cost of UV mapping (and unwrapping a complicated mesh where every edge is a seam takes a long time, at least on my computer). But by ripping all edges on export, this could be done without sacrificing UV mapping. Quadrangulation could then be done automatically on import. (If anyone felt like it, doubles could be merged automatically, if and only if they had identical position, material, edge, UV, and custom normals, which would completely automate the process.)

There are a lot of ways in which quad data could be stored and restored. The method I'm describing is far from elegant. But I suspect that it would be relatively easy to implement.

lordscales91 commented 7 years ago

I've observed the issue and how your workaround fixed it. But I'm not sure how to replicate it programmatically. Can you give me the intermediate PMX files as well? That way I can compare their data structures and spot the differences

powroupi commented 7 years ago

@nathanvasil I found another way to rip quad edges by using Edge Split modifier. This way, existing UV mapping could be maintained. :smile:

  1. Store custom normals
    • sadly, this might need to use python scripting :cry:
  2. Mark all edges as sharp
    • Go to edit mode, Ctrl+a select all, Ctrl+e choose Mark Sharp
  3. Add Edge Split modifier
    • Go to [Properties] window -> [Modifiers] tab -> Add Modifier Edge Split (in Generate column) -> only check Sharp Edges option (uncheck Edge Angle)
  4. Export MMD model
lordscales91 commented 7 years ago

@powroupi Yeah! I've checked it and works fine. We could automate that process during export as an option. It should be easy, we just need to mark the edges with the corresponding operator bpy.ops.mesh.mark_sharp() and add the edge split modifier. But I'm not sure whether that modifier may look weird in MMD or not. I will test it.

Update: UV vs Split Edge

The one on the left uses the UV workaround, the one on the right uses sharp edges + edge split. The color is not the same because I didn't copied the same values, but the problem is that the one on the right looks very blocky (because all faces are separated), so the sharp edges is not an option

powroupi commented 7 years ago

@lordscales91 You forgot the 1st step, but it is fine... I found the 3rd way: :blush:

  1. Store custom normals
    • Duplicate your mesh (say new mesh called obj.001, original called obj), then enable Auto Smooth option of obj, and clear parent of obj.001 (detach obj.001 from the MMD model, we want to use it to apply custom normals)
  2. Add Edge Split modifier to obj
    • Add Modifier Edge Split (in Generate column) -> only check Edge Angle option (uncheck Sharp Edges) -> set Split Angle to 0 (this way, we don't need to mark all edges as sharp)
  3. Add Data Transfer modifier to obj (in Modify column)
    • set Source Object to obj.001
    • enable Face Corner Data -> change method to Typology -> select Custom Normals
  4. Export MMD model
nathanvasil commented 7 years ago

@lordscales91 Doesn't sound like you need it anymore, but the pmx is at http://www.mediafire.com/file/gt2mcdqpugjhjib/quaddemo.pmx . (It's just the rear two tori exported to pmx.)

@powroupi Thanks for the info regarding edge split, that definitely improves the process.

lordscales91 commented 7 years ago

@powroupi Yeah, I forgot the custom normals step. So, the sharp look was caused by that. There are a lot of things about 3D modelling that I don't know.

@nathanvasil The idea of keeping the quad data looks interesting, but I've been thinking about it and.... I don't really see the point...

If you want to create a model for MMD, as long as the model looks as expected it shouldn't matter how the model is processed. Will make any difference using some workaround to keep the original quad data?

Besides, why do you want to restore the original quads? I mean, as long as you keep the original blend file it shouldn't be necessary to import the model back. Finally, unless you come up with some "magic algorithm" to properly quadrangulate any imported model, those workarounds are not really useful. Because you can't expect other people to create their models using some specific workaround. Furthermore, if someone really wants to make easier the modification of the model (very rare in the MMD Community) he/she can just give out the original file (blend, mqo, 3ds, etc...).

nathanvasil commented 7 years ago

@lordscales91

I totally understand if you're not interested in pursuing this further-- it's your time after all! And with powroupi's information regarding the edge split modifier, this is relatively easy to do manually, as needed, instead of automatically. Still, I'll try to answer your questions as to why I want to preserve quad structure.

First, as regards import/export cycles: there are some things that PMXE still does better than Blender for MMD models. The obvious things are physics and bones, where mmd_tools does its best to emulate MMD behavior in Blender, but doesn't exactly match it. Additionally, PMXE gives a much better approximation of MMD style renders than Blender does, which can be important for things like fiddling with material settings. There are work-arounds for this-- you can transcribe material settings once you settle on them, you can import a Blender mesh into a PMXE file containing only bones and physics-- but they're fiddly.

Some other things that are possible in Blender aren't always obvious. Like many people involved in MMD, I understand PMXE a lot better than I do Blender. So even when it's not quite necessary to run a model through an import/export, I might not be aware of that fact, or even if I am, the PMXE tools might be more comfortable for me than the Blender tools.

Now, as to why I want to preserve quad structure. Obviously, the vertices are at the same location, regardless of quads. But vertex normals aren't-- for a non-planar surface, the exact direction that the vertex normals point differs, depending on if there's an edge triangulating that surface, or no edge. For most of my purposes, the normal provided by the quad surface looks smoother than the normal provided by the triangulated surface. However, this depends on the exact toplogy and thing being modelled. Triangulated cylinders are bad, but there's nothing wrong with a triangulated icosphere.

This is borne out by my own reasoning and by looking at models. There's plenty of room for me to have gotten something wrong, and if you have good evidence that I'm mistaken, I'd love to hear about it.

There's also the ease of use of a model with good quad structure. There are a lot of benefits to, for example, being able to make decisions about subdivision late in a model's timeline, rather than earlier. Subdivision isn't the only thing made cleaner and easier by good quad structure, it's just an example.

I use files from a lot of different places, including .blend, .MQO, .3DS ... I don't rely on just MMD community models as resources, not when there's so much great stuff out there, released under permissive terms.

lordscales91 commented 7 years ago

@nathanvasil

Thank you for your elaborated answer.

Firstly, I'm aware of the limitations in blender mmd tools. It just happens that I hate PMXE 😃. So, if I make a model for MMD in the future I will definitely stick to my loved mmd tools, except perhaps for minor edits such as playing around with material settings which probably is better suited for PMXE because of the shader as you said. By the way I have noticed that PMXE has an option to export part settings as a CSV file, we could implement an option to import those settings, this includes materials, bones and physics. So, maybe it's better to implement an option for that, it's just reading CSV after all so it should be easy to implement. This should avoid the process of import/export cycles in most cases.

Secondly, I understand that if you feel more comfortable working with PMXE you will use it even for some things that are possible in Blender. So, instead of trying to offer an alternative to PMXE our aim should be to provide a toolset to work cooperatively with PMXE, exploiting the best of both pieces of software.

Finally about quads and normals, despite being a user of MMD for years I haven't looked so carefully to any MMD model. On top of that I don't know very much about 3D modelling. So, probably you are right with your reasoning, or at least you understand it better than me.

Now I will start working on CSV import option that I mentioned.

Have a nice day (^_^)/

nathanvasil commented 7 years ago

I'm going to close this.

I think if somebody had a beautiful, elegant way to preserve quads, it would be worth implementing. But I believe that manual use of the edge split modifier is a reasonable enough way to preserve quads. The solution is too clumsy (4x vertex count) to justify any coding work when it's so easy to do the same thing manually.