glTF importer: convert glTF meters to the units of the Blender scene
This commit is contained in:
parent
b0b94bf49e
commit
fc7cf5847a
|
@ -15,7 +15,7 @@
|
|||
bl_info = {
|
||||
'name': 'glTF 2.0 format',
|
||||
'author': 'Julien Duroure, Scurest, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors',
|
||||
"version": (1, 4, 19),
|
||||
"version": (1, 4, 20),
|
||||
'blender': (2, 90, 0),
|
||||
'location': 'File > Import-Export',
|
||||
'description': 'Import-Export as glTF 2.0',
|
||||
|
|
|
@ -50,23 +50,34 @@ class BlenderGlTF():
|
|||
|
||||
@staticmethod
|
||||
def set_convert_functions(gltf):
|
||||
gltf.yup2zup = bpy.app.debug_value != 100
|
||||
if bpy.app.debug_value != 100:
|
||||
# Unit conversion factor in (Blender units) per meter
|
||||
u = 1.0 / bpy.context.scene.unit_settings.scale_length
|
||||
|
||||
if gltf.yup2zup:
|
||||
# glTF Y-Up space --> Blender Z-up space
|
||||
# X,Y,Z --> X,-Z,Y
|
||||
def convert_loc(x): return Vector([x[0], -x[2], x[1]])
|
||||
def convert_loc(x): return u * Vector([x[0], -x[2], x[1]])
|
||||
def convert_quat(q): return Quaternion([q[3], q[0], -q[2], q[1]])
|
||||
def convert_normal(n): return Vector([n[0], -n[2], n[1]])
|
||||
def convert_scale(s): return Vector([s[0], s[2], s[1]])
|
||||
def convert_matrix(m):
|
||||
return Matrix([
|
||||
[ m[0], -m[ 8], m[4], m[12]],
|
||||
[-m[2], m[10], -m[6], -m[14]],
|
||||
[ m[1], -m[ 9], m[5], m[13]],
|
||||
[ m[3], -m[11], m[7], m[15]],
|
||||
[ m[0], -m[ 8], m[4], m[12]*u],
|
||||
[ -m[2], m[10], -m[6], -m[14]*u],
|
||||
[ m[1], -m[ 9], m[5], m[13]*u],
|
||||
[ m[3]/u, -m[11]/u, m[7]/u, m[15]],
|
||||
])
|
||||
|
||||
# Batch versions operate in place on a numpy array
|
||||
def convert_locs_batch(locs):
|
||||
# x,y,z -> x,-z,y
|
||||
locs[:, [1,2]] = locs[:, [2,1]]
|
||||
locs[:, 1] *= -1
|
||||
# Unit conversion
|
||||
if u != 1: locs *= u
|
||||
def convert_normals_batch(ns):
|
||||
ns[:, [1,2]] = ns[:, [2,1]]
|
||||
ns[:, 1] *= -1
|
||||
|
||||
# Correction for cameras and lights.
|
||||
# glTF: right = +X, forward = -Z, up = +Y
|
||||
# glTF after Yup2Zup: right = +X, forward = +Y, up = +Z
|
||||
|
@ -77,17 +88,20 @@ class BlenderGlTF():
|
|||
else:
|
||||
def convert_loc(x): return Vector(x)
|
||||
def convert_quat(q): return Quaternion([q[3], q[0], q[1], q[2]])
|
||||
def convert_normal(n): return Vector(n)
|
||||
def convert_scale(s): return Vector(s)
|
||||
def convert_matrix(m):
|
||||
return Matrix([m[0::4], m[1::4], m[2::4], m[3::4]])
|
||||
|
||||
def convert_locs_batch(_locs): return
|
||||
def convert_normals_batch(_ns): return
|
||||
|
||||
# Same convention, no correction needed.
|
||||
gltf.camera_correction = None
|
||||
|
||||
gltf.loc_gltf_to_blender = convert_loc
|
||||
gltf.locs_batch_gltf_to_blender = convert_locs_batch
|
||||
gltf.quaternion_gltf_to_blender = convert_quat
|
||||
gltf.normal_gltf_to_blender = convert_normal
|
||||
gltf.normals_batch_gltf_to_blender = convert_normals_batch
|
||||
gltf.scale_gltf_to_blender = convert_scale
|
||||
gltf.matrix_gltf_to_blender = convert_matrix
|
||||
|
||||
|
|
|
@ -232,11 +232,10 @@ def do_primitives(gltf, mesh_idx, skin_idx, mesh, ob):
|
|||
for sk_locs in sk_vert_locs:
|
||||
sk_locs += vert_locs
|
||||
|
||||
if gltf.yup2zup:
|
||||
locs_yup_to_zup(vert_locs)
|
||||
locs_yup_to_zup(vert_normals)
|
||||
for sk_locs in sk_vert_locs:
|
||||
locs_yup_to_zup(sk_locs)
|
||||
gltf.locs_batch_gltf_to_blender(vert_locs)
|
||||
gltf.normals_batch_gltf_to_blender(vert_normals)
|
||||
for sk_locs in sk_vert_locs:
|
||||
gltf.locs_batch_gltf_to_blender(sk_locs)
|
||||
|
||||
if num_joint_sets:
|
||||
skin_into_bind_pose(
|
||||
|
@ -463,12 +462,6 @@ def colors_linear_to_srgb(color):
|
|||
color[:] = np.where(not_small, large_result, small_result)
|
||||
|
||||
|
||||
def locs_yup_to_zup(vecs):
|
||||
# x,y,z -> x,-z,y
|
||||
vecs[:, [1,2]] = vecs[:, [2,1]]
|
||||
vecs[:, 1] *= -1
|
||||
|
||||
|
||||
def uvs_gltf_to_blender(uvs):
|
||||
# u,v -> u,1-v
|
||||
uvs[:, 1] *= -1
|
||||
|
|
Loading…
Reference in New Issue