Skip to content

Instantly share code, notes, and snippets.

@ViennaMike
Created March 24, 2025 16:44
Converts Microsoft 3D builder object files to "standard" vertex colored object files
import sys
def convert_obj_vertex_colors(input_file, output_file):
with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:
for line in infile:
parts = line.strip().split()
if parts and parts[0] == 'v' and len(parts) == 7:
# Convert RGB from [0, 255] to [0, 1]
r, g, b = map(float, parts[4:7])
r, g, b = r / 255.0, g / 255.0, b / 255.0
outfile.write(f"{parts[0]} {parts[1]} {parts[2]} {parts[3]} {r:.6f} {g:.6f} {b:.6f}\n")
else:
outfile.write(line)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python convert_obj_rgb.py input.obj output.obj")
else:
convert_obj_vertex_colors(sys.argv[1], sys.argv[2])
@ViennaMike
Copy link
Author

The .obj file standard doesn't support colors in the object file, instead is uses a separate .mtl file. However many program support vertex coloring by adding the RGB values for color to the end of the vertex line. The de facto "standard" for this non-standard usage is to code the RBB values as decimals between 0 and 1. However for some reason, Microsoft's 3D Builder codes them as integers between 0 and 255, so a lot of other programs, e.g., Bambu Studio, won't read the color information if you import the file. This simple script converts the RGB values from a range of 0-255 to a range of 0.0 - 1.0 and writes out the modified file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment