Sunday, 3 May 2015

Blender: Out of the Frying Pan and Into the Fire

This final installment of my novice set of tutorials covers the subject of getting your models out of Blender and into Opensim. This is what distinguishes us from the majority of Blender artists: we are only using Blender as a step in a creative process rather than using the software to produce our finished products.

There are four steps involved once you've finished modeling and you're ready to export your object:
  • Prepare the model for export
  • Do the actual export to Collada (.dae) format
  • Import the object into Opensim
  • Rez the object and get it to look the way you want it to in-world
For the purposes of this tutorial I will be assuming that you have UV-mapped the object you want to export and assigned at least one material to it.

Preparing a Model For Export

If you follow the recommendations I've made earlier in this series, the majority of your models will be made up almost entirely of quads which is (by far!) the optimum method when working in Blender. Opensim (and SL) only allow us to import objects made entirely of tris; so before we can export something we need to convert all of our quads (and perhaps a handful of n-gons) into triangles. We can do this quite easily in one of three ways and I'll show you all three methods.

Actually, there's a fourth method but nobody in their right mind would contemplate it for anything other than the most simple, basic objects. You could, if you were really patient (and insane?) go in and do it by hand, manually creating the diagonal edge to divide each quad in half.

With a very, very simple mesh model like the monkey Suzanne this would involve manually selecting about 500 faces, one by one, and for each of these you would need to decide which pair of diagonally-opposed vertices to connect to turn the face into a pair of triangles. Needless to say, this isn't something we'd ever want to contemplate doing. It is instructive, however, to think about that operation for a moment though, since that's exactly what we're going to be asking Blender to do for us.

For any quad there are two different ways to "cut" it into a pair of triangles, and unless the quad is perfectly flat and rectangular the choice of which of those two diagonals to use can be quite important. Let's spend a moment looking at that.

In an empty scene, add a simple mesh plane using the default values, then switch into edit mode. We'll do a side by side comparison so we want to duplicate it. With the entire object selected use the hotkey combination SHITF+D X 2.5 (Shift+D duplicates the selection and automatically actives the grab -- move -- action so we follow that up by telling it to move along the x-asix by 2.5m).

Now in vertex select mode on the left hand plane select the back left and front right corners then press the hotkey J to join the two vertices with an edge. For the right hand plane, select the front left and back right corners and J to join them as well.

We don't see any difference at all between our two identical "triangulated" planes because they're perfectly rectangular so let's change that a little bit. Select the two rear vertices of one of the planes which will select the rear edge (we're going to do the same thing with both so it doesn't matter which plane you do first) then use the following hotkey sequences:

  • S 0.75 (scale the edge to 75% of its previous size)
  • G X -0.1 (move it -0.1m on the x-axis)
  • R Y 10 (rotate the edge by 10 degrees on the y-axis)
Now select the two rear vertices of the other plane and repeat exactly the same sequences. Now switch back into Object mode (the picture below is still in edit mode) and look at the planes as you pan your view around them..

As a quad, these two planes are identical because the four corners are in identical locations; but the effect of "triangulating" it manually (cutting it into two triangles results by connecting one of the diagonal pairs of vertices) has quite a different visual effect. The quad on the left now looks like it bends a bit downwards whereas the one on the right looks like it bends a bit upwards.

In Object mode, on the Toolshelf's Tools tab, look for the two buttons in the Shading section called "Smooth" and "Flat." By default the plane is created with all-flat normals so click the "Smooth" button which will now use a built-in algorithm to try to make the surfaces appear smooth (see the Context III tutorial for if you don't remember what normals are). You'll probably find the two planes are almost indistinguishable although you might see a slight difference if you look extremely carefully. Now click "Flat" to return to having the normals calculated independently for the two triangular faces of each plane and the visible crease will reappear.

Now consider this: each quad face of a mesh has to be cut in half just like we've cut our plane in half so when we're asking Blender to do it for us automatically it has to figure out which of the two diagonally opposite corners to connect and there will be a distinct visual impact from Blender's decision, although this will be far more noticeable for portions of meshes where the faces are using flat normals and much less visible for portions that are smooth.

Now that we've seen there's a tangible difference in the way we triangulate a quad, let's see a more representative example of this effect in action. Thankfully, Blender gives us the mesh monkey Suzanne to play with was we look at things like this. Let's pretend she's our finished model that we want to export and upload to Opensim.

