1

经过长时间自己寻找问题的解决方案,我在这里寻求帮助,希望有人可以帮助我解冻我的实际情况。因此,如果有任何专家或优秀的“Python Guru”有时间帮助我,这里是上下文:

由于 Python 3.6 上出色的 Trimesh 库,我正在编写一个网格操作脚本,我想在应用一些矩阵旋转变换的同时,刷新网格可视化,以便查看网格的实时旋转演变。

没有成功,我按照 Trimesh GitHub 上的以下脚本进行了一些尝试,但如果不单击右上角的“关闭十字”,我无法停止它。这是原始代码:


"""
view_callback.py
------------------

Show how to pass a callback to the scene viewer for
easy visualizations.
"""

import time
import trimesh
import numpy as np


def sinwave(scene):
    """
    A callback passed to a scene viewer which will update
    transforms in the viewer periodically.

    Parameters
    -------------
    scene : trimesh.Scene
      Scene containing geometry

    """
    # create an empty homogenous transformation
    matrix = np.eye(4)
    # set Y as cos of time
    matrix[1][3] = np.cos(time.time()) * 2
    # set Z as sin of time
    matrix[2][3] = np.sin(time.time()) * 3

    # take one of the two spheres arbitrarily
    node = s.graph.nodes_geometry[0]
    # apply the transform to the node
    scene.graph.update(node, matrix=matrix)


if __name__ == '__main__':
    # create some spheres
    a = trimesh.primitives.Sphere()
    b = trimesh.primitives.Sphere()

    # set some colors for the balls
    a.visual.face_colors = [255, 0, 0, 255]
    b.visual.face_colors = [0, 0, 100, 255]

    # create a scene with the two balls
    s = trimesh.Scene([a, b])

    # open the scene viewer and move a ball around
    s.show(callback=sinwave)

这是我尝试整合矩阵旋转变换(在导入的网格上应用旋转)以查看演变。但是旋转不平滑(动画呈锯齿状),我无法自动停止它,让我们说在 z 上旋转 97° 后。(并且代码基于时间,而我希望它基于角度位置)。

from pathlib import Path

import pandas as pd
import time
import xlsxwriter

import numpy as np

import trimesh
from trimesh import transformations as trf

# Actual directory loading and stl adress saving
actual_dir = Path(__file__).resolve().parent
stl = Path(actual_dir/"Belt_Bearing_Gear.stl")

mesh = trimesh.load(f"{stl}")


def R_matrix(scene):

    u= 0
    o= 0
    t= time.time()

    time.sleep(0.1)

    rotation = (u, o, t)

    # Angle conversion from degres to radian
    def trig(angle):
        r = np.deg2rad(angle)
        return r

    alpha = trig(rotation[0])
    beta = trig(rotation[1])
    gamma = trig(rotation[2])

    origin, xaxis, yaxis, zaxis = [0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]

    Rx = trf.rotation_matrix(alpha, xaxis)
    Ry = trf.rotation_matrix(beta, yaxis)
    Rz = trf.rotation_matrix(gamma, zaxis)

    R = trf.concatenate_matrices(Rx, Ry, Rz)           
    R2=R[:3,:3]


    # The rotation matrix is applyed to the mesh
    mesh.vertices = np.matmul(mesh.vertices,R2)


    # apply the transform to the node
    s = trimesh.Scene([mesh])

    scene.graph.update(s, matrix=R)


if __name__ == '__main__':

    # set some colors for the mesh and the bounding box
    mesh.visual.face_colors = [102, 255, 255, 255]

    # create a scene with the mesh and the bounding box
    s = trimesh.Scene([mesh])
    liste=list(range(1,10))

    # open the scene viewer and move a ball around
    s.show(callback=R_matrix)

欢迎您的所有想法和建议,因为我是一名年轻的 Python 初学者 :) 在此先感谢您的帮助,致以诚挚的问候,

房车

4

0 回答 0