#将urdf文件转化为webots格式 from urdf2webots.importer import convert2urdf convert2urdf('G:\\四足机器人\\小双足\\模型\\test_T\\urdf\\test_T.urdf')
optParser = optparse.OptionParser(usage='usage: %prog --input=my_robot.urdf [options]') optParser.add_option('--input', dest='inFile', default='', help='Specifies the urdf file to convert.') optParser.add_option('--output', dest='outFile', default='', help='Specifies the path and, if ending in ".proto", name of the resulting PROTO file.' ' The filename minus the .proto extension will be the robot name.') optParser.add_option('--normal', dest='normal', action='store_true', default=False, help='If set, the normals are exported if present in the URDF definition.') optParser.add_option('--box-collision', dest='boxCollision', action='store_true', default=False, help='If set, the bounding objects are approximated using boxes.') optParser.add_option('--disable-mesh-optimization', dest='disableMeshOptimization', action='store_true', default=False, help='If set, the duplicated vertices are not removed from the meshes (this can speed up a lot the ' 'conversion).') optParser.add_option('--multi-file', dest='enableMultiFile', action='store_true', default=False, help='If set, the mesh files are exported as separated PROTO files') optParser.add_option('--static-base', dest='staticBase', action='store_true', default=False, help='If set, the base link will have the option to be static (disable physics)') optParser.add_option('--tool-slot', dest='toolSlot', default=None, help='Specify the link that you want to add a tool slot too (exact link name from urdf)') optParser.add_option('--rotation', dest='initRotation', default='0 1 0 0', help='Set the rotation field of your PROTO file.') optParser.add_option('--init-pos', dest='initPos', default=None, help='Set the initial positions of your robot joints. Example: --init-pos="[1.2, 0.5, -1.5]" would set ' 'the first 3 joints of your robot to the specified values, and leave the rest with their default value.') optParser.add_option('--link-to-def', dest='linkToDef', action='store_true', default=False, help='If set, urdf link names are also used as DEF names as well as solid names.') optParser.add_option('--joint-to-def', dest='jointToDef', action='store_true', default=False, help='If set, urdf joint names are also used as DEF names as well as joint names.') options, args = optParser.parse_args() convert2urdf(options.inFile, options.outFile, options.normal, options.boxCollision, options.disableMeshOptimization, options.enableMultiFile, options.staticBase, options.toolSlot, options.initRotation, options.initPos, options.linkToDef, options.jointToDef)
default='', help='Specifies the urdf file to convert.') optParser.add_option('--output', dest='outFile', default='', help='Specifies the name of the resulting PROTO file.') optParser.add_option( '--normal', dest='normal', action='store_true', default=False, help='If set, the normals are exported if present in the URDF definition.') optParser.add_option( '--box-collision', dest='boxCollision', action='store_true', default=False, help='If set, the bounding objects are approximated using boxes.') optParser.add_option( '--disable-mesh-optimization', dest='disableMeshOptimization', action='store_true', default=False, help= 'If set, the duplicated vertices are not removed from the meshes (this can speed up a lot the conversion).' ) options, args = optParser.parse_args() convert2urdf(options.inFile, options.outFile, options.normal, options.boxCollision, options.disableMeshOptimization)
def main(args=None, input=None): parser = argparse.ArgumentParser( usage='usage: %prog --input=my_robot.urdf [options]') parser.add_argument('--input', dest='inFile', default='', help='Specifies the urdf file to convert.') parser.add_argument('--output', dest='outFile', default='', help='Specifies the name of the resulting PROTO file.') parser.add_argument('--normal', dest='normal', action='store_true', default=False, help='If set, the normals are exported if defined.') parser.add_argument( '--box-collision', dest='boxCollision', action='store_true', default=False, help='If set, the bounding objects are approximated using boxes.') parser.add_argument( '--disable-mesh-optimization', dest='disableMeshOptimization', action='store_true', default=False, help='If set, the duplicated vertices are not removed from the meshes.' ) # use 'parse_known_args' because ROS2 adds a lot of internal arguments arguments, unknown = parser.parse_known_args() file = os.path.abspath(input) if input is not None else os.path.abspath( arguments.inFile) if not file: sys.exit( 'Input file not specified (should be specified with the "--input" argument).' ) if not os.path.isfile(file): sys.exit('"%s" file does not exist.' % arguments.inFile) elif not file.endswith('.urdf'): sys.exit('"%s" is not an urdf file.' % file) content = '' with open(file, 'r') as f: content = f.read() # look for package-relative file path and replace them urdfFile = file generatedFile = False packages = re.findall('filename="package:\/\/([^\/]*)', content) for package in set(packages): # do the replacement packagePath = '' try: packagePath = get_package_share_directory(package) except LookupError: sys.exit( 'This urdf depends on the "%s" package, but this package could not be found' % package) content = content.replace('package://%s' % package, '%s' % packagePath) if packages: # some package-relative file paths have been found # generate a temporary file with the replacement content urdfFile = tempfile.mkstemp(suffix='.urdf')[1] generatedFile = True with open(urdfFile, 'w') as f: f.write(content) convert2urdf(inFile=urdfFile, outFile=arguments.outFile, normal=arguments.normal, boxCollision=arguments.boxCollision, disableMeshOptimization=arguments.disableMeshOptimization) # remove temporary file if generatedFile: os.remove(urdfFile)
def main(args=None, input=None): parser = argparse.ArgumentParser( usage='usage: %prog --input=my_robot.urdf [options]') parser.add_argument('--input', dest='inFile', default='', help='Specifies the urdf file to convert.') parser.add_argument('--output', dest='outFile', default='', help='Specifies the name of the resulting PROTO file.') parser.add_argument('--normal', dest='normal', action='store_true', default=False, help='If set, the normals are exported if defined.') parser.add_argument( '--box-collision', dest='boxCollision', action='store_true', default=False, help='If set, the bounding objects are approximated using boxes.') parser.add_argument( '--disable-mesh-optimization', dest='disableMeshOptimization', action='store_true', default=False, help='If set, the duplicated vertices are not removed from the meshes.' ) parser.add_argument( '--multi-file', dest='enableMultiFile', action='store_true', default=False, help='If set, the mesh files are exported as separated PROTO files') parser.add_argument( '--static-base', dest='staticBase', action='store_true', default=False, help= 'If set, the base link will have the option to be static (disable physics)' ) parser.add_argument( '--tool-slot', dest='toolSlot', default=None, help= 'Specify the link that you want to add a tool slot to (exact link name from urdf)' ) parser.add_argument('--rotation', dest='initRotation', default='0 1 0 0', help='Set the rotation field of your PROTO file.)') parser.add_argument( '--init-pos', dest='initPos', default=None, help='Set the initial positions of your robot joints. ' 'Example: --init-pos="[1.2, 0.5, -1.5]" would set ' 'the first 3 joints of your robot to the specified values, ' 'and leave the rest with their default value.') # use 'parse_known_args' because ROS2 adds a lot of internal arguments arguments, _ = parser.parse_known_args() file = os.path.abspath(input) if input is not None else os.path.abspath( arguments.inFile) if not file: sys.exit( 'Input file not specified (should be specified with the "--input" argument).' ) if not os.path.isfile(file): sys.exit('"%s" file does not exist.' % arguments.inFile) elif not file.endswith('.urdf'): sys.exit('"%s" is not an urdf file.' % file) content = '' with open(file, 'r') as f: content = f.read() # look for package-relative file path and replace them urdf_file = file generated_file = False packages = re.findall(r'filename="package:\/\/([^\/]*)', content) for package in set(packages): # do the replacement package_path = '' try: package_path = get_package_share_directory(package) except LookupError: sys.exit( 'This urdf depends on the "%s" package, but this package could not be found' % package) content = content.replace('package://%s' % package, '%s' % package_path) if packages: # some package-relative file paths have been found # generate a temporary file with the replacement content urdf_file = tempfile.mkstemp(suffix='.urdf')[1] generated_file = True with open(urdf_file, 'w') as f: f.write(content) convert2urdf(inFile=urdf_file, outFile=arguments.outFile, normal=arguments.normal, boxCollision=arguments.boxCollision, disableMeshOptimization=arguments.disableMeshOptimization, enableMultiFile=arguments.enableMultiFile, staticBase=arguments.staticBase, toolSlot=arguments.toolSlot, initRotation=arguments.initRotation, initPos=arguments.initPos) # remove temporary file if generated_file: os.remove(urdf_file)