--- biped related functions library
-- fns only work on single items
-- add mapping manually

--IsBip = is object a biped object?
--IsPartOfbip = is object part of a biped hierarchy?
--ParentBipOf = gets root Bip node of passed object
--HideBipDummies = hides dummies in passed biped
--BipStrip = returns part name sans bip prefix
--Opposite = returns opposite part (hand, foot, etc)
--SelOpp (select Opposite) -- selects opposite part
-- SelectBoth -- selects symmetrical parts
-- bipPart -- returns bip object given root (name or object)  and part name

fn IsBip obj = (classof obj == biped_object)
-------------------------------------------------------------------------
fn IsPartOfBip obj =
	(
	if classof obj == objectset do return undefined
	if obj == undefined do return undefined
	if classof obj == biped_object then
		(
		return true
		)
	else
		if obj.parent == undefined then 
			(
			return false
			)
		else
			(
			tempobj = obj.parent
			while tempobj != undefined do
				(			
				if classof tempobj == biped_object do (return true)
				tempobj = tempPaobj.parent
				)
			return false	
			)	
	)
-------------------------------------------------------------------------
fn ParentBipOf obj = 
	(
	if classof obj == objectset do return undefined
	if obj == undefined do return undefined
	if obj.parent == undefined and classof obj == biped_object do (return obj)
	tempobj = obj.parent
	while tempobj != undefined do
			(
			if classof tempobj == biped_object and classof tempobj.parent != biped_object do (return tempobj)
			tempobj = tempobj.parent
				)
			return undefined
		
		)
-------------------------------------------------------------------------
fn BipStrip obj = 
	(
	if classof obj == objectset do return undefined
	if obj == undefined do return undefined
	longname = obj.name as string
	root = (ParentBipOf obj).name as string
	if findstring longname root != undefined then
		(return substring longname (root.count + 2) 999)
	else
		(
		return longname
		)
	)
-------------------------------------------------------------------------
fn Opposite obj = 
	(
	if classof obj == objectset do return undefined
	rootname = obj.name
	findL = findstring rootname " L "
	findR = findstring rootname " R "
	case of
		(
		(findL == undefined and findR == undefined): return undefined
		(findL == undefined):	(
								newname = replace rootname findR 3 " L "
								)
		(findR == undefined): 	(
								newname = replace rootname findL 3 " R "
								)		
		)
	candidates = for items in (ParentBipOf obj) where items.name == newname collect items
	if candidates.count != 1 then 
		(return undefined)
	else
		(return candidates[1])
	)
-------------------------------------------------------------------------
fn HideBipDummies bip =
	(for items in bip where classof items != biped_object do hide items)
-------------------------------------------------------------------------
fn SelOpp BipObj =
	(if IsPartOfBip BipObj do 
			(
			try (select (Opposite BipObj))
			catch (return "Object had no opposite")
			)
	)
--------------------------------------------------------------------------
fn SelectOpposite BipObj = (SelOpp BipObj)			
--------------------------------------------------------------------------
fn SelectBoth BipObj = (try (selectmore (Opposite BipObj))
						catch (return "no opposite passed")
						)
---------------------------------------------------------------------------
fn BipPart Root Part =
	(
	if classof Root == Biped_Object then Root = (Root.name)
	if part == undefined or part == "" then
		(
		test = for items in objects where (items.name as name) == (Root as name) collect items
		)
	else	
		(
		test = for items in objects where (items.name as name) == ((Root + " " + Part) as name) collect items
		)
	case test.count of
			(	0: return undefined
				1: return test[1]
				default:(format "too many matching items\n"; return test[1])
			)
	)