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"]])
@callaghansan
Copy link

Hello there ! The code is working, I am able to create a camera from a .json file BUT in nuke the camera is not pointing in the right direction...
Am I missing something ? Do I need to specify something in fpsy before exporting the camera (vanishing point axes, reference distance...) ?

@MitchellKehn
Copy link
Author

it's been some time since I've had to use fSpy, so I can't recall off the top of my head what the particulars were.

But you should be able to get this working in one of two ways:

  1. use different axes in fSpy (I'd start with X and Z if your geometry is on the horizontal plane), OR
  2. plug in an Axis node above your camera, and then try rotating in 90 degree increments around each of the axes (use the 3D viewer to help you decide which). if that's still not working, scaling an axis by -1 might also get you there.

@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