Home Coding, Scripting, Shaders

MEL script, how to query what materials are applied to what faces.

polycount sponsor
Offline / Send Message
malcolm polycount sponsor
I'm trying to create a script which stores the current materials applied to the asset, then apply one material to the whole thing, then be able to paste my original materials back on at a later. Is that possible, and what command should I use to query what materials are assigned to my faces.

Replies

  • poopipe
    Offline / Send Message
    poopipe grand marshal polycounter
    I don't do Mel but you need to do this... 

    1.Get a list of shading groups
    2.Get a list of members from each shading group - which will  be shape nodes or faces
    3. Store all that for later

    It's about 5 lines of pymel,  no idea how it'd work in mel. 

     It gets awkward if you're only interested in a subset of objects or your scene isn't cleaned up.  

    Would it be possible to simply make a copy, apply the material and then delete the copy?  Much less risk of maya exploding.. 

  • malcolm
    Offline / Send Message
    malcolm polycount sponsor
    Yeah I actually thought about duping the object, but that won't work because the user needs to be able to make changes to UV mapping while the script is active. I'm trying to create a toggle button that assigns a checker map, then when you toggle remove the checker map it puts back your previous materials. Very tricky, I've been reading online for two days now and apparently this is quite a complex problem. I'm currently exploring a alternate way of doing this script where I generate the checker map material and instead of applying it to the mesh I plug it into the existing material graph and then just delete it when I'm done, this is working better because I don't have to worry about what was applied first, and when I delete my checker map the viewport pops back to whatever they had applied before. It has its own challenges though.
  • poopipe
    Offline / Send Message
    poopipe grand marshal polycounter
    Yeah,
    I'd be inclined to avoid anything that's reliant on face numbering because that's subject to change and would completely knacker any lists you store.
    The material swap is relatively safe as you're dealing with a list of things that won't go away unless you explicitly delete them, it's easy to store a list of material:shading group and it'll be a lot quicker to run.

    You can probably connect one material to multiple shading groups actually... Not tried it though
  • PolyHertz
    Offline / Send Message
    PolyHertz polycount lvl 666
    For the second part; Sounds like you're looking for a way to tag / add metadata to faces so as to identify them even when their indexs have changed. From a quick google search Mayas version of this is called "blind data":
    http://download.autodesk.com/global/docs/maya2014/en_us/index.html?url=files/Blind_data_What_is_blind_data_.htm,topicNumber=d30e168928
     http://download.autodesk.com/us/maya/2011help/Commands/polyBlindData.html
  • Klaudio2U
    Offline / Send Message
    Klaudio2U polycounter lvl 8
    You can maybe use that new Render Setup where using scripting do the following: 
    1. Create new material with your checker map. 
    2. In render setup create a new layer
    3. In that new layer create (RMB click) a new collection and there (on the right side) add your object(s) where you want that new material with checker map. 
    4. Now RMB click on that collection and choose to create shader override then on the right side in "Override Shader" field type your checker material name and press enter. 
    5. On layer created in step 2 press that eye button to toggle the override. 
    6. Once done simply delete the layer and everything is back to original material setup. 


  • malcolm
    Offline / Send Message
    malcolm polycount sponsor
    I've got something just about working sorry don't have the code here will share the solution once I've figured it out.

    @poopipe yes the face order changing is an issue, ideally the script would be robust, but after working on it for 3 days I'd be happy if it worked at all. If I could just plug the checker into all the shading groups that would be perfect, I haven't found a way to do that though. Thanks for the tip I might already have the code working since I've got the materials pasting back to the faces.

    @PolyHertz thanks I'll check that out if I ever get past the basic functionality stage of this script

    @Klaudio2U that would actually work perfectly, any way to script this stuff in MEL, it doesn't appear in the history stack.
  • malcolm
    Offline / Send Message
    malcolm polycount sponsor
    Hi guys, made some good progress, still working on it. But it does appear to be working in a contained environment. I've given up on trying to paste the materials back to the faces, and instead just plugging the checker map into the materials' shading group is a much better way to go. This is much safer and easier to code.

    //Get the names of the shading gruops applied to the object
    string $shadingEngines[] = listConnections("-type","shadingEngine", $maCheckerMapSelectedOBJ);

    //Remove duplicate names so you can actually use the list you get back
    string $shadingEngineRemoveDupes[] = stringArrayRemoveDuplicates($shadingEngines);

    //Convert the names into a size so you can use if statements on it
    float $numberOfMatsOnObject = (size($shadingEngineRemoveDupes));

    //Get the names of the materials applied to the object
    hyperShade -smn "";
    string $maMaterialsToPasteBack[] = `ls -sl`;

Sign In or Register to comment.