Home Technical Talk

[Melscript] Removing The NameSpace from an Array Item

StephenVyas
polycounter lvl 18
Offline / Send Message
StephenVyas polycounter lvl 18
** nevermind***
2 mins after I posted this .. I figured out that I don't need to put "String" infront of the array
D'oh!"

************


What I'd like to do is remove the Namespace characters from the selected items in an array.
So far, I've done exactly that.
I've successfully removed the namespace name from the selected object, but now I'm having trouble putting that name back into the Array.

Last month I spent learning Python, so now my mind is twisted around.
Please help me to clean up this mess! :icon60:
string $array[] = `ls -sl`; 


for($node in $array){
    $arraynumber = 0;
    string $buffer[];
    tokenize $node ":" $buffer;
    string $newname = $buffer[size($buffer)-1];
   string $array[$arraynumber] = $newname;   // <<< This obviously isn't the correct syntax. 
  // print $array[$arraynumber];
  // print $newname;
    $arraynumber = $arraynumber + 1;
}

The error in the code is::
"Cannot cast data of type string to string[]"

Replies

  • passerby
    Options
    Offline / Send Message
    passerby polycounter lvl 12
    if your more familiar with python why not do it in python than?
    import pymel.core as pm
    
    sel = pm.selected()
    for i in sel:
        i.rename(str(i).split(":")[-1])
    
    or with list comprehensions
    import pymel.core as pm
    [i.rename(str(i).split(":")[-1]) for i in pm.selected()]
    

    EDIT: i noticed you didnt rename things in the mel code so if you just want the names in a list you could do this.
    import pymel.core as pm
    names = [str(i).split(":")[-1] for i in pm.selected()]
    print names
    
  • StephenVyas
    Options
    Offline / Send Message
    StephenVyas polycounter lvl 18
    passerby wrote: »
    if your more familiar with python why not do it in python than?

    Thanks Passerby! I appreciate the time & effort you put in doing this. I'll likely use your code for future reference

    Though, the only reason why I had to do it with melscript, rather than Python, was it had to fit into a larger chunk of mel that I had written a couple months ago
Sign In or Register to comment.