Render animation frame and stream to video file

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
circuit
Posts: 8
Joined: Sun May 03, 2020 6:53 am

Render animation frame and stream to video file

Post by circuit »

Hi all. New user here. Nice to meet you all. I'm an Electrical Engineer by profession, but I tinker with robotics in my spare time. I'm planning to use FreeCAD's scripting capabilities to simulate the gait of a hexapod. I originally hand-wrote a Python-based OpenGL simulator for a 6 DOF robotic arm (here's a screenshot: https://i.imgur.com/UnfD8Fj.jpg) but it's a huge pain to rig. FreeCAD looks like a promising upgrade for complex mechanisms.

I've been playing around with script-based animation via A2Plus and I really appreciate the simplicity of joint actuation when imposing constraints. My concern, however, is slow rendering on the active view due to expensive computation at each time step.

So I had an idea. I'd like to script a job that renders one frame at a time, exporting each frame to a video stream output. I would gladly sacrifice the real-time animation for a smooth video end result. I'd imagine PySide2 can stream to a video buffer, I'm just not sure how to grab the active view (with transparent background).

Any thoughts how I could make this happen? Does this idea seem reasonable?

Note: I'd like to avoid Blender as this would require re-rigging the model.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Render animation frame and stream to video file

Post by vocx »

circuit wrote: Sun May 03, 2020 7:22 am I've been playing around with script-based animation via A2Plus ...
Have you investigated Assembly4? It can also do animations, and since it doesn't use a constraint solver, it may be faster. You may give it a try.

See Challenge: Constant Velocity Joint (M. Gesik)
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
circuit
Posts: 8
Joined: Sun May 03, 2020 6:53 am

Re: Render animation frame and stream to video file

Post by circuit »

vocx wrote: Sun May 03, 2020 3:46 pm
circuit wrote: Sun May 03, 2020 7:22 am I've been playing around with script-based animation via A2Plus ...
Have you investigated Assembly4? It can also do animations, and since it doesn't use a constraint solver, it may be faster. You may give it a try.

See Challenge: Constant Velocity Joint (M. Gesik)
Thanks for the link. I suppose my question is not how to improve rendering time but rather how to capture the active view with a transparent background so that I can feed it into a video buffer stream. Sorry if that wasn't clear. According to https://forum.freecadweb.org/viewtopic.php?t=29968 I can screen-grab with:

Code: Select all

activeView = FreeCADGui.activeDocument().activeView()
activeView.saveImage(fileName,1000,800,'White')
then stitch them together into a video, but that really doesn't seem like an efficient method.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Render animation frame and stream to video file

Post by vocx »

circuit wrote: Sun May 03, 2020 8:01 pm ... how to capture the active view with a transparent background so that I can feed it into a video buffer stream.
...
I know what you wanted to do, I just mentioned Assembly4 in case you wanted to test that as well.

As for taking pictures, you can test different options of the Std_ViewScreenShot tool. Various rendering options were added in 0.19. I haven't tested them myself. Maybe one of them helps.

See Issue with "saveImage" and Parts with Transparency Nonzero.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
circuit
Posts: 8
Joined: Sun May 03, 2020 6:53 am

Re: Render animation frame and stream to video file

Post by circuit »

vocx wrote: Sun May 03, 2020 8:40 pm
circuit wrote: Sun May 03, 2020 8:01 pm ... how to capture the active view with a transparent background so that I can feed it into a video buffer stream.
...
I know what you wanted to do, I just mentioned Assembly4 in case you wanted to test that as well.

As for taking pictures, you can test different options of the Std_ViewScreenShot tool. Various rendering options were added in 0.19. I haven't tested them myself. Maybe one of them helps.

See Issue with "saveImage" and Parts with Transparency Nonzero.
I had a further read and it seems that Qt5 isn't able to write a video file natively on Windows anyway. So unless I use something like OpenCV I'll be forced to export screenshots and stitch them together afterwards. Not too elegant, but not the end of the world!
mario52
Veteran
Posts: 4692
Joined: Wed May 16, 2012 2:13 pm

Re: Render animation frame and stream to video file

Post by mario52 »

hi

try this macro Macro_Screen_Wiki Image (special for the wiki worker) only for FC 0.19

choice the definition image , background visualise wit Set Screen or Tile Screen (facultatif) save your first image (name path...) after, if you want create serial image for create a gif animation (with other program) click the Follow button and the image are saved with the same name, path + 00x number without passing to the window Save image. (You have also window for create Rotation)

install with AddonManager

Image

EDIT: 15h53 PARIS uprgade Version=0.02: 2020/05/04 (correct bug color, restore)

mario
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.
circuit
Posts: 8
Joined: Sun May 03, 2020 6:53 am

Re: Render animation frame and stream to video file

Post by circuit »

mario52 wrote: Mon May 04, 2020 9:24 am hi

try this macro Macro_Screen_Wiki Image (special for the wiki worker) only for FC 0.19

choice the definition image , background visualise wit Set Screen or Tile Screen (facultatif) save your first image (name path...) after, if you want create serial image for create a gif animation (with other program) click the Follow button and the image are saved with the same name, path + 00x number without passing to the window Save image. (You have also window for create Rotation)

install with AddonManager

EDIT: 15h53 PARIS uprgade Version=0.02: 2020/05/04 (correct bug color, restore)

mario
Hey that's pretty neat. It's nearly what I'm looking for, except that I'll also need to animate joint movements. I had a look at the source and there's a lot of good stuff to get me started on writing a script of my own. Thanks for sharing. Out of curiosity, is there something in particular that needs PySide2 (Qt5) that I should be aware of? 0.19 has a few visual bugs so I'm planning to stick with 0.18 for now.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Render animation frame and stream to video file

Post by vocx »

circuit wrote: Mon May 04, 2020 9:26 pm ...Out of curiosity, is there something in particular that needs PySide2 (Qt5) that I should be aware of? 0.19 has a few visual bugs so I'm planning to stick with 0.18 for now.
If you see bugs they won't be solved unless you report them.

Independently of that, Qt4 is obsolete, so you should be using Python 3 and Qt5, instead of staying with Python 2 and Qt4.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
mario52
Veteran
Posts: 4692
Joined: Wed May 16, 2012 2:13 pm

Re: Render animation frame and stream to video file

Post by mario52 »

hi
circuit wrote: Mon May 04, 2020 9:26 pm PySide2 (Qt5) that I should be aware of? 0.19
natural evolution and i work for convert my all macros

for animation you have :

Animation_Workbench

here for rotate and save image step by step
Manipulator_Workbench
Placement
my other macro Macro_Rotate_To_Point Image

mario
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.
User avatar
jimmihenry
Posts: 238
Joined: Sat Jan 11, 2020 3:36 pm

Re: Render animation frame and stream to video file

Post by jimmihenry »

Screen_Wiki is awesome thank you :)
Image
Post Reply