Link Array (directional)

-14 votes
Version: 
1
Date Updated: 
02/07/2012
Author Name: 
JokerMartini

This code simply allows a users to link a selection of objects based on the order of selection. You can use either 0 or 1 to define the direction. That allows you to link everything forwards or backwards. Rather simple code but I figured someone would find use for it.

clearlistener()
 
fn fnLinkArray arr dir:0= (
 
	if dir != 0 then (
		reverseArr = for i = arr.count to 1 by -1 collect arr[i]
		arr = reverseArr	
	)
 
	for o in arr do o.parent = undefined
 
	for i = 1 to (arr.count-1) do
	(
		child = arr[i]
		parent = arr[i+1]
		child.parent = parent
		print (child.name + "---->" + parent.name)
	)
)
 
fnLinkArray selection dir:0
fnLinkArray selection dir:1

Comments

Comment viewing options

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

I'm fine, thanks

Yeah, thats the idea to make the functions separate. This way they become re-usable for other scripts, the base logic then build function library.

my recent MAXScripts RSS (archive here)

JokerMartini's picture

Hey Anubis - how's it going?

You definitely simplified it.
I had originally have two separate functions, one which reversed the array. I then decided to combine it into one.
I think it makes more sense to have it separate like you have posted above.

Essentially could make one function which just links the given array.
Then use other functions to build arrays out of selections.
That way users could use other functions to build arrays based on options, example being qsort based on distance from an object.
Then the user could decide whether or not they want to reverse it before running the linking function.

Just some ideas.

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

Anubis's picture

Hey John

I hope its welcome to append similar function...

-- simple function
fn LinkArray arr = (
	for i = 1 to (arr.count - 1) do
	append arr[i].children arr[i + 1]
)

And if the main idea is to make it working with optional direction argument...

fn ReverseArray arr = ( -- operate on input array
	local delta = arr.count/2, zeta = arr.count+1
	for i = 1 to delta do swap arr[i] arr[zeta-i]
)
 
fn LinkArray arr = ( -- just link in order
	if arr.count > 0 do (
		arr.parent = undefined
		for i = 2 to arr.count do
		arr[i].parent = arr[i-1]
	)
)
 
-- mixin version with optional direction argument 
-- (used above 2 functions)
fn LinkArrayInDir arr dir:0 = (
	if dir != 0 do ReverseArray arr
	LinkArray arr
)

Cheers!

my recent MAXScripts RSS (archive here)

Comment viewing options

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