From bca635e85bbd161a76d8727c9c55bfe6c2d8de68 Mon Sep 17 00:00:00 2001 From: "Olivier J.N. Bertrand" <olivier.bertrand@uni-bielefeld.de> Date: Fri, 1 Jun 2018 17:29:31 +0200 Subject: [PATCH] Start to add two new script to overlay a trajectory in a blender environment (in order to debug misaligment between world and trajectory to render images along a trajectory --- navipy/sensors/blend_alongtraj.py | 121 +++++++++++++++++++++++++++ navipy/sensors/blend_overlaytraj.py | 122 ++++++++++++++++++++++++++++ 2 files changed, 243 insertions(+) create mode 100644 navipy/sensors/blend_alongtraj.py create mode 100644 navipy/sensors/blend_overlaytraj.py diff --git a/navipy/sensors/blend_alongtraj.py b/navipy/sensors/blend_alongtraj.py new file mode 100644 index 0000000..cac6173 --- /dev/null +++ b/navipy/sensors/blend_alongtraj.py @@ -0,0 +1,121 @@ +""" + +""" +import sys +import argparse +import os +import inspect +import pkg_resources +import tempfile +# Following need to be imported in blender as well +from navipy.sensors.renderer import BlenderRender +from navipy.tools.trajectory import Trajectory + + +importwithinblender = [ + 'from navipy.sensors.renderer import BlenderRender', + 'from navipy.tools.trajectory import Trajectory'] + + +def parser_blend_alongtraj(): + # Create command line options + parser = argparse.ArgumentParser() + arghelp = 'Path to the environment (.blend) in which your agent lives' + defaultworld = pkg_resources.resource_filename( + 'navipy', 'resources/twocylinders_world.blend') + defaultconfig = pkg_resources.resource_filename( + 'navipy', 'resources/configs/BlenderRender.yaml') + defaultoutput = tempfile.NamedTemporaryFile().name + defaulttraj = pkg_resources.resource_filename( + 'navipy', 'resources/twocylinders_traj.hdf') + parser.add_argument('--blender-world', + type=str, + default=defaultworld, + help=arghelp) + arghelp = 'Outputfile to store the rendered database' + parser.add_argument('--output-file', + type=str, + default=defaultoutput, + help=arghelp) + arghelp = 'Configuration file' + parser.add_argument('--config-file', + type=str, + default=defaultconfig, + help=arghelp) + arghelp = 'File containing the trajectory' + parser.add_argument('--trajectory', + type=str, + default=defaulttraj, + help=arghelp) + arghelp = 'Command to run blender\n' + arghelp += 'If not provided, the script will try to find the command' + arghelp += " by using: shutil.which('blender')" + parser.add_argument('--blender-command', + type=str, + default=None, + help=arghelp) + + arghelp = 'To display some stuff \n' + arghelp += ' * -v print command \n' + arghelp += ' * -vv print also script' + parser.add_argument('-v', '--verbose', + action='count', + default=0, + help=arghelp) + + return parser + + +def run(config_file, outputfile, trajfile): + renderer = BlenderRender() + renderer.config_file = config_file + # Load trajectory + trajectory = Trajectory().fromfile(trajfile) + renderer.render_trajectory(outputfile, trajectory) + + +def main(): + # encoding for temporary file + encoding = 'utf-8' + + # Fetch arguments + args = parser_blend_alongtraj().parse_args() + # Some output + print('-----') + print('Config file:\n{}'.format(args.config_file)) + print('Blender file:\n{}'.format(args.blender_world)) + print('Output file:\n{}'.format(args.output_file)) + print('-----') + # Create tempfile with testing code and then call blendnavipy + header = '# Generated by {}\n'.format(sys.argv[0]) + with tempfile.NamedTemporaryFile() as tfile: + # Start of file + tfile.write(header.encode(encoding)) + for line in importwithinblender: + tfile.write(line.encode(encoding)) + tfile.write('\n'.encode(encoding)) + for line in inspect.getsourcelines(run)[0]: + tfile.write(line.encode(encoding)) + tfile.write('\n\n'.encode(encoding)) + tfile.write('try:\n'.encode(encoding)) + tfile.write(' run("{}","{}","{}")\n'.format( + args.config_file, + args.output_file, + args.trajectory).encode(encoding)) + tfile.write(' sys.exit(0)\n'.encode(encoding)) + tfile.write('except Exception:\n'.encode(encoding)) + tfile.write(' sys.exit(1)\n'.encode(encoding)) + tfile.seek(0) + + command = 'blendnavipy --blender-world {} --python-script {}' + command = command.format(args.blender_world, tfile.name) + if args.blender_command is not None: + command += ' --blender-command {}'.format(args.blender_command) + for _ in range(args.verbose): + command += ' -v' + os.system(command) + + +if __name__ == "__main__": + # execute only if run as a script + main() diff --git a/navipy/sensors/blend_overlaytraj.py b/navipy/sensors/blend_overlaytraj.py new file mode 100644 index 0000000..8b35bac --- /dev/null +++ b/navipy/sensors/blend_overlaytraj.py @@ -0,0 +1,122 @@ +""" + +""" +import sys +import argparse +import os +import inspect +import pkg_resources +import tempfile +# Following need to be imported in blender as well +from navipy.tools.trajectory import Trajectory + + +importwithinblender = [ + 'from navipy.tools.trajectory import Trajectory'] + + +def parser_blend_overlaytraj(): + # Create command line options + parser = argparse.ArgumentParser() + arghelp = 'Path to the environment (.blend) in which your agent lives' + defaultworld = pkg_resources.resource_filename( + 'navipy', 'resources/twocylinders_world.blend') + defaulttraj = pkg_resources.resource_filename( + 'navipy', 'resources/twocylinders_traj.hdf') + parser.add_argument('--blender-world', + type=str, + default=defaultworld, + help=arghelp) + arghelp = 'File containing the trajectory' + parser.add_argument('--trajectory', + type=str, + default=defaulttraj, + help=arghelp) + arghelp = 'Command to run blender\n' + arghelp += 'If not provided, the script will try to find the command' + arghelp += " by using: shutil.which('blender')" + parser.add_argument('--blender-command', + type=str, + default=None, + help=arghelp) + arghelp = 'To display some stuff \n' + arghelp += ' * -v print command \n' + arghelp += ' * -vv print also script' + parser.add_argument('-v', '--verbose', + action='count', + default=0, + help=arghelp) + + return parser + + +def run(trajfile): + import bpy + # Load trajectory + trajectory = Trajectory().fromfile(trajfile) + # create the Curve Datablock + curveData = bpy.data.curves.new('myCurve', type='CURVE') + curveData.dimensions = '3D' + curveData.resolution_u = 2 + # map coords to spline + polyline = curveData.splines.new('POLY') + polyline.points.add(trajectory.shape[0]) + i = 0 + for _, coord in trajectory.iterrows(): + polyline.points[i].co = (coord.loc[('location', 'x')], + coord.loc[('location', 'y')], + coord.loc[('location', 'z')], + 1) + + # create Object + curveOB = bpy.data.objects.new('my_trajectory', curveData) + # attach to scene and validate context + scn = bpy.context.scene + scn.objects.link(curveOB) + scn.objects.active = curveOB + curveOB.select = True + + +def main(): + # encoding for temporary file + encoding = 'utf-8' + + # Fetch arguments + args = parser_blend_overlaytraj().parse_args() + # Some output + print('-----') + print('Config file:\n{}'.format(args.config_file)) + print('Blender file:\n{}'.format(args.blender_world)) + print('Output file:\n{}'.format(args.output_file)) + print('-----') + # Create tempfile with testing code and then call blendnavipy + header = '# Generated by {}\n'.format(sys.argv[0]) + with tempfile.NamedTemporaryFile() as tfile: + # Start of file + tfile.write(header.encode(encoding)) + for line in importwithinblender: + tfile.write(line.encode(encoding)) + tfile.write('\n'.encode(encoding)) + for line in inspect.getsourcelines(run)[0]: + tfile.write(line.encode(encoding)) + tfile.write('\n\n'.encode(encoding)) + tfile.write('try:\n'.encode(encoding)) + tfile.write(' run("{}")\n'.format( + args.trajectory).encode(encoding)) + tfile.write(' sys.exit(0)\n'.encode(encoding)) + tfile.write('except Exception:\n'.encode(encoding)) + tfile.write(' sys.exit(1)\n'.encode(encoding)) + tfile.seek(0) + + command = 'blendnavipy --blender-world {} --python-script {}' + command = command.format(args.blender_world, tfile.name) + if args.blender_command is not None: + command += ' --blender-command {}'.format(args.blender_command) + for _ in range(args.verbose): + command += ' -v' + os.system(command) + + +if __name__ == "__main__": + # execute only if run as a script + main() -- GitLab