r/blenderTutorials • • 1d ago

8 Camera User Perspective Python script

Eight Cameras in User Perspective. 8 Recordable positions with record and go buttons. Press N to bring up sidebar. Loc 8 shows up as an add on.

Having quick access to different user perspective positions is important to me. Recorded locations will save with the file, but the script must be run every time I want to use Loc 8 menu.

Originally I had this idea working in Maya in mel scripting. I did a variation in Zscript when I started working in Zbrush.  For both I used search engines to find code and hacked it to make it do what I want.  When I got to blender 5.1 the search engines post code that sometimes works...  The first few requests did other things. My intent was to hack together collected codes to make it happen.  I got a 4 camera array that worked and expanded it to 8.  I added  #comments articulating editing for experimentation.  I got 16 camera function working which adds a scrolling interface to the menu.

#########################

#lOC 8 cameras User Perspective

import bpy

from mathutils import Matrix

# Define data properties to hold the viewport matrices

class ViewSaverProperties(bpy.types.PropertyGroup):

# Numbered Store matrix elements as a flat float array of size 16 (4x4 matrix)

matrix_1: bpy.props.FloatVectorProperty(size=16, default=[0.0]*16)

matrix_2: bpy.props.FloatVectorProperty(size=16, default=[0.0]*16)

matrix_3: bpy.props.FloatVectorProperty(size=16, default=[0.0]*16)

matrix_4: bpy.props.FloatVectorProperty(size=16, default=[0.0]*16)

matrix_5: bpy.props.FloatVectorProperty(size=16, default=[0.0]*16)

matrix_6: bpy.props.FloatVectorProperty(size=16, default=[0.0]*16)

matrix_7: bpy.props.FloatVectorProperty(size=16, default=[0.0]*16)

matrix_8: bpy.props.FloatVectorProperty(size=16, default=[0.0]*16)

# Numbered Flags to check if a perspective has been recorded yet

has_1: bpy.props.BoolProperty(default=False)

has_2: bpy.props.BoolProperty(default=False)

has_3: bpy.props.BoolProperty(default=False)

has_4: bpy.props.BoolProperty(default=False)

has_5: bpy.props.BoolProperty(default=False)

has_6: bpy.props.BoolProperty(default=False)

has_7: bpy.props.BoolProperty(default=False)

has_8: bpy.props.BoolProperty(default=False)

# Operator to Record or Recall a Perspective

class VIEW3D_OT_save_recall_perspective(bpy.types.Operator):

bl_idname = "view3d.save_recall_perspective"

bl_label = "Save or Recall Perspective"

bl_description = "Record or switch to a stored user perspective"

bl_options = {'REGISTER', 'UNDO'}

slot: bpy.props.IntProperty(default=1)

action: bpy.props.EnumProperty(

items=[('RECORD', "Record", ""), ('GO', "Go", "")],

default='RECORD'

)

def execute(self, context):

props = context.scene.view_saver_props

# Safely acquire the active 3D Viewport region data

rv3d = context.region_data

if not rv3d:

self.report({'ERROR'}, "Must be run or triggered from inside a 3D Viewport")

return {'CANCELLED'}

matrix_attr = f"matrix_{self.slot}"

has_attr = f"has_{self.slot}"

if self.action == 'RECORD':

# Get the flat list representation of the 4x4 viewport view matrix

flat_matrix = [val for row in rv3d.view_matrix for val in row]

setattr(props, matrix_attr, flat_matrix)

setattr(props, has_attr, True)

self.report({'INFO'}, f"Recorded Perspective Slot {self.slot}")

elif self.action == 'GO':

if not getattr(props, has_attr):

self.report({'WARNING'}, f"Slot {self.slot} is empty!")

return {'CANCELLED'}

# Reconstruct the 4x4 math matrix from the saved array

flat_vals = getattr(props, matrix_attr)

mat = Matrix([flat_vals[i:i+4] for i in range(0, 16, 4)])

# Instantly snap the viewport perspective matrix to the saved location

rv3d.view_matrix = mat

self.report({'INFO'}, f"Switched to Perspective Slot {self.slot}")

return {'FINISHED'}

# Custom UI Panel inside the Sidebar (N key)

class VIEW3D_PT_view_saver_panel(bpy.types.Panel):

bl_space_type = 'VIEW_3D'

bl_region_type = 'UI'

bl_category = 'Loc 8' #UI Label

bl_label = "User Perspective Locations"

def draw(self, context):

layout = self.layout

props = context.scene.view_saver_props

# on more than number of locations

for i in range(1, 9):

has_view = getattr(props, f"has_{i}")

row = layout.row(align=True)

row.label(text=f" Location {i}:")

# Record Button

rec = row.operator("view3d.save_recall_perspective", text="Record", icon='REC')

rec.slot = i

rec.action = 'RECORD'

# Go Button (Disabled if nothing is recorded yet)

sub = row.row(align=True)

sub.enabled = has_view

go = sub.operator("view3d.save_recall_perspective", text="Go", icon='FORWARD')

go.slot = i

go.action = 'GO'

layout.separator()

# Registration

classes = (

ViewSaverProperties,

VIEW3D_OT_save_recall_perspective,

VIEW3D_PT_view_saver_panel,

)

def register():

for cls in classes:

bpy.utils.register_class(cls)

bpy.types.Scene.view_saver_props = bpy.props.PointerProperty(type=ViewSaverProperties)

def unregister():

for cls in reversed(classes):

bpy.utils.unregister_class(cls)

del bpy.types.Scene.view_saver_props

if __name__ == "__main__":

register()

1 Upvotes

0 comments sorted by