Saturday, 21 March 2015

Blender: Screwing Up


Here's something fun you can say after completing this tutorial: "I was making something in Blender and screwed up!" and you won't have to feel embarrassed about it.

Why? Because today we revisit the Blender screw modifier as I show you another handy use for it the takes advantage of a couple of its settings that we skipped over yesterday. This is towards the upper end of the "beginner scale" and will take longer to compete than my previous tutorials because I also show how to UV unwrap the object.

If you're more on the novice end of the scale and find the UV-mapping part just too hard or intimidating you can still use this technique. Just do everything up to step 22, skip step 23, then do step 24. Stop there and export. If you put seamless textures on it they'll probably still look reasonably decent if it's a fairly bland texture (plain paint, concrete, or just a blank shiny surface). There will be some parts that are very out-of-whack in terms of scaling or orientation, but many people won't notice that unless they look closely. It's still worth coming back to later when you have a bit more confidence to try the mapping part again.

Let's dive in.
  1. Fire up Blender.
     
  2. Add a mesh plane (Add > Mesh > Plane)
     
  3. Switch to edit mode (tab) and select all (A)
     
  4. We're going to be using the screw modifier on the z-axis again and this time we want our plane alignment to be on the y-z axes (you'll see why in a moment) so rotate the entire plane 90 degrees on the y-axis (R Y 90). Then switch to side ortho view (numpad5 for ortho, numpad3 for side).
     
  5. In an earlier tutorial we made a spiral staircase using a simple cube, an array modifier, and an empty. This time we're going to make a spiral ramp using only the screw modifier. Our view right now is a cross-section of our ramp and let's say we want the ramp floor to be 4m wide and 0.2m thick; so let's scale our plane to those dimensions with two quick hotkey sequences. S Y 2 scales the plane on the y-axis by 2, doubling its width from the default 2m to our desired 4m; and S Z 0.1 will scale it to 10% of it's original default height of 2m down to our desired 0.2m. (Hotkey "S" is scale)
     

  6. Avatars often have a bit of trouble negotiating ramps and stairs, particularly if they've been out having a few adult beverages, so let's also add some side rails that are 0.2m thick and 1m high. For a little extra visual appeal let's also have them extend slightly below the ramp. This calls for an additional plan cross-section on either side. We could model the rails as separate objects with their own modifiers but for this simple ramp let's keep it all as a single object. We'll duplicate our plane with the hotkey "shift+D" which immediately selects that copy and activates "grab" so we can use the combined hotkey sequence shift+D Y 4 to move it over adjacent to our existing piece.
     
  7. We want it only 0.2m wide and it's currently 4m wide so select the right-hand edge and move it to the left (that's the negative y direction) by 3.8m (G Y -3.8).
     
  8. We want it to extend upwards by 1m above the ramp's upper surface so select the top edge and move it up 1m (G Z 1.0).
     
  9. And we want it a little bit below the bottom edge of the ramp so let's select the bottom edge and move it down by 0.05m (G Z -0.05)
     

  10. We need a second identical railing on the other side so let's duplicate this one using the same quick method we used a moment ago. A quick bit of mental math says the that this means we'll need to move our copy by 4.2m in the negative y direction. Switch to face select and select the face for our railing, then our hotkey sequence this time will be: shift+D Y -4.2.
     

  11.  Now we have the desired cross-section of our ramp with handrails. We can make it considerably fancier in shape but for a beginner exercise this is just fine. Now all we need to do is model the entire thing in a circular, upwards slope. Yikes! Fear not, it's easy. But we want to do one more thing first...set up our two materials: one of the ramp itself, and a separate one for the rails.

    As I mentioned yesterday, I use Cycles Render for reasons that are beyond the scope of a beginner's tutorial. Let's just say that Blender Render is a bit like ODE and Cycles Render is a bit like BulletSim -- the latter is under much more active development and has a more powerful set of tool available under the hood (particularly in Blender 2.70 and higher). I'd recommend   that it's not a bad idea getting used to it or even making it your default, go-to engine as I do.

    For this tutorial we'll set up our surfaces in Cycles so if you're in the default Blender Render mode use the drop-down box in the top menu bar to switch to Cycles Render.
     
  12. Select the materials properties tab which is where we add and adjust materials (in both engines). Click the "New" button to add a new material and call it "ramp." Since this is the first material assigned to the object it will be applied to all faces.
     
  13. In the settings section at the bottom, pick a viewport colour you like (I'll use a pale green) and set the specular colour to black. We can leave all of the (many!) other settings at their defaults because we're not actually going to be using this texture in-world. This is just for our easy viewing convenience in Blender. The reason we're doing this at all is to allow us to assign two different materials to our single mesh object. These materials become "faces" in world, letting us put two different textures on a single mesh. You can have up to eight materials assigned to a mesh in SL/Opensim.
    As you become more expert in Blender you can use the many powerful render engine tools to create textures for export, as well as generating ("baking") normal maps and specular maps that you can apply in your viewer's advanced materials editor.
     
  14. Now we need a second material which we'll use for our side rails so click the small plus sign beside this object's materials list, then click the "new" button that just appeared in this area. We'll call this material "railing" and let's use a different viewport colour so it's easy to spot which faces belong to which material. I picked a salmon colour.
     
  15. Nothing in the viewport changed because we haven't yet assigned this second material to any faces. If you aren't already in face select mode, switch to it now, then select your two railing faces. With the "railing" material highlighted in the object's materials list, click the "Assign" button to indicate that your selected faces should use this material instead.
     
  16. As a side note, even if I'm pretty sure I'll be using the same texture for both the ramp and the railings, I'd still assign different materials to them just in case I later have a different use for the same object. Also, I would then be able to set the railings to 100% transparent and just use this as a ramp that still has the physics effect of preventing an avatar from accidentally walking over the edge. This is also why I'm not going to delete the exterior sides of the ramp floor (because they'll become visible if I make the railings transparent). Even if I have a specific use I'm building something for, it doesn't hurt to think about other potential future uses and make it suitable for those, too, if it's convenient.
     
  17. Now we're almost ready for our screw modifier magic which will be rotating this entire object around the z axis; but it's currently centered on that axis which isn't normally the way spiral ramps are constructed, so we need to shift the whole plane away from the z axis.

    Select the entire mesh (A) then move it 4m in the positive y direction (G Y 4). That's a good starting point and we can always move it around some more later to get the offset spacing we like. 
  18. Now we can add our modifier (which we can do while still in edit mode). Switch to the modifier properties panel and use the "Add Modifier" drop-down box to select the "Screw" modifier. Your cross-section will instantly become a circular platform with handrails (worth keeping in mind in case you ever need one in future) but that's not what we're after this time...we want an ascending spiral ramp.
     
  19. Now it's modifier magic time...yesterday I was a little elusive about the meaning of the various settings. Today let's look at (play with!) them a bit. Here's what each one means:
    • Axis: a drop-down box giving you the choice of which axis the screw rotation will use. We want to use the default zx-axis for our ramp.
    • AxisO: instead of using the x-y-z axes, we could assign another scene object as our axis of rotation instead. We won't be doing that in this tutorial but clicking the little box here would bring up a list of other scene objects available to rotate around if you wanted to.
    • Iterations: even though its location in modifiers properties box make this looks like it ought to come later in the list of options, this is a key value because all of the other settings prior to it are per iteration. Iterations is "how many times are we doing to do this. For now leave it set to 1 but we'll come back to it in a moment.
    • Screw: This value is how many meters to "screw" along our axis of rotation with each successive iteration. A somewhat unrealistic (in RL terms) but very common value to use for a ramp would be 5m, meaning that in one iteration of our modifier, the top of the ramp will be 5m above the bottom. Set it to 5.0 now and you'll see what I mean. Suddenly that circular platform is magically a spiral ramp!
    • Angle: This is how many degrees to rotate through per iteration. If we set this to 180 degrees instead of the default 360, our ramp would only do a half-turn while still ascending the 5m. If we change the iteration value to 2 it would do this twice, giving us the full 360 degree rotation; but now because we have 2 iterations we'd be rising 10m instead of 5. If you set the angle to 720 degrees and 1 iteration, it would spiral around in a circle twice while rising 5m. For now let's just leave it at 360 degrees.
    • Steps: We saw the meaning of this one yesterday: it determines how many individual steps (or pieces) to use to make the screwed shape. This isn't "steps" in the sense of stairs...we'll still have a smooth ramp. This is more like deciding how many vertices to use when adding a cylinder mesh object. The more steps we use, the smoother everything will look, but each step will require a full set of vertices (in the case our our mesh, 12 vertices per step) so we want to keep this to a sane value. For our purposes 32 should make it look pretty good and would still only generate a total of 396 vertices in the final object. If you want it to look really smooth you could increase this further. (Note: our plane is "flat" by default so it's going to look very angular at the moment...you can switch quickly to object mode, then in the toolshelf's Tools tab click the "Smooth" shading button, then return to edit mode. This will give you a better idea of what it will look like when we're finished.)
    • Render Steps: ignore this completely because we aren't going to do any rendering.
    • Calc Order: to be perfectly honest I haven't the faintest idea what this is supposed to do (Blender's documentation says it may be needed for mesh to calculate normals but I've never found it at all useful) so I leave it un-checked. Perhaps it's buggy or maybe I simply haven't used it on an object where enabling it has a beneficial effect.
    • Flip: this determined whether the normals should be flipped (to invert which side of the mesh the face is on: inside our outside) and for our ramp we need to check this so our outside faces are the ones we'll see in world. If you were making a spiral ascending tunnel that you walk inside, you'd leave it unchecked.
    • Stretch U/V: These two related to objects that we've previously UV-mapped but in our case we haven't so we can leave them unchecked.
    Here's what mine currently looks like
     
  20. This is a really good time to play a bit with some of these values just to get a feel for what they do. In particular make sure you see what happens if you start adding iterations (extra spirals!) or change the screw value (changes the slope of the ramp). Also you might want to change the mesh's position by sliding it back and forth along the y-axis (G Y then move your mouse) to watch how this changes the "hole" down the middle of your spiral. Once you have something you like, move on to the next step.
     
  21. You may have noticed that the two ends of our spiral ramp don't have any faces so once we have the final shape we want we need to apply the modifier to have the mesh created for us, then we can edit it to add the cap faces. You have to switch to Object mode to apply a modifier so do that, so tab to switch to object mode, then click the screw modifier's "apply" button, then tab again to switch back into edit mode.
     
  22. We can very easily fill our end caps by selecting the entire mesh if it isn't already (A) and then use menu Mesh > Edges > Select Boundary Loop which will now only select the edges that have an open side (the ends) and then press hotkey F ("fill"). Blender will automatically add the faces to close in the ends for you. (If you try this with a complex mesh Blender can "guess" wrong about how to fill in faces and you'll have to do it in smaller pieces, manually, but for something like this it won't have any trouble at all.)

    Unfortunately Blender isn't all that "smart" about the way it fills faces when you use this technique. Rather than looking at all the adjacent faces and realizing that they're one material and then assigning that same material to the new face, it simply assigns whatever your currently selected material is, no matter where it's going. You'll either have your railing texture on the ends of your ramp (like mine) or your ramp texture on the ends of the railings. We'll need to fix that in a moment, but first....
     
  23. We'll probably want to put a couple of nice textures on this ramp so let's take the time to UV-unwrap it properly as we work on it. There are a couple of ways we could do this depending on what textures we want to apply, and since I typically use seamless textures that tile nicely (much more flexible in world) I'll use the method best suited to that.

    Explaining the various methods and techniques used to employ them is well beyond the scope of a beginner's tutorial. UV-unwrapping is an art form that takes a lot of practice to master so I'll ask you to trust me that the method I use here will generally produce the best results for this type of object.

    Before we can unwrap, we need to mark some seams*, and we happen to have most of them selected right now since they'll be the ones selected after you did the "fill" of the previous step. On the toolshelf switch to the Shading/UVs tab and in the UVs section click the "Mark Seam" button. If you look closely after doing so, you'll notice that the edges of all your end caps are now red instead of black, indicating that the edge is a seam.

    * If you don't know what unwrapping is at all or you're not sure why we need to mark seams you'll want to refer to other beginner tutorials on UV-unwrapping I haven't written one (yet), but Chic talked about it in several of her tutorials and there are a number of very good basic ones you can find with a simple Google search. For a general idea of what this is for see the Blender manual.
     
  24. Now let's fix whichever end caps are the wrong material. In my case it's the two ramp end caps but for you it's possible that it will be the four railing end caps instead. Whichever it is...

    Switch to face selection mode. Select the faces that are wrong (holding down shift as you select the 2nd and any additional ones to add to the selection rather than replace it).

    Switch to the materials properties tab (since currently you'll still be on the modifiers tab) then select the correct material in the list of materials for the object. In my case it's the ramp material.

    Now click the "Assign" button to assign that material to the currently-selected faces.
     
  25. We still have a few more seams that we need to mark, as well as a few that we need to unmark, then we can unwrap. This will be easier for us to do in two parts, working first with the ramp to mark its seams and unwrap it, then working with the railings to do the same thing with them. Let's hide the ones we aren't working on.

    Select none (reminder: hotkey A toggles between all and none)
    . In the materials properties tab select the railing material in the list of materials assigned to this object, then click the "Select" button. Now all of your railing faces are selected. Press hotkey H to hide them all. Don't worry, they're still there. They're just hidden for now and out of our way, making it much easier to see what we're working on.
     
  26. The way I want to unwrap this ramp is into one, single long straight map. This will cause any textures that have a pattern to them (for instance a floor tile) to follow the curvature of the ramp which is very close to the way a ramp would be tiled in RL. It will cause the texture to stretch a little as it goes from the inside edge to the outside edge, but the difference in this case will be very small and unnoticeable. If it were more drastic, I'd avoid using a patterned texture or I'd choose a different unwrapping technique.

    To achieve this unwrapping I only need to mark, one more seam to cut along one of my long spiraling edges. Because of our side railings all of these seams will be hidden so it really doesn't matter which one I pick, but if I hide the railings at some point I'd like to have the seam be in the least visible place which is likely to be the bottom interior edge.

    To select this edge I switch to edge select mode, the hold down the ALT key, then click on one of those edges and it will select the entire spiraling loop of them.

    With that loop selected I click the "Mark Seam" button again on the toolshelf's Shading/UVs tab and it will be marked in red to indicate the seam.
     
  27. Right now my endcaps have seams marked on all edges which will cuase issues for the type of map I want. To prevent that from happening I need to remove three seams.

    Pan up on your screen until you can see the end cap at the top of the ramp. When the map gets to this point I don't want it to break off my end cap. Instead I want it to wrap back around the end so the top and bottom become one long chain. This means I need to remove the seams from the top and bottom edges of this upper end cap.

    I remove them by selecting the two edges (you must do this in edge select mode, not vertex select mode or it will select your side edges too) and then click the "Clear Seam" button that's right under the Mark Seam button. This removes the red line seam indicators.
     
  28. Now pan down to the bottom of the ramp so you can  easily see the bottom end cap. With this end we only want to remove the upper edge seam but leave the other three. Select that single edge, then click "Clear Seam".
     
  29. Now we're ready to unwrap so switch from Default view into Blender's UV Editing view. That's selected from the drop-down box on the top menu bar and will show you a split screen with your model on the right and UV map on the left. I switch my modeling view to ortho by making sure my mouse pointer is somewhere in the window on that side of the screen and press numpad5.
     
  30.  For the unwrap method we'll be using it's extremely important which face is selected first prior to selecting all. Make sure you do these ones precisely:
    • Select none (hotkey A)
    • Switch to face select mode
    • Select the very first face on the upper side of the ramp (not the end cap)
    • Now use the hotkey A twice. The first time will unselect the face, and the second time will select all, but the critical thing is that the "primary" selected face is that first one. You'll see it's a different shading in the highlighting.
    • Now on the toolshelf's Shading/UVs tab click the Unwrap drop-down box and select the option "Follow Active Quads" which will show a pop-up dialog asking for the Edge Length Mode to use: pick "Length" then click okay.
     
  31. If the unwrapping was done correctly you should see the UV map side of the screen filled with a very long map (you'll have to zoom out a lot to see it all) which may be oriented either vertically or horizontally (I haven't the faintest idea how Blender decides which orientation to use as it seems almost random). It doesn't matter which, since in world it will simply require rotating the texture to the correct orientation; or if you prefer you can select it all in the UV window and rotate the map here. Don't worry that it's extending far, far beyond the limits of the map area. We'll be using a seamless tiled texture so it doesn't matter other than to determine what scale we end up putting on it in world. I tend to leave it "as is" most often since scaling it down can introduce slight calculation errors in scaling that can distort face shapes.
     
  32.  That takes care of the ramp unless you're an obsessive perfectionist (raises guilty hand) and want to adjust the UV map to fix the width of the two end cap pieces which will be a little bit out of proportion to everything else. In most cases those will be hidden by floor, so you can skip it. Otherwise on one end you'll need to select the end cap outside edge and move it in to be about the same width as the side pieces. On the other end grab the "extra" full ramp face and move it in (which will narrow the end cap piece's face) until the cap is the right width.
     
  33. One final comment before we do the ra♦ilings: if you think you might want to use a specific floor texture for the top side of the ramp (perhaps a carpet or ceramic tile) that wouldn't be appropriate for the end caps or bottom, this would be the time to add that extra material (or two) and assign faces to it. This gives you the flexibility of being able to use a single texture on the entire thing if you want to (just place it on both in-world faces) or use different ones without having to do a separate upload. The same thing applies if you wanted to use different textures for the inner, top, bottom or outer railings. Map them all as a single surface, then assign faces to other newly defined materials at the end. This lets you keep your options open.
     
  34. Now let's move on to our long-hidden railings. Put your mouse somewhere in the main window on the model side of the screen. Now select none so no part of the ramp is selected. The hotkey combination ALT+H will unhide our hidden railings and when they un-hide they are also automatically selected for us. We can now use the hotkey combination shift + H which is "hide anything that isn't selected" so it hides our ramp, leaving only the railings. (All of these are also options under the menu Mesh > Show/Hide > but that's a lot slower than learning the hotkeys, and the hide/unhide actions are one you'll be doing all the time when modeling and UV-mapping so it's best to get into the hotkey habit as early as you can).
     
  35. Unwrapping the railings is going to involve almost the identical set of steps that we used when doing the ramp and we'll work on both pieces at the same time. The long spiral edge we'll use for each is the one that is on the bottom, closest to the ramp. Switch to edge select then ALT + click on an edge to pick the whole loop. You can add the other railing's edge to the selection by holding down the shift key as well as ALT + H (shift is always "add" when working with selections). With both selected, mark seams.
     

  36. We'll do the same with the upper end caps that we did with the ramp, clearing the seams marked for both of the longer edges and leaving the short edge seams. For the bottom end caps, clear the seam on the outer long edges but keep the other three.
     
  37. Now that the seams are correct, we are ready to unwrap but for technical reasons we can only unwrap one railing at a time using the "Follow Active Quads" method. Select one of the two "primary" outer faces at the bottom of the railing as marked on the picture above. We can't use the selection methods used for the ramp so we'll hold down the ctrl key and repeatedly press the numpad+ key to expand the selection until the entire railing on that side is selected all the way to the top. Then Unwrap > Follow Active Quads > Length. Repeat this for the other side, selecting first just the primary face, then expanding the selection to the entire railing, then the same unwrapping method. When you have done both sides, select all and your UV map should look like this (although yours might might be rotated 90 degrees):
     
  38. While the railing maps are nice and straight, as desired, and share the same face lengths that will correspond to the 1m height of the railings; the method results in both being identical lengths whereas the interior spiral will be shorter (often by quite a lot) than the other one. We could use math or generate a texture to precisely scale the length of the interior, but unless that kind of precision is needed we can make a good estimate of the ratio by switching to side view (numpad3) and comparing how many interior panels cover the (approximate) same distance as an exterior one. As you'll see in the following picture, this is visually approximately 3 interior panels for each exterior one so the rough ratio would be 1:3.
     
  39. Now we reduce the long-dimension scale of the interior railing's map by this (estimated) ratio which should make our railings' textures appear very close to correct in world -- good enough to look natural for the majority of textures.

    Select none, then select only the interior railing (you can do it the same way as when we selected it to unwrap it). This should make it the only one visible in the UV map side of the screen.
     
  40. With your mouse in that part of the screen, select all (if not already selected) and scale in whichever the long direction is. If your map is very long horizontally like mine, you'll scale it by 0.333 on the x axis using the hotkey sequence S X 0.3333. If yours if vertical it's in the y axis (UV maps only have x- and y-axes, and no z-axis) your sequence will be S Y 0.3333.
     
  41. Believe it or not, we're finally finished! You can switch back from UV Editing into Default view and the use ALT+H to unhide your hidden ramp.
     
  42. As a final comment...Opensim (and Second Life) made the lamentable choice only to support triangular faces and not quads. This means any well-mapped quad mesh (like ours now is) will be subjected to conversion to tri. This, in combination with the method the viewer uses to map textures to faces, frequently results in distortion of the UV map, particularly when the surface is curved like ours is. Thus a texture may still not display correctly in world even though it's perfect when viewed or rendered in Blender.

    In this case it's the ramp that is likely to experience distortion, so I would still avoid using a highly geometric pattern if possible. If not, I would add a number of loop cuts in the long direction (up the spiral) which will also help but, unfortunately, at the expense of a higher poly count. Since the ramp, as it is right now, is just under 400 vertices (and a 75KB collada file when exported), adding 5 loopcuts to just the ramp still results in fewer than 800 verts and a 125MB file -- still extremely small. If you don't have to do so, great. If you do, it's still not the end of the world.
Now you can save, export, and upload the file to Opensim which could look something like this once textured.