Noob question

Hi,
I'm new to maxscript and I managed to make this little script to attach every selected objects together.

	objArr = for i in $ collect i 
	mainObj = objArr[1]
	count = objArr.count
 
	undo off(
 
		convertTo objArr Editable_Poly	
 
		for i = 2 to count do (
 
		polyOp.attach mainObj objArr[i]
 
		)							
	)

I used google a lot to make this first script and I don't quite understand that part:

objArr = for i in $ collect i 

if I used only $ instead to select my objects, only one object on 2 was attached...

Can someone please help me understand why it was doing that and what this for loop does exactly?

Thank you!

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dekorkh's picture

the reason objArr = $ only merges 2 objects is...

the reason objArr = $ only merges 2 objects is because in this case objArr is assigned the reference (points to) the actual $selection which changes dynamically depending on what you have selected.
objArr = $ as array or objArr = selection as array or collect loop that you used all will return an array of references to the objects themselves and will not change with your selection, which is what you need.

phantomx's picture

thanks for the hint, I

thanks for the hint, I understand that better :)

miauu's picture

Read this thread. Don't use

Read this thread.

Don't use $, use this:

objArr = <span style="color: #b1b100;">for</span> i in selection as array collect i
but this for loop is unnecessary. You don't need to loop through all objects to make an
array, because Selection as array will give you what you want

        mainObj = objArr<span style="color: #66cc66;">[</span>1<span style="color: #66cc66;">]</span><br><br>              objArr = selection as array
              count = objArr.count
&nbsp;
	undo off<span style="color: #66cc66;">(</span>
&nbsp;
		convertTo objArr Editable_Poly	
&nbsp;
		<span style="color: #b1b100;">for</span> i = 2 to count <span style="color: #b1b100;">do</span> <span style="color: #66cc66;">(</span>
&nbsp;
		polyOp.attach mainObj objArr<span style="color: #66cc66;">[</span>i<span style="color: #66cc66;">]</span>
&nbsp;
		<span style="color: #66cc66;">)</span>							
	<span style="color: #66cc66;">)</span><br>
Arahnoid's picture

Just deleted html tags

Miauu's code without html tags ;)

mainObj = objArr[1]
objArr = selection as array
count = objArr.count
undo off
(
	convertTo objArr Editable_Poly	
	for i = 2 to count do polyOp.attach mainObj objArr[i]
)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.