ScriptSpot

Forum topic · General Scripting

Noob question

By phantomx · 2012-01-14

Description

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 (4)

dekorkh · 2012-01-15

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 · 2012-01-14

thanks for the hint, I understand that better :)

Read this thread. Don't use

miauu · 2012-01-14

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>


Just deleted html tags

Arahnoid · 2012-01-14

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]
)