Isotropix Forums

A simple OBJ parser/importer generating actual geometry

Clarisse Scripting related topics

A simple OBJ parser/importer generating actual geometry

Unread postby sam » Tue Feb 05, 2019 1:38 pm

Hey guys,

During lunch we were discussing about Python and I just remembered I had written this script a while back (2013) in Python. I wanted to put it as an example in the documentation but we never found the time to do it.

I thought I could share it so you guys can see how you can generate a polymesh in Clarisse.

python code

'''
Basic Wavefront OBJ importer
This script demonstrates how to import a custom geometry
in Clarisse.

What's highlighted:

- Call to Clarisse's filebrowser
- Reading Wavefront OBJ file
- Working with progress bar
- Creating and setting a Polymesh from imported data

Copyright 2009-2013 Isotropix SAS.
All Rights Reserved
'''

# Helper class describing a polymesh data structure
class Polymesh:
def __init__(self):
self.vertices = ix.api.GMathVec3fVector()
self.faces_vertex_count = ix.api.UIntVector()
self.faces_definition = ix.api.UIntVector()
self.shading_group_face_association = ix.api.UIntVector()
self.shading_groups = ix.api.CoreStringVector()
# as we don't read shading groups we create a default one
self.shading_groups.add('obj_import')

# Function filling a Polymesh data structure from the specified OBJ filename
# Please note the following OBJ importer is very limited and imports only
# vertices and faces

def import_obj(filename):
# Creating a progress bar
progress = ix.application.create_progress_bar('Importing OBJ ' + filename + '.')
# Creating an empty polymesh data structure
polymesh = Polymesh()
# Creating a vertex variable
vertex = ix.api.GMathVec3f()
# Open the file
f = open(filename, 'r')
lines = f.readlines()
# Initializing progress bar initial value
progress.set_value(0.0)
# Setting progress range to the total number of lines in the file
progress.set_step_count(len(lines))
# Starting the progress
progress.start()
current_line_index = 0
for line in lines:
if line[0] == '#': pass
elif line[0:2] == 'v ':
# Found a vertex
values = line[2:].split()
# Setting new vertex coordinates
vertex[0] = float(values[0])
vertex[1] = float(values[1])
vertex[2] = float(values[2])
# Adding the new vertex
polymesh.vertices.add(vertex)
elif line[0:2] == 'f ':
# Found a face
values = line[2:].split()
# values length is our number of vertices defining this face
polymesh.faces_vertex_count.add(len(values))
for vindex in values:
# OBJ vertex indices starts at 1 instead of 0 in Clarisse
polymesh.faces_definition.add(int(vindex.split('/')[0]) - 1)

current_line_index += 1
# Incrementing the progress bar with the current line index
progress.step(current_line_index)

# Now that we finished, we must destroy it or it will stay as a ghost
# there is no way in clarisse to know a progress bar is completed
progress.destroy()
f.close()
# Initializing shading groups
polymesh.shading_group_face_association.resize(polymesh.faces_vertex_count.get_count())
for i in range(polymesh.shading_group_face_association.get_count()):
# assigning to each primitives the shading group 0 (named obj_import)
polymesh.shading_group_face_association[i] = 0

return polymesh

# Test is the application supports graphical user interface
if ix.is_gui_application():
# Displaying a file browser to import OBJ
files = ix.api.GuiWidget.open_files(ix.application, '', \
'Browse for OBJ files', \
'Wavefront OBJ (*.obj)\t*.{obj}')
# Iterate over chosen files to import one or multiple OBJs
for i in range(files.get_count()):
file = files[i]
polymesh = import_obj(file)
ix.api.IOHelpers.create_polymesh(ix.application, file.split('/')[-1], \
polymesh.vertices,
polymesh.faces_vertex_count, \
polymesh.faces_definition, \
polymesh.shading_groups, \
polymesh.shading_group_face_association)
else:
ix.log_warning('This script requires GUI.')
Sam Assadian
Isotropix
CEO/Founder
User avatar
sam
 
