0
$\begingroup$

Hi I am currently making simple pose preset system with json for specific Armatures. It just discribe non driven bone matrix as json (export), then import and apply for pose bones when there is same name bone in context armature.

Then is there good format function which convert blender Matrix or Vector as tupul for json? when I get Matrix of bones, bpy return like this

'''Matrix(((1.0, 0.0, 0.0, 0.0), (0.0, 1.0, 0.0, 0.0), (0.0, 0.0, 1.0, 0.0), (0.0, 0.0, 0.0, 1.0)))'''

then,, mat_z[0] rerturn Vector Vector((1.0, 0.0, 0.0, 0.0))

I know, I can divide each component, then compose as tupul, for each 4*4 element, (or make function which convert matrix avlue as tupul,) But I think if I miss easy step which bpy or python already offered? or python json module auto format them without convert tupul? (Though I do not suppose so)

$\endgroup$
3
$\begingroup$

List comprehension

Can be done with list comprehension to work with any M x N matrix

Example below, as a list of rows and a list of columns.

>>> M = Matrix.Translation((1, 2, 4))

>>> M
Matrix(((1.0, 0.0, 0.0, 1.0),
        (0.0, 1.0, 0.0, 2.0),
        (0.0, 0.0, 1.0, 4.0),
        (0.0, 0.0, 0.0, 1.0)))

>>> [list(row) for row in M]
[[1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 2.0], [0.0, 0.0, 1.0, 4.0], [0.0, 0.0, 0.0, 1.0]]

>>> [list(col) for col in M.col]
[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [1.0, 2.0, 4.0, 1.0]]
$\endgroup$
  • $\begingroup$ Thanks batFinger now it work as I expected $\endgroup$ – tokikake Aug 31 '20 at 3:38
0
$\begingroup$

Ah It was simple. I only need tuple() or list() (for list) to convert Matrix, Vector

if I hope to convert 4*4 Matrix mat_z to list type mat_list

mat_list = [list(mat_z[0], list(mat_z[1]), list(mat_z[2]), list(mat_z[3])]

then I could get [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]] so I suppose I can dump each bone matrix value as json.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.