Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Trying to create a UVW flattener but having issues.

Trying to create a UVW flattener but having issues.

Anonymous
Not applicable
489 Views
9 Replies
Message 1 of 10

Trying to create a UVW flattener but having issues.

Anonymous
Not applicable
Hi,

Just getting into Maxscript after having a scene that I had to flatten the UVW maps of lots of models, but I can't seem to get my test script to work in the way I think it should work.

What I want to happen is this:

A Unwrap UVW is applied to an object
The Channel of the UVW is set to 2
The Unwrap UVW is in Face Sub Object Mode
Then the faces are flattened.

Here is my script

mybox = box length:20 width:40 height:60
convertto mybox editable_poly
Unwrapper = Unwrap_UVW()
Unwrapper.setApplyToWholeObject true
addModifier mybox (Unwrapper)
Unwrapper.setMapChannel 2
mybox.Unwrapper.setTVSubObjectMode 3
Unwrapper.flattenMap 45.0 #() 0.001 true 0 true true


The box is created as a test. The Unwrap UVW does get applied and the channel is set to 2, but it doesn't go into Face Subobject mode and doesn't flatten the map. Any ideas?
0 Likes
490 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable
Hi,

Just getting into Maxscript after having a scene that I had to flatten the UVW maps of lots of models, but I can't seem to get my test script to work in the way I think it should work.

What I want to happen is this:

A Unwrap UVW is applied to an object
The Channel of the UVW is set to 2
The Unwrap UVW is in Face Sub Object Mode
Then the faces are flattened.

Here is my script

mybox = box length:20 width:40 height:60
convertto mybox editable_poly
Unwrapper = Unwrap_UVW()
Unwrapper.setApplyToWholeObject true
addModifier mybox (Unwrapper)
Unwrapper.setMapChannel 2
mybox.Unwrapper.setTVSubObjectMode 3
Unwrapper.flattenMap 45.0 #() 0.001 true 0 true true


The box is created as a test. The Unwrap UVW does get applied and the channel is set to 2, but it doesn't go into Face Subobject mode and doesn't flatten the map. Any ideas?


You need to select your box after you create it and make sure you are in the modify mode so that it can go into sub-object mode properly. Also, you might want to get to your new modifier differently because mybox.Unwrapper doesnt work as you have it currently.

Try this:

max modify mode
mybox = box length:20 width:40 height:60
select mybox
convertto mybox editable_poly
Unwrapper = Unwrap_UVW()
Unwrapper.setApplyToWholeObject true
addModifier mybox (Unwrapper)
Unwrapper.setMapChannel 2
-- Access the index of Modifiers
mybox.modifiers.setTVSubObjectMode 3
Unwrapper.flattenMap 45.0 #() 0.001 true 0 true true


For accessing the modifier you could do it many ways. In the example above I accessed it as the 1st modifier in the stack but you could do any of the following.

-- Access the Class of Modifier
mybox.modifiers.setTVSubObjectMode 3

-- Rename the modifier
Unwrapper.name = "Unwrapper"
-- Access the modifier with this name specifically
mybox.modifiers.setTVSubObjectMode 3
0 Likes
Message 3 of 10

