Search code examples
c#pythonjsonmatrixrhinoceros

Extract matrix from jSon file


i use fSpy software : Open source still image camera matching to use the result with Rhino 3d i export to jSon file which have matrix of the camera position i need to extract the rows to a list of numbers with Python Thanks for help

enter image description here

from this :

  "cameraTransform": {
    "rows": [
      [
        -0.7469096503244566,
        -0.0048362499036055774,
        -0.6649079522302825,
        -6.756347957309588
      ],
      [
        -0.6649255299159078,
        0.005255128514812499,
        0.7468911723205343,
        7.333944516331069
      ],
      [
        -0.00011797562067967932,
        0.9999744968303753,
        -0.007140852231397498,
        -4.200072574252649
      ],
      [
        0,
        0,
        0,
        1
      ]
    ]
  }

to :

0   -0.7469096503244566
1   -0.0048362499036055774
2   -0.6649079522302825
3   -6.756347957309588
4   -0.6649255299159078
5    0.005255128514812499
6   0.7468911723205343
7   7.333944516331069
8   -0.00011797562067967932
9   0.9999744968303753
10   -0.007140852231397498
11   -4.200072574252649
12   0
13   0
14   0
15   1

Solution

  • You can try this after convert json to python format:

    cameraTransform= {
            "rows": [
              [
                -0.7469096503244566,
                -0.0048362499036055774,
                -0.6649079522302825,
                -6.756347957309588
              ],
              [
                -0.6649255299159078,
                0.005255128514812499,
                0.7468911723205343,
                7.333944516331069
              ],
              [
                -0.00011797562067967932,
                0.9999744968303753,
                -0.007140852231397498,
                -4.200072574252649
              ],
              [
                0,
                0,
                0,
                1
              ]
            ]
          }
    
    from itertools import chain
    for idx, value in enumerate(chain(*cameraTransform['rows'])):
        print(idx, value)
    

    Output:

    0 -0.7469096503244566
    1 -0.0048362499036055774
    2 -0.6649079522302825
    3 -6.756347957309588
    4 -0.6649255299159078
    5 0.005255128514812499
    6 0.7468911723205343
    7 7.333944516331069
    8 -0.00011797562067967932
    9 0.9999744968303753
    10 -0.007140852231397498
    11 -4.200072574252649
    12 0
    13 0
    14 0
    15 1