-
Notifications
You must be signed in to change notification settings - Fork 40
/
io_mesh_fast_obj.py
791 lines (685 loc) · 31.4 KB
/
io_mesh_fast_obj.py
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# ##### 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
bl_info = {"name": "Fast Wavefront (.obj)",
"description": "Import/Export single mesh as Wavefront OBJ. Only active mesh is exported. Only single mesh is expected on import. Supported obj features: UVs, normals, shading, vertex colors using MRGB format (ZBrush) or so called 'extended' format when each vertex is defined by 6 values (x, y, z, r, g, b).",
"author": "Jakub Uhlik",
"version": (0, 2, 0),
"blender": (2, 80, 0),
"location": "File > Import/Export > Fast Wavefront (.obj)",
"warning": "work in progress",
"wiki_url": "",
"tracker_url": "",
"category": "Import-Export", }
import os
import io
import sys
import time
import datetime
import shutil
import bpy
import bmesh
from mathutils import Matrix
from bpy_extras.io_utils import ExportHelper, ImportHelper, axis_conversion
from bpy.types import Operator
from bpy.props import StringProperty, BoolProperty, FloatProperty, IntProperty
def log(msg="", indent=0, prefix="> "):
m = "{}{}{}".format(" " * indent, prefix, msg, )
print(m)
class FastOBJWriter():
def __init__(self, o, path, apply_modifiers=False, apply_transformation=True, convert_axes=True, triangulate=False, use_uv=True, use_shading=False, use_vertex_colors=False, use_vcols_mrgb=True, use_vcols_ext=False, global_scale=1.0, precision=6, ):
log("{}: {}".format(self.__class__.__name__, o.name))
log("will write .obj at: {}".format(path), 1)
t = time.time()
log("prepare..", 1)
me = None
if(apply_modifiers):
me = o.to_mesh(context.depsgraph, apply_modifiers, )
bm = bmesh.new()
if(me is not None):
bm.from_mesh(me)
else:
bm.from_mesh(o.data)
log_args_align = 25
log("{} {}".format("{}: ".format("triangulate").ljust(log_args_align, "."), triangulate), 1)
if(triangulate):
bmesh.ops.triangulate(bm, faces=bm.faces)
log("{} {}".format("{}: ".format("apply_transformation").ljust(log_args_align, "."), apply_transformation), 1)
if(apply_transformation):
m = o.matrix_world.copy()
bm.transform(m)
log("{} {}".format("{}: ".format("convert_axes").ljust(log_args_align, "."), convert_axes), 1)
if(convert_axes):
axis_forward = '-Z'
axis_up = 'Y'
cm = axis_conversion(to_forward=axis_forward, to_up=axis_up).to_4x4()
bm.transform(cm)
log("{} {}".format("{}: ".format("use_uv").ljust(log_args_align, "."), use_uv), 1)
log("{} {}".format("{}: ".format("use_shading").ljust(log_args_align, "."), use_shading), 1)
log("{} {}".format("{}: ".format("use_vertex_colors").ljust(log_args_align, "."), use_vertex_colors), 1)
log("{} {}".format("{}: ".format("use_vcols_mrgb").ljust(log_args_align, "."), use_vcols_mrgb), 1)
log("{} {}".format("{}: ".format("use_vcols_ext").ljust(log_args_align, "."), use_vcols_ext), 1)
log("{} {}".format("{}: ".format("global_scale").ljust(log_args_align, "."), global_scale), 1)
if(global_scale != 1.0):
sm = Matrix.Scale(global_scale, 4)
bm.transform(sm)
log("{} {}".format("{}: ".format("precision").ljust(log_args_align, "."), precision), 1)
# update normals after transforms
bm.normal_update()
sio = io.StringIO(initial_value='', newline='', )
siov = io.StringIO(initial_value='', newline='', )
siovn = io.StringIO(initial_value='', newline='', )
siof = io.StringIO(initial_value='', newline='', )
siovt = io.StringIO(initial_value='', newline='', )
# sort-of-header
sio.write('# %s %s %s\n' % ('teoplib', self.__class__.__name__, 'Wavefront .OBJ file'))
sio.write('#\n')
sio.write('o %s_%s\n' % (o.name, o.data.name))
# vertices
vs = bm.verts
bm.verts.ensure_lookup_table()
# vertex colors
col_layer = None
if(use_vertex_colors):
# try to find active vertex color layer
col_layer = bm.loops.layers.color.active
if(col_layer is None):
# no vertex colors, turn them off
use_vertex_colors = False
use_vcols_mrgb = False
use_vcols_ext = False
log("no vertex colors found..", 1)
if(col_layer is not None):
if(use_vcols_mrgb and use_vcols_ext):
# if both True, use mrgb
log("using MRGB vertex colors..", 1)
use_vcols_ext = False
if(not use_vcols_mrgb and not use_vcols_ext):
# if both False, use mrgb
log("using MRGB vertex colors..", 1)
use_vcols_mrgb = True
fwv = siov.write
vfmt = 'v %.{0}f %.{0}f %.{0}f\n'.format(precision)
vfmtvcolext = 'v %.{0}f %.{0}f %.{0}f %.{0}f %.{0}f %.{0}f\n'.format(precision)
if(use_vertex_colors and use_vcols_ext):
# write vertices and vertex colors in extended format
log("writing vertices and extended obj format vertex colors..", 1)
cols = [[] for i in range(len(vs))]
rgbf = []
bm.faces.ensure_lookup_table()
# get all colors for single vertex, ie, colors of all loops belonging to vertex
if(col_layer is not None):
fs = bm.faces
for f in fs:
ls = f.loops
for l in ls:
vi = l.vert.index
# get rgb only
c = l[col_layer][:3]
cols[vi].append(c)
# average color values
for cl in cols:
r = 0
g = 0
b = 0
l = len(cl)
for c in cl:
r += c[0]
g += c[1]
b += c[2]
def limit(v):
if(v < 0.0):
v = 0.0
if(v > 1.0):
v = 1.0
return v
rgbf.append((limit(r / l), limit(g / l), limit(b / l)))
for v in vs:
fwv(vfmtvcolext % tuple(v.co[:] + rgbf[v.index]))
else:
# no ext vcols, write regular vertices
log("writing vertices..", 1)
for v in vs:
fwv(vfmt % v.co[:])
if(use_vertex_colors and use_vcols_mrgb):
# vertex colors in mrgb format
log("writing mrgb vertex colors..", 1)
# '#MRGB ' block polypaint and mask as 4hex values per vertex.
# format is MMRRGGBB with up to 64 entries per line
cols = [[] for i in range(len(vs))]
hexc = []
bm.faces.ensure_lookup_table()
# get all colors from loops for each vertex
if(col_layer is not None):
fs = bm.faces
for f in fs:
ls = f.loops
for l in ls:
vi = l.vert.index
# get rgb only
c = l[col_layer][:3]
cols[vi].append(c)
# average colors and convert to 0-255 format and then to hexadecimal values, leave 'm' to ff
for cl in cols:
r = 0
g = 0
b = 0
l = len(cl)
for c in cl:
r += c[0]
g += c[1]
b += c[2]
def limit(v):
if(v < 0):
v = 0
if(v > 255):
v = 255
return v
rgb8 = (limit(int((r / l) * 255.0)), limit(int((g / l) * 255.0)), limit(int((b / l) * 255.0)))
h = 'ff%02x%02x%02x' % rgb8
hexc.append(h)
for i in range(0, len(hexc), 64):
# write in chunks of 64 entries per line as per specification directly after vertices
ch = hexc[i:i + 64]
s = "".join(ch)
fwv('#MRGB %s\n' % s)
# faces
log("writing normals, faces and texture coordinates..", 1)
fs = bm.faces
fs.ensure_lookup_table()
# texture coordinates stuff
vtlocs = dict()
vtmaps = dict()
vtli = 0
uvl = None
if(use_uv):
uvl = bm.loops.layers.uv.active
if(uvl is None):
use_uv = False
# smoothing stuff
fsmooth = None
normap = dict()
norlen = 0
nori = 0
# shortcuts
fwvn = siovn.write
vnfmt = 'vn %.{0}f %.{0}f %.{0}f\n'.format(precision)
fwf = siof.write
fwvt = siovt.write
vtfmt = 'vt %.{0}f %.{0}f\n'.format(precision)
if(use_uv):
# if using texture coordinates, precalculate them first, each coordinate should be reused, no duplicates
for f in fs:
fl = f.loops
for l in fl:
uv = l[uvl].uv[:]
vi = l.vert.index
li = l.index
fi = f.index
if(uv not in vtlocs):
vtlocs[uv] = vtli
vtli += 1
fwvt(vtfmt % uv)
e = [fi, vi, li, vtlocs[uv]]
if(fi not in vtmaps):
vtmaps[fi] = [e, ]
else:
vtmaps[fi].append(e)
for f in fs:
if(use_shading):
# write shading flag, only change when flag is changed for following face
if(fsmooth != f.smooth):
fsmooth = f.smooth
if(fsmooth):
fwf("s 1\n")
else:
fwf("s off\n")
# start writing face
fwf("f")
fi = f.index
vs = f.verts
n = len(vs)
for i in range(n):
if(use_shading):
if(fsmooth):
# write smooth face with vertex normals
nor = vs[i].normal[:]
else:
# write flat face with face normal
nor = f.normal[:]
if(nor in normap):
# use already defined normal
nori = normap[nor]
else:
# create new normal
normap[nor] = norlen + 1
norlen += 1
nori = norlen
fwvn(vnfmt % nor)
vi = vs[i].index
if(use_uv):
# get correct texture coordinates
m = vtmaps[fi]
uvi = None
for j in m:
if(j[1] == vi):
uvi = j[3]
break
# write face vertex with uv and normal
fwf(" %d/%d/%d" % (vi + 1, uvi + 1, nori, ))
else:
# write face vertex and normal
fwf(" %d//%d" % (vi + 1, nori))
# finish face
fwf("\n")
# put it all to one string
log("writing to disk..", 1)
tp = "{}.tmp".format(path)
sio.write(siov.getvalue())
sio.write(siovt.getvalue())
sio.write(siovn.getvalue())
sio.write(siof.getvalue())
# write to temporary file
with open(tp, mode='w', encoding='utf-8', newline="\n", ) as of:
of.write(sio.getvalue())
sio.close()
siov.close()
siovn.close()
siof.close()
siovt.close()
# remove existing file
if(os.path.exists(path)):
os.remove(path)
# rename to final file name
shutil.move(tp, path)
log("cleanup..", 1)
if(me is not None):
bpy.data.meshes.remove(me)
bm.free()
d = datetime.timedelta(seconds=time.time() - t)
log("completed in {}.".format(d), 1)
class FastOBJReader():
def __init__(self, path, convert_axes=True, with_uv=True, with_shading=True, with_vertex_colors=True, use_vcols_mrgb=True, use_mask_as_vertex_group=False, use_vcols_ext=False, with_polygroups=True, global_scale=1.0, apply_conversion=False, ):
log("{}:".format(self.__class__.__name__), 0, )
name = os.path.splitext(os.path.split(path)[1])[0]
log("will import .obj at: {}".format(path), 1)
log_args_align = 30
t = time.time()
def add_object(name, data, ):
so = bpy.context.scene.objects
for i in so:
i.select_set(False)
o = bpy.data.objects.new(name, data)
# so.link(o)
context = bpy.context
view_layer = context.view_layer
collection = view_layer.active_layer_collection.collection
collection.objects.link(o)
view_layer.objects.active = o
# o.select = True
o.select_set(True)
# if(so.active is None or so.active.mode == 'OBJECT'):
# so.active = o
return o
# def activate_object(obj, ):
# bpy.ops.object.select_all(action='DESELECT')
# sc = bpy.context.scene
# obj.select = True
# sc.objects.active = obj
log("reading..", 1)
ls = None
with open(path, mode='r', encoding='utf-8') as f:
ls = f.readlines()
def v(l):
a = l.split()[1:]
return tuple(map(float, a))
def vt(l):
a = l.split()[1:]
return tuple(map(float, a))
def f(l):
a = l.split()[1:]
ls = map(int, a)
return tuple([i - 1 for i in ls])
def fn(l):
a = l.split()[1:]
ls = [i.split('/') for i in a]
f = []
for i, p in enumerate(ls):
f.append(int(p[0]) - 1)
return f
def ftn(l):
a = l.split()[1:]
ls = [i.split('/') for i in a]
f = []
t = []
for i, p in enumerate(ls):
f.append(int(p[0]) - 1)
t.append(int(p[1]) - 1)
return f, t
def vc_mrgb(l):
r = []
m = []
l = l[6:]
l = l.strip()
for i in range(0, len(l), 8):
v = l[i:i + 8]
c = (int(v[2:4], 16) / 255, int(v[4:6], 16) / 255, int(v[6:8], 16) / 255)
r.append(c)
m.append(int(v[0:2], 16) / 255)
return r, m
def v_vc_ext(l):
a = l.split()[1:]
return tuple(map(float, a))
groups = {}
verts = []
tverts = []
faces = []
tfaces = []
vcols = []
shading = []
shading_flag = None
mask = []
log("parsing..", 1)
parsef = None
has_uv = None
cg = None
for l in ls:
if(l.startswith('s ')):
if(with_shading):
if(l.lower() == 's off' or l.lower() == 's 0'):
shading_flag = False
else:
shading_flag = True
elif(l.startswith('g ')):
if(with_polygroups):
g = l[2:]
g = g.strip()
if(g not in groups):
groups[g] = []
cg = g
elif(l.startswith('v ')):
if(with_vertex_colors and use_vcols_ext):
a = v_vc_ext(l)
verts.append(a[:3])
vcols.append(a[3:])
else:
verts.append(v(l))
elif(l.startswith('vt ')):
if(with_uv):
tverts.append(vt(l))
elif(l.startswith('f ')):
if(parsef is None):
if('//' in l):
parsef = fn
elif('/' not in l):
parsef = f
else:
parsef = ftn
has_uv = True
if(has_uv):
a, b = parsef(l)
else:
a = parsef(l)
faces.append(a)
if(with_shading):
shading.append(shading_flag)
if(has_uv):
if(with_uv):
tfaces.append(b)
if(with_polygroups):
if(cg is not None):
groups[cg].extend(a)
elif(l.startswith('#MRGB ')):
if(with_vertex_colors):
if(use_vcols_mrgb):
c, m = vc_mrgb(l)
vcols.extend(c)
if(use_mask_as_vertex_group):
mask.extend(m)
else:
pass
log("making mesh..", 1)
me = bpy.data.meshes.new(name)
me.from_pydata(verts, [], faces)
log("{} {}".format("{}: ".format("with_uv").ljust(log_args_align, "."), with_uv), 1)
if(len(tverts) > 0):
log("making uv map..", 1)
me.uv_layers.new(name="UVMap")
loops = me.uv_layers[0].data
i = 0
for j in range(len(tfaces)):
f = tfaces[j]
for k in range(len(f)):
loops[i + k].uv = tverts[f[k]]
i += (k + 1)
log("{} {}".format("{}: ".format("with_vertex_colors").ljust(log_args_align, "."), with_vertex_colors), 1)
log("{} {}".format("{}: ".format("use_vcols_mrgb").ljust(log_args_align, "."), use_vcols_mrgb), 1)
log("{} {}".format("{}: ".format("use_mask_as_vertex_group").ljust(log_args_align, "."), use_mask_as_vertex_group), 1)
log("{} {}".format("{}: ".format("use_vcols_ext").ljust(log_args_align, "."), use_vcols_ext), 1)
if(len(vcols) > 0):
log("making vertex colors..", 1)
me.vertex_colors.new()
vc = me.vertex_colors.active
vcd = vc.data
for l in me.loops:
vcd[l.index].color = vcols[l.vertex_index] + (1.0, )
log("{} {}".format("{}: ".format("convert_axes").ljust(log_args_align, "."), convert_axes), 1)
log("{} {}".format("{}: ".format("apply_conversion").ljust(log_args_align, "."), apply_conversion), 1)
if(convert_axes):
if(apply_conversion):
axis_forward = '-Z'
axis_up = 'Y'
cm = axis_conversion(from_forward=axis_forward, from_up=axis_up).to_4x4()
me.transform(cm)
log("{} {}".format("{}: ".format("global_scale").ljust(log_args_align, "."), global_scale), 1)
if(global_scale != 1.0):
sm = Matrix.Scale(global_scale, 4)
me.transform(sm)
me.update()
log("adding to scene..", 1)
self.name = name
self.object = add_object(name, me)
if(len(mask) > 0):
log("making mask vertex group..", 1)
g = self.object.vertex_groups.new("mask")
indexes = [i for i in range(len(me.vertices))]
g.add(indexes, 0.0, 'REPLACE')
ind = g.index
for i, v in enumerate(me.vertices):
v.groups[ind].weight = mask[i]
if(convert_axes):
if(not apply_conversion):
axis_forward = '-Z'
axis_up = 'Y'
cm = axis_conversion(from_forward=axis_forward, from_up=axis_up).to_4x4()
m = self.object.matrix_world
mm = m @ cm
self.object.matrix_world = mm
log("{} {}".format("{}: ".format("with_shading").ljust(log_args_align, "."), with_shading), 1)
if(with_shading):
log("setting shading..", 1)
for i, p in enumerate(me.polygons):
p.use_smooth = shading[i]
log("{} {}".format("{}: ".format("with_polygroups").ljust(log_args_align, "."), with_polygroups), 1)
if(len(groups) > 0):
log("making polygroups..", 1)
o = self.object
me = o.data
for k, v in groups.items():
o.vertex_groups.new(name=k)
vg = o.vertex_groups[k]
vg.add(list(set(v)), 1.0, 'REPLACE')
log("imported object: '{}'".format(self.object.name), 1)
d = datetime.timedelta(seconds=time.time() - t)
log("completed in {}.".format(d), 1)
class ExportFastOBJ(Operator, ExportHelper):
bl_idname = "export_mesh.fast_obj"
bl_label = 'Export Fast OBJ'
bl_options = {'PRESET'}
# filepath = StringProperty(name="File Path", description="Filepath used for exporting the file", maxlen=1024, subtype='FILE_PATH', )
filename_ext = ".obj"
filter_glob: StringProperty(default="*.obj", options={'HIDDEN'}, )
check_extension = True
apply_modifiers: BoolProperty(name="Apply Modifiers", default=False, description="Apply all modifiers.", )
apply_transformation: BoolProperty(name="Apply Transformation", default=True, description="Zero-out mesh transformation.", )
convert_axes: BoolProperty(name="Convert Axes", default=True, description="Convert from blender (y forward, z up) to forward -z, up y.", )
triangulate: BoolProperty(name="Triangulate", default=False, description="", )
use_uv: BoolProperty(name="With UV", default=True, description="Export active UV layout.", )
use_shading: BoolProperty(name="With Shading", default=False, description="Export face shading.", )
use_vertex_colors: BoolProperty(name="With Vertex Colors", default=False, description="Export vertex colors, this is not part of official file format specification.", )
use_vcols_mrgb: BoolProperty(name="VCols MRGB", default=True, description="Use ZBrush vertex colors style.", )
use_vcols_ext: BoolProperty(name="VCols Ext", default=False, description="Use 'Extended Vertex' vertex colors style.", )
global_scale: FloatProperty(name="Scale", default=1.0, precision=3, description="", )
precision: IntProperty(name="Precision", default=6, description="", )
@classmethod
def poll(cls, context):
o = context.active_object
return (o and o.type == 'MESH')
def draw(self, context):
l = self.layout
sub = l.column()
sub.prop(self, 'apply_modifiers')
sub.prop(self, 'apply_transformation')
sub.prop(self, 'convert_axes')
sub.prop(self, 'triangulate')
sub.prop(self, 'use_uv')
sub.prop(self, 'use_shading')
sub.prop(self, 'use_vertex_colors')
c = sub.column()
c.prop(self, 'use_vcols_mrgb')
c.prop(self, 'use_vcols_ext')
c.enabled = self.use_vertex_colors
sub.prop(self, 'global_scale')
sub.prop(self, 'precision')
def execute(self, context):
o = context.active_object
d = {'o': o,
'path': self.filepath,
'apply_modifiers': self.apply_modifiers,
'apply_transformation': self.apply_transformation,
'convert_axes': self.convert_axes,
'triangulate': self.triangulate,
'use_uv': self.use_uv,
'use_shading': self.use_shading,
'use_vertex_colors': self.use_vertex_colors,
'use_vcols_mrgb': self.use_vcols_mrgb,
'use_vcols_ext': self.use_vcols_ext,
'global_scale': self.global_scale,
'precision': self.precision, }
w = FastOBJWriter(**d)
return {'FINISHED'}
class ImportFastOBJ(Operator, ImportHelper):
bl_idname = "import_mesh.fast_obj"
bl_label = 'Import Fast OBJ'
bl_options = {'PRESET'}
# filepath = StringProperty(name="File Path", description="Filepath used for exporting the file", maxlen=1024, subtype='FILE_PATH', )
filename_ext = ".obj"
filter_glob: StringProperty(default="*.obj", options={'HIDDEN'}, )
check_extension = True
convert_axes: BoolProperty(name="Convert Axes", default=True, description="Convert from blender (y forward, z up) to forward -z, up y.", )
with_uv: BoolProperty(name="With UV", default=True, description="Import texture coordinates.", )
with_shading: BoolProperty(name="With Shading", default=False, description="Import face shading.", )
with_vertex_colors: BoolProperty(name="With Vertex Colors", default=False, description="Import vertex colors, this is not part of official file format specification.", )
use_vcols_mrgb: BoolProperty(name="VCols MRGB", default=True, description="Use ZBrush vertex colors style.", )
use_mask_as_vertex_group: BoolProperty(name="Mask as Vertex Group", default=False, description="Import ZBrush mask as vertex group.", )
use_vcols_ext: BoolProperty(name="VCols Ext", default=False, description="Use 'Extended Vertex' vertex colors style.", )
with_polygroups: BoolProperty(name="With Polygroups", default=False, description="", )
global_scale: FloatProperty(name="Scale", default=1.0, precision=3, description="", )
apply_conversion: BoolProperty(name="Apply Conversion", default=False, description="Apply new axes directly to mesh or only transform at object level.", )
def draw(self, context):
l = self.layout
sub = l.column()
sub.prop(self, 'convert_axes')
sub.prop(self, 'with_uv')
sub.prop(self, 'with_shading')
sub.prop(self, 'with_vertex_colors')
c = sub.column()
c.prop(self, 'use_vcols_mrgb')
r = c.row()
r.prop(self, 'use_mask_as_vertex_group')
if(self.with_vertex_colors and self.use_vcols_mrgb):
r.enabled = True
c.prop(self, 'use_vcols_ext')
c.enabled = self.with_vertex_colors
sub.prop(self, 'with_polygroups')
sub.prop(self, 'global_scale')
sub.prop(self, 'apply_conversion')
def execute(self, context):
d = {'path': self.filepath,
'convert_axes': self.convert_axes,
'with_uv': self.with_uv,
'with_shading': self.with_shading,
'with_vertex_colors': self.with_vertex_colors,
'use_vcols_mrgb': self.use_vcols_mrgb,
'use_mask_as_vertex_group': self.use_mask_as_vertex_group,
'use_vcols_ext': self.use_vcols_ext,
'with_polygroups': self.with_polygroups,
'global_scale': self.global_scale,
'apply_conversion': self.apply_conversion, }
w = FastOBJReader(**d)
return {'FINISHED'}
def menu_func_export(self, context):
self.layout.operator(ExportFastOBJ.bl_idname, text="Fast Wavefront (.obj)")
def menu_func_import(self, context):
self.layout.operator(ImportFastOBJ.bl_idname, text="Fast Wavefront (.obj)")
def setup():
export_presets = {'photoscan_final': {'apply_modifiers': False, 'apply_transformation': False, 'convert_axes': False, 'triangulate': False, 'use_uv': True,
'use_shading': False, 'use_vertex_colors': False, 'use_vcols_mrgb': False, 'use_vcols_ext': False, 'global_scale': 1.0,
'precision': 6, },
'zbrush_cleanup': {'apply_modifiers': False, 'apply_transformation': True, 'convert_axes': True, 'triangulate': False, 'use_uv': True,
'use_shading': False, 'use_vertex_colors': False, 'use_vcols_mrgb': False, 'use_vcols_ext': False, 'global_scale': 1.0,
'precision': 6, }, }
import_presets = {'photoscan_raw': {'convert_axes': True, 'with_uv': False, 'with_shading': False, 'with_vertex_colors': False, 'use_vcols_mrgb': False,
'use_mask_as_vertex_group': False, 'use_vcols_ext': False, 'with_polygroups': False, 'global_scale': 1.0, 'apply_conversion': False, },
'zbrush_cleanup': {'convert_axes': True, 'with_uv': True, 'with_shading': False, 'with_vertex_colors': False, 'use_vcols_mrgb': False,
'use_mask_as_vertex_group': False, 'use_vcols_ext': False, 'with_polygroups': False, 'global_scale': 1.0, 'apply_conversion': False, },
'zbrush_with_vcols': {'convert_axes': True, 'with_uv': True, 'with_shading': False, 'with_vertex_colors': True, 'use_vcols_mrgb': True,
'use_mask_as_vertex_group': True, 'use_vcols_ext': False, 'with_polygroups': False, 'global_scale': 1.0, 'apply_conversion': False, }, }
defines = ['import bpy', 'op = bpy.context.active_operator', '', ]
# TODO: do this only when all presets are missing, if only some, assume it is intentional.. also skip when all default are missing, but some user defined are present.
def write_presets(pdir, presets, defs):
pde = os.path.join(bpy.utils.user_resource('SCRIPTS'), "presets", "operator", pdir)
if(not os.path.exists(pde)):
os.makedirs(pde)
for pname, pdata in presets.items():
pp = os.path.join(pde, '{}.py'.format(pname))
if(not os.path.exists(pp)):
a = defs[:]
for k, v in pdata.items():
a.append('op.{} = {}'.format(k, v))
s = "\n".join(a)
with open(pp, mode='w', encoding='utf-8') as f:
f.write(s)
write_presets("export_mesh.fast_obj", export_presets, defines)
write_presets("import_mesh.fast_obj", import_presets, defines)
classes = (
ExportFastOBJ,
ImportFastOBJ,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
setup()
def unregister():
bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()