# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(ExportSTL.+?global_scale = .+?default=)(.+?),"
test_str = ("# ##### BEGIN GPL LICENSE BLOCK #####\n"
"#\n"
"# This program is free software; you can redistribute it and/or\n"
"# modify it under the terms of the GNU General Public License\n"
"# as published by the Free Software Foundation; either version 2\n"
"# of the License, or (at your option) any later version.\n"
"#\n"
"# This program is distributed in the hope that it will be useful,\n"
"# but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"# GNU General Public License for more details.\n"
"#\n"
"# You should have received a copy of the GNU General Public License\n"
"# along with this program; if not, write to the Free Software Foundation,\n"
"# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
"#\n"
"# ##### END GPL LICENSE BLOCK #####\n\n"
"# <pep8-80 compliant>\n\n"
"bl_info = {\n"
" \"name\": \"STL format\",\n"
" \"author\": \"Guillaume Bouchard (Guillaum)\",\n"
" \"version\": (1, 1, 1),\n"
" \"blender\": (2, 74, 0),\n"
" \"location\": \"File > Import-Export > Stl\",\n"
" \"description\": \"Import-Export STL files\",\n"
" \"warning\": \"\",\n"
" \"wiki_url\": \"http://wiki.blender.org/index.php/Extensions:2.6/Py/\"\n"
" \"Scripts/Import-Export/STL\",\n"
" \"support\": 'OFFICIAL',\n"
" \"category\": \"Import-Export\",\n"
"}\n\n\n"
"# @todo write the wiki page\n\n"
"\"\"\"\n"
"Import-Export STL files (binary or ascii)\n\n"
"- Import automatically remove the doubles.\n"
"- Export can export with/without modifiers applied\n\n"
"Issues:\n\n"
"Import:\n"
" - Does not handle endien\n"
"\"\"\"\n\n"
"if \"bpy\" in locals():\n"
" import importlib\n"
" if \"stl_utils\" in locals():\n"
" importlib.reload(stl_utils)\n"
" if \"blender_utils\" in locals():\n"
" importlib.reload(blender_utils)\n\n"
"import os\n\n"
"import bpy\n"
"from bpy.props import (StringProperty,\n"
" BoolProperty,\n"
" CollectionProperty,\n"
" EnumProperty,\n"
" FloatProperty,\n"
" )\n"
"from bpy_extras.io_utils import (ImportHelper,\n"
" ExportHelper,\n"
" orientation_helper_factory,\n"
" axis_conversion,\n"
" )\n"
"from bpy.types import Operator, OperatorFileListElement\n\n\n"
"IOSTLOrientationHelper = orientation_helper_factory(\"IOSTLOrientationHelper\", axis_forward='Y', axis_up='Z')\n\n\n"
"class ImportSTL(Operator, ImportHelper, IOSTLOrientationHelper):\n"
" \"\"\"Load STL triangle mesh data\"\"\"\n"
" bl_idname = \"import_mesh.stl\"\n"
" bl_label = \"Import STL\"\n"
" bl_options = {'UNDO'}\n\n"
" filename_ext = \".stl\"\n\n"
" filter_glob = StringProperty(\n"
" default=\"*.stl\",\n"
" options={'HIDDEN'},\n"
" )\n"
" files = CollectionProperty(\n"
" name=\"File Path\",\n"
" type=OperatorFileListElement,\n"
" )\n"
" directory = StringProperty(\n"
" subtype='DIR_PATH',\n"
" )\n\n"
" global_scale = FloatProperty(\n"
" name=\"Scale\",\n"
" soft_min=0.001, soft_max=1000.0,\n"
" min=1e-6, max=1e6,\n"
" default=1.0,\n"
" )\n\n"
" use_scene_unit = BoolProperty(\n"
" name=\"Scene Unit\",\n"
" description=\"Apply current scene's unit (as defined by unit scale) to imported data\",\n"
" default=False,\n"
" )\n\n"
" def execute(self, context):\n"
" from . import stl_utils\n"
" from . import blender_utils\n"
" from mathutils import Matrix\n\n"
" paths = [os.path.join(self.directory, name.name)\n"
" for name in self.files]\n\n"
" scene = context.scene\n\n"
" # Take into account scene's unit scale, so that 1 inch in Blender gives 1 inch elsewhere! See T42000.\n"
" global_scale = self.global_scale\n"
" if scene.unit_settings.system != 'NONE' and self.use_scene_unit:\n"
" global_scale /= scene.unit_settings.scale_length\n\n"
" global_matrix = axis_conversion(from_forward=self.axis_forward,\n"
" from_up=self.axis_up,\n"
" ).to_4x4() * Matrix.Scale(global_scale, 4)\n\n"
" if not paths:\n"
" paths.append(self.filepath)\n\n"
" if bpy.ops.object.mode_set.poll():\n"
" bpy.ops.object.mode_set(mode='OBJECT')\n\n"
" if bpy.ops.object.select_all.poll():\n"
" bpy.ops.object.select_all(action='DESELECT')\n\n"
" for path in paths:\n"
" objName = bpy.path.display_name(os.path.basename(path))\n"
" tris, pts = stl_utils.read_stl(path)\n"
" blender_utils.create_and_link_mesh(objName, tris, pts, global_matrix)\n\n"
" return {'FINISHED'}\n\n\n"
"class ExportSTL(Operator, ExportHelper, IOSTLOrientationHelper):\n"
" \"\"\"Save STL triangle mesh data from the active object\"\"\"\n"
" bl_idname = \"export_mesh.stl\"\n"
" bl_label = \"Export STL\"\n\n"
" filename_ext = \".stl\"\n"
" filter_glob = StringProperty(default=\"*.stl\", options={'HIDDEN'})\n\n"
" global_scale = FloatProperty(\n"
" name=\"Scale\",\n"
" min=0.01, max=1000.0,\n"
" default=1.0,\n"
" )\n\n"
" use_scene_unit = BoolProperty(\n"
" name=\"Scene Unit\",\n"
" description=\"Apply current scene's unit (as defined by unit scale) to exported data\",\n"
" default=False,\n"
" )\n"
" ascii = BoolProperty(\n"
" name=\"Ascii\",\n"
" description=\"Save the file in ASCII file format\",\n"
" default=False,\n"
" )\n"
" use_mesh_modifiers = BoolProperty(\n"
" name=\"Apply Modifiers\",\n"
" description=\"Apply the modifiers before saving\",\n"
" default=False,\n"
" )\n\n\n"
" def execute(self, context):\n"
" from . import stl_utils\n"
" from . import blender_utils\n"
" import itertools\n"
" from mathutils import Matrix\n"
" keywords = self.as_keywords(ignore=(\"axis_forward\",\n"
" \"axis_up\",\n"
" \"global_scale\",\n"
" \"check_existing\",\n"
" \"filter_glob\",\n"
" \"use_scene_unit\",\n"
" \"use_mesh_modifiers\",\n"
" ))\n\n"
" scene = context.scene\n\n"
" # Take into account scene's unit scale, so that 1 inch in Blender gives 1 inch elsewhere! See T42000.\n"
" global_scale = self.global_scale\n"
" if scene.unit_settings.system != 'NONE' and self.use_scene_unit:\n"
" global_scale *= scene.unit_settings.scale_length\n\n"
" global_matrix = axis_conversion(to_forward=self.axis_forward,\n"
" to_up=self.axis_up,\n"
" ).to_4x4() * Matrix.Scale(global_scale, 4)\n\n"
" faces = itertools.chain.from_iterable(\n"
" blender_utils.faces_from_mesh(ob, global_matrix, self.use_mesh_modifiers)\n"
" for ob in context.selected_objects)\n\n"
" stl_utils.write_stl(faces=faces, **keywords)\n\n"
" return {'FINISHED'}\n\n\n"
"def menu_import(self, context):\n"
" self.layout.operator(ImportSTL.bl_idname, text=\"Stl (.stl)\")\n\n\n"
"def menu_export(self, context):\n"
" default_path = os.path.splitext(bpy.data.filepath)[0] + \".stl\"\n"
" self.layout.operator(ExportSTL.bl_idname, text=\"Stl (.stl)\")\n\n\n"
"def register():\n"
" bpy.utils.register_module(__name__)\n\n"
" bpy.types.INFO_MT_file_import.append(menu_import)\n"
" bpy.types.INFO_MT_file_export.append(menu_export)\n\n\n"
"def unregister():\n"
" bpy.utils.unregister_module(__name__)\n\n"
" bpy.types.INFO_MT_file_import.remove(menu_import)\n"
" bpy.types.INFO_MT_file_export.remove(menu_export)\n\n\n"
"if __name__ == \"__main__\":\n"
" register()\n")
subst = "${1}10.0,"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 1, re.DOTALL)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html