run_blender_script.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 # Copyright (C) 2014 Mikael Arguedas
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 # run_blender_script.py
18 # Authors: Mikael Arguedas [mikael.arguedas@gmail.com]
19 
20 # This script is a launcher for every blender script available in naoqi_tools
21 # It defines a set of parameter and format them to sned the right bash commands
22 # to Launch blender with the chosen script and the right arguments
23 
24 from __future__ import print_function
25 import os
26 import argparse
27 import subprocess
28 
29 parser = argparse.ArgumentParser(usage='Export meshes and convert them')
30 parser.add_argument('-s', '--scriptfile', default='io_export_collision.py',
31  help='name of the blender script to Launch')
32 parser.add_argument('-b', '--blenderdir', default='/usr/bin',
33  help='location of your blender directory')
34 parser.add_argument('-i', '--inputmeshdir', default=None,
35  help='directory where collada meshes are located')
36 parser.add_argument('-o', '--outputdir', default=None,
37  help='directory to export the meshes to')
38 parser.add_argument('-r', '--ratio', default=0.1,
39  help='float value, ratio used to decimate meshes',
40  type=float)
41 parser.add_argument('-t', '--threshold', default=50,
42  help='integer: minimum number of vertices for decimation',
43  type=int)
44 parser.add_argument('--scale', default=1,
45  help='scale to resize the collada meshes to', type=float)
46 parser.add_argument('-n', '--outputfilename', default=None,
47  help='name of the output file (for material file)')
48 parser.add_argument('-f', '--blenderfile', default=None,
49  help='path of the blender file to process')
50 
51 args = parser.parse_args()
52 
53 clean = False
54 
55 
57  if not os.path.isdir(out):
58  print('creating the output folder because it does not exist')
59  os.makedirs(out)
60  return out
61 
62 # Create path to script
63 script_path = subprocess.check_output('rospack find naoqi_tools',
64  stderr=subprocess.STDOUT,
65  shell=True)[:-1]
66 script_path = os.path.join(script_path, 'scripts', 'blender', args.scriptfile)
67 # Check script existence
68 if(not os.path.isfile(script_path)):
69  print("script doesn't exist\nExiting now")
70  exit(1)
71 
72 # Check if args.outputdir is valid
73 if args.scriptfile != 'io_export_visual.py':
74  if args.outputdir is None:
75  print('\nno valid output directory: using ' + str(args.inputmeshdir) +
76  ' as destination folder')
77  output_dir = args.inputmeshdir
78  else:
79  output_dir = check_output_dir(args.outputdir)
80 
81  # Check existence of the input directory
82  if args.inputmeshdir is None or not os.path.isdir(args.inputmeshdir):
83  print('Invalid mesh folder provided\nExiting now')
84  exit(1)
85 else:
86  if args.outputdir is None:
87  output_dir = os.path.dirname(args.blenderfile)
88  print('\nno valid output directory: using ' +
89  output_dir + ' as destination folder')
90  else:
91  output_dir = check_output_dir(args.outputdir)
92 
93 # Set the parameters and bash command for each script
94 if(args.scriptfile == 'normalize_meshes.py'):
95  cmd = (script_path + ' -i ' + args.inputmeshdir + ' -s ' +
96  str(args.scale) + ' -o ' + output_dir)
97 
98 elif(args.scriptfile == 'io_export_collision.py'):
99  cmd = (args.blenderdir + '/blender --background -P ' + script_path +
100  ' -- ' + args.inputmeshdir + ' ' + str(args.ratio) + ' ' +
101  str(args.threshold) + ' ' + output_dir)
102 
103 elif(args.scriptfile == 'io_export_visual.py'):
104  if(args.blenderfile is None or not os.path.isfile(args.blenderfile)):
105  print('invalid blender file provided\nExiting now')
106  exit(1)
107  if(args.outputfilename is None):
108  basename = os.path.basename(args.blenderfile)
109  print('no name specified for output material file. unsing: ' +
110  basename)
111  else:
112  basename = os.path.basename(args.outputfilename)
113  if(basename.rfind('.') != -1):
114  basename = basename[0:basename.rfind('.')]
115  print('exporting material to ' + basename + '.material')
116  cmd = (os.path.join(args.blenderdir, 'blender') + ' ' + args.blenderfile +
117  ' -P ' + script_path + ' -- ' + output_dir +
118  ' ' + basename)
119 
120 elif(args.scriptfile == 'io_export_ogre.py'):
121  cmd = ((os.path.join(args.blenderdir, 'blender')) + ' -P ' + script_path +
122  ' -- ' + args.inputmeshdir + ' ' + output_dir)
123  clean = True
124 
125 os.system(cmd)
126 
127 # If ogre exporter called
128 # Remove files left behind by the OGRE exporter
129 if clean is True:
130  file_list = sorted(os.listdir(output_dir))
131  for file in file_list:
132  if file.endswith('.mesh.xml'):
133  print('removing ' + file)
134  os.remove(os.path.join(output_dir, file))


naoqi_tools
Author(s): Mikael Arguedas
autogenerated on Thu Jul 16 2020 03:18:37