Skip to content

Instantly share code, notes, and snippets.

@MitchellKehn
Last active December 15, 2023 13:12
Show Gist options
  • Save MitchellKehn/e7ccdfad932886c7e9e4c072a08006c8 to your computer and use it in GitHub Desktop.
Save MitchellKehn/e7ccdfad932886c7e9e4c072a08006c8 to your computer and use it in GitHub Desktop.
[Import fSpy JSON to Nuke] load a json file with camera data from fSpy into Nuke for single frame camera alignment https://fspy.io/ #fspy #nuke
import json
import nuke
def Matrix4toList(M):
mVals = []
for i in range(len(M)):
mVals.append(M[i])
return mVals
# --- load file ---
fp = nuke.getFilename("Select fSpy data file", "*.json")
with open(fp) as datafile:
data = json.load(datafile)
# --- create camera, set knobs
camera = nuke.createNode("Camera")
# camera transform
M = nuke.math.Matrix4() # camera pose
matrixVals = data["cameraTransform"]["rows"]
for row, values in enumerate(matrixVals):
rowStart = row * 4
M[rowStart + 0] = values[0]
M[rowStart + 1] = values[1]
M[rowStart + 2] = values[2]
M[rowStart + 3] = values[3]
camera["useMatrix"].setValue(True)
camera["matrix"].setValue(Matrix4toList(M))
# projection parameters
aspectRatio = float(data["imageWidth"]) / float(data["imageHeight"])
camera["vaperture"].setExpression("haperture / {aspect}".format(aspect=aspectRatio))
camera["focal"].setExpression("haperture / (2 * tan({hFOV} / 2))".format(hFOV=data["horizontalFieldOfView"]))
principal = data["principalPoint"]
camera["win_translate"].setValue([principal["x"], principal["y"]])
@mstergiou
Copy link

Hi Mitchell, I want to share the tool with students so they can import static cameras for their projects.
Ideally I would like to wrap it in a function so they don't need to run it via the script editor.
Would that be ok for you? With link to your gist, etc of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment