Home Technical Talk

Normalize UVs Maxscript

13
MoP
polycounter lvl 18
Offline / Send Message
MoP polycounter lvl 18
Normalize UVs maxScript

Let me know if you find any bugs.

I find it works best if you put it in your Unwrap UVW context quad menu.

As with all macroscript containers, put the .ms file into 3dsmax/scripts and use "run script" to run the script once, which will add the macroscript to your Customize UI menu - look in the "MoP's Tools" group for "Normalize UVs" and put it on a hotkey/quadmenu/toolbar.

Replies

  • romi
    Options
    Offline / Send Message
    hey Mop,

    i cant seem to download the script.

    question - do you put this script in ...\scripts\startup?

    thanks,
    Rom
    snowblindstudios.com
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Sorry, I typoed the link. Fixed now :)
    Put it in ..\scripts\ and run it once from Max, then assign it through the Customize menu.
  • Mark Dygert
    Options
    Offline / Send Message
    Works great MoP! Thanks!
    It kind of freaked me out when the UVW-Editor window disappeared and things started flashing but it made it back ok.
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Yeah I made it draw a progress bar for each element it finds, because on 10,000+ poly meshes it was getting quite slow, so if you've got a really lowpoly mesh with just a couple of elements it's gonna draw several progress bars really fast.
    Maybe I should make it check the number of polys and only start a progress bar if there's a lot of work to do, that'd probably be more friendly.
  • romi
    Options
    Offline / Send Message
    ditto, thanks Mop! i've been looking for a normalize UV script that actually works. i've been going to DeepUV before. now i can just stay in Max, thanks to your script!

    cheers,
    Rom
    snowblindstudios.com
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Sorry to you guys for being guinea pigs, but I just did another ninja update on the script - it now only shows progress bars for objects over 3000 polys (arbitrary value, if you dig around in the script you can change it or remove it yourself). Objects lower than that should be fast enough.

    I also squashed a bug where the script would error out if you ran it on an object with an Unwrap UVW modifier already applied, but you didn't have the modify panel open.

    So yeah, re-download and re-run it smile.gif
  • romi
    Options
    Offline / Send Message
    thanks Mop! i did ran into that bug smile.gif

    cheers,
    Rom
    snowblindstudios.com
  • Rob Galanakis
    Options
    Offline / Send Message
    [ QUOTE ]
    Known issues: It will not work if you have a topology-changing modifier in the stack below your Unwrap UVW, for example Symmetry, since it can't get corresponding poly selections.
    I'll try to update it with a graceful solution to this at some point.

    [/ QUOTE ]

    Have you tried using snapshotasmesh ? Use a snapshotted mesh for things you would need to calculate from the polymesh.
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Problem with that is, it results in triangles - unless there's a way of returning a snapshotAsMesh as an EPoly... I'm working out face areas as exact matches between UV and poly (ie. quad to quad etc), and using snapshotAsMesh means I end up with an entirely triangulated mesh. I can't seem to convert the mesh snapshot to an EPoly without actually making a physical copy of the mesh and collapsing it down, then deleting the mesh copy afterwards, which seems a little heavy-handed ... I guess I could do that if there's no better way though. Annoying that there's no "snapshotAsPoly" function tongue.gif

    Edit: Eh, I said screw it and just did the copying method. Doesn't seem much slower really, and it means it now works if you have stuff like Symmetry in the stack below your Unwrap UVW.

    Updated:
    http://www.greveson.co.uk/scripts/normalizeUvScale.ms

    TODO: Make it so that you can select just one or two elements and have it normalize those in relation to the unselected elements, or in relation to only the selection. This will probably require a pop-up dialog so you can choose "normalize all" or "normalize selection", then pick if you want to normalize the selection only in relation to itself, or in relation to all the UV elements.
  • Joshua Stubbles
    Options
    Offline / Send Message
    Joshua Stubbles polycounter lvl 19
    Is there a specific way to use that, MoP? It made my UV's disappear, and the model turned completely transparent - just a wireframe.
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Whoa! I never encountered that. What version of Max are you using, and what sort of object are you using the script on (modifier stack, base object etc). I've only tested it on Max 8 here.

    The script can be run from a hotkey, toolbar button, or quad menu shortcut - all of those methods should produce the same results.

    If you can send me a sample scene where this can be reproduced consistently (providing it's not NDA'ed or private), that'd be a great help.
  • Joshua Stubbles
    Options
    Offline / Send Message
    Joshua Stubbles polycounter lvl 19
    Ah max8 might be the issue then - I'm running 2008. Doh. I'll give it a go in 8 and see if I get the same result. If I do, I'll bundle the scene for ya.
    thx.
  • Joshua Stubbles
    Options
    Offline / Send Message
    Joshua Stubbles polycounter lvl 19
    Yeah, it works in max8, thanks smile.gif
    It REALLY doesn't like mirrored/welded UVs though. yeow.
    Good for fully unique unwraps laugh.gif
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    I've moved this to a new topic since it was taking up loads of space in a thread that should be just more like a list of plugins and requests... any feedback on this script should go in here now smile.gif

    Vassago: You're right, it does seem to go a bit crazy on welded & mirrored elements.
    Just to confirm, if you run it on UVs that are welded & mirrored, all the other UV elements get scaled really small, right?
    I notice it works fine if your mirrored sections are unwelded (which they should be most of the time, for normalmapped assets anyway?), problems only arise if a mirrored section is welded - I imagine it's a problem in the UV area calculation at that point. I'll check it out tomorrow night.

    I'll also give it a spin on Max2008 at work tomorrow and see if I can figure out what's breaking there. Hopefully it's something simple smile.gif
  • Joshua Stubbles
    Options
    Offline / Send Message
    Joshua Stubbles polycounter lvl 19
    For me, the mirrored UVs get jumbled to a single point in the top left of the unwrap window. If I delete the mirrored portion of the mesh and just do it to that first half then yes, everything gets scaled small.
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    OK. If you can email me the .max file, that'd help a lot for debugging. If it's private, no problem, I'll try to recreate the issue myself. Email address is in my polycount profile.
  • Joshua Stubbles
    Options
    Offline / Send Message
    Joshua Stubbles polycounter lvl 19
    Sure thing, I'll forward it off after dinner.
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Hmm, yeah, I see the problem that arises when using the script on your particular UV layout, Vassago.

    You have a lot of elements stacked on top of each other and then welded together, which means that for each of those elements, it's calculating the whole element's geometric area, but then only getting about half the UV area since it comes across the same UV coords more than once.

    Personally I would avoid doing UVs like that anyway (ie. stack them but don't weld)... but that's just me, and I should probably find a way to make the script deal with situations like this anyway.
    Might take a while though... we'll see.

    Oh, and I think I know why you were getting a transparent wireframe in Max2008 - were you using a similar file you sent to me? The material has an alpha map for opacity, and I think the script is just scaling your UVs so small that they end up completely covering a transparent area of the texture. If you applied a different material I bet you'd see the mesh is still all there and healthy smile.gif

    I'm also considering trying to make an EPoly version of UVPaint, sounds like an interesting challenge for me to try and improve my scripting skills with smile.gif
  • romi
    Options
    Offline / Send Message
    yes, yes, yes! an epoly version of UVPaint would be awesome. i did try to send an email to the author but i havent heard back. thanks MoP in advance for this effort.

    cheers,
    Rom
    snowblindstudios.com
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    romi: Yeah, we'll see how that goes ... it sounds pretty complicated so I don't know if I'll be able to get anything working, but it should be fun to try smile.gif

    Vassago: After some more debugging it turns out that in fact some pieces of your mesh are returning UV areas of 0.0, which I think is cos of their small size ... again not something I tested myself, but obviously a huge problem, so I'll try to fix it ASAP! smile.gif
  • Joshua Stubbles
    Options
    Offline / Send Message
    Joshua Stubbles polycounter lvl 19
    Is there something particularly wrong with overlapping and welding the UVs?
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Nope, not in this case! If you've just got a diffuse texture then it might even be better since you end up with less vertex data than if you left them unwelded (I assume).

    The reason I'd never do this is because recently I've only been working on normalmapped assets, and welding UVs changes how most engines/viewers calculate their tangents, so you get more predictable and generally better results when you don't weld mirrored/stacked parts - plus a lot of the time you will move stacked parts out of the 0-1 range by 1 UV unit for baking purposes, so it's impossible to weld them anyway.

    So yeah, it's not a bad thing, and I don't mean to sound like I'm telling you not to do it, it's just something I don't do anymore smile.gif

    I will use your scene as a test-bed for fixing various aspects of the script, since it seems like a really good scene for troubleshooting with (lots of different sized elements, both welded/unwelded and mirrored/unique).
  • Joshua Stubbles
    Options
    Offline / Send Message
    Joshua Stubbles polycounter lvl 19
    Coolbeans, hope it helps ya out smile.gif

    SERIOUSLY looking forward to that EditPoly UVPaint. Would be nice if that can be done.
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    New version up, same link as before.

    I've fixed the issues with mad scaling when you've got mirrored/welded parts... it was actually a pretty easy fix and partially an oversight on my part. The script now works perfectly on the majority of the objects in your test scene (and everything I threw at it myself).

    ...then I ran into a different issue (again on your test scene, Vassago), but this doesn't seem to a problem with my scripting or your models, but more like a Max issue itself.

    Here's an image of the issue:
    snapshot_problem.jpg

    Basically as soon as you convert this particular object to Editable Mesh (either through the modifier stack in the standard way, or through the Maxscript "snapshot" command), it decides to kindly remove about 8 edges.

    Which means that the polygon counts no longer match up between the original object and the snapshot. Great.

    So now, I've given it two separate routes for the script to go through - so that it will only use the snapshot method if there are other modifiers in the stack, and just skip that whole process if you just have a base EPoly with an Unwrap UVW modifier on top.

    Still, this is something that shouldn't happen... I blame Max!

    Stuff to come later will include ability to normalize only selected UVs either relative to the selection, or relative to the whole UV set's average scale. I will probably end up implementing a pop-up floater window for that.
  • sinistergfx
    Options
    Offline / Send Message
    sinistergfx polycounter lvl 18
    Wow Mop, it works awesomely. Thanks for all the time you'll be saving me in the future!
    (and YAY for not having to write it myself!)
  • Mark Dygert
    Options
    Offline / Send Message
    MoP, I've also hit the issue of edges disappearing, it is a Max bug. Often I don't realize the edges are missing until I'm in mudbox and something doesn't sub-d well. The way I get around it is to use the "turn to poly modifier" check on "keep polys convex" and collapse the stack back down to edit poly.

    Thanks again for the script! Normalize selection would be great!
  • Joshua Stubbles
    Options
    Offline / Send Message
    Joshua Stubbles polycounter lvl 19
    Awesome work, MoP. You're a rockstar laugh.gif

    Nice tip, Vig. I'll keep that in mind if I run into that issue.
  • Eric Chadwick
    Options
    Offline / Send Message
    Wow this sounds awesome. Been out sick for a couple days. I don't have Max at home either, so I can't play with it yet. frown.gif

    A UV Paint re-write sounds great. MoP on a roll. With sauce!
  • Eric Chadwick
    Options
    Offline / Send Message
    Hey I just had an idea for an option that might cool for a UV Paint-like script.

    Sometimes I want the UVs to bend around a curve, like how the UVs bend around the top of a torus.

    It would be cool if the script had an option to auto-straighten the curve into a straight UV. So a texture could tile around the bend.

    To solve stretching, it could also either break the verts along one side (because the faces are usually trapezoids) or keep the string of faces completely whole (more stretching, but avoids seams between faces).

    I have some notes at work from Zola about his script, when I asked him about it way back when. I'll dig them up on Monday.
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Hehe Eric, that's exactly what I was thinking - two options, one to just try and preserve the UV mapping as planar as possible (so your UVs will have no distortion, but probably be odd shapes), and the other to try and keep the UVs in a straight line if possible, keeping them welded and just averaging the positions to minimise stretching.

    I just noticed another stupid thing I did in my script here (basically just duplicates the object twice for no reason and deletes them both afterwards), not a huge issue, but I've uploaded the tweaked one anyway... thanks for the feedback so far smile.gif
  • Arathrax
    Options
    Offline / Send Message
    Arathrax polycounter lvl 17
    Thanks so much for this script MoP. Going to give it a try.
    /drools at the thought of UV paint working in 2008
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Normalize UV Scale v1.2

    OK, I've added ability to normalize only selected elements. If you have no selection, or if you're at the base subobject level, it'll normalize everything.
    If you have even just one vert/edge/face of an element selected, it will include that entire element in the calculation.
    Hopefully that method makes the most sense, I thought about including different options but decided I didn't want a popup menu or options thing, it all works from the same command this way.

    Again if you come across any errors or bugs, please let me know and I will try to fix them.

    I will also look at trying to make the whole script faster, I'm sure it will be possible, but I have other things to work on for now smile.gif
  • Eric Chadwick
    Options
    Offline / Send Message
    MoP, FWIW here's some info from the UVPaint coder when I asked him about it, way back in 2004.

    [ QUOTE ]
    If UVPaint can't work in some situation then there is a good reason. It takes your cursor position in the viewport and projects it to the main grid; between these two points there is a line which intersects the actually painted face. If your view direction is near parallel to the main grid then I got an invalid projection. If your "viewport camera" is above (* below) the grid and looks upward (* downward) then I also have an invalid projection point. So it is important to look against the grid while painting.
    This is another problem that I have to solve in one of the future versions....

    UVGrabIt is a tool that makes you able to pick a vertex and grab its UVs with your cursor around it. So you can fix some bad distortions of mapping on a lowpoly mesh object by hand.


    [/ QUOTE ]

    UVGrabIt was a grayed-out link on his site (still is!). It sounded interesting, like you could pick a vert in model-space and wiggle the mouse to get the UVs to slide around on the model, right in the viewport. Getting that working with your soft-select radius mouse gizmo might be cool too.

    Anyhow, some ideas to play with. Thanks for looking into it MoP.
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Cheers for that info Eric. His "UVGrabIt" thing sounds like what Tweak UV in Polyboost does.
  • Eric Chadwick
    Options
    Offline / Send Message
  • Michael Knubben
    Options
    Offline / Send Message
    That UVGrabIt script is exactly what I was thinking of yesterday. Does anyone have this laying around, and does it work with recent max versions?
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Oops, just realised this script currently won't work if you have other modifiers on the stack ABOVE your current Unwrap_UVW modifier... fixing now...
  • Eric Chadwick
    Options
    Offline / Send Message
    MoP, thanks again for this script, it's really great. It worked flawlessly, even all the way up on UV channel 99, where we store unique-per-vert UVs. Much appreciated!
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Heh, I don't even do anything with UV channels, I just let the Unwrap UVW modifier handle it - the script just takes whatever values are currently found in Unwrap UVW, and uses those ... so I just kinda assumed it worked with multiple channels. Good to know it does! tongue.gif
  • adam
    Options
    Offline / Send Message
    adam polycounter lvl 19
    Hey man - great script, it did its job really well. However, I have run in to quite the issue. I cannot edit my UV's - except for when I am in Edge mode within the UV editor. If I go to Vert mode, my UV editor goes completely white. If I go in to Poly mode, everything is locked (I cannot select anything).

    Even in Edge mode, I cannot scale - all I can do is move.

    Any ideas???

    Updates/notes:
    • I'm using Max 9 for this.
    • Reset XForm doesn't do anything
    • If I select the faces through the viewport, they can be selected. From there, I can scale, rotated, etc
    • Only the object i ran the script on is effected. Anything new I make is fine
    • Starting a new max scene and merging in the problematic one doesn't fix the issue
    • Hmm - I've brought it in to Max 2009 and in the UV Editor it seems to be selecting something thats very large, although if I zoom out for awhile I can never see its boundaries. Nor can I delete it. I'll keep on fiddling in Max 2009 to see if I can find a solution.
    • Here's a screenshot of what my Max 9 screen is looking like. If I apply the modifier, go to Face mode, then click anywhere inside the UV editor that Red + shows up, as if its selecting something. But I cannot delete whatever it is, but I feel as though it is whats causing the issue. At lease in Face mode. In Vert mode, that UV Editor is completely white.
    To summarize the problem - I've lost all major functionality through the UV editor.

    Update #2!

    Problem found! It seems as though this script runs in to problems on meshes that were created from splines - even when they were switched to editable poly's long ago. Quite odd. Anyhow, the problematic areas meshes were box mapped and I've continued on.
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Crazy. I'll look into it :)
  • Joshua Stubbles
    Options
    Offline / Send Message
    Joshua Stubbles polycounter lvl 19
    hey MoP - are you looking into getting this working with a multi-object unwrap? That'd be perfect :D Having no issues with it right now.
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Yeah, I've got Max2008 at work now so I'll see about sorting that out. Most of my scripts were done in Max8 so they don't support the multi-unwrap functionality of 2008/2009. I guess I'll have to do a sweep over all of them and update them to work properly like that.
  • Joshua Stubbles
    Options
    Offline / Send Message
    Joshua Stubbles polycounter lvl 19
    That'd be suuuuper cool MoP :)
    Works wonders for getting the lightmap UV's all scaled correctly to start with. Very helpful.
  • kio
    Options
    Offline / Send Message
    kio polycounter lvl 15
    is there a way to normalize another uv channel than number one?

    (sure i can workaround with copy & pasting the uv channels, but thats shitty :) )
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    kio, it sounds like EricChadwick successfully used it on multiple UV channels. I haven't built anything in specifically to deal with that, I kind of assumed that it will work with whatever selection that the Unwrap UVW modifier passes it - so whatever channel the Unwrap UVW modifier is set to use, should be the one that the script operates on.
  • kio
    Options
    Offline / Send Message
    kio polycounter lvl 15
    oh thanks - didnt knew it uses the last unwrap modifier, i always collapsed everything for safety reasons, so i assumed it would only scale channel 1, i'll give it a try tomorrow at work.
  • PeterK
    Options
    Offline / Send Message
    PeterK greentooth
    Paul, what does this script do? :)
  • Eric Chadwick
    Options
    Offline / Send Message
    I didn't use it with multi-unwrap, I just had a uvw unwrap that was set to uv#99 and used it there.

    PeterK: It proportionally scales the UV elements, so the UV proportions match the 3d space proportions. Here's an example of the same idea...
    byf2_figure6.jpg

    If you put a checker map on the model and your checkers are all different sizes, the script will fix it so the checkers are all the same size.
  • PeterK
    Options
    Offline / Send Message
    PeterK greentooth
    Thanks for the info Eric. looks like a nice release. I would have just downloaded and tried it, but I did not have access to max earlier.
13
Sign In or Register to comment.