Jump to content

How can I read and format text files and write attributes?


eho

Recommended Posts

Hi all, 

** The thing I want to do is to obtain the coords from this kind of file and write vector positions. The positions corresponds to the place and orientation of each photo was taken. I want to track the path in wich the photos was taked. 

I have a file called cameras.sfm from "structure from motion" created with meshroom app (not inside houdini). I can read the file and split the lines with python, but I don't know how can I give format and write attributes to pass to an object. I'm new coding in python :(

The file has two basic sections: 

- one called "views" 

Quote

 "views": [
        {
            "viewId": "6118912",
            "poseId": "6118912",
            "intrinsicId": "736567190",
            "resectionId": "28",
            .....      

that is an array of specs of a camera... that I don't need,

but at the bottom of the file, is an array is what I need to give some format and pass to the attribute in sop level

The array starts with the tag "poses":
 

Quote

 "poses": [
        {
            "poseId": "6118912",
            "pose": {
                "transform": {
                    "rotation": [
                        "0.98982336752524025",
                        "0.072896579753590415",
                        "-0.12221206880346322",
                        "0.10582479401646359",
                        "-0.9512485660871246",
                        "0.28970205123292164",
                        "-0.095135766525355991",
                        "-0.29968692693779247",
                        "-0.94928232457484174"
                    ],
                    "center": [
                        "0.69696621776308543",
                        "-0.31747383205362439",
                        "2.373249965779185"
                    ]
                },

                "locked": "0"
            }
        },
        {
            "poseId": "22074724",
            "pose": {
                "transform": {
                    "rotation": [
                        "0.99502699332647049",
                        "0.0328141098056354",
                        "-0.094045290947223789",
                        "0.069938396969630473",
                        "-0.9024453840585126",
                        "0.42508934286900463",
                        "-0.07092181033365301",
                        "-0.42955274762146206",
                        "-0.90025248337888519"
                    ],
                    "center": [
                        "0.71394379109147477",
                        "-0.60145305402496574",
                        "2.3423162804821467"
                    ]
                },
                "locked": "0"
            }
        }
    ]

And each "pose" has an Id, maybe could be usefull to write an attribute for the id and others for the vector for the values in transform/rotation and the "center"

I have some code for read the file and print/split lines but... I need help, hope you can guide me  

Quote

node = hou.pwd()
geo = node.geometry()

# Add code to modify contents of geo.
# Use drop down menu to select examples.
s = hou.readFile("..path_to_file/cameras_edited.sfm")

#I have some code lines of failures here.... :(
# and the test for write some atts ... 
geo.addAttrib(hou.attribType.Global, "Strings", value, False, True)
geo.setGlobalAttribValue("Strings", value)

Thanks in advanced... 

Link to comment
Share on other sites

Ok, not finished yet, but a little progress:

The code looks like this now:

Quote

import json

node = hou.pwd()
geo = node.geometry()

with open("path/cameras.sfm" , "r") as archivo:
    data = archivo.read()
    data = json.loads(data)

#Generar los arrays con info // json read with the help of Diego Trujillo
id=[x["poseId"] for x in data["poses"]]
rot=[x["pose"]["transform"]["rotation"] for x in data["poses"]]
center=[x["pose"]["transform"]["center"] for x in data["poses"]]

print(len(id), len(rot), len(center)) # we can print the lenght of id's, rotations and center values

# and now, write points over each "center"

n = 0
for id_v in id:
    point = geo.createPoint()
    c_X = float(center[n][0])
    c_Y = float(center[n][1])
    c_Z = float(center[n][2])
    
    point.setPosition((c_X, c_Y, c_Z))
    n += 1

Now, I need to write and configure the cameras based on the sfm file. 

cameras_shared.sfm

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...