Python 如何在pythreejs中同时设置多个对象的动画,并且每个对象都具有不同的KeyFrameTrack?

Python 如何在pythreejs中同时设置多个对象的动画,并且每个对象都具有不同的KeyFrameTrack?,python,three.js,jupyter,pythreejs,Python,Three.js,Jupyter,Pythreejs,我随时间为场景中的每个对象生成4x4变换矩阵,并使用VectorKeyframeTrack在网格对象上设置变换矩阵。我能够为每个对象分别使用AnimateAction设置对象的动画,但无法想出如何使用单个.play()调用同时设置所有对象的动画并保持它们同步。这就是我所能做到的,希望从第二个循环开始,对象将是时间同步的,但它们不是: 将pythreejs导入为pjs 杆式网格=pjs.网格( pjs.CylinderBufferGeometry(0.005,0.005,系统常数[lB]-系统常数

我随时间为场景中的每个对象生成4x4变换矩阵,并使用
VectorKeyframeTrack
在网格对象上设置变换矩阵。我能够为每个对象分别使用
AnimateAction
设置对象的动画,但无法想出如何使用单个
.play()
调用同时设置所有对象的动画并保持它们同步。这就是我所能做到的,希望从第二个循环开始,对象将是时间同步的,但它们不是:

将pythreejs导入为pjs
杆式网格=pjs.网格(
pjs.CylinderBufferGeometry(0.005,0.005,系统常数[lB]-系统常数[h]/2,),
pjs.MeshStandardMaterial(颜色=红色)
)
plate_mesh=pjs.mesh(pjs.PlaneBuffer几何体(系统常数[h],系统常数[w]),
pjs.MeshStandardMaterial(颜色为“蓝色”,侧面为“双面”)
#没有此集合,动画将不会更新。
rod_mesh.matrixAutoUpdate=False
plate_mesh.Matrix自动更新=假
#设置初始位置/方向
rod_mesh.matrix=rod_矩阵[0]
plate_mesh.matrix=plate_矩阵[0]
#设置场景
视图宽度=600
视图高度=400
摄像头=pjs.透视摄像头(位置=[0.25,0.25,0.25],纵横比=视图\宽度/视图\高度)
方向灯(位置=[0,10,10])
环境光=pjs.AmbientLight()
场景=pjs.scene(儿童=[杆网、板网、摄像机、关键光、环境光])
控制器=pjs.OrbitControls(控制=摄像机)
renderer=pjs.renderer(摄影机=摄影机,场景=场景\ pjs,控件=[控制器],宽度=视图\宽度,高度=视图\高度)
#指定关键帧跟踪
rod_track=pjs.VectorKeyframeTrack(名称='.matrix',时间=列表(sys.times),值=rod_矩阵)
plate_track=pjs.VectorKeyframeTrack(名称='.matrix',时间=列表(sys.times),值=plate_矩阵)
rod\u clip=pjs.AnimationClip(轨迹=[rod\u轨迹])
plate\u clip=pjs.AnimationClip(轨迹=[plate\u轨迹])
rod_mixer=pjs.AnimationMixer(rod_网格)
plate_mixer=pjs.AnimationMixer(plate_网格)
rod_action=pjs.AnimationAction(rod_混合器、rod_剪辑、rod_网格)
plate_action=pjs.AnimationAction(plate_混合器、plate_剪辑、plate_网格)
#尝试在每个循环开始时在操作之间强制同步
plate_action.exec_three_obj_method('syncWith','IPY_MODEL_'+rod_action.get_view_spec()['MODEL_id']))
rod_action.exec_three_obj_method('syncWith','IPY_MODEL_'+plate_action.get_view_spec()['MODEL_id']))
listener_func='()=>{{{{}.syncWith({});}}.format('IPY_MODEL_'+plate_action.get_view_spec()['MODEL_id'],'IPY_MODEL_'+rod_action.get_view_spec()['MODEL_id'])
rod_mixer.exec_three_obj_方法('addEventListener','loop',listener_func)

必须在每次帧转换时调用
syncWith()
,但我不确定如何影响PythroJS的动画帧循环。

这里给出了一个解决方案:

关键是为场景而不是单个网格设置动画,这样做的技巧是命名网格,然后使用
scene/

rod_mesh = pjs.Mesh(pjs.CylinderBufferGeometry(0.005, 0.005, sys.constants[lB] - sys.constants[h] / 2),
                    pjs.MeshStandardMaterial(color='red'),
                    name='rod'  # name each mesh!
)

plate_mesh = pjs.Mesh(pjs.PlaneBufferGeometry(sys.constants[h], sys.constants[w]),
                      pjs.MeshStandardMaterial(color='blue', side='DoubleSide'),
                      name="plate"  # name each mesh!
)

# For updating the transform matrices directly set:
rod_mesh.matrixAutoUpdate = False
plate_mesh.matrixAutoUpdate = False

# Scene setup
view_width = 600
view_height = 400
camera = pjs.PerspectiveCamera(position=[0.25, 0.25, 0.25], aspect=view_width/view_height)
key_light = pjs.DirectionalLight(position=[0, 10, 10])
ambient_light = pjs.AmbientLight()
scene_pjs = pjs.Scene(children=[rod_mesh, plate_mesh, camera, key_light, ambient_light])
controller = pjs.OrbitControls(controlling=camera)
renderer = pjs.Renderer(camera=camera, scene=scene_pjs, controls=[controller], width=view_width, height=view_height)

# Key thing here is to set the attribute you want to change as a sub-item of the scene. Use the names of the meshes above.
rod_track = pjs.VectorKeyframeTrack(name='scene/rod.matrix',
                                    times=list(sys.times),
                                    values=rod_matrices)
plate_track = pjs.VectorKeyframeTrack(name='scene/plate.matrix',
                                      times=list(sys.times),
                                      values=plate_matrices)

# Now create a single clip with both tracks and animate the scene:
clip = pjs.AnimationClip(tracks=[rod_track, plate_track], duration=sys.times[-1])
action = pjs.AnimationAction(pjs.AnimationMixer(scene_pjs), clip, scene_pjs)