Notifications
Clear all

[Closed] Forced to use the selection command?

Hey all.

I’m having an issue where I need to be able to select a number of objects, and then run a certain function on each object that requires it to actually be selected. It’s the exporter for an engine im working with and it uses the export selected flag, so I believe I’m forced to have to use ‘select node[i]’ and then do the export, then move to the next file. Lets just assume that this is unchangable. How can I actually do this?


  initialselection = $
  
  for i = 1 to initialselection.count do (
   select initialselection[i]
   --now i will do an export selected command through a certain engine's exporter
   --instead of that, ill just print the selection name for the purposes of this test
   print initialselection[i].name
  )
  

It errors on the second time through, saying that initialselection[i] is undefined when i is 2. The initial selection array is definitely filled with more than 1 object in the beginning but doing that selection in the loop seems to be killing it. How should I do this? It’s ugly and undesirable but im just looking for a working hack. Thanks.

7 Replies

initialselection = $ as array

Try that first …it takes a snapshot of the selection before the loop starts, otherwise it’ll update the selection every iteration.

initialselection = selection as array
  
  for i = 1 to initialselection.count do (
   select initialselection[i]
   --now i will do an export selected command through a certain engine's exporter
   --instead of that, ill just print the selection name for the purposes of this test
   print initialselection[i].name
  )

Try it now.

Nice.

What I don’t understand is why the line outside the for loop that gets the selection is evaluated again during the for loop… What happened to the logical code progression?

I doesn’t get evaluated again. Basically when you do:

initialselection = $

You are telling Maxscript that you want your variable to hold whatever it’s in the selection. Thus the contents on the variable will change if the selection changes. Think of it as creating an “instance” of the $ variable.

However when you do:

initialselection = $ as array

You are effectively making a “copy” of whatever is in the current selection. So even if the actual selection changes, the contents of the variable will remain intact.

Cool.

 Yet it doesn't make a pointer when accessing the count property.

 numberOfSelections = $.count
--at the time, the count was 10
select $Object01
--count is now 1
print numberOfSelection
--prints 10 , not 1
 So does it only do pointers when talking about objects and not properties? I didn't need to do $.count as integer in this case to force a 'copy'.

Mostly the problem is the $/selection variable that has some special rules you have to consider…

This has to do with the way arrays are handled in max vs. scalars. Array assignment always creates an instance whereas scalar assignment creates a ‘copy’. Most high-level languages do this because you have no idea how big an array might be.

When you cast “selection” to an array, you’re making a copy of its contents.