ScriptSpot

Script

Link Array (directional)

By JokerMartini · 2012-02-07

Version
1
Date updated
2012-02-07
Votes
14
Description

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

Tags: rigging, hierarchy, linking

Comments (3)

I'm fine, thanks

Anubis · 2012-02-08

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.

Hey Anubis - how's it going?

JokerMartini · 2012-02-07

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.

Hey John

Anubis · 2012-02-07

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!