-
Notifications
You must be signed in to change notification settings - Fork 38
/
gltf2usd.py
executable file
·923 lines (729 loc) · 41.3 KB
/
gltf2usd.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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
#!/usr/local/bin/python
import argparse
import base64
import collections
import filecmp
import json
import logging
import ntpath
import numpy
import os
import tempfile
import re
import shutil
from io import BytesIO
from PIL import Image
from pxr import Usd, UsdGeom, Sdf, UsdShade, Gf, UsdSkel, Vt, Ar, UsdUtils
from _gltf2usd.gltf2loader import GLTF2Loader, PrimitiveMode, TextureWrap, MinFilter, MagFilter
from _gltf2usd.gltf2usdUtils import GLTF2USDUtils
from _gltf2usd.usd_material import USDMaterial
from _gltf2usd import version
__version__ = version.Version.get_version_name()
class GLTF2USD(object):
"""
Class for converting glTF 2.0 models to Pixar's USD format. Currently openly supports .gltf files
with non-embedded data and exports to .usda .
"""
TEXTURE_SAMPLER_WRAP = {
TextureWrap.CLAMP_TO_EDGE : 'clamp',
TextureWrap.MIRRORED_REPEAT : 'mirror',
TextureWrap.REPEAT: 'repeat',
}
def __init__(self, gltf_file, usd_file, fps, scale, verbose=False, use_euler_rotation=False, optimize_textures=False, generate_texture_transform_texture=True, scale_texture=False):
"""Initializes the glTF to USD converter
Arguments:
gltf_file {str} -- path to the glTF file
usd_file {str} -- path to store the generated usda file
verbose {boolean} -- specifies if the output should be verbose from this tool
"""
self.logger = logging.getLogger('gltf2usd')
self.logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
self.logger.addHandler(console_handler)
self.fps = fps
self.gltf_loader = GLTF2Loader(gltf_file, optimize_textures, generate_texture_transform_texture)
self.verbose = verbose
self.scale = scale
self.use_euler_rotation = use_euler_rotation
self._scale_texture = scale_texture
self.output_dir = os.path.dirname(os.path.abspath(usd_file))
if self.verbose:
self.logger.info("Converting {0} to {1}".format(gltf_file, usd_file))
#if usdz file is desired, change to usdc file
if usd_file.endswith('usdz'):
usd_file = usd_file[:-1] + 'c'
self.stage = Usd.Stage.CreateNew(usd_file)
self.gltf_usd_nodemap = {}
self.gltf_usdskel_nodemap = {}
self._usd_mesh_skin_map = {}
self._joint_hierarchy_name_map = {}
self.convert()
def convert_nodes_to_xform(self):
"""
Converts the glTF nodes to USD Xforms. The models get a parent Xform that scales the geometry by 100
to convert from meters (glTF) to centimeters (USD).
"""
parent_transform = UsdGeom.Xform.Define(self.stage, '/root')
parent_transform.AddScaleOp().Set((self.scale, self.scale, self.scale))
main_scene = self.gltf_loader.get_main_scene()
nodes = main_scene.get_nodes()
root_nodes = [node for node in nodes if node.parent == None]
for node in root_nodes:
self._convert_node_to_xform(node, parent_transform)
def _convert_node_to_xform(self, node, usd_xform):
"""Converts a glTF node to a USD transform node.
Arguments:
node {dict} -- glTF node
node_index {int} -- glTF node index
xform_name {str} -- USD xform name
"""
xformPrim = UsdGeom.Xform.Define(self.stage, '{0}/{1}'.format(usd_xform.GetPath(), GLTF2USDUtils.convert_to_usd_friendly_node_name(node.name)))
if self._node_has_animations(node):
self._convert_animation_to_usd(node, xformPrim)
else:
xformPrim.AddTransformOp().Set(self._compute_rest_matrix(node))
mesh = node.get_mesh()
if mesh != None:
usd_mesh = self._convert_mesh_to_xform(mesh, xformPrim, node)
children = node.get_children()
for child in children:
self._convert_node_to_xform(child, xformPrim)
if node.extras:
self.stage.OverridePrim(xformPrim.GetPath()).SetCustomData(node.extras)
def _create_usd_skeleton(self, gltf_skin, usd_xform, usd_joint_names):
"""Creates a USD skeleton from a glTF skin
Arguments:
gltf_skin {Skin} -- gltf skin
usd_xform {Xform} -- USD Xform
Returns:
Skeleton -- USD skeleton
"""
# create skeleton
root_joints = gltf_skin.root_joints
root_joint_names = [GLTF2USDUtils.convert_to_usd_friendly_node_name(root_joint.name) for root_joint in root_joints]
skeleton = None
if len(root_joints) == 1:
skeleton = UsdSkel.Skeleton.Define(self.stage, '{0}/{1}'.format(usd_xform.GetPath(), root_joint_names[0]))
else:
skeleton = UsdSkel.Skeleton.Define(self.stage, '{0}/{1}'.format(usd_xform.GetPath(), '__root__'))
gltf_bind_transforms = [Gf.Matrix4d(*xform).GetInverse() for xform in gltf_skin.get_inverse_bind_matrices()]
gltf_rest_transforms = [GLTF2USDUtils.compute_usd_transform_matrix_from_gltf_node(joint) for joint in gltf_skin.get_joints()]
if len(root_joints) > 1:
matrix = Gf.Matrix4d()
matrix.SetIdentity()
skeleton.CreateJointsAttr().Set(usd_joint_names)
skeleton.CreateBindTransformsAttr(gltf_bind_transforms)
skeleton.CreateRestTransformsAttr(gltf_rest_transforms)
return skeleton
def _create_usd_skeleton_animation(self, gltf_skin, usd_skeleton, joint_names):
#get the animation data per joint
skelAnim = None
gltf_animations = self.gltf_loader.get_animations()
if len(gltf_animations):
skelAnim = UsdSkel.Animation.Define(self.stage, '{0}/{1}'.format(usd_skeleton.GetPath(), 'anim'))
usd_skel_root_path = usd_skeleton.GetPath().GetParentPath()
usd_skel_root = self.stage.GetPrimAtPath(usd_skel_root_path)
skelAnim.CreateJointsAttr().Set(joint_names)
gltf_animation = self.gltf_loader.get_animations()[0]
min_sample = 999
max_sample = -999
for sampler in gltf_animation.get_samplers():
input_data = sampler.get_input_data()
min_sample = min(min_sample, input_data[0])
max_sample = max(max_sample, input_data[-1])
rotate_attr = skelAnim.CreateRotationsAttr()
for input_key in numpy.arange(min_sample, max_sample, 1./self.fps):
entries = []
for joint in gltf_skin.get_joints():
anim = self._get_anim_data_for_joint_and_path(gltf_animation, joint, 'rotation', input_key)
entries.append(anim)
if len(gltf_skin.get_joints()) != len(entries):
raise Exception('up oh!')
rotate_attr.Set(Vt.QuatfArray(entries), Usd.TimeCode(input_key * self.fps))
translate_attr = skelAnim.CreateTranslationsAttr()
for input_key in numpy.arange(min_sample, max_sample, 1./self.fps):
entries = []
for joint in gltf_skin.get_joints():
anim = self._get_anim_data_for_joint_and_path(gltf_animation, joint, 'translation', input_key)
entries.append(anim)
if len(gltf_skin.get_joints()) != len(entries):
raise Exception('up oh!')
translate_attr.Set(entries, Usd.TimeCode(input_key * self.fps))
scale_attr = skelAnim.CreateScalesAttr()
for input_key in numpy.arange(min_sample, max_sample, 1./self.fps):
entries = []
for joint in gltf_skin.get_joints():
anim = self._get_anim_data_for_joint_and_path(gltf_animation, joint, 'scale', input_key)
entries.append(anim)
if len(gltf_skin.get_joints()) != len(entries):
raise Exception('up oh!')
scale_attr.Set(entries, Usd.TimeCode(input_key * self.fps))
return skelAnim
def _get_anim_data_for_joint_and_path(self, gltf_animation, gltf_joint, path, time_sample):
anim_channel = gltf_animation.get_animation_channel_for_node_and_path(gltf_joint, path)
if not anim_channel:
if path == 'translation':
return gltf_joint.translation
elif path == 'rotation':
gltf_rotation = gltf_joint.rotation
usd_rotation = Gf.Quatf(gltf_rotation[3], gltf_rotation[0], gltf_rotation[1], gltf_rotation[2])
return usd_rotation
elif path == 'scale':
return gltf_joint.scale
else:
if path == 'rotation':
rotation = anim_channel.sampler.get_interpolated_output_data(time_sample)
return rotation
elif path == 'scale' or path =='translation':
return anim_channel.sampler.get_interpolated_output_data(time_sample)
else:
raise Exception('unsupported animation type: {}'.format(path))
def _get_usd_joint_hierarchy_name(self, gltf_joint, root_joints):
if gltf_joint in self._joint_hierarchy_name_map:
return GLTF2USDUtils.convert_to_usd_friendly_node_name(self._joint_hierarchy_name_map[gltf_joint])
joint = gltf_joint
joint_name_stack = [GLTF2USDUtils.convert_to_usd_friendly_node_name(joint.name)]
while joint.parent != None and joint not in root_joints:
joint = joint.parent
joint_name_stack.append(GLTF2USDUtils.convert_to_usd_friendly_node_name(joint.name))
joint_name = ''
while len(joint_name_stack) > 0:
if joint_name:
joint_name = '{0}/{1}'.format(joint_name, joint_name_stack.pop())
else:
joint_name = joint_name_stack.pop()
self._joint_hierarchy_name_map[gltf_joint] = joint_name
return GLTF2USDUtils.convert_to_usd_friendly_node_name(joint_name)
def _convert_mesh_to_xform(self, gltf_mesh, usd_node, gltf_node):
"""
Converts a glTF mesh to a USD Xform.
Each primitive becomes a submesh of the Xform.
Arguments:
mesh {dict} -- glTF mesh
parent_path {str} -- xform path
node_index {int} -- glTF node index
"""
#for each mesh primitive, create a USD mesh
primitive_count = 1
max_primitive_count = len(gltf_mesh.get_primitives())
for primitive in gltf_mesh.get_primitives():
self._convert_primitive_to_mesh(primitive, usd_node, gltf_node, gltf_mesh)
if self.verbose:
self.logger.debug('mesh {0}: primitive {1}/{2}'.format(gltf_mesh.name, primitive_count, max_primitive_count))
primitive_count += 1
def _convert_primitive_to_mesh(self, gltf_primitive, usd_node, gltf_node, gltf_mesh):
"""
Converts a glTF mesh primitive to a USD mesh
Arguments:
name {str} -- name of the primitive
primitive {dict} -- glTF primitive
usd_parent_node {str} -- USD parent xform
node_index {int} -- glTF node index
double_sided {bool} -- specifies if the primitive is double sided
"""
parent_node = usd_node
parent_path = parent_node.GetPath()
attributes = gltf_primitive.get_attributes()
skel_root = None
targets = gltf_primitive.get_morph_targets()
if 'JOINTS_0' in attributes or len(targets) > 0:
skeleton_path = '{0}/{1}'.format(usd_node.GetPath(), 'skeleton_root')
skel_root = UsdSkel.Root.Define(self.stage, skeleton_path)
parent_node = skel_root
parent_path = parent_node.GetPath()
mesh = UsdGeom.Mesh.Define(self.stage, '{0}/{1}'.format(parent_node.GetPath(), GLTF2USDUtils.convert_to_usd_friendly_node_name(gltf_primitive.get_name())))
mesh.CreateSubdivisionSchemeAttr().Set('none')
material = gltf_primitive.get_material()
if material != None:
if material.is_double_sided():
mesh.CreateDoubleSidedAttr().Set(True)
usd_material = self.usd_materials[material.get_index()]
UsdShade.MaterialBindingAPI(mesh).Bind(usd_material.get_usd_material())
for attribute_name in attributes:
attribute = attributes[attribute_name]
if attribute_name == 'POSITION':
override_prim = self.stage.OverridePrim(mesh.GetPath())
override_prim.CreateAttribute('extent', Sdf.ValueTypeNames.Float3Array).Set([attribute.get_min_value(), attribute.get_max_value()])
mesh.CreatePointsAttr(attribute.get_data())
if attribute_name == 'NORMAL':
mesh.CreateNormalsAttr(attribute.get_data())
if attribute_name == 'COLOR_0':
prim_var = UsdGeom.PrimvarsAPI(mesh)
data = attribute.get_data()
if attribute.accessor_type == 'VEC4':
print('Vertex color alpha currently not supported. Defaulting to vertex color without alpha.')
data = [Gf.Vec3f(entry[0:3]) for entry in attribute.get_data()]
colors = prim_var.CreatePrimvar('displayColor', Sdf.ValueTypeNames.Color3f, 'vertex').Set(data)
if attribute_name == 'TEXCOORD_0':
data = attribute.get_data()
invert_uvs = []
for uv in data:
new_uv = (uv[0], 1 - uv[1])
invert_uvs.append(new_uv)
prim_var = UsdGeom.PrimvarsAPI(mesh)
uv = prim_var.CreatePrimvar('primvars:st0', Sdf.ValueTypeNames.TexCoord2fArray, 'vertex')
uv.Set(invert_uvs)
if attribute_name == 'TEXCOORD_1':
data = attribute.get_data()
invert_uvs = []
for uv in data:
new_uv = (uv[0], 1 - uv[1])
invert_uvs.append(new_uv)
prim_var = UsdGeom.PrimvarsAPI(mesh)
uv = prim_var.CreatePrimvar('primvars:st1', Sdf.ValueTypeNames.TexCoord2fArray, 'vertex')
uv.Set(invert_uvs)
if attribute_name == 'JOINTS_0':
self._convert_skin_to_usd(gltf_node, gltf_primitive, parent_node, mesh)
weights = gltf_mesh.get_weights()
if targets:
skinBinding = UsdSkel.BindingAPI.Apply(mesh.GetPrim())
skeleton = UsdSkel.Skeleton.Define(self.stage, '{0}/skel'.format(parent_path))
# Create an animation for this mesh to hold the blendshapes
skelAnim = UsdSkel.Animation.Define(self.stage, '{0}/skel/anim'.format(parent_path))
# link the skeleton animation to skelAnim
skinBinding.CreateAnimationSourceRel().AddTarget(skelAnim.GetPath())
skeleton_skel_binding = UsdSkel.BindingAPI(skeleton)
skeleton_skel_binding.CreateAnimationSourceRel().AddTarget(skelAnim.GetPath())
# link the skeleton to skeleton
skinBinding.CreateSkeletonRel().AddTarget(skeleton.GetPath())
# Set blendshape names on the animation
names = []
for i, _ in enumerate(gltf_mesh.get_weights()):
targets[i].get_name()
blend_shape_name = GLTF2USDUtils.convert_to_usd_friendly_node_name(targets[i].get_name())
names.append(blend_shape_name)
skelAnim.CreateBlendShapesAttr().Set(names)
self._set_blendshape_weights(skelAnim, usd_node, gltf_node)
skinBinding.CreateBlendShapesAttr(names)
# Set the starting weights of each blendshape to the weights defined in the glTF primitive
blend_shape_targets = skinBinding.CreateBlendShapeTargetsRel()
# Define offsets for each blendshape, and add them as skel:blendShapes and skel:blendShapeTargets
for i, name in enumerate(names):
offsets = targets[i].get_attributes()['POSITION']
blend_shape_name = '{0}/{1}'.format(mesh.GetPath(), name)
# define blendshapes in the mesh
blend_shape = UsdSkel.BlendShape.Define(self.stage, blend_shape_name)
blend_shape.CreateOffsetsAttr(offsets)
blend_shape_targets.AddTarget(name)
indices = gltf_primitive.get_indices()
num_faces = len(indices)/3
face_count = [3] * num_faces
mesh.CreateFaceVertexCountsAttr(face_count)
mesh.CreateFaceVertexIndicesAttr(indices)
def _get_texture__wrap_modes(self, texture):
"""Get the USD texture wrap modes from a glTF texture
Arguments:
texture {dict} -- glTF texture
Returns:
dict -- dictionary mapping wrapS and wrapT to
a USD texture sampler mode
"""
texture_data = {'wrapS': 'repeat', 'wrapT': 'repeat'}
if 'sampler' in texture:
sampler = self.gltf_loader.json_data['samplers'][texture['sampler']]
if 'wrapS' in sampler:
texture_data['wrapS'] = GLTF2USD.TEXTURE_SAMPLER_WRAP[TextureWrap(sampler['wrapS'])]
if 'wrapT' in sampler:
texture_data['wrapT'] = GLTF2USD.TEXTURE_SAMPLER_WRAP[TextureWrap(sampler['wrapT'])]
return texture_data
def _convert_images_to_usd(self):
"""
Converts the glTF images to USD images
"""
if 'images' in self.gltf_loader.json_data:
self.images = []
for i, image in enumerate(self.gltf_loader.json_data['images']):
image_name = ''
# save data-uri textures
if 'bufferView' in image or image['uri'].startswith('data:image'):
img = None
if 'bufferView' in image:
buffer_view = self.gltf_loader.json_data['bufferViews'][image['bufferView']]
buffer = self.gltf_loader.json_data['buffers'][buffer_view['buffer']]
img_base64 = buffer['uri'].split(',')[1]
buff = BytesIO()
buff.write(base64.b64decode(img_base64))
buff.seek(buffer_view['byteOffset'])
img = Image.open(BytesIO(buff.read(buffer_view['byteLength'])))
elif image['uri'].startswith('data:image'):
uri_data = image['uri'].split(',')[1]
img = Image.open(BytesIO(base64.b64decode(uri_data)))
# NOTE: image might not have a name
image_name = image['name'] if 'name' in image else 'image{}.{}'.format(i, img.format.lower())
image_path = os.path.join(self.gltf_loader.root_dir, image_name)
img.save(image_path)
# otherwise just copy the texture over
else:
image_path = os.path.join(self.gltf_loader.root_dir, image['uri'])
image_name = os.path.join(self.output_dir, ntpath.basename(image_path))
if (self.gltf_loader.root_dir is not self.output_dir) and (image_path is not image_name):
if not (os.path.isfile(image_name) and filecmp.cmp(image_path, image_name)):
shutil.copyfile(image_path, image_name)
self.images.append(ntpath.basename(image_name))
def _convert_materials_to_preview_surface_new(self):
"""
Converts the glTF materials to preview surfaces
"""
self.usd_materials = []
material_path_root = '/root/Materials'
scope = UsdGeom.Scope.Define(self.stage, material_path_root)
material_name_map = []
for i, material in enumerate(self.gltf_loader.get_materials()):
material_name = '{0}_index_{1}'.format(GLTF2USDUtils.convert_to_usd_friendly_node_name(material.get_name()), i)
#check for unique material name
if material_name in material_name_map:
count = 1
new_material_name = '{0}_{1}'.format(material_name, count)
while new_material_name in material_name_map:
count += 1
new_material_name = '{0}_{1}'.format(material_name, count)
material_name = new_material_name
material_name_map.append(material_name)
usd_material = USDMaterial(self.stage, material_name, scope, i, self.gltf_loader, self._scale_texture)
usd_material.convert_material_to_usd_preview_surface(material, self.output_dir, material_name)
self.usd_materials.append(usd_material)
def _node_has_animations(self, gltf_node):
animations = self.gltf_loader.get_animations()
for animation in animations:
animation_channels = animation.get_animation_channels_for_node(gltf_node)
if len(animation_channels) > 0:
return True
return False
def _convert_animation_to_usd(self, gltf_node, usd_node):
animations = self.gltf_loader.get_animations()
if (len(animations) > 0): # only support first animation group
animation = animations[0]
animation_channels = animation.get_animation_channels_for_node(gltf_node)
if len(animation_channels) > 0:
total_max_time = -999
total_min_time = 999
min_max_time = self._create_usd_animation2(usd_node, gltf_node, animation_channels)
total_max_time = max(total_max_time, min_max_time.max)
total_min_time = min(total_min_time, min_max_time.min)
self.stage.SetStartTimeCode(total_min_time * self.fps)
self.stage.SetEndTimeCode(total_max_time * self.fps)
self.stage.SetTimeCodesPerSecond(self.fps)
def _create_keyframe_transform_node(self, gltf_node, animation_channels, input_sample):
matrix = gltf_node.matrix
if matrix:
translation = Gf.Vec3f()
rotation = Gf.Quatf()
scale = Gf.Vec3h()
usd_matrix = self._convert_to_usd_matrix(matrix)
UsdSkel.DecomposeTransform(usd_matrix, translation, rotation, scale)
else:
translation = Gf.Vec3f(gltf_node.translation)
rotation = Gf.Quatf(gltf_node.rotation[3], gltf_node.rotation[0], gltf_node.rotation[1], gltf_node.rotation[2])
scale = Gf.Vec3h(gltf_node.scale)
for animation_channel in animation_channels:
if animation_channel.target.path == 'translation':
translation = animation_channel.sampler.get_interpolated_output_data(input_sample)
elif animation_channel.target.path == 'rotation':
rotation = animation_channel.sampler.get_interpolated_output_data(input_sample)
elif animation_channel.target.path == 'scale':
scale = animation_channel.sampler.get_interpolated_output_data(input_sample)
elif animation_channel.target.path == 'weights':
weights = animation_channel.sampler.get_output_data()
return UsdSkel.MakeTransform(translation, rotation, scale)
def _convert_skin_to_usd(self, gltf_node, gltf_primitive, usd_node, usd_mesh):
"""Converts a glTF skin to a UsdSkel
Arguments:
gltf_node {dict} -- glTF node
node_index {int} -- index of the glTF node
usd_parent_node {UsdPrim} -- parent node of the usd node
usd_mesh {[type]} -- [description]
"""
skel_binding_api = UsdSkel.BindingAPI(usd_mesh)
gltf_skin = gltf_node.get_skin()
if not gltf_skin:
self.logger.warning('The glTF node has joints, but no skin associated with them')
else:
gltf_joint_names = [GLTF2USDUtils.convert_to_usd_friendly_node_name(joint.name) for joint in gltf_skin.get_joints()]
usd_joint_names = [Sdf.Path(self._get_usd_joint_hierarchy_name(joint, gltf_skin.root_joints)) for joint in gltf_skin.get_joints()]
skeleton = self._create_usd_skeleton(gltf_skin, usd_node, usd_joint_names)
skeleton_animation = self._create_usd_skeleton_animation(gltf_skin, skeleton, usd_joint_names)
parent_path = usd_node.GetPath()
bind_matrices = []
rest_matrices = []
skeleton_root = self.stage.GetPrimAtPath(parent_path)
skel_binding_api = UsdSkel.BindingAPI(usd_mesh)
skel_binding_api.CreateGeomBindTransformAttr(Gf.Matrix4d(((1,0,0,0),(0,1,0,0),(0,0,1,0),(0,0,0,1))))
skel_binding_api.CreateSkeletonRel().AddTarget(skeleton.GetPath())
if skeleton_animation:
skel_binding_api.CreateAnimationSourceRel().AddTarget(skeleton_animation.GetPath())
skeleton_skel_binding_api = UsdSkel.BindingAPI(skeleton)
skeleton_skel_binding_api.CreateAnimationSourceRel().AddTarget(skeleton_animation.GetPath())
bind_matrices = self._compute_bind_transforms(gltf_skin)
primitive_attributes = gltf_primitive.get_attributes()
if 'WEIGHTS_0' in primitive_attributes and 'JOINTS_0' in primitive_attributes:
total_vertex_weights = primitive_attributes['WEIGHTS_0'].get_data()
total_vertex_joints = primitive_attributes['JOINTS_0'].get_data()
total_joint_indices = []
total_joint_weights = []
for joint_indices, weights in zip(total_vertex_joints, total_vertex_weights):
for joint_index, weight in zip(joint_indices, weights):
total_joint_indices.append(joint_index)
total_joint_weights.append(weight)
joint_indices_attr = skel_binding_api.CreateJointIndicesPrimvar(False, 4).Set(total_joint_indices)
total_joint_weights = Vt.FloatArray(total_joint_weights)
UsdSkel.NormalizeWeights(total_joint_weights, 4)
joint_weights_attr = skel_binding_api.CreateJointWeightsPrimvar(False, 4).Set(total_joint_weights)
def _compute_bind_transforms(self, gltf_skin):
"""Compute the bind matrices from the skin
Arguments:
gltf_skin {Skin} -- glTF skin
Returns:
[list] -- List of bind matrices
"""
bind_matrices = []
inverse_bind_matrices = gltf_skin.get_inverse_bind_matrices()
for matrix in inverse_bind_matrices:
bind_matrix = self._convert_to_usd_matrix(matrix).GetInverse()
bind_matrices.append(bind_matrix)
return bind_matrices
def _convert_to_usd_matrix(self, matrix):
"""Converts a glTF matrix to a Usd Matrix
Arguments:
matrix {[type]} -- [description]
Returns:
[type] -- [description]
"""
return Gf.Matrix4d(
matrix[0], matrix[1], matrix[2], matrix[3],
matrix[4], matrix[5], matrix[6], matrix[7],
matrix[8], matrix[9], matrix[10], matrix[11],
matrix[12], matrix[13], matrix[14], matrix[15]
)
def _compute_rest_matrix(self, gltf_node):
"""
Compute the rest matrix from a glTF node.
The translation, rotation and scale are combined into a transformation matrix
Returns:
Matrix4d -- USD matrix
"""
xform_matrix = None
matrix = gltf_node.matrix
if matrix != None:
xform_matrix = self._convert_to_usd_matrix(matrix)
return xform_matrix
else:
usd_scale = Gf.Vec3h(1,1,1)
usd_rotation = Gf.Quatf().GetIdentity()
usd_translation = Gf.Vec3f(0,0,0)
scale = gltf_node.scale
usd_scale = Gf.Vec3h(scale[0], scale[1], scale[2])
rotation = gltf_node.rotation
usd_rotation = Gf.Quatf(rotation[3], rotation[0], rotation[1], rotation[2])
translation = gltf_node.translation
usd_translation = Gf.Vec3f(translation[0], translation[1], translation[2])
return UsdSkel.MakeTransform(usd_translation, usd_rotation, usd_scale)
def _create_usd_animation(self, usd_node, animation_channel):
"""Converts a glTF animation to a USD animation
Arguments:
usd_node {[type]} -- usd node
animation_channel {AnimationMap} -- map of animation target path and animation sampler indices
Returns:
[type] -- [description]
"""
sampler = animation_channel.sampler
max_time = int(round(sampler.get_input_max()[0] ))
min_time = int(round(sampler.get_input_min()[0] ))
input_keyframes = sampler.get_input_data()
output_keyframes = sampler.get_output_data()
num_values = sampler.get_output_count() / sampler.get_input_count()
(transform, convert_func) = self._get_keyframe_conversion_func(usd_node, animation_channel)
for i, keyframe in enumerate(numpy.arange(min_time, max_time, 1./self.fps)):
convert_func(transform, keyframe, output_keyframes, i, num_values)
MinMaxTime = collections.namedtuple('MinMaxTime', ('max', 'min'))
return MinMaxTime(max=max_time, min=min_time)
def _create_usd_animation2(self, usd_node, gltf_node, animation_channels):
"""Converts a glTF animation to a USD animation
Arguments:
usd_node {[type]} -- usd node
gltf_node {[type]} -- glTF node
animation_channel {AnimationMap} -- map of animation target path and animation sampler indices
Returns:
[type] -- [description]
"""
max_time = -999
min_time = 999
for channel in animation_channels:
max_time = max(max_time, channel.sampler.get_input_max()[0])
min_time = min(min_time, channel.sampler.get_input_min()[0])
transform = usd_node.AddTransformOp(opSuffix='transform')
for animation_channel in animation_channels:
#if animation_channel.target.path != 'weights':
for i, keyframe in enumerate(numpy.arange(min_time, max_time, 1./self.fps)):
transform_node = self._create_keyframe_transform_node(gltf_node, animation_channels, keyframe)
transform.Set(transform_node, Usd.TimeCode(i))
MinMaxTime = collections.namedtuple('MinMaxTime', ('max', 'min'))
return MinMaxTime(max=max_time, min=min_time)
def _set_blendshape_weights(self, skel_anim, usd_node, gltf_node):
animations = self.gltf_loader.get_animations()
if (len(animations) > 0): # only support first animation group
animation = animations[0]
animation_channels = animation.get_animation_channels_for_node(gltf_node)
for animation_channel in animation_channels:
if animation_channel.target.path == 'weights':
output_data = animation_channel.sampler.get_output_data()
input_data = animation_channel.sampler.get_input_data()
output_data_entries = []
for index in range(0, len(output_data)/2):
output_data_entries.append([output_data[index * 2], output_data[index * 2 + 1]])
usd_blend_shape_weights = skel_anim.CreateBlendShapeWeightsAttr()
for entry in zip(output_data_entries, input_data):
usd_blend_shape_weights.Set(Vt.FloatArray(entry[0]), Usd.TimeCode(entry[1] * self.fps))
def _get_keyframe_conversion_func(self, usd_node, animation_channel):
"""Convert glTF key frames to USD animation key frames
Arguments:
usd_node {UsdPrim} -- USD node to apply animations to
animation_channel {obj} -- glTF animation
Raises:
Exception -- [description]
Returns:
[func] -- USD animation conversion function
"""
path = animation_channel.target.path
animation_sampler = animation_channel.sampler
def convert_translation(transform, time, output, i, _):
value = animation_sampler.get_interpolated_output_data(time)
transform.Set(time=time * self.fps, value=(value[0], value[1], value[2]))
def convert_scale(transform, time, output, i, _):
value = animation_sampler.get_interpolated_output_data(time)
transform.Set(time=time * self.fps, value=(value[0], value[1], value[2]))
def convert_rotation(transform, time, output, i, _):
value = animation_sampler.get_interpolated_output_data(time)
if self.use_euler_rotation:
value = Gf.Rotation(value).Decompose((1,0,0), (0,1,0), (0,0,1))
transform.Set(time=time * self.fps, value=value)
def convert_weights(transform, time, output, i, values_per_step):
start = i * values_per_step
end = start + values_per_step
values = output[start:end]
value = list(map(lambda x: round(x, 5) + 0, values))
transform.Set(time=time * self.fps, value=value)
if path == 'translation':
return (usd_node.AddTranslateOp(opSuffix='translate'), convert_translation)
elif path == 'rotation':
if self.use_euler_rotation:
return (usd_node.AddRotateXYZOp(opSuffix='rotate'), convert_rotation)
else:
return (usd_node.AddOrientOp(opSuffix='rotate'), convert_rotation)
elif path == 'scale':
return (usd_node.AddScaleOp(opSuffix='scale'), convert_scale)
elif path == 'weights':
prim = usd_node.GetPrim().GetChild("skeleton_root").GetChild("skel").GetChild("anim")
anim_attr = prim.GetAttribute('blendShapeWeights')
return (anim_attr, convert_weights)
else:
raise Exception('Unsupported animation target path! {}'.format(path))
def convert(self):
if hasattr(self, 'gltf_loader'):
self._convert_images_to_usd()
self._convert_materials_to_preview_surface_new()
self.convert_nodes_to_xform()
def check_usd_compliance(rootLayer, arkit=False):
#An API change in v18.11 changed the sytax for UsdUtils.ComplianceChecker...
if Usd.GetMinorVersion() > 18 or (Usd.GetMinorVersion() == 18 and Usd.GetPatchVersion() >= 11):
checker = UsdUtils.ComplianceChecker(arkit=arkit, skipARKitRootLayerCheck=False)
checker.CheckCompliance(rootLayer)
else:
#Behavior in v18.09
checker = UsdUtils.ComplianceChecker(rootLayer, arkit=arkit, skipARKitRootLayerCheck=False)
errors = checker.GetErrors()
failedChecks = checker.GetFailedChecks()
for msg in errors + failedChecks:
print(msg)
return len(errors) == 0 and len(failedChecks) == 0
def convert_to_usd(gltf_file, usd_file, fps, scale, arkit=False, verbose=False, use_euler_rotation=False, optimize_textures=False, generate_texture_transform_texture=True, scale_texture=False):
"""Converts a glTF file to USD
Arguments:
gltf_file {str} -- path to glTF file
usd_file {str} -- path to write USD file
Keyword Arguments:
verbose {bool} -- [description] (default: {False})
"""
temp_dir = tempfile.mkdtemp()
temp_usd_file = os.path.join(temp_dir, ntpath.basename(usd_file))
try:
usd = GLTF2USD(gltf_file=gltf_file, usd_file=temp_usd_file, fps=fps, scale=scale, verbose=verbose, use_euler_rotation=use_euler_rotation, optimize_textures=optimize_textures, generate_texture_transform_texture=generate_texture_transform_texture, scale_texture=scale_texture)
if usd.stage:
asset = usd.stage.GetRootLayer()
gltf_asset = usd.gltf_loader.get_asset()
if gltf_asset:
gltf_metadata = {'creator': 'gltf2usd v{}'.format(__version__)}
if gltf_asset.generator:
gltf_metadata['gltf_generator'] = gltf_asset.generator
if gltf_asset.version:
gltf_metadata['gltf_version'] = gltf_asset.version
if gltf_asset.minversion:
gltf_metadata['gltf_minversion'] = gltf_asset.minversion
if gltf_asset.copyright:
gltf_metadata['gltf_copyright'] = gltf_asset.copyright
if gltf_asset.extras:
for key, value in gltf_asset.extras.items():
gltf_metadata['gltf_extras_{}'.format(key)] = value
asset.customLayerData = gltf_metadata
usd.logger.info('Conversion complete!')
asset.Save()
usd.logger.info('created {}'.format(asset.realPath))
if not os.path.isdir(os.path.dirname(usd_file)):
os.makedirs(os.path.dirname(usd_file))
if temp_usd_file.endswith('.usdz') or temp_usd_file.endswith('.usdc'):
usdc_file = '%s.%s' % (os.path.splitext(temp_usd_file)[0], 'usdc')
asset.Export(usdc_file, args=dict(format='usdc'))
usd.logger.info('created {}'.format(usdc_file))
if temp_usd_file.endswith('.usdz'):
#change to directory of the generated usd files to avoid issues with
# relative paths with CreateNewUsdzPackage
os.chdir(os.path.dirname(usdc_file))
temp_usd_file = ntpath.basename(temp_usd_file)
r = Ar.GetResolver()
resolved_asset = r.Resolve(ntpath.basename(usdc_file))
context = r.CreateDefaultContextForAsset(resolved_asset)
success = check_usd_compliance(resolved_asset, arkit=args.arkit)
with Ar.ResolverContextBinder(context):
if arkit and not success:
usd.logger.warning('USD is not ARKit compliant')
return
success = UsdUtils.CreateNewUsdzPackage(resolved_asset, temp_usd_file) and success
if success:
shutil.copyfile(temp_usd_file, usd_file)
usd.logger.info('created package {} with contents:'.format(usd_file))
zip_file = Usd.ZipFile.Open(usd_file)
file_names = zip_file.GetFileNames()
for file_name in file_names:
usd.logger.info('\t{}'.format(file_name))
else:
usd.logger.error('could not create {}'.format(usd_file))
else:
# Copy textures referenced in the Usda/Usdc files from the temp directory to the target directory
temp_stage = Usd.Stage.Open(temp_usd_file)
usd_uv_textures = [x for x in temp_stage.Traverse() if x.IsA(UsdShade.Shader) and UsdShade.Shader(x).GetShaderId() == 'UsdUVTexture']
for usd_uv_texture in usd_uv_textures:
file_name = usd_uv_texture.GetAttribute('inputs:file').Get()
if file_name:
file_name = str(file_name).replace('@', '')
if os.path.isfile(os.path.join(temp_dir, file_name)):
shutil.copyfile(os.path.join(temp_dir, file_name), os.path.join(os.path.dirname(usd_file), file_name))
shutil.copyfile(temp_usd_file, usd_file)
finally:
shutil.rmtree(temp_dir)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Convert glTF to USD: v{}'.format(__version__))
parser.add_argument('--gltf', '-g', action='store', dest='gltf_file', help='glTF file (in .gltf format)', required=True)
parser.add_argument('--fps', action='store', dest='fps', help='The frames per second for the animations', type=float, default=24.0)
parser.add_argument('--output', '-o', action='store', dest='usd_file', help='destination to store generated .usda file', required=True)
parser.add_argument('--verbose', '-v', action='store_true', dest='verbose', help='Enable verbose mode')
parser.add_argument('--scale', '-s', action='store', dest='scale', help='Scale the resulting USDA', type=float, default=100)
parser.add_argument('--arkit', action='store_true', dest='arkit', help='Check USD with ARKit compatibility before making USDZ file')
parser.add_argument('--use-euler-rotation', action='store_true', dest='use_euler_rotation', help='sets euler rotations for node animations instead of quaternion rotations')
parser.add_argument('--optimize-textures', action='store_true', dest='optimize_textures', default=False, help='Specifies if image file size should be optimized and reduced at the expense of longer export time')
parser.add_argument('--generate_texture_transform_texture', dest='generate_texture_transform_texture', action='store_true', help='Enables texture transform texture generation')
parser.add_argument('--no-generate_texture_transform_texture', dest='generate_texture_transform_texture', action='store_false', help='Disables texture transform texture generation')
parser.add_argument('--scale-texture', dest='scale_texture', action='store_true', help='Multiplies metallic/roughness factors by textures', default=False)
parser.set_defaults(generate_texture_transform_texture=True)
args = parser.parse_args()
if args.gltf_file:
convert_to_usd(os.path.expanduser(args.gltf_file), os.path.abspath(os.path.expanduser(args.usd_file)), args.fps, args.scale, args.arkit, args.verbose, args.use_euler_rotation, args.optimize_textures, args.generate_texture_transform_texture, args.scale_texture)