-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexportGif.FCMacro
97 lines (80 loc) · 2.83 KB
/
exportGif.FCMacro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from ast import If
import math
import time
import os
import subprocess
import glob
from FreeCAD import Base
from pivy import coin
size=(1000,1000)
doc = FreeCAD.ActiveDocument
docDir, docFilename = os.path.split(doc.FileName)
filePrefix = os.path.splitext(docFilename)[0]
for o in doc.RootObjects:
if o.ViewObject.isVisible():
filePrefix=filePrefix+'-'+o.Label.split('[')[0]
break
gifdir = "gif/"
if not os.path.exists(gifdir):
os.makedirs(gifdir)
filename = os.path.join(docDir, 'gif/', filePrefix)
steps=36
angle=2*math.pi/steps
matX=Base.Matrix()
matX.rotateX(angle)
stepsX=Base.Placement(matX).Rotation
matY=Base.Matrix()
matY.rotateY(angle)
stepsY=Base.Placement(matY).Rotation
matZ=Base.Matrix()
matZ.rotateZ(angle)
stepsZ=Base.Placement(matZ).Rotation
view=Gui.ActiveDocument.ActiveView
cam=view.getCameraNode()
rotCamera=Base.Rotation(*cam.orientation.getValue().getValue())
# this sets the lookat point to the center of circumsphere of the global bounding box
view.fitAll()
# the camera's position, i.e. the user's eye point
position=Base.Vector(*cam.position.getValue().getValue())
distance=cam.focalDistance.getValue()
# view direction
vec=rotCamera.multVec(Base.Vector(0,0,-1))
# this is the point on the screen the camera looks at
# when rotating the camera we should make this point fix
lookat=position+vec*distance
# around x axis
for i in range(steps):
rotCamera=stepsX.multiply(rotCamera)
cam.orientation.setValue(*rotCamera.Q)
vec=rotCamera.multVec(Base.Vector(0,0,-1))
pos=lookat-vec*distance
cam.position.setValue(pos.x,pos.y,pos.z)
Gui.updateGui()
time.sleep(0.3)
view.saveImage(filename+"-x-" + '{:02d}'.format(i) + ".png",*size)
if subprocess.call("convert " + filename + "-x-*.png " + filename + "-x.gif",shell=True) == 0:
subprocess.call("rm " + filename + "-x-*.png",shell=True)
# around y axis
for i in range(steps):
rotCamera=stepsY.multiply(rotCamera)
cam.orientation.setValue(*rotCamera.Q)
vec=rotCamera.multVec(Base.Vector(0,0,-1))
pos=lookat-vec*distance
cam.position.setValue(pos.x,pos.y,pos.z)
Gui.updateGui()
time.sleep(0.3)
view.saveImage(filename+"-y-" + '{:02d}'.format(i) + ".png",*size)
if subprocess.call("convert " + filename + "-y-*.png " + filename + "-y.gif",shell=True) == 0:
subprocess.call("rm " + filename + "-y-*.png",shell=True)
# around z axis
for i in range(steps):
rotCamera=stepsZ.multiply(rotCamera)
cam.orientation.setValue(*rotCamera.Q)
vec=rotCamera.multVec(Base.Vector(0,0,-1))
pos=lookat-vec*distance
cam.position.setValue(pos.x,pos.y,pos.z)
Gui.updateGui()
time.sleep(0.3)
view.saveImage(filename+"-z-" + '{:02d}'.format(i) + ".png",*size)
if subprocess.call("convert " + filename + "-z-*.png " + filename + "-z.gif",shell=True) == 0:
subprocess.call("rm " + filename + "-z-*.png",shell=True)