io_export_collision.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 # Copyright (C) 2014 Aldebaran
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 # io_export_collision.py
18 # Authors: Mikael Arguedas [mikael.arguedas@gmail.com]
19 
20 # This script import one by one each collada file in a folder,
21 # decimate the meshes and export them as stl files
22 
23 from __future__ import print_function
24 import bpy
25 import os
26 import sys
27 
28 argv = sys.argv
29 argv = argv[argv.index("--") + 1:] # get all args after "--"
30 
31 mesh_dir = argv[0]
32 # Define decimation ratio
33 RATIO = float(argv[1])
34 # Define minimum number of vertices required for decimation
35 THRESHOLD = int(argv[2])
36 # Define output directory
37 output_dir = argv[3]
38 print("Exporting collision meshes to <%s>." % mesh_dir)
39 
40 scene = bpy.context.scene
41 # Delete all existing objects in the current scene
42 for ob in scene.objects:
43  ob.select = True
44 
45 bpy.ops.object.delete()
46 
47 file_list = sorted(os.listdir(mesh_dir))
48 for file in file_list:
49  # Import the visual meshes one by one
50  if file.endswith('.dae') == True:
51  print(str(mesh_dir + '/' + file))
52  bpy.ops.wm.collada_import(filepath= os.path.join(mesh_dir , file))
53  # Decimate the meshes only if there are more than THRESHOLD meshes in it
54  if(len(bpy.context.scene.objects[0].data.vertices) > THRESHOLD):
55  bpy.ops.object.modifier_add(type='DECIMATE')
56  mod = bpy.context.scene.objects.active.modifiers[0]
57  mod.ratio = RATIO
58 
59  # Apply decimation
60  bpy.ops.object.modifier_apply(apply_as='DATA')
61  else:
62  print (" does not have enough vertices for DECIMATION")
63  # Export them
64  bpy.ops.export_mesh.stl(filepath=os.path.join(output_dir,
65  file[0:file.find('.dae')]
66  + "_" + "{:.2f}".format(RATIO)+ ".stl"))
67  # Delete them
68  bpy.ops.object.delete()
69 
70 bpy.ops.wm.quit_blender()


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