/* CMD: Path to Motion
 * Convert Path in layer to LW Motion file.  All points in layer are used,
 * in the order they were created (??)
 * By Arnie Cachelin © 1993 NewTek Inc.
 */
/* Thu Feb  4 14:51:15 1993 */

NUMERIC DIGITS 6
address "LWModelerARexx.port"
call addlib "LWModelerARexx.port", 0
SIGNAL ON ERROR
SIGNAL ON SYNTAX
call addlib "rexxsupport.library", 0, -30, 0
sysnam = 'Path to Motion '
version = 'v1.0'
/* Set up requester */
call req_begin sysnam

id_frames  = req_addcontrol("Frames", 'n')
id_name = req_addcontrol("Motion Name", 's')
id_skip1 = req_addcontrol("Skip First Point", 'b')
id_skipn = req_addcontrol("Skip Last Point", 'b')
call req_setval id_frames, 30
call req_setval id_name,'ModelerPath'

/* Post requester, exit on cancel. */
if (~req_post()) then do
    call req_end
    exit
end
req=1

/* Get input from Requester */
frames   = req_getval(id_frames) % 1
File.name = 'RAM:'req_getval(id_name)
skip1= req_getval(id_skip1)
skipn= req_getval(id_skipn)

call req_end
req=0
say skip1 skipn
/* Done Getting Data! */
h=0; p=0; b=0
xsc=1; ysc=1; zsc=1
File.type="LWMO"
n = xfrm_begin()
  if (n-skip1-skipn)>frames then frames=n-skip1-skip2
  Keys=n-skip1-skipn
  KeySpacing=(Frames/Keys)%1+1
  call WriteHeader(mot,keys)
  do i=1+skip1 to n-skipn
    parse value xfrm_getpos(i) with x y z .
    k=(i-1-skip1)*keyspacing
    call WriteKey(mot,k,0)
    end
call xfrm_end
call close mot
call notify(1,'!Created Motion File: 'File.name)
exit

WriteKey:  PROCEDURE  EXPOSE x y z h p b xsc ysc zsc
  arg MotFile, frame, lin
  channels=x y z h p b xsc ysc zsc
  spline=frame lin "0.0 0.0 0.0"
  say channels spline
  call writeln(MotFile,channels)
  call writeln(MotFile,spline)
  return frame

WriteHeader: PROCEDURE EXPOSE File.
  arg MotFile, Keys
  if open(MotFile,File.name,'W') then do
    call writeln(MotFile,File.type)
    call writeln(MotFile,"1") /* magic # */
    call writeln(MotFile,"9") /* Channels */
    call writeln(MotFile,Keys)
  end
  else do
    Bummer("Can't open Motion file : "File.name)
  end
  return 1

Bummer:
 ARG etxt
 SAY "ERROR: "||RC||etxt
SYNTAX:
ERROR:
  say 'Sorry, Error #'RC' on line 'SIGL' has been detected.'
  if etxt=''| etxt='ETXT' then etxt= errortext(rc)
  say sourceline(SIGL)
  t=notify(1,'!Total Bummer Dude!','!An error has been detected.','@'etxt,'Line 'SIGL)
  exit
