chongxi / playground

Low-latency interactive multi-stream Behavior Platform: a control console that integrate Jovian (Virtual Reality Platform) and FPGA&Spiketag (Real-time Ephys) and other sensor streams
Other
3 stars 0 forks source link

Load mesh from Blender #4

Closed chongxi closed 6 years ago

chongxi commented 6 years ago

image

chongxi commented 6 years ago

1. Convert the mesh obj file into triangle-only mesh

filename = "constraint_cue.obj"
ofilename = 'tri_' + filename 

f = open(filename)
ofile = open(ofilename, "w")

line = "#SHOULD NOT BE HERE THIS LINE IS"
while line:
    line = f.readline()
    if len(line) == 0: continue
    if line[0] == "f":
        # if the number of entries following the f is more than 3,
        # write multiple face lines as all triangles
        faces = []
        oldindex = 1
        index = line.find(" ", 2)
        while index != -1:
            faces.append(line[oldindex+1:index])
            oldindex = index
            index = line.find(" ", index+1)
        # I'm assuming no faces at end of line
        faces.append(line[oldindex+1:len(line)-1])
        #print faces
        start = 1
        while start + 1 < len(faces):
            ofile.write("f " + faces[0] + " " + faces[start] + " " + faces[start+1] + "\n")
            start += 1
    else:
        ofile.write(line)
ofile.close()
f.close()
chongxi commented 6 years ago

2. Load the mesh with color to gray conversion and then certain Matrixtranform()

from vispy import scene
import numpy as np
from scipy.special import sph_harm
from vispy.visuals.transforms import STTransform, MatrixTransform
from vispy.io.mesh import read_mesh

canvas = scene.SceneCanvas(keys='interactive')
view = canvas.central_widget.add_view()

(vertices, faces, vertex_colors, _) = read_mesh('tri_constraint_cue.obj')
gray_mat = np.repeat(np.array([0.5989, 0.6870, 0.4140]), 3).reshape(-1,3)
vertex_colors = abs(vertex_colors.dot(gray_mat))
transparency  = np.ones((vertex_colors.shape[0], 1))
print vertex_colors
# color = np.zeros((ys.shape[0], 4)) * np.array([0,1,1,1])
N = vertex_colors.shape[0]

# gray_vertex_colors = rgb2gray(vertex_colors)
# vertex_colors[:,0] = np.linspace(0,1,N)
# vertex_colors[:,2] = vertex_colors[::-1, 0] 
mesh = scene.visuals.Mesh(vertices, faces, vertex_colors, shading=None)
# mesh.ambient_light_color = vispy.color.Color('white')

view.camera = 'turntable'

maze_scale_factor = 100
transform = MatrixTransform()
# transform.rotate(angle=90, axis=(1, 0, 0))  
transform.scale(scale=[100,100,100,100])
transform.translate(pos=[0, 0])
mesh.transform = transform 

view.add(mesh)

canvas.show()

if __name__ == '__main__':
    canvas.app.run()