scriptSOP to simulate camera

There was a question on the Facebook helpgroup how to simulate a cameras view for SOPs.

Using a Script SOP and this callback:

# me - this DAT
# scriptOp - the OP which is cooking

# press 'Setup Parameters' in the OP to call this function to re-create the parameters
def onSetupParameters(scriptOp):
	page = scriptOp.appendCustomPage('Setup')
	p = page.appendPython('Camtrans', label='Camera Transform Matrix')
	p = page.appendPython('Camview', label='Camera Projection Matrix')
	return

# called whenever custom pulse parameter is pushed
def onPulse(par):
	return

def onCook(scriptOp):
	scriptOp.copy(scriptOp.inputs[0])
	tMat = scriptOp.par.Camtrans.eval()
	tMat.invert()
	vMat = scriptOp.par.Camview.eval()
	nMat = vMat * tMat
	for i in scriptOp.points:
		newP = nMat * i.P
		i.P = [newP.x/newP.z, newP.y/newP.z, 0]
	return

you can use a cameras transform and projection matrix to calculate each SOP points position in screen space.
The Script SOP’s parameter Camtrans should be set to op('cam1').worldTransform while the Camview parameter should be set to op('cam1').projection(1,1)

I believe this should be correct…

cameraToSOP.tox (1.8 KB)

Cheers
Markus

5 Likes

Thanks for posting these here @snaut - I often find it hard to find my way back to threads on the FB Group, and these pieces are a little more stable to track here IMO.

3 Likes

https://github.com/DBraun/matrixchop my C++ chop does the same thing, so using the CHOP to SOP with the SOP already plugged into it might be a good solution. The x dimension would need to be multiplied by the aspect ratio.

1 Like

This is quite interesting. Thanks for sharing. But could someone provide an example of when this might be useful?

I did an interactive installation with a Kinect. The user can move in XYZ while looking at a large screen to interact with game items. The Kinect provides 3D XYZ position of the user skeleton joints. It’s useful to use a projection matrix to project those 3D points down to 2D with an orthographic camera and then use them in some collision mechanic. This is even more important when you have a large play area or many Kinects calibrated together.

Also useful for flattening 3D to 2D for SVG graphics, pen plotter drawings.

I had meant to add this one here. A workflow with almost all CHOPs (except getting at the projection matrix still required a script CHOP)
Otherwise this uses the Transform CHOP to multiply transform and projection matrix and another Transform CHOP to multiply the points with the resulting matrix.

Scales much better then the pure script version and nicely shows some usage of the Transform CHOP.
cameraToSOPwithCHOP.tox (3.4 KB)

Cheers
Markus

1 Like