Python Forum
Preprocessing an OBJ file, what is the best way to?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Preprocessing an OBJ file, what is the best way to?
#8
(Mar-02-2024, 09:00 AM)Gribouillis Wrote: You could extract all the relevant lines with one pattern
import io
import re

file = io.StringIO('''\
v 6.806145 3.287188 34.507591
v 6.889372 3.314378 34.482964
...... repeat large number of times with different values
g groupName
usemtl groupMaterial
f 1 2 3
f 4 5 6
f 4 7 8
f 5 4 8
f 8 7 9
...... repeat a large number of times with different values
v -5.852165 2.924480 33.700558
v -5.861451 2.937897 33.763584
v -5.978782 2.973776 33.743969
v -5.635860 2.871064 33.685860
v -5.709496 2.899417 33.751705
v -5.442479 2.838525 33.676365
v -5.522288 2.871565 33.741238
v -5.261642 2.836869 33.678814
v -5.349662 2.853023 33.73287
.... repeat a large number with different values

g nextGroupName
usemtl nextGroupMaterial

... more v & f definitions''')

pattern = re.compile(r"^(?:[og]|usemtl)\s.*", re.MULTILINE)

print(pattern.findall(file.read()))
Output:
['g groupName', 'usemtl groupMaterial', 'g nextGroupName', 'usemtl nextGroupMaterial']
You could then split the result with more_itertools.bucket()
lines = pattern.findall(file.read())

from more_itertools import bucket
from operator import itemgetter

s = bucket(lines, itemgetter(0))
print(list(s['o']))
print(list(s['u']))
print(list(s['g']))
Output:
[] ['usemtl groupMaterial', 'usemtl nextGroupMaterial'] ['g groupName', 'g nextGroupName']

Thanks that is VERY helpful.
Reply


Messages In This Thread
RE: Preprocessing an OBJ file, what is the best way to? - by KeithSloan - Mar-02-2024, 04:48 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020