Notifications
Clear all

[Closed] UVWmap modifier & fit

Hello,

I’m working on a script, and for the life of me, I can’t find, nor figure out, how MAX is doing the UVWMap Fit. All that is recorded in the macro recorder is the final width, height, and depth that’s assigned to the Gizmo. I have no clue how those values are derived. They’re not the same as the values displayed in the properties of the object being fit to.

I’ve seen a number of scripts that will align the UVWMap to a face normal, or an average of many face normals, and have used that quite easily. Some of these scripts include disclaimers such as “We’ll do the ‘fit’ functionallity in a later version.” Which of course I can’t find to save my life.

Any help finding a code snippet that does the job of the UVWMap fit button would be most appreciated. If not that, can you think of a work-around or algorithm? I’m stuck.

  • William Maness
6 Replies

Hi,
you can use the Unwrap do do this:

$.modifiers[#unwrap_uvw].unwrap5.mappingFit ()

Thanks for your help, but that isn’t quite what I’m trying to do. I’m trying to use the UVWMap modifier, not the Unwrap. I’ve got an editable mesh, largely planar object that is sitting in a non-ortho orientation to any coordinate system (generated outside of Max) Properly align a UVW Map to it. Manually, I can apply a UVWMap, do a Normal Align to one of its polygons, then press the fit button and I get exactly what I want. In MAXScript, I found the normal align code, and was able to integrate that easily, but I can’t find anything that will repeat what the FIT button is doing. Maybe I’m just being stupid (more than likely, I’m an old-school programmer, but a MAXScript newbie).

  • William Maness

This function will calculate the dimensions of the UVW map modifier, so I guess it does what it does the “Fit” button (or at least something similar)…

fn fitUVWMap obj uvwmod = (
	local bbMin = getModContextBBoxMin obj uvwmod
	local bbMax = getModContextBBoxMax obj uvwmod
	
	uvwmod.width = bbMax.x - bbMin.x
	uvwmod.length = bbMax.y - bbMin.y
	uvwmod.height = bbMax.z - bbMin.z
	
	OK
)

Wow,

That’s a lot closer, but it doesn’t quite seem to do it. Here’s the relevant code I’ve got going…

-- Do Sea Level Work
 importFile "T:\USA\CO\GOLDEN\dxf\SL-CO-Golden_01_A.dxf" #noPrompt
 sealevel = getNodeByName "Layer:3D_FACE"
 addModifier sealevel (normalModifier())
 sealevel.modifiers[1].flip=true
 collapseStack sealevel
 sealevel.name = "SL-CO-GOLDEN_A_01"
 
 -- Make a UVMap on Sea Level
 theFace = sealevel.numfaces / 2 -- Try to get a face near the middle of the mesh
 faceNormal = in coordsys sealevel (getFaceNormal sealevel theFace)
 worldUpVector = [0,0,1] 
 rightVector = normalize (cross worldUpVector faceNormal) 
 upVector = normalize ( cross rightVector faceNormal )
 theMatrix = matrix3 rightVector upVector faceNormal [0,0,0] 
 addModifier sealevel (UVWmap())
 sealevel.modifiers[1].Gizmo.transform = theMatrix

This is what I get at the end of the code above…
http://forums.cgsociety.org/attachment.php?attachmentid=105301&stc=1

At the end of the code you just gave me, it looks almost identical… I would say your fit calculation is what Max does by default…
http://forums.cgsociety.org/attachment.php?attachmentid=105303&stc=1

Here’s what I get when I press the Fit button manually (which is what I want, axis flip being irrelevant)

http://forums.cgsociety.org/attachment.php?attachmentid=105304&stc=1

I hope this illustrates the difference. I tried a bounding box technique, similar to what you’d done, with similar results. Anyone have any thoughts?

  • William

Here is a reverse-engineered version of the Gizmo Fit code with detailed explanations.

http://www.scriptspot.com/bobo/mxs9/uvwgizmofit/

That is exactly what I needed. Thanks Bobo!

  • William