Convert STEP to Wavefront OBJ with colors of faces

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
Rock007
Posts: 5
Joined: Wed Jun 02, 2021 8:26 am

Re: Convert STEP to Wavefront OBJ with colors of faces

Post by Rock007 »

wmayer wrote: Fri Jul 05, 2019 9:21 am For total GUI-less solution try this:

Code: Select all

import Mesh
import MeshPart
import Import

data=Import.open("C:/ACE TANK.STEP")
shape = data[0][0].Shape
shape_colors = data[0][1]

mesh = MeshPart.meshFromShape(Shape=shape, LinearDeflection=0.1, Segments=True)

# len(shape_colors) must be equal to mesh.countSegments()
#
face_colors=[(0,0,0)] * mesh.CountFacets
for i in range(mesh.countSegments()):
    color = shape_colors[i]
    segm = mesh.getSegment(i)
    for j in segm:
        face_colors[j] = color


mesh.write(Filename="C:/test.obj", Material=face_colors, Format="obj")
wmayer wrote: Fri Jul 05, 2019 9:21 am For total GUI-less solution try this:

Code: Select all

import Mesh
import MeshPart
import Import

data=Import.open("C:/ACE TANK.STEP")
shape = data[0][0].Shape
shape_colors = data[0][1]

mesh = MeshPart.meshFromShape(Shape=shape, LinearDeflection=0.1, Segments=True)

# len(shape_colors) must be equal to mesh.countSegments()
#
face_colors=[(0,0,0)] * mesh.CountFacets
for i in range(mesh.countSegments()):
    color = shape_colors[i]
    segm = mesh.getSegment(i)
    for j in segm:
        face_colors[j] = color


mesh.write(Filename="C:/test.obj", Material=face_colors, Format="obj")
wmayer wrote: Fri Jul 05, 2019 9:21 am For total GUI-less solution try this:

Code: Select all

import Mesh
import MeshPart
import Import

data=Import.open("C:/ACE TANK.STEP")
shape = data[0][0].Shape
shape_colors = data[0][1]

mesh = MeshPart.meshFromShape(Shape=shape, LinearDeflection=0.1, Segments=True)

# len(shape_colors) must be equal to mesh.countSegments()
#
face_colors=[(0,0,0)] * mesh.CountFacets
for i in range(mesh.countSegments()):
    color = shape_colors[i]
    segm = mesh.getSegment(i)
    for j in segm:
        face_colors[j] = color


mesh.write(Filename="C:/test.obj", Material=face_colors, Format="obj")
Hello Wmayer,

I am using the code mentioned as it is mentioned for converting the STEP to OBJ file but its always result in error with mentioned packages like Mesh, MeshPart, Import.
I have recently started looking in this area of file types and related conversions. I am using Python-3.6.5 and list of needed packages as below.

certifi 2021.5.30, chardet 4.0.0, et-xmlfile 1.1.0, idna 2.10, mesh 1.0.0a1, numpy 1.19.5, openpyxl 3.0.7, Pandas 1.1.5, pip 21.1.2, pymesh 1.0.2, python-dateutil 2.8.1, pytz 2021.1, requests 2.25.1, setuptools 39.0.1, six 1.16.0, urllib3 1.26.5

The error always points the packages like Mesh, MeshPart, Import.

import Mesh
ModuleNotFoundError: No module named 'Mesh'


Any suggestion on this is appreciated.

Thanks You.
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Convert STEP to Wavefront OBJ with colors of faces

Post by wmayer »

import Mesh
ModuleNotFoundError: No module named 'Mesh'
Depending on your OS your FreeCAD installation should have a file Mesh.pyd (Windows) or Mesh.so (Linux or macOS). Can you find the file?
Rock007
Posts: 5
Joined: Wed Jun 02, 2021 8:26 am

Re: Convert STEP to Wavefront OBJ with colors of faces

Post by Rock007 »

wmayer wrote: Mon Jun 14, 2021 2:17 pm
import Mesh
ModuleNotFoundError: No module named 'Mesh'
Depending on your OS your FreeCAD installation should have a file Mesh.pyd (Windows) or Mesh.so (Linux or macOS). Can you find the file?
Hello wmayer, Thanks for the inputs i was able to resolve this issue. However the using FreeCAD to convert a step file to an .obj file still has a issue as i do not get the complete attributes out from a step file. Here i just get the .obj file having only ‘v’ & ‘f’ , whereas vt and vn are missing. Below is two sample code. Request your input.
Sample Code-1

Code: Select all

data = Import.open("C:/Sample.stp")
shape = Part[0][0].Shape()
shape_colors = data[0][1]
mesh = MeshPart.meshFromShape(Shape=shape, LinearDeflection=0.1, Segments=True)
face_colors=[(0,0,0)]*mesh.CountFacets
for i in range(mesh.countSegments()):
    color = shape_color[i]
    segm = mesh.getSegment(i)
    for j in segm:
        face_colors[j]=color
mesh.write(Filename="C:/sample.obj", Material=face_color,Format="obj")
Sample Code-2

Code: Select all

import FreeCAD
import Part
import Mesh
doc = App.newDocument('Doc')
shape = Part.Shape()
shape.read('C:/Sample.stp')
pf = doc.addObject("Part::Feature","Shape")
pf.Shape = shape
Mesh.export([pf], 'C:/sample.obj')
FreeCadP3
Posts: 3
Joined: Mon Dec 20, 2021 9:44 am

Re: Convert STEP to Wavefront OBJ with colors of faces

Post by FreeCadP3 »

Hi wmayer!

I used your script and it works like a charm.
But in my case i have a step file with multiple shapes and would need to export them into a single .obj file.
Which changes would be neccessary to make that possible?
wmayer wrote: Fri Jul 05, 2019 9:21 am For total GUI-less solution try this:

Code: Select all

import Mesh
import MeshPart
import Import

data=Import.open("C:/ACE TANK.STEP")
shape = data[0][0].Shape
shape_colors = data[0][1]

mesh = MeshPart.meshFromShape(Shape=shape, LinearDeflection=0.1, Segments=True)

# len(shape_colors) must be equal to mesh.countSegments()
#
face_colors=[(0,0,0)] * mesh.CountFacets
for i in range(mesh.countSegments()):
    color = shape_colors[i]
    segm = mesh.getSegment(i)
    for j in segm:
        face_colors[j] = color


mesh.write(Filename="C:/test.obj", Material=face_colors, Format="obj")
Merry christmas in advance!
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Convert STEP to Wavefront OBJ with colors of faces

Post by wmayer »

Cross referenced from: https://forum.freecadweb.org/viewtopic. ... 83#p555083

Code: Select all

import Mesh
import MeshPart
import Import

data=Import.open("C:/ACE TANK.STEP")

compound = Mesh.Mesh()
compound_colors = []

for d in data:
    shape = d[0].Shape
    shape_colors = d[1]
    
    mesh = MeshPart.meshFromShape(Shape=shape, LinearDeflection=0.1, Segments=True)
    num_facets = compound.CountFacets
    compound.addMesh(mesh)
    
    # len(shape_colors) must be equal to mesh.countSegments()
    #
    face_colors=[(0,0,0)] * mesh.CountFacets
    for i in range(mesh.countSegments()):
        color = shape_colors[i]
        segm = mesh.getSegment(i)
        for j in segm:
            face_colors[j] = color
        segm = [k + num_facets for k in segm]
        compound.addSegment(segm)
    compound_colors.extend(face_colors)

compound.write(Filename="C:/test.obj", Material=compound_colors, Format="obj")
Post Reply