Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

16.) Cleanup stl loader example #23

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 37 additions & 95 deletions examples/stl_loader/main.py
Original file line number Diff line number Diff line change
@@ -1,113 +1,55 @@
import os

os.environ["KIVY_NO_CONSOLELOG"] = "1"
import math
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scatterlayout import ScatterLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.properties import ListProperty
from kivy.clock import Clock
from kivy.core.image import Image

from kivy3 import Scene, Renderer, PerspectiveCamera
from kivy3.extras.geometries import (
PlaneGeometry,
ConeGeometry,
BoxGeometry,
CylinderGeometry,
SphereGeometry,
GridGeometry,
)
from kivy3.extras.objects import ArrowObject, AxisObject
from kivy3.loaders import URDFLoader, STLLoader
from kivy3 import Mesh, Material, Object3D
from kivy3.objects.lines import Lines
from kivy3.widgets import OrbitControlWidget, SelectionWidget, Object3DWidget

from kivy.graphics.opengl import (
glEnable,
glDisable,
GL_DEPTH_TEST,
glReadPixels,
GL_RGBA,
GL_UNSIGNED_BYTE,
)


_this_path = os.path.dirname(os.path.realpath(__file__))
shader_file = os.path.join(_this_path, "../blinnphong.glsl")
obj_file = os.path.join(_this_path, "./monkey.obj")
stl_file = os.path.join(_this_path, "./stl_mesh.stl")
urdf_file = os.path.join(
_this_path, "./rs1_description/urdf/generated_rs1_parallel.urdf"
)
package_path = os.path.join(_this_path, "./") # parent of the package path
arrow_img_file = os.path.join(_this_path, "./assets/icon-rotate-360.png")
prismatic_arrow_img_file = os.path.join(_this_path, "./assets/icon-arrows.png")
from kivy3 import Material
from kivy3 import PerspectiveCamera
from kivy3 import Renderer
from kivy3 import Scene
from kivy3 import shaders
from kivy3.loaders.stlloader import STLLoader
from kivy3.widgets import OrbitControlWidget
import os

base_path = os.path.dirname(os.path.realpath(__file__))
stl_file = os.path.join(base_path, "stl_mesh.stl")

class VisualisationWidget(FloatLayout):
def __init__(self, **kw):
super(VisualisationWidget, self).__init__(**kw)

self.renderer = Renderer(shader_file=shader_file)
self.renderer.set_clear_color((0.16, 0.30, 0.44, 1.0))
class STLLoaderApp(App):
"""Demonstrate STL file loading.
"""

scene = Scene()

base = Object3D()
# id_color = (1,0,0)
material = Material(
color=(1.0, 0, 1,), diffuse=(1.0, 0, 1.0), specular=(0.35, 0.35, 0.35)
)
stl_loader = STLLoader()
stl_object = stl_loader.load(stl_file, material)
stl_object.pos.z = 0.3
# object1 = Mesh(geometry, material)

base.add(stl_object)
def build(self):
renderer = self.renderer = Renderer(shader_file=shaders.blinnphong)
renderer.set_clear_color((0.16, 0.30, 0.44, 1.0))

geometry = GridGeometry()
loader = STLLoader()
material = Material(
color=(1.0, 1.0, 1.0),
diffuse=(1.0, 1.0, 1.0),
specular=(0.35, 0.35, 0.35),
transparency=0.3,
color=(0.3, 0.0, 0.3),
diffuse=(0.3, 0.3, 0.3),
specular=(0.0, 0.0, 0.0)
)
object = Lines(geometry, material)
base.add(object)
obj = loader.load(stl_file, material)
obj.pos.z = 0

base.rot.x = -90
scene.add(base)
scene = Scene()
scene.add(obj)

self.camera = PerspectiveCamera(90, 0.3, 0.1, 1000)
self.camera.pos.z = 1.5
self.camera.look_at((0, 0, 0))
camera = self.camera = PerspectiveCamera(75, 0.3, 0.5, 1000)
camera.pos.z = 1.5
camera.look_at((0, 0, 0))
camera.bind_to(renderer)

self.camera.bind_to(self.renderer)
self.renderer.render(scene, self.camera)
renderer.render(scene, camera)
renderer.bind(size=self._adjust_aspect)

self.add_widget(self.renderer, index=30)
self.orbit = OrbitControlWidget(self.renderer, 4.0)
self.add_widget(self.orbit, index=99)
# self.add_widget(self.selection_widget, index=98)
self.renderer.bind(size=self._adjust_aspect)
root = FloatLayout()
root.add_widget(renderer)
root.add_widget(OrbitControlWidget(renderer, 4.0))
return root

def _adjust_aspect(self, inst, val):
rsize = self.renderer.size
aspect = rsize[0] / float(rsize[1])
self.renderer.camera.aspect = aspect


class VisualisationApp(App):
def build(self):

return VisualisationWidget()
size = self.renderer.size
self.camera.aspect = size[0] / float(size[1])


if __name__ == "__main__":
from kivy.config import Config

# Config.set('input', 'mouse', 'mouse,disable_multitouch')
VisualisationApp().run()
STLLoaderApp().run()