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

sort according to z position

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
alican_90
591 Views, 4 Replies

sort according to z position

hello;

 

i am new in scripting. i am trying to sort my objects in an array according to their z position value. İ mean have 10 objects in the scene and they are randomly positioned in z axis. How can i manage to sort them in an array according to their z position value

 

thanks

4 REPLIES 4
Message 2 of 5
blakestone
in reply to: alican_90

You can use qsort to define your own functions and how you want your array sorted - below is sorting the selection based on the z position

 

(
	local my_selection = selection as array
	
	fn sort_z v1 v2 = (
		local result = v1.position.z - v2.position.z
	)
	
	qsort my_selection sort_z
	
	for i in my_selection do print i
)
--------------------------------------------------------------------------------------
Technical 3D Graphic Artist
Autodesk 3dsMax 2015 - Service Pack 4
--------------------------------------------------------------------------------------
Message 3 of 5
alican_90
in reply to: blakestone

thank you but how can i reverse qsort array ?
Message 4 of 5
alican_90
in reply to: alican_90

i mean in reverse direction
Message 5 of 5
blakestone
in reply to: alican_90

v1 - v2 = accending || v2 - v1 = decending... I have adjusted the script so you can simply set the mode: 1 = accending || 2 = decending

Hopefully this makes sense?

 

 

(
	local my_selection = selection as array
	local mode = 1	-- *** 1 = accending | 2 = decending ***
	
	fn sort_z v1 v2 = (
		
		if (mode == 1) then local result = v1.position.z - v2.position.z
		else if (mode == 2) then local result = v2.position.z - v1.position.z
	)
	
	qsort my_selection sort_z
	
	for i in my_selection do print i
)

 

--------------------------------------------------------------------------------------
Technical 3D Graphic Artist
Autodesk 3dsMax 2015 - Service Pack 4
--------------------------------------------------------------------------------------

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

Post to forums  

Autodesk Design & Make Report