export_meshes.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 # Copyright (C) 2014 Aldebaran
00004 #
00005 # Licensed under the Apache License, Version 2.0 (the "License");
00006 # you may not use this file except in compliance with the License.
00007 # You may obtain a copy of the License at
00008 #
00009 # http://www.apache.org/licenses/LICENSE-2.0
00010 #
00011 # Unless required by applicable law or agreed to in writing, software
00012 # distributed under the License is distributed on an "AS IS" BASIS,
00013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 # See the License for the specific language governing permissions and
00015 # limitations under the License.
00016 #
00017 # export_meshes.py
00018 # Authors: Mikael Arguedas [mikael.arguedas@gmail.com]
00019 
00020 # This script opens a blender file, exports all meshes to collada,
00021 # merges every mesh group and exports them as .mesh file (OGRE),
00022 # removes the offset from the colladas, decimates them
00023 # exports collision meshes in STL format.
00024 # Finally cleans your output directory of every .xml files
00025 # This is an Aldebaran robots specific scripts.
00026 # It will very likely not work on blender files of robots from other companies
00027 
00028 from __future__ import print_function
00029 import os
00030 import argparse
00031 import subprocess
00032 
00033 parser = argparse.ArgumentParser(usage='Export meshes and convert them')
00034 parser.add_argument('-b', '--blenderdir', default='/usr/bin',
00035                     help='location of your blender directory')
00036 parser.add_argument('-f', '--file', default='nao-v4.blend',
00037                     help='full path of the blender file to process')
00038 parser.add_argument('-o', '--outputmeshdir', default=None,
00039                     help='directory to export the meshes to')
00040 
00041 args = parser.parse_args()
00042 
00043 if os.path.basename(args.file).lower().startswith('nao'):
00044     robot = 'nao'
00045     version = 'V40'
00046     suffix = '_meshes'
00047     scale = 0.01
00048 elif os.path.basename(args.file).lower().startswith('juliette') or \
00049         os.path.basename(args.file).lower().startswith('pepper'):
00050     robot = 'pepper'
00051     version = ''
00052     suffix = '_description'
00053     scale = 0.01
00054 elif os.path.basename(args.file).lower().startswith('romeo'):
00055     robot = 'romeo'
00056     version = ''
00057     suffix = '_description'
00058     scale = 1
00059 else:
00060     print('robot name unknown')
00061     exit(1)
00062 package = robot + suffix
00063 
00064 if args.outputmeshdir is None:
00065     print('\nno valid output directory, looking for ' + package +
00066           ' ROS package\n')
00067     cmd = 'rospack find ' + package
00068     path_meshes = subprocess.check_output(cmd,
00069                                           stderr=subprocess.STDOUT,
00070                                           shell=True)[:-1]
00071     if not path_meshes:
00072         print('package "' + path_meshes + '" not found')
00073 else:
00074     if not os.path.isdir(args.outputmeshdir):
00075         print('creating the output folder because it does not exist')
00076         os.makedirs(args.outputmeshdir)
00077     path_meshes = args.outputmeshdir
00078 
00079 extractor_path = subprocess.check_output('rospack find naoqi_tools',
00080                                          stderr=subprocess.STDOUT,
00081                                          shell=True)[:-1]
00082 script_path = os.path.join(extractor_path,
00083                            'scripts', 'blender')
00084 
00085 if version:
00086     path_meshes = os.path.join(path_meshes, version)
00087 
00088 print("extractor path :" + extractor_path)
00089 
00090 # Export meshes as collada files and all the materials of the scene
00091 os.system('./run_blender_script.py -s io_export_visual.py -o ' + path_meshes +
00092           ' -f ' + args.file)
00093 
00094 # Import collada files one by one
00095 # export each of them as a single .mesh(OGRE) file
00096 os.system('./run_blender_script.py -s io_export_ogre.py -i ' + path_meshes)
00097 
00098 # Normalize collada meshes to give them the right scale and orientation
00099 os.system('./run_blender_script.py -s normalize_meshes.py -i ' + path_meshes +
00100           ' --scale ' + str(scale))
00101 
00102 # Import all collada meshes, decimate them and export them as stl files
00103 os.system('./run_blender_script.py -s io_export_collision.py -i ' +
00104           path_meshes)
00105 
00106 # Remove files left by the OGRE exporter
00107 file_list = sorted(os.listdir(path_meshes))
00108 for file in file_list:
00109     if file.endswith('.mesh.xml') or file.endswith('.mesh') or \
00110             file.endswith('.material'):
00111         print('removing ' + file)
00112         os.remove(os.path.join(path_meshes, file))


naoqi_tools
Author(s): Mikael Arguedas
autogenerated on Wed Aug 16 2017 02:28:16