Home Technical Talk

Declaring arrays in a for loop

I've been trying to get my head around some max script where I have a loop and I want to create and populate some arrays within the loop. I already have an array which I can use to name.
-- get all object names
Allobjects = for g in geometry collect g.name
-- get base object names
for a=1 to Allobjects.count do
(
	s = filterString Allobjects[a] "z"
	appendIfUnique NamesArray s[1]
)

for n=1 to NamesArray.count do
(
        tempArray[n] = #()
	for g in geometry do
	(
		if (findstring g.name NamesArray[n] !=undefined) do append tempArray[n] g
	)
	print tempArray[n]
)

I know that tempArray[n] = #() doesn't work but I thought it's pretty clear what I'm trying to do there.

The backstory:- I have a scene with objects that also have copies in it named in the following fashion aaaaaZ,aaaaaZ01,aaaaaZ02,bbbbbZ,bbbbbZ01,bbbbbZ02. Where aaaaaZ and bbbbbZ are different objects and things with 01, 02 are copies of the original objects. My objective is to have an array for each object which stores the object and it's copies.

So:

aaaaaArray = #(aaaaaZ,aaaaaZ01,aaaaaZ02...)
bbbbbArray = #(bbbbbZ,bbbbbZ01,bbbbbZ02...)

The array should contain the actual objects not just their names as I want to do further things with them later. I hope that's clear in what I'm trying to achieve.

PS the Z in the object name is there purely so it's easy to pick out the copies. Some of the object names will have numbers at the end so having a Z helps here.

Replies

  • miauu
    Options
    Offline / Send Message
    miauu polycounter lvl 14
    aaaaaArray = $aaaaaZ*
    print aaaaaArray 
    
    bbbbbArray = $bbbbbZ*
    print bbbbbArray
    

    The array contain the actual objects.
  • eddybrown
    Options
    Offline / Send Message
    miauu wrote: »
    aaaaaArray = $aaaaaZ*
    print aaaaaArray 
    
    bbbbbArray = $bbbbbZ*
    print bbbbbArray
    
    The array contain the actual objects.

    Cheers dude, that allowed me to access the objects quicker than the string filtering but I was after generating the array names dynamically. I'm not sure if that's possible so I managed to get around it by using a temporary array and filling that in a loop instead. It means I have to do a few more calculations each time it loops through but I can't see a way around it apart from maybe using execute which I didn't really want to do.
  • miauu
    Options
    Offline / Send Message
    miauu polycounter lvl 14
    You can post the new code and maybe someone can help you. :)
  • LoTekK
    Options
    Offline / Send Message
    LoTekK polycounter lvl 17
    As far as I know, execute() would be the only way to dynamically generate (and subsequently access) variable names (I tend to abuse the hell out of it to generate my rollouts).
  • cw
    Options
    Offline / Send Message
    cw polycounter lvl 17
    I avoid using execute if possible, just on principle. There are very few cases where it is strictly necessary.

    This is how I would do what you are trying:
    b = for obj in (selection as array) where (matchpattern obj.name pattern:"sphere*") collect obj
    

    this collects the stuff in the loop which matches the criteria after the 'where' clause into the array b.
  • eddybrown
    Options
    Offline / Send Message
    Thanks that's pretty neat, it's better than what I did:
    NamesArrayCopy[p] = ("$" + NamesArray[p] + "*")
    TempArray = (execute NamesArrayCopy[p]) as array
    

    I'd read that using execute should be avoided unless really necessary but I don't know the reasons why. I'm guessing performance.
Sign In or Register to comment.