Welcome to mirror list, hosted at ThFree Co, Russian Federation.

obj_import.py « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6930ef86ef5590fef52f44198bf8cda679299c6c (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#!BPY
 
"""
Name: 'Wavefront (.obj)...'
Blender: 232
Group: 'Import'
Tooltip: 'Load a Wavefront OBJ File'
"""

__author__ = "Campbell Barton"
__url__ = ["blender", "elysiun"]
__version__ = "0.9"

__bpydoc__ = """\
This script imports OBJ files to Blender.

Usage:

Run this script from "File->Import" menu and then load the desired OBJ file.
"""

# $Id$
#
# --------------------------------------------------------------------------
# OBJ Import v0.9 by Campbell Barton (AKA Ideasman)
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------

NULL_MAT = '(null)' # Name for mesh's that have no mat set.
NULL_IMG = '(null)' # Name for mesh's that have no mat set.

MATLIMIT = 16 # This isnt about to change but probably should not be hard coded.

DIR = ''

#==============================================#
# Return directory, where is file              #
#==============================================#
def pathName(path,name):
	length=len(path)
	for CH in range(1, length):
		if path[length-CH:] == name:
			path = path[:length-CH]
			break
	return path

#==============================================#
# Strips the slashes from the back of a string #
#==============================================#
def stripPath(path):
	return path.split('/')[-1].split('\\')[-1]
	
#====================================================#
# Strips the prefix off the name before writing      #
#====================================================#
def stripName(name): # name is a string
	prefixDelimiter = '.'
	return name[ : name.find(prefixDelimiter) ]


from Blender import *
import sys as py_sys

#==================================================================================#
# This function sets textures defined in .mtl file                                 #
#==================================================================================#
def getImg(img_fileName):
	for i in Image.Get():
		if i.filename == img_fileName:
			return i
	
	# if we are this far it means the image hasnt been loaded.
	try:
		return Image.Load(img_fileName)
	except IOError:
		print '\tunable to open image file: "%s"' % img_fileName
		return



#==================================================================================#
# This function sets textures defined in .mtl file                                 #
#==================================================================================#
def load_mat_image(mat, img_fileName, type, meshDict):
	
	
	texture = Texture.New(type)
	texture.setType('Image')
	
	image = getImg(img_fileName)
	if image:
		texture.image = image
	
	# adds textures to faces (Textured/Alt-Z mode)
	# Only apply the diffuse texture to the face if the image has not been set with the inline usemat func.
	if type == 'Kd':
		for meshPair in meshDict.values():
			for f in meshPair[0].faces:
				if meshPair[0].materials[f.mat].name == mat.name:
					# the inline usemat command overides the material Image
					if not f.image:
					  f.image = image
		
	# adds textures for materials (rendering)
	elif type == 'Ka':
		mat.setTexture(0, texture, Texture.TexCo.UV, Texture.MapTo.CMIR)
	elif type == 'Kd':
		mat.setTexture(1, texture, Texture.TexCo.UV, Texture.MapTo.COL)
	elif type == 'Ks':
		mat.setTexture(2, texture, Texture.TexCo.UV, Texture.MapTo.SPEC)

#==================================================================================#
# This function loads materials from .mtl file (have to be defined in obj file)    #
#==================================================================================#
def load_mtl(dir, mtl_file, meshDict):
	
	#===============================================================================#
	# This gets a mat or creates one of the requested name if none exist.           #
	#===============================================================================#
	def getMat(matName):
		# Make a new mat
		try:
			return Material.Get(matName)
		except NameError:
			return Material.New(matName)
			
	mtl_file = stripPath(mtl_file)
	mtl_fileName = dir + mtl_file
	
	try:
		fileLines= open(mtl_fileName, 'r').readlines()
	except IOError:
		print '\tunable to open referenced material file: "%s"' % mtl_fileName
		return
	
	lIdx=0
	while lIdx < len(fileLines):
		l = fileLines[lIdx].split()
	
		# Detect a line that will be ignored
		if len(l) == 0:
			pass
		elif l[0] == '#' or len(l) == 0:
			pass
		elif l[0] == 'newmtl':
			currentMat = getMat('_'.join(l[1:])) # Material should alredy exist.
		elif l[0] == 'Ka':
			currentMat.setMirCol(float(l[1]), float(l[2]), float(l[3]))
		elif l[0] == 'Kd':
			currentMat.setRGBCol(float(l[1]), float(l[2]), float(l[3]))
		elif l[0] == 'Ks':
			currentMat.setSpecCol(float(l[1]), float(l[2]), float(l[3]))
		elif l[0] == 'Ns':
			currentMat.setHardness( int((float(l[1])*0.51)) )
		elif l[0] == 'd':
			currentMat.setAlpha(float(l[1]))
		elif l[0] == 'Tr':
			currentMat.setAlpha(float(l[1]))
		elif l[0] == 'map_Ka':
			img_fileName = dir + l[1]
			load_mat_image(currentMat, img_fileName, 'Ka', meshDict)
		elif l[0] == 'map_Ks':
			img_fileName = dir + l[1]
			load_mat_image(currentMat, img_fileName, 'Ks', meshDict)
		elif l[0] == 'map_Kd':
			img_fileName = dir + l[1]
			load_mat_image(currentMat, img_fileName, 'Kd', meshDict)
		lIdx+=1

#===========================================================================#
# Returns unique name of object/mesh (preserve overwriting existing meshes) #
#===========================================================================#
def getUniqueName(name):
	newName = name
	uniqueInt = 0
	while 1:
		try:
			ob = Object.Get(newName)
			# Okay, this is working, so lets make a new name
			newName = '%s.%d' % (name, uniqueInt)
			uniqueInt +=1
		except AttributeError:
			if newName not in NMesh.GetNames():
				return newName
			else:
				newName = '%s.%d' % (name, uniqueInt)
				uniqueInt +=1


# Gets the meshs index for this material, -1 if its not in the list
def getMeshMaterialIndex(mesh, material):
	meshMatIndex = -1
	matIdx = 0
	meshMatList = mesh.materials
	while matIdx < len(meshMatList):
		if meshMatList[matIdx].name == material.name:
			meshMatIndex = matIdx # The current mat index.
			break
		matIdx+=1
	# -1 if not found
	return meshMatIndex




#==================================================================================#
# This loads data from .obj file                                                   #
#==================================================================================#
def load_obj(file):
	time1 = sys.time()
	
	TEX_OFF_FLAG = ~NMesh.FaceModes['TEX']
	
	# Get the file name with no path or .obj
	fileName = stripName( stripPath(file) )

	mtl_fileName = ''

	DIR = pathName(file, stripPath(file))

	fileLines = open(file, 'r').readlines()
	
	uvMapList = [(0,0)] # store tuple uv pairs here

	# This dummy vert makes life a whole lot easier-
	# pythons index system then aligns with objs, remove later
	vertList = [NMesh.Vert(0, 0, 0)]
	
	# Store all imported materials in a dict, names are key
	materiaDict = {}
	
	# Store all imported images in a dict, names are key
	imageDict = {}
	
	# This stores the index that the current mesh has for the current material.
	# if the mesh does not have the material then set -1
	contextMeshMatIdx = -1
	
	# Keep this out of the dict for easy accsess.
	nullMat = Material.New(NULL_MAT)
	
	currentMat = nullMat # Use this mat.
	currentImg = NULL_IMG # Null image is a string, otherwise this should be set to an image object.\
	currentSmooth = 1
	
	# Store a list of unnamed names
	currentUnnamedGroupIdx = 0
	currentUnnamedObjectIdx = 0
	
	quadList = (0, 1, 2, 3)
	
	#==================================================================================#
	# Load all verts first (texture verts too)                                         #
	#==================================================================================#
	nonVertFileLines = []
	lIdx = 0
	print '\tfile length: %d' % len(fileLines)
	while lIdx < len(fileLines):
		
		# Dont Bother splitting empty or comment lines.
		if len(fileLines[lIdx]) == 0:
			pass
		elif fileLines[lIdx][0] == '\n':
			pass
		elif fileLines[lIdx][0] == '#':
			pass
		
		else:
			fileLines[lIdx] = fileLines[lIdx].split()		
			l = fileLines[lIdx]
			
			# Splitting may 
			if len(l) == 0:
				pass
			# Verts
			elif l[0] == 'v':
				vertList.append( NMesh.Vert(float(l[1]), float(l[2]), float(l[3]) ) )
			
			# UV COORDINATE
			elif l[0] == 'vt':
				uvMapList.append( (float(l[1]), float(l[2])) )
			else:
				nonVertFileLines.append(l)
		lIdx+=1
	
	del fileLines
	fileLines = nonVertFileLines
	del nonVertFileLines	
	
	# Make a list of all unused vert indicies that we can copy from
	VERT_USED_LIST = [-1]*len(vertList)
	
	# Here we store a boolean list of which verts are used or not
	# no we know weather to add them to the current mesh
	# This is an issue with global vertex indicies being translated to per mesh indicies
	# like blenders, we start with a dummy just like the vert.
	# -1 means unused, any other value refers to the local mesh index of the vert.

	# objectName has a char in front of it that determins weather its a group or object.
	# We ignore it when naming the object.
	objectName = 'omesh' # If we cant get one, use this
	
	meshDict = {}
	currentMesh = NMesh.GetRaw()
	meshDict[objectName] = (currentMesh, VERT_USED_LIST[:]) # Mesh/meshDict[objectName][1]
	currentMesh.verts.append(vertList[0])
	currentMesh.hasFaceUV(1)
	

	#==================================================================================#
	# Load all faces into objects, main loop                                           #
	#==================================================================================#
	lIdx = 0
	# Face and Object loading LOOP
	while lIdx < len(fileLines):
		l = fileLines[lIdx]
		
		# FACE
		if l[0] == 'f': 
			# Make a face with the correct material.
			f = NMesh.Face()
			
			# Add material to mesh
			if contextMeshMatIdx == -1:
				tmpMatLs = currentMesh.materials
				
				if len(tmpMatLs) == MATLIMIT:
					contextMeshMatIdx = 0 # Use first material
					print 'material overflow, attempting to use > 16 materials. defaulting to first.'
				else:
					contextMeshMatIdx = len(tmpMatLs)
					currentMesh.addMaterial(currentMat)

			# Set up vIdxLs : Verts
			# Set up vtIdxLs : UV
			# Start with a dummy objects so python accepts OBJs 1 is the first index.
			vIdxLs = []
			vtIdxLs = []
			fHasUV = len(uvMapList)-1 # Assume the face has a UV until it sho it dosent, if there are no UV coords then this will start as 0.
			for v in l[1:]:
				# OBJ files can have // or / to seperate vert/texVert/normal
				# this is a bit of a pain but we must deal with it.
				objVert = v.split('/')
				
				# Vert Index - OBJ supports negative index assignment (like python)
				
				vIdxLs.append(int(objVert[0]))
				if fHasUV:
					# UV
					if len(objVert) == 1:
						#vtIdxLs.append(int(objVert[0])) # replace with below.
						vtIdxLs.append(vIdxLs[-1]) # Sticky UV coords
					elif objVert[1]: # != '' # Its possible that theres no texture vert just he vert and normal eg 1//2
						vtIdxLs.append(int(objVert[1])) # Seperate UV coords
					else:
						fHasUV = 0

					# Dont add a UV to the face if its larger then the UV coord list
					# The OBJ file would have to be corrupt or badly written for thi to happen
					# but account for it anyway.
					if len(vtIdxLs) > 0:
						if vtIdxLs[-1] > len(uvMapList):
							fHasUV = 0
							print 'badly written OBJ file, invalid references to UV Texture coordinates.'
			
			# Quads only, we could import quads using the method below but it polite to import a quad as a quad.
			if len(vIdxLs) == 4:
				for i in quadList: #  quadList == [0,1,2,3] 
					if meshDict[objectName][1][vIdxLs[i]] == -1:
						currentMesh.verts.append(vertList[vIdxLs[i]])
						f.v.append(currentMesh.verts[-1])
						meshDict[objectName][1][vIdxLs[i]] = len(currentMesh.verts)-1
					else:
						f.v.append(currentMesh.verts[meshDict[objectName][1][vIdxLs[i]]])
				
				# UV MAPPING
				if fHasUV:
					f.uv.extend([uvMapList[ vtIdxLs[0] ],uvMapList[ vtIdxLs[1] ],uvMapList[ vtIdxLs[2] ],uvMapList[ vtIdxLs[3] ]])
					#for i in [0,1,2,3]:
					#	f.uv.append( uvMapList[ vtIdxLs[i] ] )

				if f.v > 0:
					f.mat = contextMeshMatIdx
					if currentImg != NULL_IMG:
						f.image = currentImg
					else:
						f.mode &= TEX_OFF_FLAG
					currentMesh.faces.append(f) # move the face onto the mesh
					if len(f) > 0:
						f.smooth = currentSmooth
			
			elif len(vIdxLs) >= 3: # This handles tri's and fans
				for i in range(len(vIdxLs)-2):
					f = NMesh.Face()
					
					for ii in [0, i+1, i+2]:
						
						if meshDict[objectName][1][vIdxLs[ii]] == -1:
							currentMesh.verts.append(vertList[vIdxLs[ii]])
							f.v.append(currentMesh.verts[-1])
							meshDict[objectName][1][vIdxLs[ii]] = len(currentMesh.verts)-1
						else:
							f.v.append(currentMesh.verts[meshDict[objectName][1][vIdxLs[ii]]])

					# UV MAPPING
					if fHasUV:
						f.uv.extend([uvMapList[ vtIdxLs[0] ], uvMapList[ vtIdxLs[i+1] ], uvMapList[ vtIdxLs[i+2] ]])
					
					if f.v > 0:
						f.mat = contextMeshMatIdx
						if currentImg != NULL_IMG:
							f.image = currentImg
						else:
							f.mode |= TEX_OFF_FLAG
						currentMesh.faces.append(f) # move the face onto the mesh
						if len(f) > 0:
							f.smooth = currentSmooth
		
		
		# FACE SMOOTHING
		elif l[0] == 's':
			# No value? then turn on.
			if len(l) == 1:
				currentSmooth = 1
			else:
				if l[1] == 'off':
					currentSmooth = 0
				else: 
					currentSmooth = 1

		# OBJECT / GROUP
		elif l[0] == 'o' or l[0] == 'g':
			# This makes sure that if an object and a group have the same name then
			# they are not put into the same object.
			
			# Only make a new group.object name if the verts in the existing object have been used, this is obscure
			# but some files face groups seperating verts and faces which results in silly things. (no groups have names.)
			if len(l) > 1:
				objectName = '_'.join(l[1:])
			else: # No name given
				# Make a new empty name
				if l[0] == 'g': # Make a blank group name
					objectName = 'unnamed_grp_%d' % currentUnnamedGroupIdx
					currentUnnamedGroupIdx +=1
				else: # is an object.
					objectName = 'unnamed_ob_%d' % currentUnnamedObjectIdx
					currentUnnamedObjectIdx +=1
			
			
			# If we havnt written to this mesh before then do so.
			# if we have then we'll just keep appending to it, this is required for soem files.
			
			# If we are new, or we are not yet in the list of added meshes
			# then make us new mesh.
			if len(l) == 1 or objectName not in meshDict.keys():
				currentMesh = NMesh.GetRaw()
				meshDict[objectName] = (currentMesh, VERT_USED_LIST[:])
				currentMesh.hasFaceUV(1)
				currentMesh.verts.append( vertList[0] )
				contextMeshMatIdx = -1
				
			else: 
				# Since we have this in Blender then we will check if the current Mesh has the material.
				# set the contextMeshMatIdx to the meshs index but only if we have it.
				currentMesh = meshDict[objectName]
				contextMeshMatIdx = getMeshMaterialIndex(currentMesh, currentMat)



		# MATERIAL
		elif l[0] == 'usemtl':
			if len(l) == 1 or l[1] == NULL_MAT:
				#~ currentMat = getMat(NULL_MAT)
				newMatName = NULL_MAT
				currentMat = nullMat
			else:
				#~ currentMat = getMat(' '.join(l[1:])) # Use join in case of spaces
				newMatName = '_'.join(l[1:])
				
				
				try: # Add to material list if not there
					currentMat = materiaDict[newMatName]
					newMatName = currentMat.name # Make sure we are up to date, Blender might have incremented the name.
					
					# Since we have this in Blender then we will check if the current Mesh has the material.
					matIdx = 0
					tmpMeshMaterials = currentMesh.materials
					while matIdx < len(tmpMeshMaterials):
						if tmpMeshMaterials[matIdx].name == newMatName:
							contextMeshMatIdx = matIdx # The current mat index.
							break
						matIdx+=1
					
					
					
				except KeyError: # Not added yet, add now.
					currentMat = Material.New(newMatName)
					materiaDict[newMatName] = currentMat
					contextMeshMatIdx = -1 # Mesh cant possibly have the material.
			
		# IMAGE
		elif l[0] == 'usemat' or l[0] == 'usemap':
			if len(l) == 1 or l[1] == '(null)' or l[1] == 'off':
				currentImg = NULL_IMG
			else:
				# Load an image.
				newImgName = stripPath(' '.join(l[1:]))
				
				try:
					# Assume its alredy set in the dict (may or maynot be loaded)
					currentImg = imageDict[newImgName]
				
				except KeyError: # Not in dict, add for first time.
					try: # Image has not been added, Try and load the image
						currentImg = Image.Load( '%s%s' % (DIR, newImgName) ) # Use join in case of spaces 
						imageDict[newImgName] = currentImg
						
					except IOError: # Cant load, just set blank.
						imageDict[newImgName] = NULL_IMG
						currentImg = NULL_IMG
		
		# MATERIAL FILE
		elif l[0] == 'mtllib':
			mtl_fileName = ' '.join(l[1:]) # SHOULD SUPPORT MULTIPLE MTL?
		
		lIdx+=1

	
	#==============================================#
	# Write all meshs in the dictionary            #
	#==============================================# 
	for ob in Scene.GetCurrent().getChildren(): # Deselect all
		ob.sel = 0	
	
	
	# Applies material properties to materials alredy on the mesh as well as Textures.
	if mtl_fileName != '':
		load_mtl(DIR, mtl_fileName, meshDict)	
	
	
	importedObjects = []
	for mk in meshDict.keys():
		meshDict[mk][0].verts.pop(0)
		
		# Ignore no vert meshes.
		if not meshDict[mk][0].verts:
			continue
		
		name = getUniqueName(mk)
		ob = NMesh.PutRaw(meshDict[mk][0], name)
		ob.name = name
		
		importedObjects.append(ob)
	
	# Select all imported objects.
	for ob in importedObjects:
		ob.sel = 1

	print "obj import time: ", sys.time() - time1

Window.FileSelector(load_obj, 'Import Wavefront OBJ')


# For testing compatibility
'''
TIME = sys.time()
import os
for obj in os.listdir('/obj/'):
	if obj.lower().endswith('obj'):
		print obj
		newScn = Scene.New(obj)
		newScn.makeCurrent()
		load_obj('/obj/' + obj)
'''
#print "TOTAL IMPORT TIME: ", sys.time() - TIME
#load_obj('/obj/her.obj')