How did view_camera_zoom get it?

view_camera_zoom this property is the default in the blender api, does anyone know how it is calculated? In my plugin to monitor whether the camera view is maximized, this property seems to be very suitable, but I don’t know how it is calculated.

Searching the source code doesn’t hurt…

#blenkernel\intern\screen.cc
float BKE_screen_view3d_zoom_to_fac(float camzoom)
{
  return powf((float(M_SQRT2) + camzoom / 50.0f), 2.0f) / 4.0f;
}


#editors\space_view3d\view3d_edit.cc
static int view3d_center_camera_exec(bContext *C, wmOperator * /*op*/)
{
  Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
  Scene *scene = CTX_data_scene(C);
  float xfac, yfac;
  float size[2];

  View3D *v3d;
  ARegion *region;
  RegionView3D *rv3d;

  /* no nullptr check is needed, poll checks */
  ED_view3d_context_user_region(C, &v3d, &region);
  rv3d = static_cast<RegionView3D *>(region->regiondata);

  rv3d->camdx = rv3d->camdy = 0.0f;

  ED_view3d_calc_camera_border_size(scene, depsgraph, region, v3d, rv3d, size);

  /* 4px is just a little room from the edge of the area */
  xfac = float(region->winx) / float(size[0] + 4);
  yfac = float(region->winy) / float(size[1] + 4);

  rv3d->camzoom = BKE_screen_view3d_zoom_from_fac(min_ff(xfac, yfac));
  CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);

  WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);

  return OPERATOR_FINISHED;
}
2 Likes

Nice, thank you very much for your reply. Is this source code on github?

Yes. But the github is just a mirror of the official repository.
Github:

Official:

1 Like

The source code is in C, which is too difficult for me. Is it possible to achieve the same result with Python?

To replicate the same result, you need to go through the C code, see whatever math is involved, and write your own function in python. I think you can access every variable that’s needed for the calculation, from the RegionView3D and from the Camera parameters.
i forgot to add the source for the ED_view_calc_camera_border_size, but you can find it in source\blender\editors\space_view3d\view3d_draw.cc, which also calls functions in source\blender\blenkernel\intern\camera.cc

Why would this help if you’re just monitoring if the camera view is maximized?

Because I have a Gizmo display to be displayed according to whether the camera view is maximized, otherwise there will be errors.

I think the view matrix should be sufficient for displaying the gizmo, no matter if the view is maximized or not.
thought i don’t fully imagine what exactly are you after

1 Like

This has nothing to do with gizmo display, I really have a hard time explaining my purpose, in short I want to use python to calculate a value if the camera view is maximized to determine whether it is equal to view_camera_zoom to determine whether the camera view is maximized

What I don’t think i’m understanding is what you mean by ‘camera view is maximized’…

If an editor is maximized, then the amount of areas in that screen will be 1 (no more, no less). It doesn’t matter if it’s a camera view or top view, or even any other editor.
is_maximized = len(context.window.screen.areas)==1

1 Like

if x==a(region.width) or y==b(region.height):
camera view is maximized=True