Pscale by the camera
I've described how we can set a unified pscale by the camera in SOPs here which uses this simple algebraic approach: $$ \text{Pixel Size} = \left(\frac{\text{Aperture}}{\text{Focal Length}}\right) \times \left(\frac{\text{Distance}}{\text{Resolution}}\right) $$ However, if we want to set up the same behavior in LOPs, you will notice that getting distance (Pz) by NDC or UV might not be straightforward, as it would require jumping back into the SOP/OBJ context.
As an alternative, we can calculate it by projecting the vector from the camera to the point onto the camera Z. You can also read more about vector projections here
VEXpression
string campath = chs("camera");
float apertH = usd_attrib(0, campath, "horizontalAperture");
float apertV = usd_attrib(0, campath, "verticalAperture");
float focal = usd_attrib(0, campath, "focalLength");
float resx = chf("render_resolution_x");
float pixsize = apertH / focal ;
// get camera position
matrix camM = usd_worldtransform(0, campath);
vector camPos = set(camM.wx, camM.wy, camM.wz);
// vector from cam to point
vector camPtoP = v@points-camPos;
// camera direction vector
vector camDir = -set(camM.zx, camM.zy, camM.zz)/100;
// projection distance and direction
float dotC = max(0, dot(camPtoP, normalize(camDir)));
// projected vector
vector ProjZ = dotC*normalize(camDir);
float Pz = length(ProjZ);
// pscale size of pixel (based on camera RESOLUTION)
f@widths = pixsize * (Pz/resx);
// adjust unified pscale by user input
f@widths *= ch("pscale_multi");
Download:
sop-camera-pscale-1.hipnc