run_blender_script.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 # Copyright (C) 2014 Mikael Arguedas
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 # run_blender_script.py
00018 # Authors: Mikael Arguedas [mikael.arguedas@gmail.com]
00019 
00020 # This script is a launcher for every blender script available in naoqi_tools
00021 # It defines a set of parameter and format them to sned the right bash commands
00022 # to Launch blender with the chosen script and the right arguments
00023 
00024 from __future__ import print_function
00025 import os
00026 import argparse
00027 import subprocess
00028 
00029 parser = argparse.ArgumentParser(usage='Export meshes and convert them')
00030 parser.add_argument('-s', '--scriptfile', default='io_export_collision.py',
00031                     help='name of the blender script to Launch')
00032 parser.add_argument('-b', '--blenderdir', default='/usr/bin',
00033                     help='location of your blender directory')
00034 parser.add_argument('-i', '--inputmeshdir', default=None,
00035                     help='directory where collada meshes are located')
00036 parser.add_argument('-o', '--outputdir', default=None,
00037                     help='directory to export the meshes to')
00038 parser.add_argument('-r', '--ratio', default=0.1,
00039                     help='float value, ratio used to decimate meshes',
00040                     type=float)
00041 parser.add_argument('-t', '--threshold', default=50,
00042                     help='integer: minimum number of vertices for decimation',
00043                     type=int)
00044 parser.add_argument('--scale', default=1,
00045                     help='scale to resize the collada meshes to', type=float)
00046 parser.add_argument('-n', '--outputfilename', default=None,
00047                     help='name of the output file (for material file)')
00048 parser.add_argument('-f', '--blenderfile', default=None,
00049                     help='path of the blender file to process')
00050 
00051 args = parser.parse_args()
00052 
00053 clean = False
00054 
00055 
00056 def check_output_dir(out):
00057     if not os.path.isdir(out):
00058         print('creating the output folder because it does not exist')
00059         os.makedirs(out)
00060     return out
00061 
00062 # Create path to script
00063 script_path = subprocess.check_output('rospack find naoqi_tools',
00064                                       stderr=subprocess.STDOUT,
00065                                       shell=True)[:-1]
00066 script_path = os.path.join(script_path, 'scripts', 'blender', args.scriptfile)
00067 # Check script existence
00068 if(not os.path.isfile(script_path)):
00069     print("script doesn't exist\nExiting now")
00070     exit(1)
00071 
00072 # Check if args.outputdir is valid
00073 if args.scriptfile != 'io_export_visual.py':
00074     if args.outputdir is None:
00075         print('\nno valid output directory: using ' + str(args.inputmeshdir) +
00076               ' as destination folder')
00077         output_dir = args.inputmeshdir
00078     else:
00079         output_dir = check_output_dir(args.outputdir)
00080 
00081     # Check existence of the input directory
00082     if args.inputmeshdir is None or not os.path.isdir(args.inputmeshdir):
00083         print('Invalid mesh folder provided\nExiting now')
00084         exit(1)
00085 else:
00086     if args.outputdir is None:
00087         output_dir = os.path.dirname(args.blenderfile)
00088         print('\nno valid output directory: using ' +
00089               output_dir + ' as destination folder')
00090     else:
00091         output_dir = check_output_dir(args.outputdir)
00092 
00093 # Set the parameters and bash command for each script
00094 if(args.scriptfile == 'normalize_meshes.py'):
00095     cmd = (script_path + ' -i ' + args.inputmeshdir + ' -s ' +
00096            str(args.scale) + ' -o ' + output_dir)
00097 
00098 elif(args.scriptfile == 'io_export_collision.py'):
00099     cmd = (args.blenderdir + '/blender --background -P ' + script_path +
00100            ' -- ' + args.inputmeshdir + ' ' + str(args.ratio) + ' ' +
00101            str(args.threshold) + ' ' + output_dir)
00102 
00103 elif(args.scriptfile == 'io_export_visual.py'):
00104     if(args.blenderfile is None or not os.path.isfile(args.blenderfile)):
00105         print('invalid blender file provided\nExiting now')
00106         exit(1)
00107     if(args.outputfilename is None):
00108         basename = os.path.basename(args.blenderfile)
00109         print('no name specified for output material file. unsing: ' +
00110               basename)
00111     else:
00112         basename = os.path.basename(args.outputfilename)
00113     if(basename.rfind('.') != -1):
00114         basename = basename[0:basename.rfind('.')]
00115     print('exporting material to ' + basename + '.material')
00116     cmd = (os.path.join(args.blenderdir, 'blender') + ' ' + args.blenderfile +
00117            ' -P ' + script_path + ' -- ' + output_dir +
00118            ' ' + basename)
00119 
00120 elif(args.scriptfile == 'io_export_ogre.py'):
00121     cmd = ((os.path.join(args.blenderdir, 'blender')) + ' -P ' + script_path +
00122            ' -- ' + args.inputmeshdir + ' ' + output_dir)
00123     clean = True
00124 
00125 os.system(cmd)
00126 
00127 # If ogre exporter called
00128 # Remove files left behind by the OGRE exporter
00129 if clean is True:
00130     file_list = sorted(os.listdir(output_dir))
00131     for file in file_list:
00132         if file.endswith('.mesh.xml'):
00133             print('removing ' + file)
00134             os.remove(os.path.join(output_dir, file))


naoqi_tools
Author(s): Mikael Arguedas
autogenerated on Sat Jun 8 2019 20:30:21