def multiwindow_template(world): """Tests multiple windows and views.""" vis.add("world", world) vp = vis.getViewport() vp.w, vp.h = 400, 600 vis.setViewport(vp) vis.addText("label1", "This is Window 1", (20, 20)) vis.setWindowTitle("Window 1") vis.show() id1 = vis.getWindow() print("First window ID:", id1) id2 = vis.createWindow("Window 2") vis.add("Lone point", [0, 0, 0]) vis.setViewport(vp) vis.addText("label1", "This is Window 2", (20, 20)) print("Second window ID:", vis.getWindow()) vis.setWindow(id2) vis.spin(float('inf')) #restore back to 1 window, clear the text vis.setWindow(id1) vis.clearText() vp = vis.getViewport() vp.w, vp.h = 800, 800 vis.setViewport(vp) vis.setWindowTitle("vis.spin test: will close in 5 seconds...") vis.spin(5.0) #Now testing ability to re-launch windows vis.setWindowTitle("Shown again. Close me to proceed.") vis.spin(float('inf')) vis.setWindowTitle("Dialog test. Close me to proceed.") vp = vis.getViewport() vp.w, vp.h = 400, 600 vis.setViewport(vp) vis.dialog() vp.w, vp.h = 640, 480 vis.setViewport(vp) for i in range(3): widgets = GLWidgetPlugin() widgets.addWidget(RobotPoser(world.robot(0))) vis.addPlugin(widgets) vis.setWindowTitle("Split screen test") vis.spin(float('inf')) vis.setPlugin(None) vis.setWindowTitle("Back to normal. Close me to quit.") vis.dialog() vis.kill()
from klampt import vis import klampt import time world = WorldModel() world_file = "../data/simulation_test_worlds/drc_rough_terrain_world.xml" if not world.readFile(world_file): raise RuntimeError("Unable to load terrain model") robot_file = "../data/robot_model/robosimian_caesar_new.rob" if not world.readFile(robot_file): raise RuntimeError("Unable to load robot model") vis_window_id = vis.createWindow(world_file) vis.setWindow(vis_window_id) vis.add("world", world) vp = vis.getViewport() vis.setViewport(vp) vis.autoFitCamera() world.robot(0).setConfig([ 12.621508747630084, 1.3060978650033888, 0.7271994997360561, -0.18389666460947365, -0.2336561986984183, 0.23915345995072382, 0.0, 0.12877367095232392,
def launchwindow(): origwindow = vis.getWindow() vis.createWindow("Pop up window") vis.add("world2", world) vis.show() vis.setWindow(origwindow)
def make_object_pile(world,container,objects,container_wall_thickness=0.01,randomize_orientation=True, visualize=False,verbose=0): """For a given container and a list of objects in the world, drops the objects inside the container and simulates until stable. Args: world (WorldModel): the world containing the objects and obstacles container: the container RigidObject / Terrain in world into which objects should be spawned. Assumed axis-aligned. objects (list of RigidObject): a list of RigidObjects in the world, at arbitrary locations. They are placed in order. container_wall_thickness (float, optional): a margin subtracted from the container's outer dimensions into which the objects are spawned. randomize_orientation (bool or str, optional): if True, the orientation of the objects are completely randomized. If 'z', only the z orientation is randomized. If False or None, the orientation is unchanged visualize (bool, optional): if True, pops up a visualization window to show the progress of the pile verbose (int, optional): if > 0, prints progress of the pile. Side effect: the positions of objects in world are modified Returns: (tuple): (world,sim), containing - world (WorldModel): the original world - sim (Simulator): the Simulator instance at the state used to obtain the stable placement of the objects. Note: Since world is modified in-place, if you wish to make multiple worlds with piles of the same objects, you should use world.copy() to store the configuration of the objects. You may also wish to randomize the object ordering using random.shuffle(objects) between instances. """ container_outer_bb = _get_bound(container) container_inner_bb = (vectorops.add(container_outer_bb[0],[container_wall_thickness]*3),vectorops.sub(container_outer_bb[1],[container_wall_thickness]*3)) spawn_area = (container_inner_bb[0][:],container_inner_bb[1][:]) collision_margin = 0.0025 if visualize: from klampt import vis from klampt.model import config import time oldwindow = vis.getWindow() newwindow = vis.createWindow("make_object_pile dynamic visualization") vis.setWindow(newwindow) vis.show() visworld = world.copy() vis.add("world",visworld) sim = Simulator(world) sim.setSetting("maxContacts","20") sim.setSetting("adaptiveTimeStepping","0") Tfar = (so3.identity(),[0,0,-100000]) for object in objects: R,t = object.getTransform() object.setTransform(R,Tfar[1]) sim.body(object).setTransform(*Tfar) sim.body(object).enable(False) if verbose: print("Spawn area",spawn_area) if visualize: vis.lock() config.setConfig(visworld,config.getConfig(world)) vis.unlock() for index in range(len(objects)): #always spawn above the current height of the pile if index > 0: objects_bound = _get_bound(objects[:index]) if verbose: print("Existing objects bound:",objects_bound) zshift = max(0.0,objects_bound[1][2] - spawn_area[0][2]) spawn_area[0][2] += zshift spawn_area[1][2] += zshift object = objects[index] obb = _get_bound(object) zmin = obb[0][2] R0,t0 = object.getTransform() feasible = False for sample in range(1000): R,t = R0[:],t0[:] if randomize_orientation == True: R = so3.sample() t[2] = spawn_area[1][2] - zmin + t0[2] + collision_margin object.setTransform(R,t) xy_randomize(object,spawn_area[0],spawn_area[1]) if verbose: print("Sampled position of",object.getName(),object.getTransform()[1]) if not randomize_orientation: _,t = object.getTransform() object.setTransform(R,t) #object spawned, now settle sobject = sim.body(object) sobject.enable(True) sobject.setTransform(*object.getTransform()) res = sim.checkObjectOverlap() if len(res[0]) == 0: feasible = True #get it low as possible without overlapping R,t = object.getTransform() for lower in range(100): sobject.setTransform(R,vectorops.add(t,[0,0,-(lower+1)*0.01])) res = sim.checkObjectOverlap() if len(res[0]) != 0: if verbose: print("Terminated lowering at",lower,"cm lower") sobject.setTransform(R,vectorops.add(t,[0,0,-lower*0.01])) res = sim.checkObjectOverlap() break sim.updateWorld() break if not feasible: if verbose: print("Failed to place object",object.getName()) return None if visualize: vis.lock() config.setConfig(visworld,config.getConfig(world)) vis.unlock() time.sleep(0.1) if verbose: print("Beginning to simulate") #start letting everything fall for firstfall in range(10): sim.simulate(0.01) if visualize: vis.lock() config.setConfig(visworld,config.getConfig(world)) vis.unlock() time.sleep(0.01) maxT = 5.0 dt = 0.01 t = 0.0 wdamping = -0.01 vdamping = -0.1 while t < maxT: settled = True for object in objects: sobject = sim.body(object) w,v = sobject.getVelocity() sobject.applyWrench(vectorops.mul(v,vdamping),vectorops.mul(w,wdamping)) if vectorops.norm(w) + vectorops.norm(v) > 1e-4: #settled settled=False break if settled: break if visualize: t0 = time.time() sim.simulate(dt) if visualize: vis.lock() config.setConfig(visworld,config.getConfig(world)) vis.unlock() time.sleep(max(0.0,dt-(time.time()-t0))) t += dt if visualize: vis.show(False) vis.setWindow(oldwindow) return (world,sim)
def make_object_pile(world,container,objects,container_wall_thickness=0.01,randomize_orientation=True,visualize=False): """For a given container and a list of objects in the world, drops the objects inside the container and simulates until stable. Arguments: - world: a WorldModel - container: the container RigidObject / Terrain in world over which objects should be spawned. Assumed axis-aligned and with an open top. - objects: a list of RigidObjects in the world, at arbitrary locations. They are placed in order. - container_wall_thickness: a margin subtracted from the container's outer dimensions into which the objects are spawned. - randomize_orientation: if True, the orientation of the objects are completely randomized. If 'z', only the z orientation is randomized. If False or None, the orientation is unchanged Side effect: the positions of objects in world are modified Return value (world,sim): - world: the original world - sim: the Simulator instance at the state used to obtain the stable placement of the objects. Note: Since world is modified in-place, if you wish to make multiple worlds with piles of the same objects, you should use world.copy() to store the configuration of the objects. You may also wish to randomize the object ordering using random.shuffle(objects) between instances. """ container_outer_bb = get_bound(container) container_inner_bb = (vectorops.add(container_outer_bb[0],[container_wall_thickness]*3),vectorops.sub(container_outer_bb[1],[container_wall_thickness]*3)) spawn_area = (container_inner_bb[0][:],container_inner_bb[1][:]) collision_margin = 0.0025 if visualize: from klampt import vis from klampt.model import config import time oldwindow = vis.getWindow() newwindow = vis.createWindow("make_object_pile dynamic visualization") vis.setWindow(newwindow) vis.show() visworld = world.copy() vis.add("world",visworld) sim = Simulator(world) sim.setSetting("maxContacts","20") sim.setSetting("adaptiveTimeStepping","0") Tfar = (so3.identity(),[0,0,-100000]) for object in objects: R,t = object.getTransform() object.setTransform(R,Tfar[1]) sim.body(object).setTransform(*Tfar) sim.body(object).enable(False) print "Spawn area",spawn_area if visualize: vis.lock() config.setConfig(visworld,config.getConfig(world)) vis.unlock() for index in xrange(len(objects)): #always spawn above the current height of the pile if index > 0: objects_bound = get_bound(objects[:index]) print "Existing objects bound:",objects_bound zshift = max(0.0,objects_bound[1][2] - spawn_area[0][2]) spawn_area[0][2] += zshift spawn_area[1][2] += zshift object = objects[index] obb = get_bound(object) zmin = obb[0][2] R0,t0 = object.getTransform() feasible = False for sample in xrange(1000): R,t = R0[:],t0[:] if randomize_orientation == True: R = so3.sample() t[2] = spawn_area[1][2] - zmin + t0[2] + collision_margin object.setTransform(R,t) xy_randomize(object,spawn_area[0],spawn_area[1]) print "Sampled position of",object.getName(),object.getTransform()[1] if not randomize_orientation: _,t = object.getTransform() object.setTransform(R,t) #object spawned, now settle sobject = sim.body(object) sobject.enable(True) sobject.setTransform(*object.getTransform()) res = sim.checkObjectOverlap() if len(res[0]) == 0: feasible = True #get it low as possible without overlapping R,t = object.getTransform() for lower in xrange(100): sobject.setTransform(R,vectorops.add(t,[0,0,-(lower+1)*0.01])) res = sim.checkObjectOverlap() if len(res[0]) != 0: print "Terminated lowering at",lower,"cm lower" sobject.setTransform(R,vectorops.add(t,[0,0,-lower*0.01])) res = sim.checkObjectOverlap() break sim.updateWorld() break if not feasible: print "Failed to place object",object.getName() return None if visualize: vis.lock() config.setConfig(visworld,config.getConfig(world)) vis.unlock() time.sleep(0.1) print "Beginning to simulate" #start letting everything fall for firstfall in xrange(10): sim.simulate(0.01) if visualize: vis.lock() config.setConfig(visworld,config.getConfig(world)) vis.unlock() time.sleep(0.01) maxT = 5.0 dt = 0.01 t = 0.0 wdamping = -0.01 vdamping = -0.1 while t < maxT: settled = True for object in objects: sobject = sim.body(object) w,v = sobject.getVelocity() sobject.applyWrench(vectorops.mul(v,vdamping),vectorops.mul(w,wdamping)) if vectorops.norm(w) + vectorops.norm(v) > 1e-4: #settled settled=False break if settled: break if visualize: t0 = time.time() sim.simulate(dt) if visualize: vis.lock() config.setConfig(visworld,config.getConfig(world)) vis.unlock() time.sleep(max(0.0,dt-(time.time()-t0))) t += dt if visualize: vis.show(False) vis.setWindow(oldwindow) return (world,sim)
def grasp_edit_ui(gripper, object, grasp=None): assert gripper.klampt_model is not None world = WorldModel() res = world.readFile(gripper.klampt_model) if not res: raise ValueError("Unable to load klampt model") robot = world.robot(0) base_link = robot.link(gripper.base_link) base_xform = base_link.getTransform() base_xform0 = base_link.getTransform() parent_xform = se3.identity() if base_link.getParent() >= 0: parent_xform = robot.link(base_link.getParent()).getTransform() if grasp is not None: base_xform = grasp.ik_constraint.closestMatch(*base_xform) base_link.setParentTransform( *se3.mul(se3.inv(parent_xform), base_xform)) robot.setConfig( gripper.set_finger_config(robot.getConfig(), grasp.finger_config)) q0 = robot.getConfig() grob = gripper.get_subrobot(robot) grob._links = [l for l in grob._links if l != gripper.base_link] #set up visualizer oldWindow = vis.getWindow() if oldWindow is None: oldWindow = vis.createWindow() vis.createWindow() vis.add("gripper", grob) vis.edit("gripper") vis.add("object", object) vis.add("base_xform", base_xform) vis.edit("base_xform") def make_grasp(): return Grasp(ik.objective(base_link, R=base_xform[0], t=base_xform[1]), gripper.finger_links, gripper.get_finger_config(robot.getConfig())) #add hooks robot_appearances = [ robot.link(i).appearance().clone() for i in range(robot.numLinks()) ] robot_shown = [True] def toggle_robot(arg=0, data=robot_shown): vis.lock() if data[0]: for i in range(robot.numLinks()): if i not in grob._links and i != gripper.base_link: robot.link(i).appearance().setDraw(False) data[0] = False else: for i in range(robot.numLinks()): if i not in grob._links and i != gripper.base_link: robot.link(i).appearance().set(robot_appearances[i]) data[0] = True vis.unlock() def randomize(): print("TODO") def reset(): vis.lock() robot.setConfig(q0) base_link.setParentTransform( *se3.mul(se3.inv(parent_xform), base_xform0)) vis.unlock() vis.add("base_xform", base_xform0) vis.edit("base_xform") vis.setItemConfig("gripper", grob.getConfig()) def save(): fmt = gripper.name + "_" + object.getName() + '_grasp_%d.json' ind = 0 while os.path.exists(fmt % (ind, )): ind += 1 fn = fmt % (ind, ) g = make_grasp() print("Saving grasp to", fn) with open(fn, 'w') as f: json.dump(g.toJson(), f) vis.addAction(toggle_robot, 'Toggle show robot', 'v') vis.addAction(randomize, 'Randomize', 'r') vis.addAction(reset, 'Reset', '0') vis.addAction(save, 'Save to disk', 's') def loop_setup(): vis.show() def loop_callback(): global base_xform xform = vis.getItemConfig("base_xform") base_xform = (xform[:9], xform[9:]) vis.lock() base_link.setParentTransform( *se3.mul(se3.inv(parent_xform), base_xform)) vis.unlock() def loop_cleanup(): vis.show(False) vis.loop(setup=loop_setup, callback=loop_callback, cleanup=loop_cleanup) # this works with Linux/Windows, but not Mac # loop_setup() # while vis.shown(): # loop_callback() # time.sleep(0.02) # loop_cleanup() g = make_grasp() #restore RobotModel base_link.setParentTransform(*se3.mul(se3.inv(parent_xform), base_xform0)) vis.setWindow(oldWindow) return g