forked from SlicerMorph/PhotoGram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOBJFile.py
More file actions
166 lines (139 loc) · 4.93 KB
/
OBJFile.py
File metadata and controls
166 lines (139 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import logging
import os
import unittest
import vtk, qt, ctk, slicer
from slicer.ScriptedLoadableModule import *
class OBJFile(ScriptedLoadableModule):
def __init__(self, parent):
ScriptedLoadableModule.__init__(self, parent)
parent.title = 'OBJFile'
parent.categories = ['Testing.TestCases']
parent.dependencies = []
parent.contributors = ["Chi Zhang (SCRI), Steve Pieper (Isomics), A. Murat Maga (UW)"]
parent.helpText = '''
This module is used to import OBJ file while automatically map .
'''
parent.acknowledgementText = '''
Thanks to:
Steve Pieper and Andras Lasso (functions from TextureModel module of SlicerIGT used in this script to map texture)
'''
self.parent = parent
# def _NIfTIFileInstallPackage():
# try:
# import conversion
# except ModuleNotFoundError:
# slicer.util.pip_install("git+https://github.com/pnlbwh/conversion.git@v2.3")
class OBJFileWidget(ScriptedLoadableModuleWidget):
def setup(self):
ScriptedLoadableModuleWidget.setup(self)
# Default reload&test widgets are enough.
# Note that reader and writer is not reloaded.
class OBJFileFileReader(object):
def __init__(self, parent):
self.parent = parent
def description(self):
return 'OBJ textured model'
def fileType(self):
return 'OBJ'
def extensions(self):
return ['OBJ (*.obj)']
def canLoadFile(self, filePath):
# assume yes if it ends in .obj
# TODO: check for .bval and .bvec in same directory
return True
def load(self, properties):
"""
uses properties:
obj_path - path to the .obj file
"""
try:
obj_path = properties['fileName'] #obj file path
obj_dir = os.path.dirname(obj_path)
obj_filename = os.path.basename(obj_path)
base_name = os.path.splitext(obj_filename)[0]
extension = os.path.splitext(obj_filename)[1]
#Add model node
obj_node = slicer.util.loadModel(obj_path)
#Meanwhie, if the model node ends in obj, search and map texture to it
if extension == ".obj":
mtl_filename = base_name + ".mtl"
mtl_path = os.path.join(obj_dir, mtl_filename)
#parse the mtl file to get texture image file name
if os.path.exists(mtl_path):
with open(mtl_path) as f:
lines = f.read().splitlines()
lines = [i for i in lines if i!='']
texture_filename = lines[len(lines)-1].split(" ")[1]
texture_path = os.path.join(obj_dir, texture_filename)
import ImageStacks
logic = ImageStacks.ImageStacksLogic()
vectorVolNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLVectorVolumeNode")
# logic._init_
logic.outputQuality = 'full'
logic.outputGrayscale = False
logic.filePaths = [texture_path]
logic.loadVolume(outputNode=vectorVolNode, progressCallback = None)
#Map texture to the imported OBJ file
modelDisplayNode = obj_node.GetDisplayNode()
modelDisplayNode.SetBackfaceCulling(0)
textureImageFlipVert = vtk.vtkImageFlip()
textureImageFlipVert.SetFilteredAxis(1)
textureImageFlipVert.SetInputConnection(vectorVolNode.GetImageDataConnection())
modelDisplayNode.SetTextureImageDataConnection(textureImageFlipVert.GetOutputPort())
slicer.mrmlScene.RemoveNode(vectorVolNode)
except Exception as e:
logging.error('Failed to load file: '+str(e))
import traceback
traceback.print_exc()
return False
self.parent.loadedNodes = [obj_node.GetID()]
# return True
return True
# class OBJFileWriter(object):
#
# def __init__(self, parent):
# self.parent = parent
#
# def description(self):
# return 'OBJ textured model'
#
# def fileType(self):
# return 'OBJ'
#
# def extensions(self, obj):
# return ['OBJ (*.obj)']
#
# def canWriteObject(self, obj):
# # Only enable this writer in testing mode
# if not slicer.app.testingEnabled():
# return False
#
# return bool(obj.IsA("vtkMRMLModelNode"))
#
# def write(self, properties):
# return Fale
#
#
# class OBJFileTest(ScriptedLoadableModuleTest):
# def runTest(self):
# """Run as few or as many tests as needed here.
# """
# self.setUp()
# self.test_Writer()
# self.test_Reader()
# self.tearDown()
# self.delayDisplay('Testing complete')
#
# def setUp(self):
# self.tempDir = slicer.util.tempDirectory()
# slicer.mrmlScene.Clear()
#
# def tearDown(self):
# import shutil
# shutil.rmtree(self.tempDir, True)
#
# def test_WriterReader(self):
# # Writer and reader tests are put in the same function to ensure
# # that writing is done before reading (it generates input data for reading).
#
# # TODO: rewrite this for NIfTI