Posts: 1300
Joined: Fri Jan 25, 2013 11:33 pm

Re: A simple OBJ parser/importer generating actual geometry

Unread postby sam » Tue Feb 05, 2019 1:59 pm

Just copy and paste this in Clarisse! :)

Code: Select all
#Isotropix_Clarisse_Clipboard_Serialization 0.94
GeometryPolymesh {
    name "cap_obj"
    #version 0.9
    copy_from "project://scene/cap_obj"
    resource 0 "6A308C20DA6A83BC53A422434D9C46C1"
    uv_maps_loaded yes
    shading_groups "obj_import"
    materials "project://default/material"
    clip_maps ""
    displacements ""
    sg_shading_variables ""
    visibles yes
}
#resources {
    resource "6A308C20DA6A83BC53A422434D9C46C1" 0x880400001F000000D908A4BFF0DF2C3E7F13EABDD0D097BFFF91B1BE7F2E1EBF0EA186BF1058F1BE397D4DBEBE5068BF039832BE52F27A3EB45852BF014E3BBFACAC1DBF812137BFF51F88BC698F6BBF4DA22ABF0822433F77D8103FC90321BF0300D1BCDAFE993F1FB9FDBEFE4578BF3E798CBFB2BBF8BEFA8054BF4EB9B6BFDEABEEBEFED5CBBE8178B5BFDF87CBBE058A783F86B8A8BCBA4CBDBE2700353C718D4F3F79CA9ABEFDBB4E3E36AB563F304FA1BB07B04D3D4EB9B63FFDA882BB0186713FAE834B3FE4D89A3EE3174E3E36AB563FB726BD3E2640283C718D4F3F5169CC3EF852783FBCB8A8BC6615EE3E0C56CCBE8178B5BFA0A9F73E05C454BF4EB9B6BFC780FC3E058A78BF3E798CBF3EE9203FFBDFDBBCDAFE993FE4F32A3F04C6423F77D8103FE4F32A3F00C7423F77D8103F1408373FEA7F94BC698F6BBFA8DF513F01BE3BBFACAC1DBF2D24683FFD8734BE52F27A3E07B1873F08AEF2BE37AA43BE6DAD973FF9D7B2BE7F2E1EBFD908A43F14202A3E7F13EABD000000009000000001000000050000000A00000002000000000000000100000004000000020000000100000001000000000000000500000009000000010000000A00000001000000090000000800000008000000040000000100000013000000090000000A00000005000000130000000A000000140000000900000013000000090000001500000008000000090000001400000015000000140000001D000000150000001D0000001400000013000000190000001D000000130000001300000005000000190000001D0000001A000000150000001E0000001D000000190000001E0000001C0000001D0000001C0000001A0000001D0000000200000003000000000000000C000000070000000300000003000000070000000D0000000000000003000000060000000B0000000500000000000000060000000B000000000000001C0000001E0000001B0000001B0000001E0000001800000012000000180000001E00000019000000120000001E0000001B00000010000000160000001B000000160000001100000019000000050000000B000000190000000B000000120000000B000000060000000F00000016000000100000000E000000160000000E00000011000000100000000D0000000E0000000C000000110000000E0000000C0000000E000000070000000D000000070000000E000000120000000B0000000F00000018000000120000000F00000017000000100000001B0000000D0000000F0000000600000010000000170000000F000000100000000F0000000D0000000D00000006000000030000003000000003000000030000000300000003000000030000000300000003000000030000000300000003000000030000000300000003000000030000000300000003000000030000000300000003000000030000000300000003000000030000000300000003000000030000000300000003000000030000000300000003000000030000000300000003000000030000000300000003000000030000000300000003000000030000000300000003000000030000000300000003000000030000000300000015C60000
}
Sam Assadian
Isotropix
CEO/Founder
User avatar
sam
 
Posts: 1300
Joined: Fri Jan 25, 2013 11:33 pm


Return to Scripting
cron