Home Technical Talk

[help!] unable to run mel script on multiple objects :/

polycounter lvl 13
Offline / Send Message
artquest polycounter lvl 13
Hi all, So I'm writing up this script that I'll be officially releasing soon but I can't seem to get it to run with multiple objects selected.   I get the error "// Warning: Line 3.49 : Redeclaration of variable "$shape" shadows previous declaration at line 2. Previous value will be overwritten by explicit initializer."

was curious if any of you mel wizzards could help me out. :)


//High Poly Preview with  no holding edges
 string $sel[]=`ls -sl`;
 string $shape[]=`listRelatives -type mesh $sel`;
 int $currentCrease = `getAttr ($shape[0]+".smoothLevel")`;


// TURN ON
if ($currentCrease == 0 || $currentCrease == 2){
   polySelectConstraint -m 3 -t 0x8000 -sm 1; // to get hard edges
   polyCrease -value 3.0;
   displaySmoothness -divisionsU 3 -divisionsV 3 -pointsWire 16 -pointsShaded 4 -polygonObject 3;
   setAttr ($shape[0]+".smoothLevel") 4;
   polySelectConstraint -sm 0; // turn off edge smoothness constraint
   }
//TURN OFF
//Toggle off high poly preview
else{
   displaySmoothness -divisionsU 0 -divisionsV 0 -pointsWire 4 -pointsShaded 1 -polygonObject 1;
   PolyRemoveAllCrease;
   setAttr ($shape[0]+".smoothLevel") 2;
   }



