To visualise the results of an animation (written using sympy.physics.mechanics) I have this cell in the Jupyter notebook:
start6 = time.time()
times1 = []
resultat1 = []
#=======================
zeitpunkte = 500
#=======================
reduction = max(1, int(len(times)/zeitpunkte))
for i in range(len(times)):
if i % reduction == 0:
times1.append(times[i])
resultat1.append(resultat[i])
schritte1 = len(times1)
resultat1 = np.array(resultat1)
times1 = np.array(times1)
print('number of points considered:',len(times1))
Dmc_loc = [[me.dot(punkt.pos_from(P[0]), uv) for uv in N] for punkt in Dmc]
punkt_loc = [[me.dot(punkt.pos_from(P[0]), uv) for uv in N] for punkt in Po]
Dmc_loc_lam = sm.lambdify(qL + pL, Dmc_loc, cse=True)
punkt_loc_lam = sm.lambdify(qL + pL, punkt_loc, cse=True)
winkel = sm.symbols('winkel')
Rotation1 = sm.Matrix([[sm.cos(winkel), -sm.sin(winkel), 0], [sm.sin(winkel), sm.cos(winkel), 0], [0., 0., 1]])
Rot_lam = sm.lambdify(winkel, Rotation1.T)
Rotation = Rot_lam(np.pi/2.)
TC_store = []
TR_store = []
TP_store = []
body_mesh_store = []
track_store = []
farben = ['orange', 'blue', 'green', 'yellow', 'red']
laengen = l1
# the list of frames A contains the inertial frame A[0] = N. This must not be here
AB = copy(A) # for whatever reason, deepcopy does not work here
AB.pop(0)
for i in range(n):
#for its mass center
TC = sm.eye(4)
TC[:3, :3] = (AB[i].dcm(N)) * Rotation
TC = TC.reshape(16, 1)
TC_lam = sm.lambdify(qL + pL, TC)
TR = sm.eye(4)
TR[:3, :3] = (AB[i].dcm(N)) * Rotation
TR = TR.reshape(16, 1)
TR_lam = sm.lambdify(qL + pL, TR)
TP = sm.eye(4)
TP[:3, :3] = (AB[i].dcm(N)) * Rotation
TP = TP.reshape(16, 1)
TP_lam = sm.lambdify(qL + pL, TP)
# store the information about the body, expressed in TAc for every time step.
TCs = [] # for the ball
TRs = [] # for the rod
TPs = [] # for the red dot
# Create the TAs, containing 'one TA' for each time step
# resultat contains the results of the numeric integration.
# where the numeric integration was evaluated
for k_1 in range(resultat1.shape[0]):
zeit = times[i]
TCi = TC_lam(*[resultat1[k_1, l] for l in range(resultat1.shape[1])], *pL_vals) # the balls
TRi = TR_lam(*[resultat1[k_1, l] for l in range(resultat1.shape[1])], *pL_vals) # the rod
TPi = TP_lam(*[resultat1[k_1, l] for l in range(resultat1.shape[1])], *pL_vals) # the dot
# TAi[12], TAi[13], TAi[14] hold the location of A[i] w.r.t. N.
# As the axis chosen for solving the equations of motion, and the axis given by pythreejs do not
# coincide, the values for TAi[..] must be permutated accordingly.
# of course here different locations for center of ball and center of mass.
TRi[12] = -Dmc_loc_lam(*[resultat1[k_1, l] for l in range(resultat1.shape[1])], *pL_vals)[i][1]
TRi[13] = Dmc_loc_lam(*[resultat1[k_1, l] for l in range(resultat1.shape[1])], *pL_vals)[i][0]
TRi[14] = Dmc_loc_lam(*[resultat1[k_1, l] for l in range(resultat1.shape[1])], *pL_vals)[i][2]
TCi[12] = -Dmc_loc_lam(*[resultat1[k_1, l] for l in range(resultat1.shape[1])], *pL_vals)[i][1]
TCi[13] = Dmc_loc_lam(*[resultat1[k_1, l] for l in range(resultat1.shape[1])], *pL_vals)[i][0]
TCi[14] = Dmc_loc_lam(*[resultat1[k_1, l] for l in range(resultat1.shape[1])], *pL_vals)[i][2]
TPi[12] = -punkt_loc_lam(*[resultat1[k_1, l] for l in range(resultat1.shape[1])], *pL_vals)[i][1]
TPi[13] = punkt_loc_lam(*[resultat1[k_1, l] for l in range(resultat1.shape[1])], *pL_vals)[i][0]
TPi[14] = punkt_loc_lam(*[resultat1[k_1, l] for l in range(resultat1.shape[1])], *pL_vals)[i][2]
TRs.append(TRi.squeeze().tolist())
TCs.append(TCi.squeeze().tolist())
TPs.append(TPi.squeeze().tolist())
TC_store.append(TCs)
TR_store.append(TRs)
TP_store.append(TPs)
# Create the objects, which will move
# 1. The ball
body_geom_C = p3js.SphereGeometry(r1, 12, 12)
body_material_C = p3js.MeshStandardMaterial(color=farben[i], wireframe=False)
body_mesh_C = p3js.Mesh(geometry=body_geom_C, material=body_material_C, name='ball_' + str(i))
# 2. Rod
body_geom_R = p3js.CylinderGeometry(radiusTop=0.05, radiusBottom=0.05, height=laengen[i],
radialSegments=6, heightSegments=10, openEnded=False)
body_material_R = p3js.MeshStandardMaterial(color='black', wireframe=False)
body_mesh_R = p3js.Mesh(geometry=body_geom_R, material=body_material_R, name='rod_' + str(i))
# 3. the dot
body_geom_P = p3js.SphereGeometry(0.25, 12, 12)
body_material_P = p3js.MeshStandardMaterial(color='red', wireframe=False)
body_mesh_P = p3js.Mesh(geometry=body_geom_P, material=body_material_P, name='punkt_' + str(i))
# locate the body in 3D space and add the coordinate system of the body
body_mesh_R.matrixAutoUpdate = False
body_mesh_R.add(p3js.AxesHelper(0.1)) # length of the axis of the ball system A2
body_mesh_R.matrix = TR_store[i][0] # starting point of the animation
body_mesh_C.matrixAutoUpdate = False
body_mesh_C.add(p3js.AxesHelper(0.01)) # length of the axis of the center of mass system A2
body_mesh_C.matrix = TC_store[i][0] # starting point of the animation
body_mesh_P.matrixAutoUpdate = False
body_mesh_P.add(p3js.AxesHelper(0.01)) # length of the axis of the center of mass system A2
body_mesh_P.matrix = TP_store[i][0] # starting point of the animation
body_mesh_store.append(body_mesh_C)
body_mesh_store.append(body_mesh_R)
body_mesh_store.append(body_mesh_P)
# Create the 'picture'.
# all the 'paramters' are taken by trial and error.
view_width = 1200
view_height = 400
# Values just found by trial an error.
p1, p2 = 5, 5
if n == 2:
p3 = 18
elif n == 3:
p3 = 22
else:
p3 = 30
camera = p3js.PerspectiveCamera(position=[p1, p2, p3],
up=[-1.0, 0.0, 0.0],
aspect=view_width/view_height)
key_light = p3js.DirectionalLight(position=[0, 0, 10])
ambient_light = p3js.AmbientLight()
axes = p3js.AxesHelper(20)
children = []
for i in range(3*n):
children = children + [body_mesh_store[i], axes, camera, key_light, ambient_light]
scene = p3js.Scene(children=children)
controller = p3js.OrbitControls(controlling=camera)
renderer = p3js.Renderer(camera=camera, scene=scene, controls=[controller],
width=view_width, height=view_height)
# Create the action, simply copied from JM's lecture.
for i in range(n):
eigenname = 'ball_'+str(i)
track_C = p3js.VectorKeyframeTrack(
name="scene/" + eigenname + ".matrix",
times=times1,
values=TC_store[i])
eigenname = 'rod_' + str(i)
track_R = p3js.VectorKeyframeTrack(
name="scene/" + eigenname + ".matrix",
times=times1,
values=TR_store[i])
eigenname = 'punkt_' + str(i)
track_P = p3js.VectorKeyframeTrack(
name="scene/" + eigenname + ".matrix",
times=times1,
values=TP_store[i])
track_store += [track_C] + [track_R] + [track_P]
duration = times1[-1] - times1[0]
clip = p3js.AnimationClip(tracks=track_store, duration=duration)
action = p3js.AnimationAction(p3js.AnimationMixer(scene), clip, scene)
# avoid error if the animation has not run before this runs.
if start5 != None:
print(' it tooks {:.3f} sec to run the complete program'.format(time.time() - start6 + start5 - start ))
renderer
I get this error message:
TraitError Traceback (most recent call last)
File c:\Users\Peter\anaconda3\envs\math\Lib\site-packages\ipywidgets\widgets\widget.py:766, in Widget._handle_msg(self, msg)
764 if 'buffer_paths' in data:
765 _put_buffers(state, data['buffer_paths'], msg['buffers'])
--> 766 self.set_state(state)
768 # Handle a state request.
769 elif method == 'request_state':
TraitError: The 'target' trait of a DirectionalLight instance expected an Uninitialized or an Object3D, not the str 'IPY_MODEL_3f5c3aea-5803-4ed3-9aa5-c7d9447925e9'.
The animation still works as expected, so my question is really more curiosity.
Thanks a lot for any help!!
To visualise the results of an animation (written using sympy.physics.mechanics) I have this cell in the Jupyter notebook:
I get this error message:
TraitError Traceback (most recent call last) File c:\Users\Peter\anaconda3\envs\math\Lib\site-packages\ipywidgets\widgets\widget.py:766, in Widget._handle_msg(self, msg) 764 if 'buffer_paths' in data: 765 _put_buffers(state, data['buffer_paths'], msg['buffers']) --> 766 self.set_state(state) 768 # Handle a state request. 769 elif method == 'request_state':
File c:\Users\Peter\anaconda3\envs\math\Lib\site-packages\ipywidgets\widgets\widget.py:648, in Widget.set_state(self, sync_data) 645 if name in self.keys: 646 from_json = self.trait_metadata(name, 'from_json', 647 self._trait_from_json) --> 648 self.set_trait(name, from_json(sync_data[name], self))
File c:\Users\Peter\anaconda3\envs\math\Lib\site-packages\traitlets\traitlets.py:1738, in HasTraits.set_trait(self, name, value) 1736 raise TraitError(f"Class {cls.name} does not have a trait named {name}") 1737 else: -> 1738 getattr(cls, name).set(self, value)
File c:\Users\Peter\anaconda3\envs\math\Lib\site-packages\traitlets\traitlets.py:703, in TraitType.set(self, obj, value) 702 def set(self, obj, value): --> 703 new_value = self._validate(obj, value) 704 try: 705 old_value = obj._trait_values[self.name]
File c:\Users\Peter\anaconda3\envs\math\Lib\site-packages\traitlets\traitlets.py:735, in TraitType._validate(self, obj, value) 733 return value 734 if hasattr(self, "validate"): --> 735 value = self.validate(obj, value) 736 if obj._cross_validation_lock is False: 737 value = self._cross_validate(obj, value)
File c:\Users\Peter\anaconda3\envs\math\Lib\site-packages\traitlets\traitlets.py:2302, in Union.validate(self, obj, value) 2300 except TraitError: 2301 continue -> 2302 self.error(obj, value)
File c:\Users\Peter\anaconda3\envs\math\Lib\site-packages\traitlets\traitlets.py:841, in TraitType.error(self, obj, value, error, info) 835 else: 836 e = "The '{}' trait expected {}, not {}.".format( 837 self.name, 838 self.info(), 839 describe("the", value), 840 ) --> 841 raise TraitError(e)
TraitError: The 'target' trait of a DirectionalLight instance expected an Uninitialized or an Object3D, not the str 'IPY_MODEL_3f5c3aea-5803-4ed3-9aa5-c7d9447925e9'.
The animation still works as expected, so my question is really more curiosity. Thanks a lot for any help!!