Anonymous
Not applicable
if speed is an issue (& it usually it is if you're iterating over a ton of objects) then you may want to try this...

max modify mode
mybox = box length:20 width:40 height:60
select mybox
convertto mybox editable_poly
Unwrapper = Unwrap_UVW()
Unwrapper.setApplyToWholeObject true
addModifier mybox (Unwrapper)
Unwrapper.setMapChannel 2

myUV = mybox.unwrap_UVW
z = mybox.getNumFaces() --no need to select faces in sub-object mode, just count them
myUV.selectFaces #{1..z} --& just select those same faces in the unwrap behind the scenes
Unwrapper.flattenMap 45.0 #() 0.001 true 0 true true


dunno if it'll speed it up by much, but every little bit counts. 🙂
0 Likes
Message 4 of 10

Anonymous
Not applicable
Brilliant!

Thanks Guys! There seem to be so few resources for Maxscript, I can't even find an up to date book that goes beyond Max 8. Both of you are stars!
0 Likes
Message 5 of 10

Anonymous
Not applicable
Whoops, bumped the wrong thread.

Still having some issues. THe first example given by Randall Hess worked fine on a newly created box, but I've moved onto trying to apply the unwrap_uvw to mutliple objects.


for obj in $ do
(
max modify mode
convertto obj editable_poly
Unwrapper = Unwrap_UVW()
Unwrapper.name = "Unwrapper"
Unwrapper.setApplyToWholeObject true
addModifier obj (Unwrapper)
Unwrapper.setMapChannel 2
obj.modifiers.setTVSubObjectMode 3
Unwrapper.flattenMap 45.0 #() 0.001 true 0 true true
)


This maxscript is run on multiple objects (In my tests a sphere in editable poly, with 3 material id's applied to different faces, a previous script has broken the sphere into 3 detached objects and these are the objects selected). Each object gets a UVW_Unwrap applied to it, but again it does not seem to select faces and does not flatten the map. Here is the script applied with the detach script.

(
local MatID = 1
local baseobj = $;
while (polyop.getNumFaces baseobj > 0) do
(

baseobj.selectByMaterial MatID
polyList = polyop.getFaceSelection baseobj

if (polyList.count > 0) do
(
newName = (uniquename "DetachedObject")
polyOp.detachfaces $ polyList delete:true asnode:true name:newName
local tempobj = Execute ("$'"+newName+"'")
max modify mode
convertto tempobj editable_poly
Unwrapper = Unwrap_UVW()
Unwrapper.name = "Unwrapper"
Unwrapper.setApplyToWholeObject true
addModifier tempobj (Unwrapper)
Unwrapper.setMapChannel 2
tempobj.modifiers.setTVSubObjectMode 3
Unwrapper.flattenMap 45.0 #() 0.001 true 0 true true
)
MatID += 1
)
)
0 Likes
Message 6 of 10

Steve_Curley
Mentor
Mentor
Not sure I'm going to be able to help with the unwrapping (though I will have a look at it), but in the meantime...

Boxes are easier to work with for this (rather than a Sphere) because they automatically have each face set to a different MatID (1 through 6).
Using "$" - probably better to use "selection", as in
for obj in selection do

"Selection" is a pre-defined global which is "a collection of the objects currently selected". However, if you need to iterate over the whole scene (excluding helpers, lights etc) then
for obj in geometry do
may be more appropriate, though it locks up my Max if I use it - probably because the geometry global changes as the new objects are created - taking a copy of it and using the copy does work.

Not much point in setting "baseObj = $" then using "$" in the code.
Not sure why you're using "execute" - tbh I'm not even sure what it's doing! There are warnings in the help about exectue and the scope in which it runs (global).
"Convertto <node> <class>" construct is a Mapped function - works on a collection, but the <node> in this case is a single object, so a simple "convertToPoly <node>" is much clearer.

Quite why the original code has 1 line using "tempobj.modifiers" and the rest just using "Unwrapper" I don't know (sorry Randall btw). If there's no difference (and I don't know if there is or isn't), then they should (for consistency) be the same - one or the other but not a mixture (after the modifier has beed added).

The overall layout of the code needs a little attention, I feel. There are several separate steps to go through, depending on exactly what you're trying to do - breaking up the code (into functions) makes it easier to follow and easier to maintain.

While it may seem easier to put everything in the one loop (and it may be quicker) it makes debuggin much more difficult. The newNodes array cound be dispensed with (below) but it's much clearer (at least to me) not to have too many nested calls - easy eough to change if you prefer it that way though.

The following appears to work in Max 2009 - you'haven't told us (unless I missed it) which version you're using - it really helps if you put this in your signature.


(
local newNodes = #()

fn addUnwrapper obj =
(
max modify mode
modPanel.setCurrentObject obj
subObjectLevel = 4
Unwrapper = Unwrap_UVW name:"Unwrapper"
Unwrapper.setApplyToWholeObject true
addModifier obj Unwrapper
Temp = obj.modifiers
Temp.setMapChannel 2
Temp.setTVSubObjectMode 3
Temp.flattenMap 45.0 #() 0.001 true 0 true true
subObjectLevel = 0
)

fn explodeToElementsByMatID obj =
(
local MatID = 1, PolyList = #()

while (polyop.getNumFaces obj > 0) do
(
obj.selectByMaterial MatID
polyList = polyop.getFaceSelection obj

if (polyList.count > 0) do
(
newName = (uniquename (obj.name + "'" + "DetachedObject" + "'"))
polyOp.detachfaces obj polyList delete:true asnode:true name:newName
append newNodes (getNodeByName newName exact:true)
)
MatID += 1
)
)

baseObjects = for bo in geometry collect bo

for bo in baseObjects do
(
convertToPoly bo
explodeToElementsByMatID bo
)

for nn in newNodes do
(
addUnwrapper nn
)

Max Select None
)

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 7 of 10

Anonymous
Not applicable
Steve, you are an utter legend. I'll create a sig in a second (Max 2009).

Where or how did you pick all this up? Max's help files are confusing and lacking good examples and tutorials seem to be very low on the ground. When googling for speciifc commands I'm starting to pick up my own questions. I really want to get my head around this as at the moment this is all you (Not ungrateful at all, just feeling a bit dim as I'm generally pretty good with coding.)
0 Likes
Message 8 of 10

Steve_Curley
Mentor
Mentor
How did I pick it up? With difficulty 😉

To be frank, I don't really know that much about it, I just keep searching for things until I find the bit I need, though that can take a while as you probably noticed. It's really all about practice, as with anything in Max. Also reading pretty much every post in this forum will help, because the occasional real guru (like Bobo) will post something which really helps in getting a "handle" on things.

Having a background in programming helps a lot though - I already know (or I tell my boss I do anyway) many of the tricks and pitfalls which you can only really learn though years of use, regardless of language. There are many more, I'm sure, I have yet to learn which are specific to MXS.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 9 of 10

Anonymous
Not applicable
Not sure I'm going to be able to help with the unwrapping (though I will have a look at it), but in the meantime...

Quite why the original code has 1 line using "tempobj.modifiers" and the rest just using "Unwrapper" I don't know (sorry Randall btw). If there's no difference (and I don't know if there is or isn't), then they should (for consistency) be the same - one or the other but not a mixture (after the modifier has beed added).

The overall layout of the code needs a little attention, I feel. There are several separate steps to go through, depending on exactly what you're trying to do - breaking up the code (into functions) makes it easier to follow and easier to maintain.


No worries Steve. In my help above, I just pointed him to a couple of ways of going about accessing a specific modifier as his original implementation wasn't functioning properly. However, it seems he chose the method that in the end I may not have chosen myself but it works just the same.
0 Likes
Message 10 of 10

Steve_Curley
Mentor
Mentor
Ahh - I see - I missed that you were showing the different options available - my bad on that. The main thing is that he now has a routine which (hopefully) works as intended - if not, it's back to the drawing board 😉

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes