Animating a part

Need help, or want to share a macro? Post here!
User avatar
iplayfast
Posts: 47
Joined: Sat Sep 07, 2019 6:55 am

Animating a part

Postby iplayfast » Sat Nov 09, 2019 6:03 pm

OpenSCAD has a method of animating where a value is changed over time. This value is sent to the angle of a gear or whatever which is then redrawn. The effect is of the gear or whatever moving over time.

Does FreeCad have anything similar to this? I'd love to see my gearbox turning.

Thanks
chrisb
Posts: 19661
Joined: Tue Mar 17, 2015 9:14 am

Re: Animating a part

Postby chrisb » Sat Nov 09, 2019 7:00 pm

Yes, you can do this in Python, usually by setting a constraint and recompute the model. Animation workbench can help.
chrisb
Posts: 19661
Joined: Tue Mar 17, 2015 9:14 am

Re: Animating a part

Postby chrisb » Sat Nov 09, 2019 7:15 pm

Here is a script used for animating a one cylinder engine. "kw" is the shortcut for "crankshaft". Ask back if you need more information.

Code: Select all

from PySide import QtCore

step = 30.0
end  = 720
timeout = 10
kwStartAngle = 0
kwAngle = 0

timer = QtCore.QTimer()

def turn(a):
  constraintNr = 4
  # save global value in case this function is called from the console
  global kwAngle
  kwAngle = a
  App.ActiveDocument.Sketch.setDatum(constraintNr,App.Units.Quantity(str(-kwAngle)+'  deg'))
  App.ActiveDocument.recompute()

def update():
  global kwAngle
  print(kwAngle)
  if kwAngle >= end:
    # reset all to the origin and stop
    kwAngle = kwStartAngle
    turn(kwAngle)
    timer.stop()
  else:
    kwAngle += step
    turn(kwAngle)
#    genframe() # Bild erzeugen

timer.timeout.connect(update)
timer.start(timeout)
User avatar
iplayfast
Posts: 47
Joined: Sat Sep 07, 2019 6:55 am

Re: Animating a part

Postby iplayfast » Sat Nov 09, 2019 8:22 pm

Thanks, that gives me the counter. Now for the other side.
I've added an attribute InAngle which specifies the angle of the EccentricShaft.

So do I constantly create the part, with the new rotation, or can I (preferably) just modify how the part is displayed?

In the FreeCAD python console, after adding the gear

Code: Select all

>>>doc = FreeCAD.ActiveDocument
>>>es = doc.getObject('EccentricShaft')
>>>es.Placement
Placement [Pos=(0,0,0), Yaw-Pitch-Roll=(0,0,0)]
>>> es.Placement.rotate(0,0,90)
raceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: PyCXX: Error creating object of type N2Py7SeqBaseINS_6ObjectEEE from 0
I don't know how to rotate an individual part with script.
chrisb
Posts: 19661
Joined: Tue Mar 17, 2015 9:14 am

Re: Animating a part

Postby chrisb » Sat Nov 09, 2019 11:20 pm

If you have to move complete parts you probably have not yet established all dependencies which your gearbox has.

If I want to set the rotation of "obj" to a certain angle (e.g. 95°) I see in the console

Code: Select all

obj.Placement = App.Placement(App.Vector(0,0,0),App.Rotation(App.Vector(0,0,1),95))
That's what I would use in the Python script.
User avatar
iplayfast
Posts: 47
Joined: Sat Sep 07, 2019 6:55 am

Re: Animating a part

Postby iplayfast » Sun Nov 10, 2019 1:15 am

I'll have to digest what you've said. But in the meantime, I've updated the library to have animations of the eccentric wheel, (controllable via a boolean in the parameters box).
This works quite well, but of course has no constraints with other parts, so the cycloidal gear, and driver gear don't move.

It appears I can go one of three ways.
1. start over using sketches and add the constraints there (I think I tried this in the first place, but wasn't able to get things working).
2. use assemblies (4) which limits it to latest/greatest freecad, (and not sure how to script it).
3. figure out the math myself and just place things where they should end up.

I'm thinking #3 is the most likely to succeed, and it has the added advantage of no constraints where parts are and can be done in the script.

Looking for thoughts. (feel free to check it out and tell me how terrible the code is). :)
FYI I tried 3d printing it yesterday and it more or less came out correctly. I scaled 1/2 size and things were too tight to rotate, so some tuning might be needed.
Bayesian
Posts: 57
Joined: Thu Aug 08, 2019 1:49 pm

Re: Animating a part

Postby Bayesian » Sun Nov 10, 2019 11:45 am

I haven't done this a lot, but I think I would look into using Blender for any animation related stuff.

Blender is not well suited for engineering applications, but it has a ton of features for animating things:

- Objects and their movements can be governed by several types of constraints
- The IPO curves make it easy to define the change of values
- You can easily animate the camera
- You can use Eeevee to render advanced, realtime materials
- Blender not only has one physics engine, but many. It may not be engineering grade, but it does support rigid bodies, fluid and cloth simulation
- Even the particle system or SPH may be useful
- It exports to videos nicely
- Blender is also scripted through Python
User avatar
iplayfast
Posts: 47
Joined: Sat Sep 07, 2019 6:55 am

Re: Animating a part

Postby iplayfast » Sun Nov 10, 2019 5:32 pm

I'm not looking to do an animation, I'm looking to have the part animate in freecad so it can be understood.
Bayesian
Posts: 57
Joined: Thu Aug 08, 2019 1:49 pm

Re: Animating a part

Postby Bayesian » Mon Nov 11, 2019 7:18 am

That's a point. Though even for understanding, Blender could be a useful tool.

I'm not trying to argue, I'm in the process of trying out these things myself. I have more experience in Blender, and those IPO curves are a killer feature for animating and visualizing.
mario52
Posts: 2692
Joined: Wed May 16, 2012 2:13 pm

Re: Animating a part

Postby mario52 » Tue Nov 12, 2019 10:21 am

Maybe you need a special feature, go into Macros_recipes and Code_snippets, Dialog creation. My macros on Gist.github.