Script to Planar Map UVs of Each Poly in a Mesh

I'm trying to write a script that will take any random Editable Poly object, apply an Unwrap_UVW modifier, then Quick Planar Map every polygon in the object individually, normalize all these UV-islands relative to each other, and pack them inside a 1-unit UV space without overlap. I'm very close to getting what I want, but Max is throwing an exception in the following code:

print "*************************";
 mybox = Box length:10 width:5 height:2;
 ConvertTo mybox Editable_Poly;
 polyCount = polyop.getNumFaces mybox;
 addModifier mybox (Unwrap_UVW());
 mybox.Unwrap_UVW.unwrap2.setTVSubObjectMode 3;
 
 for i = 1 to polyCount do
 (
 	mybox.Unwrap_UVW.unwrap.selectPolygons #{i};
 	mybox.Unwrap_UVW.unwrap2.unfoldMap 0;
 )
 mybox.Unwrap_UVW.unwrap.selectPolygons #{1..polyCount};
 mybox.Unwrap_UVW.unwrap2.pack 1 0.02 true true true;

Can anyone help me out here? This script should run fine, but it's giving me a huge headache. Try running this in Max 2012 and tell me why it doesn't work. Or is Max just buggy with this?

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
br0t's picture

I think you have to be inside

I think you have to be inside the "modify" panel and you also have to select the box when trying to work on its Unwrap_UVW modifier. So after you've added the modifier you can add this code:

 max modify mode
 select myBox

and then it should work, at least it does for me.

Cheers

Never get low & slow & out of ideas

liquidgraph's picture

Thanks, that did the

Thanks, that did the trick!

Now all I need is to test for whether an object I'm selecting is a mesh object rather than a camera, spline, or other such object that shouldn't be unwrapped. I have no idea how to test for this. Any clues?

liquidgraph's picture

After unwrapping some more

After unwrapping some more models with my script, I notice that using Unfold Map on each face doesn't work well because some long polygons don't get scaled proportionally, resulting in an uneven checker pattern. From what I've tried, a Quick Planar Map on each face is the only technique that gets the perfectly straight results I need, but I cannot get Quick Planar Map to work with MaxScript. The documentation says that the following should work:

selectedObj.Unwrap_UVW.unwrap.setProjectionType 4;
selectedObj.Unwrap_UVW.unwrap.planarMap();

But the results are NOT the same as pressing the Quick Planar Map button in the UVW Unwrap modifier with the Mouse. How can I get Quick Planar Map working? Do I have to be in a special mode?

br0t's picture

It looks like the

It looks like the "planarMap()" does not use the "Normalize Map" Option, although its checked. But thats just my guess, cause doing it by hand with "Normalize Map" not checked, it looks very similar to what you get with your script.

Have you tried flatten mapping? That might do what you want, if you pack the UVs after flattening:

-- create box and modifier here, apply modifier, set modify mode and select box etc.
 
for i = 1 to polyCount do
(
mybox.Unwrap_UVW.unwrap.selectPolygons #{i};
myBox.Unwrap_UVW.flattenMapNoParams() --uses default settings, but you can specify each one if you like, i was too lazy 
)
 
mybox.Unwrap_UVW.unwrap.selectPolygons #{1..polyCount};
mybox.Unwrap_UVW.unwrap2.pack 1 0.02 true true true;

Never get low & slow & out of ideas

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.