Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Object name from variable

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Anonymous
3621 Views, 2 Replies

Object name from variable

Hi everyone !

 

I don't know much about coding and I have a little problem. I have several objects with the same name, just a different number (objet_1, object_2, object_3...etc) and I want to refer to one of them according to a string $i that equals the number of time the code is reapeting itself, like : object_($i) for object_3 when $i = 3. And if possible with a calcul like : object_($i+1) for object_5 when $i = 4. I just don't know the right syntax...

 

Is that posible ? If someone could help me it would be really great ! 

 

Thanks anyway !

2 REPLIES 2
Message 2 of 3
saihtam
in reply to: Anonymous

There is a couple of ways of doing this. Which approach you want to take depends on what you need your code to do. I'd recommend you do some more research on basic MEL. Check out this site. It's a horrible looking 90's website, but has a ton of excellent information.

 

Using a counting for loop with an array:

{
    string $objects[] = `ls -tr "object_*"`;
    
    for($i=0;$i<size($objects);$i++)
    {
        string $currentObject = $objects[$i];
        
        print "\n";
        print $currentObject;
    }
}

Using a counting for loop to construct strings:

{
    for($i=0;$i<10;$i++)
    {
        string $currentObject = ("object_"+($i+1));
        
        print "\n";
        print $currentObject;
    }
}

 

Using an object for loop:

{
    string $objects[] = `ls -tr "object_*"`;
    
    for($object in $objects)
    {
        print "\n";
        print $object;
    }
}
- Mathias
Message 3 of 3
Anonymous
in reply to: saihtam

Thanks a lot, that's really helpful !

It works now thanks to you, IT'S ALIVE !

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report