Home Technical Talk

3ds max: detach elements off 1-1 uv space

interpolator
Offline / Send Message
BradMyers82 interpolator
I'm wondering if anyone knows if there is a 3ds max script to do this.

Basically, the problem I have is I have this super complicated mesh that has uvs off the 1to1 space. There is like 12x12 tiles off uv space so all 100 and some spaces have there own textures.
I don't have the original files and it would take forever to try and select the mesh via the uv roll out colapse and apply the textures manually.

So I'm wondering if there is a script to do this... Every part of the mesh that is in its own 1to1 space on the uvs gets detached as its own object.

This would allow me to hide stuff and apply the materials 1 by 1. Currently, the mesh is so complicated just opening a uvw modifier practically crashes max. So I really don't know any other solution other than getting a script that does something like I just mentioned.

Replies

  • Eric Chadwick
    I don't know of a script offhand. You could ask someone to write one for you, on Scriptspot. I've had great results there when asking nicely. http://www.scriptspot.com/forums/3ds-max/scripts-wanted
  • amartinez
    Offline / Send Message
    amartinez polycounter lvl 8
    Hi!
    Got interested with this question. How did it go? Did you ask on script spot?
    I wouldn't mind giving this a try, currently swamped in an utterly complicated script that might prove to not be useful... a break from it will be nice.

    If you have more information on:
    - All the faces are in their own [0,1] space right? so for example a group of faces that are in say [1,2] range will never have vertices that get on another tile.
    - Do you need the detached objects to return the uv tile to the [0,1] range?

    How is it currently textured? You say there are currently 12x12 tiles, and some tiles have their own texture? Does it have a multimaterial or something?

    If you have a 'test' model, like a 3x3 uv tiles example and a brief explanation of how it should end up?
  • amartinez
    Offline / Send Message
    amartinez polycounter lvl 8
    Would you be willing to give this a test?
    https://www.dropbox.com/s/qt5t4qao43vj7uk/AM-DetachFacesByUVQuadrant.ms
    (Save to some place, drag and drop over your scene).

    I think it probably does already the basics.
    It still has some grayed out options that could be added as needed.
    Hope it suits you well.
  • BradMyers82
    Offline / Send Message
    BradMyers82 interpolator
    Eric Chadwick: Thanks for the tip dude, I'll check with amartinez and see if he can help, and I'll use scriptspot for backup.

    amartinez: I really appreciate you taking the time to help!
    I probably didn't describe my problem well enough, but simply put I need a solution for applying all these diffuse textures to a single mesh, where I really don't know anything about what faces they should be applied other than the uv layout.

    Here is the uv layout currently...
    uv_script_request_01.jpg

    See how I selected the first 1x1 space? What I want to do, is use a script to jump from each 1x1 space and detach those faces as its own seperate object. Counting the spaces, I guess there is 107 different 1x1 uv squares. So after running the script I would have 107 individual meshes, that I could manage much easier (hide in viewport and what not). And, I could apply a material to each object as a whole without any issues.
    This is a project I am doing at work, and its an actual source model from a movie. Obviously I can't say much about it, but a lot of the textures look the same actually, so it wouldn't be that hard to apply them one by one, but if you can think of a solution for this too that would be awesome.

    Anyway, this is the only practical solution I can think of, because just opening the uv rollout selecting the faces and detaching them manually 107 times would take forever.
  • BradMyers82
    Offline / Send Message
    BradMyers82 interpolator
    Hey, I tried the script and it just got stuck trying to do everything. I'm not really sure I follow what it does exactly tho.

    Here is a print screen of my attempt to run the script. It just gets stuck there, but you can see from the viewport the mesh has over 1.5 million tris (and its already been decimated as far as I can go).
    uv_script_request_02.jpg



    Just to clarify, each one of those 107 1x1 uv spots has its own texture that goes with it. I need to apply each texture accordingly.
  • Eric Chadwick
    This is called a "udim" layout. You can setup your Max material to use it as-is, without detaching. Might save you a ton of time. Here's a tutorial.
    http://www.neilblevins.com/cg_education/multiple_uv_tiles/multiple_uvs_tiles.htm
  • amartinez
    Offline / Send Message
    amartinez polycounter lvl 8
    @Eric: Didn't know it was called a 'udim' layout.
    Thanks for the links Eric.

    @Brad: It's probably the size of the mesh. I didn't add any progress bar as of yet. No clue how much it would actually take currently, I could work on adding that tonight.
    I think the script does exactly what you ask for, it's just probably not optimized enough.
    Try creating a 'box', tessellate, move different faces to different quadrants, (or select from a bunch of random faces, uv detach/break and move them to a random quadrant) to use as a mesh test to see what the script will do.
  • amartinez
    Offline / Send Message
    amartinez polycounter lvl 8
    This should be the expected behavior:
    detach_uv_by_quadrants.jpg
  • Noors
    Offline / Send Message
    Noors greentooth
    Oh i made a script that does almost that at work. It detaches all shell that are uniquely in 0-1 square, but it could be tweaked. Will post tomorrow.
  • amartinez
    Offline / Send Message
    amartinez polycounter lvl 8
    Correction: when I said "size of the mesh" I meant the triangle count.

    @Brad: Progress bar added and minor tweaks.
    https://dl.dropboxusercontent.com/u/136334834/forums_shared/AM-DetachFacesByUVQuadrant.ms
    Tested on a 600k vertices box with 12 tiles and it takes less than 30s.
    (Boxes as usual, is never a real test case)
    Did you try the 'udim' layout explanation pointed out by Eric?
    There would be a way to put the textures automatically, if the textures are conveniently named (which is probably the case) (using the udim "1001" naming conventions or if in the name there is a hint of which tile it points to).
    (For both of these cases: to detach and a assign the proper texture for each tile or to automatically setup udim ready material for the whole mesh).

    @Noors: Please do! Would like to see how you solved it.
  • Noors
    Offline / Send Message
    Noors greentooth
    Well i'm not doing exactly that, read too quickly ^^
    I detach all shells that are in the 0-1 square.
    But it looks like you made the right thing.
    The only trick i use is from Jorge "Polytools" Rodriguez.
    I convert uvs to a trimesh, because mesh are faster to deal with than uvs, not to mention unwrap modifier.
    Also you could cache your last polyop, maybe you'll gain a bit of time.
    fn Detach01Shells node = 
    (
        tObj = copy node 
        convertTo tObj TriMeshGeometry
        channel = 1
        
        --build mesh from channel
        obj = snapshotasmesh tObj
        numtverts = meshop.getnummapverts obj channel
        getmapface = meshop.getmapface
        getmapvert = meshop.getmapvert
        verts = for j = 1 to numtverts collect (getmapvert obj channel j)
        setnumverts obj numtverts
        meshop.setvert obj #all verts
        
        for j = 1 to obj.numfaces do
        (
            face = getmapface obj channel j
            setface obj j face[1] face[2] face[3]
        )
        
        --detach elements inside 0-1 square
        processedFaces = #{}
        _getElementsUsingFace = meshop.getElementsUsingFace
        _getFaceCenter = meshop.getFaceCenter 
        _detachFaces = meshop.detachFaces
        counter = 1
            
        for i=1 to obj.numfaces where (NOT processedFaces[i]) do
        (
            elt = _getElementsUsingFace obj i
            pos = _getFaceCenter obj i
                
            if (pos.x <= 1 and pos.x >= 0 and pos.y <=1 and pos.y >=0) do 
            (
                newMesh = _detachFaces tObj elt delete:false asMesh:true
                emesh = Editable_mesh() 
                emesh.mesh = newMesh
                emesh.pos = tObj.pos
                emesh.name = ("detached_mesh_" + counter as string)
                CenterPivot emesh
                counter += 1
            )
            processedFaces += elt
        )
        
        delete tObj
    )
    
  • amartinez
    Offline / Send Message
    amartinez polycounter lvl 8
    @Noors: Oh wow, I never though of that.
    This is a very clever way of doing things, didn't know that manipulating a mesh was faster than manipulating the UVs via polyOps.

    I'm caching some of the polyOp functions to local variables and UVW Unwrap is modifier is never used. But for example, there is no convenient functions like 'getFaceCenter' that I know of, I have to get the vertices of an UV face and average it's positions.

    If I understand correctly, you flatten the UVs as a physical mesh. Then somehow you have to transfer back those positions back to the real uv right?
    Will give this a try. (Although for the case at hand, since there is no real "manipulation", just detecting where a face is in the quadrants, so the first read should be enough, I should do it on a trimesh though).

    Thanks for the tips.
  • BradMyers82
    Offline / Send Message
    BradMyers82 interpolator
    You guys are so awesome! Can't thank you enough!

    Lot to digest here since my last post, I'm gonna look everything over and see what I can come up with. I'll post an update on my progress later today. thanks again! :D
  • Noors
    Offline / Send Message
    Noors greentooth
    Yeah, in fact it's a snapshotasmesh. It's a copy of the object as mesh in memory, but it is not created physically, unless you assign it to an editable_mesh.
    I don't modify the original obj in my case.
    Also, face in uv and on mesh have the same index, and the same vertex index order.

    Yeah it's clever, that's how i guess, he performs shells detections, open edges so quickly in polytools.
    Here, i select 1 face in all faces, get its center, get its element (which is fast on mesh) and remove the element from my faces array. So I don't test every face/vert position.
  • BradMyers82
    Offline / Send Message
    BradMyers82 interpolator
    Eric: hey I tried out that method, unfortunately it isn't any easier for me as I still need to go thru the process of pluging in all the textures one by one and I actually had no idea what textures go to which uv offset. Would have worked fine though if they supplied me with textures that were in some logical order. :(
    But thanks again man, this is good to know!

    amartinez: Hey dude, thanks again! I used the script and it worked well. I still had the issue of trying to apply the various textures and not knowing with which texture each model went. But I did some guess and check and was able to get enough of them correct for my purposes. I really appreciate the help dude!
  • Eric Chadwick
    Yeah, if the textures aren't named appropriately (1001,1002,etc.) then it's going to be nearly impossible. I don't envy you one bit!
  • amartinez
    Offline / Send Message
    amartinez polycounter lvl 8
    No problem!

    Wonder if there is actually a way to extract some sort of texture information.
    Like:
    - Do the faces IDs in the original mesh object have some sort of name? (they usually are 1 - No Name, 2 - No Name, etc... but MAAAYBE this time there is something).
    - Does it come with a pre-applied material? Standard? Multi-material? none at all?

    Glad to hear that it was at least halfway useful :p.
    Cheers.
  • BradMyers82
    Offline / Send Message
    BradMyers82 interpolator
    Eric: haha!

    amartinez: They had some numbering system, but it was def. broke. Also, I believe the model was built in another program, and all I had access to was an .obj. :(
    But yea man, it was tedious but I was able to do it thanks to you! It would have been way more difficult without that script!
  • amartinez
    Offline / Send Message
    amartinez polycounter lvl 8
    Awesome!
    Glad to help and hear that it went well.
    If at some point you need it again and have any suggestions feel free to send a message.
Sign In or Register to comment.