def writeURDFGeometry(output, element, filepath): """This functions writes the URDF geometry for a given element at the end of a given String. :param output: The String to append the URDF output string on. :type output: str :param element: A certain element to parse into URDF. :type element: dict :return: str -- The extended String """ geometry = element['geometry'] try: output.append(indent * 4 + '<geometry>\n') if geometry['type'] == 'box': output.append( xmlline(5, 'box', ['size'], [l2str(geometry['size'])])) elif geometry['type'] == "cylinder": output.append( xmlline(5, 'cylinder', ['radius', 'length'], [geometry['radius'], geometry['length']])) elif geometry['type'] == "sphere": output.append( xmlline(5, 'sphere', ['radius'], [geometry['radius']])) elif geometry['type'] == 'mesh': # FIXME: the following will crash if unstructured export is used log( "writeURDFGeometry: " + filepath + ' ' + ioUtils.getOutputMeshpath(path.dirname(filepath)), "DEBUG") meshpath = ioUtils.getOutputMeshpath(path.dirname(filepath)) output.append( xmlline(5, 'mesh', ['filename', 'scale'], [ path.join( path.relpath(meshpath, filepath), geometry['filename'] + '.' + ioUtils.getOutputMeshtype()), l2str(geometry['scale']) ])) elif geometry['type'] == 'capsule': # FIXME: real capsules here! output.append( xmlline(5, 'cylinder', ['radius', 'length'], [geometry['radius'], geometry['length']])) else: raise TypeError("Unknown geometry type") output.append(indent * 4 + '</geometry>\n') except (KeyError, TypeError) as err: log( "Misdefined geometry in element " + element['name'] + " " + str(err), "ERROR")
def writeURDFGeometry(output, element, filepath): """This functions writes the URDF geometry for a given element at the end of a given String. Args: output(str): The String to append the URDF output string on. element(dict): A certain element to parse into URDF. filepath: Returns: : str -- The extended String """ geometry = element['geometry'] try: output.append(indent * 4 + '<geometry>\n') if geometry['type'] == 'box': output.append( xmlline(5, 'box', ['size'], [l2str(geometry['size'])])) elif geometry['type'] == "cylinder": output.append( xmlline(5, 'cylinder', ['radius', 'length'], [geometry['radius'], geometry['length']])) elif geometry['type'] == "sphere": output.append( xmlline(5, 'sphere', ['radius'], [geometry['radius']])) elif geometry['type'] == 'mesh': meshpath = ioUtils.getOutputMeshpath( path.dirname(filepath), None, None ) + geometry['filename'] + '.' + ioUtils.getOutputMeshtype() if not "package://" in meshpath: meshpath = path.relpath(meshpath, filepath) output.append( xmlline( 5, 'mesh', ['filename', 'scale'], [ meshpath, l2str(geometry['scale']), ], )) else: raise TypeError("Unknown geometry type") output.append(indent * 4 + '</geometry>\n') except (KeyError, TypeError) as err: log( "Misdefined geometry in element " + element['name'] + " " + str(err), "ERROR")
def writeURDFGeometry(output, element, filepath): """This functions writes the URDF geometry for a given element at the end of a given String. Args: output(str): The String to append the URDF output string on. element(dict): A certain element to parse into URDF. filepath: Returns: : str -- The extended String """ geometry = element['geometry'] try: output.append(indent * 4 + '<geometry>\n') if geometry['type'] == 'box': output.append(xmlline(5, 'box', ['size'], [l2str(geometry['size'])])) elif geometry['type'] == "cylinder": output.append( xmlline( 5, 'cylinder', ['radius', 'length'], [geometry['radius'], geometry['length']] ) ) elif geometry['type'] == "sphere": output.append(xmlline(5, 'sphere', ['radius'], [geometry['radius']])) elif geometry['type'] == 'mesh': meshpath = ioUtils.getOutputMeshpath(path.dirname(filepath)) output.append( xmlline( 5, 'mesh', ['filename', 'scale'], [ path.join( path.relpath(meshpath, filepath), geometry['filename'] + '.' + ioUtils.getOutputMeshtype(), ), l2str(geometry['scale']), ], ) ) else: raise TypeError("Unknown geometry type") output.append(indent * 4 + '</geometry>\n') except (KeyError, TypeError) as err: log("Misdefined geometry in element " + element['name'] + " " + str(err), "ERROR")
def exportModel(root, export_path, entitytypes=None, model=None): # derive model model = models.buildModelDictionary(root) if not model: model = models.buildModelDictionary(root) # export model in selected formats if entitytypes is None: entitytypes = entities.entity_types for entitytype in entitytypes: typename = "export_entity_" + entitytype # check if format exists and should be exported if not getattr(bpy.data.worlds[0], typename, False): continue # format exists and is exported: if ioUtils.getExpSettings().structureExport: model_path = os.path.join(export_path, entitytype) else: model_path = export_path securepath(model_path) try: entities.entity_types[entitytype]['export'](model, model_path) log( "Export model: " + model['name'] + ' as ' + entitytype + " to " + model_path, "DEBUG") except KeyError: log( "No export function available for selected model type: " + entitytype, "ERROR") continue # TODO: Move mesh export to individual formats? This is practically SMURF # export meshes in selected formats i = 1 mt = len([ m for m in meshes.mesh_types if getattr(bpy.data.worlds[0], "export_mesh_" + m) ]) mc = len(model['meshes']) n = mt * mc for meshtype in meshes.mesh_types: mesh_path = ioUtils.getOutputMeshpath(export_path, meshtype) try: typename = "export_mesh_" + meshtype if getattr(bpy.data.worlds[0], typename): securepath(mesh_path) for meshname in model['meshes']: meshes.mesh_types[meshtype]['export']( model['meshes'][meshname], mesh_path) display.setProgress( i / n, 'Exporting ' + meshname + '.' + meshtype + '...') i += 1 except KeyError: log( "No export function available for selected mesh function: " + meshtype, "ERROR") print(sys.exc_info()[0]) display.setProgress(0) # TODO: Move texture export to individual formats? This is practically SMURF # TODO: Also, this does not properly take care of textures embedded in a .blend file # export textures if ioUtils.getExpSettings().exportTextures: for materialname in model['materials']: mat = model['materials'][materialname] for texturetype in [ 'diffuseTexture', 'normalTexture', 'displacementTexture' ]: if texturetype in mat: sourcepath = os.path.join( os.path.expanduser(bpy.path.abspath('//')), mat[texturetype]) if os.path.isfile(sourcepath): texture_path = securepath( os.path.join(export_path, 'textures')) log("Exporting textures to " + texture_path, "INFO") try: shutil.copy( sourcepath, os.path.join( texture_path, os.path.basename(mat[texturetype]))) except shutil.SameFileError: log("{} already in place".format(texturetype), "INFO")