Edit the exporter.
Will run thru how to edit changes into addon to do this, .. although for the most part do not recommend making edits to addons....
The addon used to export blend scene to obj is available in the addons folder and can be edited. Make a backup copy to revert... or an addon copy
In version (3.8.0) of the obj exporter that ships with blender, circa line 453 in /addons/io_scene_obj/export_obj.py
# Vert
for v in me_verts:
fw('v %.6f %.6f %.6f\n' % v.co[:])
is where the vertex coordinates are written to file. The %.6f
is formatting the float to be written with 6 decimal places.
For example can limit to 4 decimal places with:
# Vert
for v in me_verts:
fw('v %.4f %.4f %.4f\n' % v.co[:])
Please note, this is simply explaining where and why the exporter is writing a consistent n decimal places. Reducing the number will reduce the accuracy for numbers with significant digits outside reduced range
See suggestions given in this stackoverflow post https://stackoverflow.com/questions/41275396/limiting-floats-to-reduce-data-size
Using :n
or :g
formatting to remove 'insignificant trailing zeros' with:
# Vert
for v in me_verts:
fw('v %g %g %g\n' % v.co[:])
Before:
o Cube
v 1.000000 1.550000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
...
After:
o Cube
v 1 1.55 -1
v 1 -1 -1
v 1 1 1
...
or a combination of two answers,
import re
def fmt(s):
def repl(matchobj):
return ''.join(i for i in matchobj.groups() if i)
return re.sub(
r"(-?)(?:\b0+(\.\d)(\d*)(?<!-0\.0)(?<!0\.0)\b|\b([1-9]\d*)\.0+\b)|-?(0)+\.0\b",
repl ,
s)
fw(fmt("v %g %g %g" % (1.000000 , 0.550000, -1.000000)))
#v 1 .55 -1
Similar could also be used to simply read and rewrite the obj file
Instead would look at uitilizing using a tool like sed which can be programmed to look for and replace patterns. If you are familiar with the vi
editor, will be familiar with sed
commands.
This I feel is somewhat outside the scope of BSE.
https://stackoverflow.com/questions/6916695/sed-for-removing-trailing-zeroes-regex-nongreedy