Either pick a different layer to add her to, or delete your simple double-plane object, then add a new mesh monkey. While you're still in Object mode, click the "Smooth" button to make Suzanne appear to have a fairly smooth surface. You'll still see some areas where she doesn't look very smooth but that's because she's a very low poly model for this sort of complex organic shape (only 500 faces). You could greatly improve her visually by modeling her at a higher resolution, but this significantly increases her poly count and therefore the file size for her mesh. Modelling for Opensim is a constant battle between improving visual quality versus keeping the file sizes small enough to transfer quickly to a visitor's viewer so they can see it.

Now switch into Edit mode (Tab) and you'll see that Suzanne is modeled almost entirely using quads, with only the central faces of her eyes being tris. To export her for use in Opensim all of those quads need to be tris, so we need to divide every single one of those faces into two triangles by connecting diagonal corners like we did with our plane. Now we can talk about the three viable methods for doing this operation since, I presume, you're not going to be interested in trying that fourth method of doing each and every one of them, one by one, manually.

The least preferred method for "triangulation" is the one we'll cover this first. While still in edit mode, ensure that the entire monkey is selected and then use the 3D View pane's menu to do Mesh > Faces > Triangulate Faces. (We could just have used the hotkey combination "Ctrl T" but since this isn't something you'll be doing all that often it's one that you probably won't remember so it's better you know where to find it in the menu.) This tells blender to automatically turn any faces that are currently selected and aren't already tris into tris. Any face that is already a tri will be left untouched. As soon as you do this you'll see that Suzanne's faces have all now been triangulated for you.

Before doing anything else, look at the Most Recent Actions area of your Toolshelf where you'll now see a pair of drop-downs containing the options for triangulating faces. One tells Blender the method to use for quads, and the other tells Blender what to do with polygons (the formal name for ngons which have 5 of more edges). By default, both will have used the "Beauty" method.

Suzanne doesn't have any ngons so the only one that actually has relevance to us is the quad method so now let's use the quad drop-down to select each of the four options, one by one to see what they do. Suzanne will be automatically updated to show the effect of the choice because until we do something else our most recent actions options are still modifying our "triangulate faces" action. Our choices are:
  • Beauty: this lets Blender try to figure out the best method for each face, separately, to try to produce the most visually pleasing result. I use this most of the time since whatever internal algorithm Blender uses to accomplish this is usually pretty good at it. The Blender documentation warns us that it's the slowest method to use, but since our models for Opensim need to be kept to a reasonable size, I've never noticed any appreciable delay in the operation. If you had a model with millions of faces, each of which had to be "calculated" for beauty, it would get slow -- but you'd also never be able to get it in-world.
  • Fixed: Blender's documentation says this connects the "1 and 3" corners of all faces, but it's a little more than that because it's also aware of symmetry on each axis. I only use this method in very rare cases for specific objects where I need each diagonal to be aligned with one another, usually as a convenience step prior to doing additional modeling work.
  • Fixed Alternate: is identical to Fixed except it connects the "2 and 4" corners so is the mirror of Fixed. Again, I only use this method on very, very rare occasions.
  • Shortest Diagonal: this tells Blender to measure the distance between the opposing corners of each face and create the edge based on whichever distance is shorter. I use this one somewhat frequently when I have a model that is extremely thin (usually as a result of "solidifying" it) since the beauty method can sometimes result in the newly created edge on one side intersect an edge from the other side -- something that can cause visual issues in-world and greatly increases the chances of problems calculating the mesh's physic on upload (see below). I also find that this method tends to produce better results for a highly geometric, round shape like a sphere or torus.
There's rarely any "best" method to can usually just go with whichever one looks the best on your screen. Most often that will be the default Beauty method.

I call this the least preferred method because you have to do this manually, one at a time, with each object in your scene; so if you're exporting a linked group of objects it will take some time to go and do this for each one. Also, you don't want to save your blend file like this because if you later want to come back and make changes to it you'd have tri faces instead of quads, making it much, much harder to work with.

The one significant advantage it does offer, though, is the ability to select only some of the faces of a mesh, then triangulate them using one method, then select other faces and triangulate them using a different method, and so on. If you need to do this for some reason, try to be sure that you also have an archived quad version of the file just in case you need to come back to it again later.

Once you've had a look at the result of all four methods, use the Ctrl+Z hotkey combination to undo the triangulation and return Suzanne to her quad-modeled state so we can look at a second method.

The second and usually "best" method is to use something called a "modifier" on our object. A modifier is something we assign to an object that will perform an action of some sort to the mesh based on its settings, but doesn't actually do the action until later, when we tell it to. This allows us to preserve our model exactly as it is, but set up something to "do later" that will affect it. I won't go into any more details about modifiers since that's a subject for a whole series of beginner-level tutorials. For the time being I'll just show you how to use one for what we want to do.

First, switch back to Object mode and ensure you've selected Suzanne, then on the Properties pane look for the tab with a small wrench symbol called the "Modifiers" tab. When you select that tab the rest of the Properties pane will be blank except for a drop-down box where we can "Add Modifier."

Click the drop-down box and you'll see an enormous list of the possible modifiers we can add to Suzanne and the one we want to use is called "Triangulate" which is near the bottom of the second column of options (the set of "Generate" modifiers).

Once you've selected this you'll see an entry appear in the Properties pane that tells you that you're adding a Triangulate modifier and then has a number of options associated with it. I won't explain the meaning of all the little icons across the top of the modifier's entry other than to mention that you'll only see the effect of the modifier on your screen when the eyeball icon is selected (which it will be by default). What we're interested in are the settings options in the main part of the modifier.

You'll see that the modifier settings has a pair of drop-down boxes that are identical to the pair of drop-downs we saw earlier when used the first method to triangulate Suzanne. Whichever one you select for quads, here, will result in the identical change to Suzanne's quads that you already saw earlier.

The difference is that until you click the button labeled "Apply" Suzanne's actual mesh isn't changing. We're just setting up a handy little button for us to click any time later to automatically triangulate her according to these settings. Clicking "Apply" will do exactly what it says: apply the modifier to the object and therefore change it to be all-tri faces. Don't do that yet, though, and if you did you can always Ctrl+Z to undo and restore the mesh and modifier.

When you save the file, Blender saves any modifiers along with everything else, so if you later want to come back to edit the object, you'll still have your nicely preserved quads and a handy modifier set up. If you have multiple objects in the scene, you'll need to give each one a triangulate modifier, but you can use different settings for each different object if you wish to.

You can add a modifier to an object at any time so you don't need to wait until you're ready to export before you do this. In fact, I typically add a triangulate modifier to each new object when I create it, just so I don't forget to do it later. Unless you "Apply" it, the model is unaffected and while you're working it lets you continually preview how the model will look after triangulation.

The only disadvantage of this approach is that modifiers apply uniformly to the entire object so you don't have the ability to use "Shortest Diagonal" for only some faces and "Beauty" for others. You can only use one method for all quads and, if desired, a different method for any ngons your model might have.

The third method is to triangulate the entire export at the time of export so let's move on to that step now.

Exporting Your Model(s)

Before doing anything else, save your (.blend) file so you'll have a copy of it to come back to.

Now select the object you want to export. If you want to export multiple objects at once, you can SHIFT+select them to add them. This will create a linkset when you import with each of your individual objects being one of the links in the set (and behaves exactly like linksets of prims and/or sculpties).

From the main menu at the top of Blender's screen select File > Export > Collada (Default) (.dae) which will bring you into a screen that is almost identical to the one you see when saving a .bllend file.

Supply a name for the file to export, making sure you keep the ".dae" at the end of it and I strongly recommend saving the dae to the same folder your .blend file is in.

Now look at the lower left side of the screen where you'll see a number of (very poorly documented) export options.

You want to be sure to check the option to "apply modifiers" and ensure that the "View" option is chosen from the drop-down. If you're using method #2 for triangulating your meshes this will automatically apply each of them first, which is what we want to have happen. The "View" part isn't relevant to the triangulate modifier but is for others and you'll always be using the view option.

You also want to check "selected only" to tell Blender that it's only the object you've currently selected that should be exported to the Collada file. The other three options here aren't ones you'll need to worry about until you're doing considerably more advanced work.

In the textures section make sure your "only selected UV map" is checked so maps are only exported for the objects we're including in the file. If you check the "Include UV textures" option a copy of your textures will be saved in the same folder and linked in the file. If you haven't made any textures for it or have already uploaded them and will apply then in-world, you can leave it unchecked. Usually (but strangely, not always) having it checked will then upload and apply your textures automatically when you import them in-world but I still haven't figured out what causes that to fail at times. See below for more about that.

You can ignore the armature section until such time as you're working with rigged mesh clothing or custom mesh avatars. For regular objects these options don't apply.

The third method of triangulating everything is by checking that option here in the export settings, and even if you're positive that everything you're exporting is already triangulated or has a triangulation modifier it's still not a bad idea to check this too. It won't alter any tri faces and just in case you forgot something it will catch any quads you might have missed. I am not certain whether this uses shortest distance or beauty as the method but I suspect it uses shortest distance. We're just making sure that all objects being exported are getting this last "safety check"  to ensure they're Opensim compatible meshes.

I have some doubts about whether the "sort by Object name" option matters at all. Supposedly if you have multiple objects selected the Collada file will have them sorted in alphabetical order which should then result in the first of those being the root object of a linkset. In practice this doesn't seem to be reliable so I always assume that the root will be assigned randomly and I might need to rez the set to ground, unlink the part I want as root, then relink it into a set with the desired root.

I wish I could offer more assurances about the above, but the documentation is sketchy (or non-existent) so all I can say with any certainty is that these are the settings I use and produce files I can reliably upload.

Import the Object(s) to Opensim

Now the we have our Collada (.dae) file we can close Blender and log into Opensim with your viewer of choice. As far as I know, all TPVs use the identical (or nearly identical) mesh import layout that Linden Labs developed for their own viewer. Yours might have a different skin or slightly different layout than mine but should have all of the same options on the same general layout. My screenshots are made using the current Firestorm 4.6.9 (42974) 64-bit with Opensim support although I believe there's a new version coming in the not-too-distant future but I don't expect this part of the viewer to change.

Depending on your viewer, accessing mesh upload will be a little different (in FS it's Avatar > Upload > Mesh Model, while in other viewers it might be under the build menu instead) and after selecting this you'll be asked which file to upload. Select the one you want to import and once it has finished loading your file into memory you'll be presented with the Upload Model window.

The Second Life documentation for upload is accurate for Opensim, too, so there isn't much point in me repeating the information here other than to add some annotations.

Level Of Detail Tab

At the top, the model name you supply doesn't need to be the same as the file name. Whatever name you use will be applied to all objects that are part of aan linkset you upload -- the original Blender object names are not preserved. The "This model represents..." dropdown is utterly meaningless (and ignored) for Opensim import so you don't even need to bother changing it.

The four LOD settings are something you should pay attention to but are beyond the scope of this tutorial so for the time being use the default values. I will details the issues involved in LOD in a future beginner-level tutorial.

Do not check the "Generate Normals" option since I find it highly unreliable and you should be making any and all adjustments to your normals in Blender, not in the limited interface for a program that isn't designed to make normal-calculations.

Physics Tab

Physics is another subject that requires a whole tutorial series of its own, and is a somewhat more advanced subject. As a beginner you can select one of the options in the drop-down box in step 1 of the tab. For anything you intend to wear, or will be made phantom in-world, or is relatively small and doesn't require much accuracy for collisions in-world, use the lowest setting. For larger objects that will need some measure of collision accuracy when interacting with other physical objects or avatars, you will need to use a higher level of detail. You'll see a preview of the "physics shape" in the preview window which can help you decide which to use but only treat this as a (very rough!) approximation.

For Opensim uploads everything else in this tab is ignored so you don't need to analyze or simplify because they have utterly no effect on your upload.

Upload Options Tab

Unless you start working with rigged mesh avatars or clothing, the only thing you need to do in the third tab is check the "Include textures" box. If for some reason you modeled at a different scale to the one you want to use for import you can scale it here (but it's usually a lot safer and more accurate to do your scaling in Blender).

In theory, checking the "include textures" box is supposed to automatically upload and apply any textures that the Collada file specifies for any material, provided the texture file is in the exact same directory as the Collada file. If it's in a subdirectory or different location, it won't. If you haven't assigned a texture to the material but have assigned it a colour (in Blender) it will usually import with that colour (but again, mysteriously, not always). I've talked to Opensim developers who assure me that their end of the code supports the data as long as the viewer sends it, so all I can assume is either a miscommunication between viewer and Opensim, or some mismatch in the Collada file formatting Blender uses to save vs the one the viewer expects to read.

The Incredibly Annoying and Unpredictable Calculation Step

While all viewers can experience this problem once in a while, Firestorm appear to be highly prone to getting "hung" when you press the "Calculate weights & fee" button. For more complex models I don't even bother trying the upload with Firestorm and will do it with Singularity or Kokua instead.

For personal reasons I prefer Firestorm as my in-world viewer for everything else so I'll often try it just in case it works, but it sure would be nice if they addressed this bug (/rant one that I reported to their Jira more than a year ago and, to date, remains  unresolved and has never had a response from a developer even though the bug itself has been confirmed by their staff. /end rant).

If this button doesn't change into a "Upload" button after a relatively short period of time you may need to cancel the upload and try again. With complex models or large linksets this calculation can take several minutes but for a simple model like Suzanne it should only take a second or two. /more rant If you have to cancel and try again, Firestorm will not release the processor that's being used for the calculation even though you've cancelled it. The only way to get that core back is to shut down -- which will often crash or take many minutes to stop the process -- and restart the viewer./end rant

Barring problems, you can click the "Upload" button and the viewer will repeat all of the lengthy calculations (why it doesn't cache the results, instead, is a complete mystery to me) and add the object to your inventory. If you've included textures they'll be uploaded to your inventory as well.

Rez and Finish the Object(s)

I'm going to assume that you all have sufficient familiarity with building in Opensim to know how to rez and texture objects. If you included the textures in your original dae and upload they should be pre-applied to your object when you rez it. On occasion this will fail for mysterious reasons and you'll have to upload and manually apply them instead.

As long as the object was UV-mapped, each material assigned to an object becomes a "SL face" that you can select and texture exactly the same way you do with a prim. If you didn't UV map the object for some reason you will need to cross your fingers and switch from the default mapping to planar mapping in your viewer's Texture tab and, if you're lucky, it might look okay. As often as not, though, it will look about as horrible as my picture of unmapped Suzanne does here.

Suzanne in-world (model was NOT UV mapped)
A single mesh object can be scaled in any direction just like a regular prim or sculptie. If you combined multiple objects in your .dae they'll import as a linkset -- with a seemingly random selection of root -- and will behave just like a regular linkset, including the ability to unlink it, etc...(each mesh object of the set is actually imported as a separate object and combined as a linkset when it's placed into your inventory).

What Now?

There is no substitute for practice and experience. Unless you spend some time working with Blender (or any complex program) on a regular basis you won't develop the necessary skills and familiarity to take that next step in your mesh-making career.

I have already posted some beginner level tutorials and will be adding more over the coming months to cover some subjects that may be of interest. There are also many excellent tutorials available, both text and video, just via some googling.

Start with simple, highly geometric shapes that would be easy to build using simple prims instead. Normally if you can build it with a reasonable number of simple prims, you should, but as a learning exercise that's a very good place to start. Don't build it exactly the same way, though. Learn Blender's set of basic modeling tools that allow you to construct the entire thing as a single object rather than as a large linkset of prims. That's one of the biggest advantages of mesh.

Once you're comfortable with things like loop cuts, extrusions, and basic modifiers, and basic UV mapping, start working on more organic, naturally-shaped objects which are quite a lot harder to model (and impossible to do with prims). Trying to do these sorts of objects before you've mastered the basics can be extremely frustrating since you'll rarely be able to achieve what you want -- or at least not in a reasonable amount of time.

Learn from your failed attempts. They're just as instructive as -- if not even more instructive than -- your successes. If something doesn't work, figure out why since this will often suggest a better approach to use for that type of object next time.

From a personal level, many moons ago I first learned a lot about prim-building by getting freebie copies of other people's objects and taking them apart to see how they were made. I did the same thing with Blender by downloading a number of the free .blend files for objects that interested me on the BlendSwap website (registration is free, most models are CC licensed, and you are restricted only by the total MB of downloads you can make in a 30 day period without a paid membership). I would then open these files to see how the models had been made, and to try to manually duplicate them myself (even though it's very tempting just to upload the other person's work). BlendSwap is only
one of many sites where you can find free models and Blender is able to import almost all common formats.

Ask questions. There are increasing numbers of people in Opensim who work with mesh and, for the most part, they're happy to share tips and explain techniques as long as you've at least made some attempt to learn the basics. It's to everyone's benefit in the community for there to be many more creators who can add mesh to their arsenal.

Mesh isn't better. It's just a tool. If you need a simple prim cube, rez a prim cube. Don't make it out of mesh just for the sake of being able to say that it's mesh. Things you make using mesh should usually either be shapes you can't achieve using prims, or are highly inefficient to make that way. (Side note to militant prim-only builders: mesh isn't worse, either...honestly! I'll take my "13 prim" single linkset Roman bath building any day over the many hundreds of prims it would take you to get somewhat close to the same appearance)

In time, you can make something like this.
There are no prims in this build and the building (including columns, murals, roof, etc
but not including small furnishings) is a 13-object mesh linkset totaling under 200k faces.
I could reduce this count significantly by going to a different style of column but this build
is intended for personal pleasure and visual opulence, not high volume (or impatient) traffic.

Avatar and clothing are mesh, as well. Hair is flexi-prim and sculptie because I suck at making mesh hair.

No comments:

Post a Comment