LDraw.org Discussion Forums
Thoughts on animation scripting language - Printable Version

+- LDraw.org Discussion Forums (https://forums.ldraw.org)
+-- Forum: LDraw Programs (https://forums.ldraw.org/forum-7.html)
+--- Forum: LDraw Editors and Viewers (https://forums.ldraw.org/forum-11.html)
+--- Thread: Thoughts on animation scripting language (/thread-12368.html)

Pages: 1 2 3 4


Re: Thoughts on animation scripting language - Ignacio Fernandez Galvan - 2014-02-19

Tore Eriksson Wrote:
Roland Melkert Wrote:Is your LDA information online somewhere as I'm very open to suggestions.

No. I'm afraid most of it, like the latest tutorials and examples, were lost when I ended the contract with the ISP who hosted the webpages I stored them at.

It might be possible to find it in the Wayback Machine (http://archive.org/web/), if you remember the address.


Re: Thoughts on animation scripting language - Roland Melkert - 2014-02-19

Thanks for sharing,

If I understood correctly your animation sequence does interpolation based on start and end angles over subsets of time (similar to the move to action in LD4D).

I think the LUA approach I hinted on in the big example script a few posts above is kinda similar, with one big difference everything can be defined by the user on top of a default set of base objects (e.g. group, matrix and type 1 line -wrappers). This is possible because when you do e.g. a=b, a will inherit all functions and data from b, and you can then create additional functions for a without changing b (standard out of the box LUA). So you could do different things for arms then you would do for legs etc. But all still get the basic API stuff from the group object.

And in the end you use all that to describe the movements based a global timer (like your LDA). But you could also use the custom functions to do e.g. position look-ups (stop animation), or real-time kinetics (no timer, just responses to external actions).

I might be a bit rambling here, because I'm still in the dreaming up stage of it all Smile But again thanks for sharing your thoughts, I'll take a closer look at LDA once I'm ready to start 1.4 for real (I need to fix a couple of bugs in 1.3 first in order to get beta 2 ready).


Re: Thoughts on animation scripting language - Sergio Reano - 2014-02-20

Hi,

there is another face of the problem you have to consider: in most technic models the connections among parts are not so easy to understand so it may be a bit hard to understand wich parts move and wich not if the program itself is not able to recognize at least that.

Furthermore the same parts can move differently depending on the rotation axle or if they are part of quadrilateral structure and so one.
Defining the rotation and scripting of a steering system may be relatively easy, but describing the movement of a deformable quadrilateral structure (or of a Theo Jensen mechanism!) will be really, really hard.

Obviously these considerations are nearly irrilevant for animating a minifig, a simple vehicle or other simple moving structures.

Sergio


Re: Thoughts on animation scripting language - Roland Melkert - 2014-02-20

Yes this is exactly why I want to go the all scripting route. It redirects the problem to the user whom might have way better understanding of the things they want to animate.

I just need to setup a decent environment and api's so they can access all the stuff you would need when hard coding solutions. I know this makes it all a bit user unfriendly but I always targeted LDCad to the more experienced LDraw users anyway.


Re: Thoughts on animation scripting language - Roland Melkert - 2014-03-08

I've been playing around with lua for a couple of weeks now, and managed to set up a decent basic interface with the LDCad internals.

The below lua script is my current test script, everything in it actually works (compile/runtime wise) and I was wondering if it is somewhat 'understandable'. Or do people think it's to lowlevel?

Code:
--Initialization----
function register()

  local myAni=ldc.animation('Test animation 1');
  myAni:setFPS(25);
  myAni:setLength(2); --seconds
  myAni:setEvent('frameChange', 'frameChange');
  myAni:setEvent('play', 'play');

  myAni=ldc.animation('Test animation 2');
  myAni:setFPS(25);
  myAni:setLength(10); --seconds
  myAni:setEvent('frameChange', 'frameChange');
  myAni:setEvent('play', 'play');
end

--ldc.setPrint2Log(true);
--ldc.setPrint2Console(true);
print('apiVersion: ',ldc.getApiVersion());
register();



--Animation functions-----
function play()

  doAltTest=false; --in order to test the matrix class wrapper
  totRotCnt=2; --nr of rotations within the animation length

  --try to bind upto 3 groups
  grp1=ldc.group('Group 1');
  grp2=ldc.group('Group 2');
  grp3=ldc.group('Group 3');

  local ani=ldc.animation.getCurrent();
  print('fps: ', ani:getFPS(), ', frameCnt: ',ani:getFrameCnt());
end

function frameChange()

  local ani=ldc.animation.getCurrent();
  local angle1=ani:getFrameNr()/ani:getFrameCnt()*totRotCnt*360;

  if (grp2:isLinked() and grp3:isLinked()) then

    --full gear rotation test
    local angle2=-0.5*angle1+(360/16/2);
    local angle3=-0.2*angle2+(360/40/2);

    print('frame: ',ani:getFrameNr(),', angle1: ',angle1, ', angle2: ', angle2, ', angle3: ', angle3);

    local ori=ldc.matrix();
    ori:mulRotateAB(angle1, 0, 0, 1);
    grp1:setOri(ori);

    ori:setRotate(angle2, 0, 0, 1);
    grp2:setOri(ori);

    if doAltTest then

      --rel rotation test (excuse to play more with matrix and vector userdata)
      pos=ldc.vector(90, 40, 70);
      mat=ldc.matrix();
      mat:mulTranslateAB(pos);
      mat:mulAB(ori);
      pos:scale(-1);
      mat:mulTranslateAB(pos);
      grp3:setPosOri(mat);
    else
      ori:setRotate(angle3, 0, 0, 1);
      grp3:setOri(ori);
    end

  else

    --single rotating group test
    local ori=ldc.matrix();
    ori:mulRotateAB(angle1, 1, 0, 0);
    grp1:setOri(ori);

  end

end

I'm also thinking to add a matrixstack object, so you could use that similar to the OpenGL intermediate mode.

Anyhow if anyone has additional suggestions for api functions/functionality I'm open to suggestions.


Re: Thoughts on animation scripting language - Ignacio Fernandez Galvan - 2014-03-09

Not knowing anything about lua, I think it's quite clear, but I generally like this kind of low-level control.

As for suggestions, it's difficult without having something to play with. Have you planned access/interaction with path data? I mean not only changing control points, etc., but for example measuring path length or setting a position/orientation along a path. Any helping functions you can add for things like the mechanism in the first of these models would be welcome too (ball joints, fixed lengths, sliding axles).


Re: Thoughts on animation scripting language - Roland Melkert - 2014-03-09

Quote:Have you planned access/interaction with path data? I mean not only changing control points, etc., but for example measuring path length or setting a position/orientation along a path.

I'm not sure changing the dynamic parts through script will be in the first version, as it needs some changes to the way they are regenerated at the moment. But they will be scriptable in the long run. Or do you mean a spline like function for plotting e.g. the route of a minifig? If so there are probably already lua libraries for that. I've set up the ldc.matrix object so it also excepts 12 numbers (ldraw style matrix), that way you can link it to any third party lua library doing (more complicated) matrix/quat/etc math.


Quote:Any helping functions you can add for things like the mechanism in the first of these models would be welcome too (ball joints, fixed lengths, sliding axles).

At the moment I'm working with the idea to keep the C++ callback api very basic, so a special lua object for common objects (matrix, group, ldraw ref line, etc). On top of that you could build pure lua libraries which might 'hide' the internal matrix stuff and let people work with just angles / predefined functionality (e.g. steering rack) etc.

That said I do plan to add special (joint) info to groups which would expose e.g. the rotation vector for a gear/minfig arm to the scripting side so you don't have to know the exact placement of stuff to rotate a minifig arm. The additional group info would also allow for interaction with those 'joints', the script could then handle realtime dependency calculations.


Thanks for the input


Re: Thoughts on animation scripting language - Michael Horvath - 2014-06-01

I also hope there will be compatibility between the scripting language and POV-Ray through the use of an exporter or converter. Not sure what extra work that will entail. POV-Ray script is a bit weird about some things (no true functions, no object model...).


Re: Thoughts on animation scripting language - Roland Melkert - 2014-06-01

At the moment I'm not taking POV-Ray into account at all.

But I'm planning to add an POV-Ray export option add some point (based on the one from LD4DStudio), probably in version 1.5.


Some progress (was re: Thoughts on ani...) - Roland Melkert - 2014-07-07

Hi all,

I've been playing around with the scripting functionality this weekend after having taken a break for some time.

So I wanted to show the progress as I'm still open to suggestions on the api / core functions available to to end user.

Short clip of a test animation

This animation uses the new direct LDraw ref line access. This makes it possible to do some mass mutating fun stuff e.g. effecting every brick in a model.

The lua source code for this information is shown at the end of the clip (has clearer highlighting), but I'll give it here too:

Code:
function limit(v, min, max)

  if v<min then
    v=min
  end
  
  if v>max then
    v=max
  end
    
  return v;
end

function register()

  --global defs
  rcktTime=0;  --timespan (seconds) for rocket to get to target
  exTime=3;    --timespan for explosion (seconds)
  totTime=rcktTime+exTime;
  exSpeed=250;
  grav=250;
  
  --create/link animation obj
  local myAni=ldc.animation('Weapons test');
  myAni:setLength(totTime); --seconds
  myAni:setFPS(25); --low fps for screencap
  myAni:setEvent('play', 'preCalcDeps');
  myAni:setEvent('frameChange', 'calcFrame');
end

function preCalcDeps()

  --Link submodels and their main refs
  local sfMain=ldc.subfile();
  local refShack=sfMain:getRef('shack.ldr');
  sfShack=refShack:getSubfile();
  local refDude=sfMain:getRef('dude.ldr');
  local sfDude=refDude:getSubfile();
  local refWeapon=sfDude:getRef('rocketLauncher.ldr');
  
  --Link needed groups
  grpRocket=ldc.group('rocket');  
  local grpTarget=ldc.group('Group 1'); --helper grp so user can drag target around

  --get floor from first tree ref
  floor=sfMain:getRef('3470.dat'):getPos():getY();

  --target
  local impCen=ldc.vector(grpTarget:getPos());
  grpTarget:setVisible(false); --hide helper part(s)
  
  --point dude to target
  -- todo
  local ori=ldc.matrix();
  local angle=45;
  ori:setRotate(angle, 0, -1, 0);
  refDude:setOri(ori);
  
  --Prep the pos and direction vectors for all refs in the shack model
  refPos={}
  refDir={}
  refCnt=sfShack:getRefCount();
  for i=1, refCnt do
    refPos[i]=ldc.vector(sfShack:getRef(i):getPos());
    local tmp=ldc.vector(refPos[i]);
    tmp:sub(impCen);
    tmp:normalize();
    refDir[i]=ldc.vector(tmp);
  end  
end

function calcFrame()

  local curAni=ldc.animation.getCurrent();

  local curTime=curAni:getFrameNr()/curAni:getFrameCnt()*totTime;
  if curTime<rcktTime then
    --Rocket animation
  else
    --explode the shack
    for i=1, refCnt do
      local dx, dy, dz=refDir[i]:get();
      local x, y, z=refPos[i]:get();
      
      x=x + curTime*exSpeed*dx;
      y=y + curTime*exSpeed*dy + 0.5*grav*curTime*curTime;;
      z=z + curTime*exSpeed*dz;
      
      --x=limit(x, -500, 500);
      y=limit(y, -10000, floor);
      --z=limit(z, -500, 500);

      local ref=sfShack:getRef(i);
      ref:setPos(x, y, z);
    end
  end
end

register();