jupyter-widgets / pythreejs

A Jupyter - Three.js bridge
https://pythreejs.readthedocs.io
Other
951 stars 188 forks source link

How to rotate objects? #69

Closed jdestgermain closed 6 years ago

jdestgermain commented 7 years ago

I want to rotate a cylinder I've created because it doesn't like up with other geometry in my system. I think I am missing something--this should be fairly easy.

Minimal working example of what I want:

from pythreejs import *
import numpy as np
import math
from IPython.display import display
from ipywidgets import HTML, Text
from traitlets import link, dlink

my_geom = CylinderGeometry(radiusTop=5, radiusBottom=5, height=20)
my_geom.quaternion = [0, 0, 1,
                      90]  #my attempt at rotating the object, doesn't work

my_mesh = Mesh(
    geometry=my_geom,
    material=LambertMaterial(
        color='red', position=[0, 0, 0]))

scene = Scene(children=[my_mesh])

c = PerspectiveCamera(position=[0, 0, 20], up=[0, 0, 1], fov=90)

renderer = Renderer(
    camera=c, scene=scene, controls=[OrbitControls(controlling=c)])
display(renderer)

Thanks

asmfreak commented 7 years ago

To rotate an object you need to use this snippet

geom.quaternion_from_rotation(
            [ 
                np.cos(alpha), -np.sin(alpha), 0, 
                np.sin(alpha), np.cos(alpha),  0, 
                0,             0,              1
            ])

The 'matrix' inside is a rotation matrix. wikipedia article on this

misolietavec commented 7 years ago

Right, but I think, quaternion_from_rotation is the method of Mesh (and similar) objects, not of geometry. So the answer is: my_mesh.quaternion_from_rotation(... as above ...)

vidartf commented 6 years ago

For version 1.0, you would do this via my_mesh.quaternion or my_mesh.rotation. There are also the utility functions rotateX/rotateY/rotateZ, lookAt and setRotationFromMatrix.

Closing as answered, but feel free to comment if there are anything that is not clear.