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

14.) Cleanup selection example #21

Open
wants to merge 3 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
127 changes: 44 additions & 83 deletions examples/selection/main.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,38 @@
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
from kivy3.extras.objects import ArrowObject, AxisObject
from kivy3.loaders import URDFLoader
from kivy3 import Mesh, Material, Object3D
from kivy3.widgets import OrbitControlWidget, SelectionWidget, Object3DWidget

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

from kivy3 import Material
from kivy3 import Mesh
from kivy3 import Object3D
from kivy3 import PerspectiveCamera
from kivy3 import Renderer
from kivy3 import Scene
from kivy3.extras.geometries import BoxGeometry
from kivy3.widgets import Object3DWidget
from kivy3.widgets import OrbitControlWidget
from kivy3.widgets import SelectionWidget
import os

_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, "./test.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")
shader_file = os.path.join(_this_path, "./blinnphong.glsl")


class VisualisationWidget(FloatLayout):
def __init__(self, **kw):
super(VisualisationWidget, self).__init__(**kw)
class SelectionExample(App):
"""This example shows how to make objects selectable
"""

self.renderer = Renderer(shader_file=shader_file)
self.renderer.set_clear_color((0.16, 0.30, 0.44, 1.0))
def build(self):
renderer = self.renderer = Renderer(shader_file=shader_file)
renderer.set_clear_color((0.16, 0.30, 0.44, 1.0))

self.selection_widget = SelectionWidget(self.renderer)
selection_widget = SelectionWidget(renderer)

scene = Scene()
camera = PerspectiveCamera(90, 0.3, 0.1, 1000)
camera.pos.z = 1.5
camera.look_at((0, 0, 0))
camera.bind_to(renderer)

base = Object3D()

id_color = (1, 0, 0)
geometry = BoxGeometry(1, 1, 1)
material = Material(
Expand All @@ -59,11 +41,10 @@ def __init__(self, **kw):
specular=(0.3, 0.3, 0.3),
id_color=(id_color),
)
object1 = Mesh(geometry, material)
obj_1 = Mesh(geometry, material)

widget1 = Object3DWidget(object1, self.renderer)
self.selection_widget.register(id_color, widget1)
base.add(object1)
widget_1 = Object3DWidget(obj_1, renderer)
selection_widget.register(id_color, widget_1)

id_color = (2, 0, 0)
geometry = BoxGeometry(1, 1, 1)
Expand All @@ -73,54 +54,34 @@ def __init__(self, **kw):
specular=(0.3, 0.3, 0.3),
id_color=(id_color),
)
object2 = Mesh(geometry, material)
object2.pos.x = 1
widget2 = Object3DWidget(object2, self.renderer)
self.selection_widget.register(id_color, widget2)
base.add(object2)

# arrow = AxisObject(alpha=0.5)

# loader = URDFLoader(package_path=package_path)
# obj = loader.load(urdf_file)
# self.robot = obj
obj_2 = Mesh(geometry, material)
obj_2.pos.x = 1
widget_2 = Object3DWidget(obj_2, renderer)
selection_widget.register(id_color, widget_2)

# base.add(self.robot)
# base.add(arrow)
base.rot.x = -90
scene.add(base)
base.add(obj_1)
base.add(obj_2)

# self.robot.joint_dict['rs2_joint_g'].set_position(0.5)
# self.robot.joint_dict['rs2_joint_f'].set_position(3.14)
# self.robot.joint_dict['rs2_gripper_joint_1'].set_position(1.)
scene = Scene()
scene.add(base)

self.camera = PerspectiveCamera(90, 0.3, 0.1, 1000)
self.camera.pos.z = 1.5
self.camera.look_at((0, 0, 0))
renderer.render(scene, camera)
renderer.bind(size=self._adjust_aspect)

self.camera.bind_to(self.renderer)
self.renderer.render(scene, self.camera)
orbit = OrbitControlWidget(renderer, 4.0)
root = FloatLayout()
root.add_widget(renderer, index=30)
root.add_widget(orbit, index=99)
root.add_widget(selection_widget, index=98)

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)
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()


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

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