Maniphest T56883

Incorrect bones matrix calculation when exporting to Fbx
Confirmed, NormalKNOWN ISSUE

Assigned To
None
Authored By
lucas veber (lucky3)
Sep 23 2018, 2:52 PM
Tags
  • Import/Export
  • Add-ons (BF-Blender)
Subscribers
Bastien Montagne (mont29)
lucas veber (lucky3)
Matt Ostgard (drflail)

Description

System Information
Win 10

Blender Version
Broken: 2.79b

Short description of error
Wrong bones rotations are exported at specific frame/values.

Exact steps for others to reproduce the error
Open the blend file attached, export it to Fbx.
Import it back in Blender, or Unity, Unreal... The right thigh has wrong rotation on frame 3.

Related Objects

Event Timeline

lucas veber (lucky3) created this task.Sep 23 2018, 2:52 PM
lucas veber (lucky3) added a comment.Sep 23 2018, 2:58 PM

I've found a workaround for this bug.
Decomposing then recomposing the bone matrix fixes it magically in the fbx_utils.py file.

def get_matrix_local(self):
		if self._tag == 'OB':
			return self.bdata.matrix_local.copy()
		elif self._tag == 'DP':
			return self._ref.matrix_world.inverted_safe() * self._dupli_matrix
		else:  # 'BO', current pose
			# PoseBone.matrix is in armature space, bring in back in real local one!
			par = self.bdata.parent	
			c_bone = self._ref.pose.bones[self.bdata.name]
			par_mat_inv = self._ref.pose.bones[par.name].matrix.inverted_safe() if par else Matrix()			
		
			# There's a bug with bones matrix, leading to wrong results when multiplying matrices
			# at some specific rotation values and when scale is different from 1.0
			# Extracting translation, rotation and scale matrices then recompose them seems to fix it		
			t_mat = Matrix.Translation(c_bone.matrix.to_translation())
			r_mat = c_bone.matrix.to_quaternion().to_matrix().to_4x4()			
			s_mat = extract_scale_matrix(c_bone.matrix.to_3x3()).to_4x4()
			bone_mat = t_mat * r_mat * s_mat			
			
			return par_mat_inv * bone_mat

This function is also needed:

def extract_scale_matrix(matrix):
	s_x = Vector((matrix[0][0], matrix[1][0],matrix[2][0])).magnitude
	s_y = Vector((matrix[0][1], matrix[1][1],matrix[2][1])).magnitude
	s_z = Vector((matrix[0][2], matrix[1][2],matrix[2][2])).magnitude		
	s_mat = Matrix(((s_x, 0, 0), (0, s_y, 0), (0, 0, s_z)))
	
	return s_mat

Not sure if I should propose a patch for it, since this is quite clumsy and only works for the Fbx exporter. Seems more like a Blender bug to me. Maybe a developer experienced with the Blender's C++ source matrix functions could fix it in a better way?

Bastien Montagne (mont29) changed the task status from Unknown Status to Unknown Status.Sep 30 2018, 5:09 PM
Bastien Montagne (mont29) claimed this task.
Bastien Montagne (mont29) lowered the priority of this task from 90 to 50.
Bastien Montagne (mont29) added a project: Import/Export.
Bastien Montagne (mont29) added a subscriber: Bastien Montagne (mont29).

Thanks for the report, but even though I can confirm the issue in 2.7x, I cannot reproduce with 2.80… All efforts are now on 2.8, so I do not intend investigating further *why* that weird glitch happens in 2.79.

lucas veber (lucky3) added a comment.Sep 30 2018, 5:29 PM

Glad to hear it's working fine in 2.8, sure I totally understand 2.79 is no more developed.

lucas veber (lucky3) changed the task status from Unknown Status to Unknown Status.Jan 3 2019, 12:39 PM

Sorry, but the bug is still happening in the latest Blender 2.8 build.
Please test and confirm.

Bastien Montagne (mont29) removed Bastien Montagne (mont29) as the assignee of this task.Aug 12 2019, 3:59 PM
Bastien Montagne (mont29) mentioned this in T68575: FBX Known issues & TODO's.Aug 12 2019, 4:10 PM
Dalai Felinto (dfelinto) added a project: Tracker Curfew.Dec 23 2019, 4:35 PM
Richard Antalik (ISS) changed the subtype of this task from "Report" to "Known Issue".Feb 15 2020, 2:36 AM
Richard Antalik (ISS) removed a project: Tracker Curfew.
Bastien Montagne (mont29) edited projects, added Add-ons (BF-Blender); removed Add-ons (Community).Jul 28 2020, 11:03 AM
Bastien Montagne (mont29) moved this task from Backlog to Long Term on the Add-ons (BF-Blender) board.Jul 28 2020, 11:08 AM
Matt Ostgard (drflail) added a subscriber: Matt Ostgard (drflail).Dec 9 2020, 9:26 PM

I encountered this in the wild and reproduced the issue with latest stable Blender 2.91.0 using the same .blend scene attached to the ticket.