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 def check_output_dir(out):
00056     if not os.path.isdir(out):
00057         print('creating the output folder because it does not exist')
00058         os.makedirs(out)
00059     return out
00060 
00061 # Create path to script
00062 script_path = subprocess.check_output('rospack find naoqi_tools',
00063                                       stderr=subprocess.STDOUT, shell=True)[:-1]
00064 script_path = os.path.join(script_path, 'scripts','blender',args.scriptfile)
00065 # Check script existence
00066 if(not os.path.isfile(script_path)):
00067     print("script doesn't exist\nExiting now")
00068     exit(1)
00069 
00070 # Check if args.outputdir is valid
00071 if args.scriptfile != 'io_export_visual.py':
00072     if args.outputdir == None :
00073         print("\nno valid output directory: using " + str(args.inputmeshdir) +
00074               " as destination folder")
00075         output_dir = args.inputmeshdir
00076     else:
00077       output_dir = check_output_dir(args.outputdir)
00078 
00079     # Check existence of the input directory
00080     if args.inputmeshdir == None or not os.path.isdir(args.inputmeshdir):
00081         print("Invalid mesh folder provided\nExiting now")
00082         exit(1)
00083 else:
00084     if args.outputdir == None :
00085         output_dir = os.path.dirname(args.blenderfile)
00086         print("\nno valid output directory: using " +
00087               output_dir + " as destination folder")
00088     else:
00089         output_dir = check_output_dir(args.outputdir)
00090 
00091 # Set the parameters and bash command for each script
00092 if(args.scriptfile == 'normalize_meshes.py'):
00093     cmd = (script_path + ' -i ' + args.inputmeshdir + ' -s ' +
00094            str(args.scale) + ' -o ' + output_dir)
00095 
00096 elif(args.scriptfile == 'io_export_collision.py'):
00097     cmd = (args.blenderdir + '/blender --background -P '+ script_path +
00098            ' -- ' + args.inputmeshdir + ' ' + str(args.ratio) + ' '
00099            + str(args.threshold) + ' ' + output_dir)
00100 
00101 elif(args.scriptfile == 'io_export_visual.py'):
00102     if(args.blenderfile == None or not os.path.isfile(args.blenderfile)):
00103         print("invalid blender file provided\nExiting now")
00104         exit(1)
00105     if(args.outputfilename == None):
00106         basename = os.path.basename(args.blenderfile)
00107         print("no name specified for output material file. unsing: " +
00108               basename)
00109     else:
00110         basename = os.path.basename(args.outputfilename)
00111     if(basename.rfind('.')!= -1):
00112         basename = basename[0:basename.rfind('.')]
00113     print("exporting material to " + basename + ".material")
00114     cmd = (os.path.join(args.blenderdir,"blender") + ' ' + args.blenderfile +
00115            ' -P ' + script_path + ' -- ' + output_dir
00116            + ' ' + basename)
00117 
00118 elif(args.scriptfile == 'io_export_ogre.py'):
00119     cmd = ((os.path.join(args.blenderdir,"blender")) + " -P " + script_path
00120            + " -- " + args.inputmeshdir + ' ' + output_dir)
00121     clean = True
00122 
00123 os.system(cmd)
00124 
00125 # If ogre exporter called
00126 # Remove files left behind by the OGRE exporter
00127 if clean == True:
00128     file_list = sorted(os.listdir(output_dir))
00129     for file in file_list:
00130         if file.endswith('.mesh.xml'):
00131             print('removing ' + file)
00132             os.remove(os.path.join(output_dir , file))


naoqi_tools
Author(s): Mikael Arguedas
autogenerated on Thu Aug 27 2015 14:05:48