io_export_collision.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 # io_export_collision.py
00018 # Authors: Mikael Arguedas [mikael.arguedas@gmail.com]
00019 
00020 # This script import one by one each collada file in a folder,
00021 # decimate the meshes and export them as stl files
00022 
00023 from __future__ import print_function
00024 import bpy
00025 import os
00026 import sys
00027 
00028 argv = sys.argv
00029 argv = argv[argv.index("--") + 1:]  # get all args after "--"
00030 
00031 mesh_dir = argv[0]
00032 # Define decimation ratio
00033 RATIO = float(argv[1])
00034 # Define minimum number of vertices required for decimation
00035 THRESHOLD = int(argv[2])
00036 # Define output directory
00037 output_dir = argv[3]
00038 print("Exporting collision meshes to <%s>." % mesh_dir)
00039 
00040 scene = bpy.context.scene
00041 # Delete all existing objects in the current scene
00042 for ob in scene.objects:
00043     ob.select = True
00044 
00045 bpy.ops.object.delete()
00046 
00047 file_list = sorted(os.listdir(mesh_dir))
00048 for file in file_list:
00049     # Import the visual meshes one by one
00050     if file.endswith('.dae') == True:
00051         print(str(mesh_dir + '/' + file))
00052         bpy.ops.wm.collada_import(filepath= os.path.join(mesh_dir , file))
00053         # Decimate the meshes only if there are more than THRESHOLD meshes in it
00054         if(len(bpy.context.scene.objects[0].data.vertices) > THRESHOLD):
00055             bpy.ops.object.modifier_add(type='DECIMATE')
00056             mod = bpy.context.scene.objects.active.modifiers[0]
00057             mod.ratio = RATIO
00058 
00059             # Apply decimation
00060             bpy.ops.object.modifier_apply(apply_as='DATA')
00061         else:
00062             print (" does not have enough vertices for DECIMATION")
00063         # Export them
00064         bpy.ops.export_mesh.stl(filepath=os.path.join(output_dir,
00065                                                       file[0:file.find('.dae')]
00066                                                       + "_" + "{:.2f}".format(RATIO)+ ".stl"))
00067         # Delete them
00068         bpy.ops.object.delete()
00069 
00070 bpy.ops.wm.quit_blender()


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