Replies

  • StephenVyas
    Options
    Offline / Send Message
    StephenVyas polycounter lvl 18
    How about this?
    //High Poly Preview with  no holding edges
     string $sel[]= `ls -sl` ;
     string $shape[]=`listRelatives -shapes $sel`;
     
     int $currentCrease = `getAttr ($shape[0] +".smoothLevel")`;
     proc  smoothingThis(int $currentCrease, string $shape[]){
    
            // TURN ON
            if (($currentCrease == 0) || ($currentCrease == 2)){
               polySelectConstraint -m 3 -t 0x8000 -sm 1; // to get hard edges
               polyCrease -value 3.0;
               displaySmoothness -divisionsU 3 -divisionsV 3 -pointsWire 16 -pointsShaded 4 -polygonObject 3;
               setAttr ($shape[0]+".smoothLevel") 4;
               polySelectConstraint -sm 0; // turn off edge smoothness constraint
               }
            //TURN OFF
            //Toggle off high poly preview
            else{
               displaySmoothness -divisionsU 0 -divisionsV 0 -pointsWire 4 -pointsShaded 1 -polygonObject 1;
               PolyRemoveAllCrease;
               setAttr ($shape[0]+".smoothLevel") 2;
               }
           
    }
    
    smoothingThis($currentCrease,$shape);
       select $sel;
    
    
    
    
  • artquest
    Options
    Offline / Send Message
    artquest polycounter lvl 13
    Looks okay to me...
    The Toggle switch functions as it should
    http://i.imgur.com/QNPmhR6.png

    Do you have any global variables declared? Maybe try shutting and restarting Maya to clear them. *Shrug*
    You'll notice that the second object is a sphere and not a cube in your screen shot. They should both be matching. I'm getting the same result as you too! But each mesh should be getting the same operation applied to it. The script works great with a single object but not with multiple selected.
  • tkvtoons
    Options
    Offline / Send Message
    tkvtoons vertex
    Since you say it works perfectly when you have 1 object selected, I've modified your script to force it to do one object at a time. Give this a try. It seems to be working for me in Maya 2015 SP6.

    **EDIT: Fixed an error with iteration and selection.
    string $sel[] = `ls -sl`;
    string $shape[] = `listRelatives -type mesh $sel`;
    int $currentCrease = `getAttr ($shape[0]+".smoothLevel")`;

    // TURN ON
    if ($currentCrease == 0 || $currentCrease == 2)
    {
        for ($i=0; $i<(size($shape)); $i++)
        {
           if ($sel[$i]==""){break;}
           select $sel[$i];
           polySelectConstraint -m 3 -t 0x8000 -sm 1; // to get hard edges
           polyCrease -value 3.0;
           displaySmoothness -divisionsU 3 -divisionsV 3 -pointsWire 16 -pointsShaded 4 -polygonObject 3;
           setAttr ($shape[$i]+".smoothLevel") 4;
           polySelectConstraint -sm 0; // turn off edge smoothness constraint
           select $sel;
        }
    }
    //TURN OFF
    //Toggle off high poly preview
    else
    {
        for ($i=0; $i<(size($shape)); $i++)
        {
            if ($sel[$i]==""){break;}
            select $sel[$i];
            displaySmoothness -divisionsU 0 -divisionsV 0 -pointsWire 4 -pointsShaded 1 -polygonObject 1;
            PolyRemoveAllCrease;
            setAttr ($shape[$i]+".smoothLevel") 2;
            select $sel;
        }
    }



  • artquest
    Options
    Offline / Send Message
    artquest polycounter lvl 13
    Absolutely beautiful! Thanks so much TKVToons! This is one piece of a tool I'm working on to make hard surface modeling a ton faster :) Hopefully I can release it soon.
  • StephenVyas
    Options
    Offline / Send Message
    StephenVyas polycounter lvl 18
  • StephenVyas
    Options
    Offline / Send Message
    StephenVyas polycounter lvl 18
    ahh.. TKVtoons beat me to it.
  • tkvtoons
    Options
    Offline / Send Message
    tkvtoons vertex
    Stephen, I tried your script and it still wasn't working for me. Try running your script on 3 polyCubes, 2 of them will appear as spheres.

    And, no problem ArtQuest. Happy to help :)
  • StephenVyas
    Options
    Offline / Send Message
    StephenVyas polycounter lvl 18
    Yeah.. maybe i'll leave it up to you guys in the future, to sort out the errors ;)
    Still have a lot to learn 
  • artquest
    Options
    Offline / Send Message
    artquest polycounter lvl 13
    On a side note, does anyone perhaps know why exporting/saving through a script would cause maya to throw an error when trying to overwrite a fie?

    EDIT: actually it's not even writing the file the first time. it simply isn't there. I've checked the properties and it says it's read + write for my user.

    Maybe I'm going about this wrong. perhaps a cleaner way would be to duplicate the model instead of exporting it and hide it in the scene?
    The goal is to be able to dynamically swap back and forth between a light version of the mesh and a more detailed one. The only difference being that one mesh has holding edges applied automatically and one is the simple low poly version. I also want to be able to do this after saving and closing maya with hours/days inbetween sessions.
    Current code:

    //EXPORT
    file -force -options "v=0" -typ "mayaBinary" -pr -es "/AppData/Local/Temp/temp.mb";
    

    string $sel[] = `ls -sl`; polySelectConstraint -m 3 -t 0x8000 -sm 1; // to get hard edges polyBevel3 -fraction 0.5 -offsetAsFraction 1 -autoFit 1 -depth 1 -mitering 0 -miterAlong 0 -chamfer 0 -segments 1 -worldSpace 1 -smoothingAngle 30 -fillNgons 1 -mergeVertices 1 -mergeVertexTolerance 0.0001 -miteringAngle 180;
    polySelectConstraint -sm 0; // turn off edge smoothness constraint

  • tkvtoons
    Options
    Offline / Send Message
    tkvtoons vertex
    It looks like you are trying to save the temp.mb to a location that doesn't exist. I'm thinking Maya doesn't know where /AppData/Local/Temp/ is. If you change the save location to include a drive directory, the save will create the file and be able to be overwritten (something like "C:/Users/username/AppData/Local/Temp/temp.mb").

    Or you can use the root directory of your current workspace stored in a variable and save the file there:

    string $startingDir = `workspace -query -rootDirectory`;
    file -force -options "v=0" -type "mayaBinary" -preserveReferences -exportSelected ($startingDir+"temp.mb");



  • artquest
    Options
    Offline / Send Message
    artquest polycounter lvl 13
    tkvtoons said:
    It looks like you are trying to save the temp.mb to a location that doesn't exist. I'm thinking Maya doesn't know where /AppData/Local/Temp/ is. If you change the save location to include a drive directory, the save will create the file and be able to be overwritten (something like "C:/Users/username/AppData/Local/Temp/temp.mb").

    Or you can use the root directory of your current workspace stored in a variable and save the file there:

    string $startingDir = `workspace -query -rootDirectory`;
    file -force -options "v=0" -type "mayaBinary" -preserveReferences -exportSelected ($startingDir+"temp.mb");



    You are correct! Also I got it working. But I'm going to need to change how I do this because using the file export breaks the undo stack in maya.

    I would love any suggestions you have, allow me to redirect you to the full (beta) release thread of the tool. Thanks to you I have all the basic functionality working and ready to go. :)


    http://polycount.com/discussion/168879/new-tool-for-maya-autohighpoly-beta#latest

  • tkvtoons
    Options
    Offline / Send Message
    tkvtoons vertex
    artquest said:
    You are correct! Also I got it working. But I'm going to need to change how I do this because using the file export breaks the undo stack in maya.
    Undo still seems to work for me in Maya 2015. Here's what I do --> make 4 polyCubes, run the 2 lines of export script, undo until I only have 1 cube. Are you doing something else before or after exporting that would cause the undo to not work?

    Maybe try this? (I've used something like this when undo stacks would break in my scripts).
    string $startingDir = `workspace -query -rootDirectory`;
    $undoState = `undoInfo -query -state`;
    undoInfo -stateWithoutFlush false;
    catch(`file -force -options "v=0" -type "mayaBinary" -preserveReferences -exportSelected ($startingDir+"temp.mb")`);
    undoInfo -stateWithoutFlush $undoState;



Sign In or Register to comment.