Animating a part

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
iplayfast
Posts: 256
Joined: Sat Sep 07, 2019 6:55 am

Animating a part

Post by iplayfast »

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
Veteran
Posts: 54145
Joined: Tue Mar 17, 2015 9:14 am

Re: Animating a part

Post by chrisb »

Yes, you can do this in Python, usually by setting a constraint and recompute the model. Animation workbench can help.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
chrisb
Veteran
Posts: 54145
Joined: Tue Mar 17, 2015 9:14 am

Re: Animating a part

Post by chrisb »

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)
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
iplayfast
Posts: 256
Joined: Sat Sep 07, 2019 6:55 am

Re: Animating a part

Post by iplayfast »

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
Veteran
Posts: 54145
Joined: Tue Mar 17, 2015 9:14 am

Re: Animating a part

Post by chrisb »

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.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
iplayfast
Posts: 256
Joined: Sat Sep 07, 2019 6:55 am

Re: Animating a part

Post by iplayfast »

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: 90
Joined: Thu Aug 08, 2019 1:49 pm

Re: Animating a part

Post by Bayesian »

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: 256
Joined: Sat Sep 07, 2019 6:55 am

Re: Animating a part

Post by iplayfast »

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: 90
Joined: Thu Aug 08, 2019 1:49 pm

Re: Animating a part

Post by Bayesian »

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
Veteran
Posts: 4690
Joined: Wed May 16, 2012 2:13 pm

Re: Animating a part

Post by mario52 »

Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
Post Reply