1
$\begingroup$

i'd like to recreate the active camera as a geometry (it's for optimizing particles in the viewport with a dynamic paint modifier)

i'm almost there, the last part is just finding the correct hypotenuse value. half of the camera and half of my created geometry are right triangle. so the FOV value of the active camera is half of the value of one of the angle (non right)

i created my geometry to have an hypotenuse value of 1 and a opposite edge value of one too. But im stuck at the angle calculation. it's not so fresh anymore.

here is the code

import bpy, bmesh, math

CAM = bpy.context.scene.camera
bpy.context.view_layer.objects.active = CAM
FOV = bpy.context.object.data.angle
#bpy.context.object.scale.xyz = 2.00
bpy.context.scene.cursor.location = CAM.location
bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
bpy.ops.mesh.primitive_cube_add(size=2)
BOOL = bpy.context.object
BOOL.rotation_euler=CAM.rotation_euler
BOOL.name = "SCATTER:[Active Cam Boolean]"
BOOL.display_type = 'WIRE'
BOOL.hide_render = True

bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='DESELECT')
mesh = bmesh.from_edit_mesh(BOOL.data)
for f in mesh.faces:
    if f.index ==5:
        f.select = True

bpy.ops.mesh.select_mode(type='EDGE') #update
bpy.ops.mesh.select_mode(type='FACE') #update
bpy.ops.mesh.merge(type='CURSOR') #now we have camera shape
bpy.ops.object.editmode_toggle()

dimension_x=bpy.context.scene.render.resolution_x /1000
dimension_y=bpy.context.scene.render.resolution_y /1000
BOOL.dimensions.xy = (dimension_x,dimension_y) #to get correct ratio

add_to_200 = 1 - bpy.context.object.scale[0] #to have 100cm value
BOOL.scale.x += add_to_200
BOOL.scale.y += add_to_200

bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
bpy.context.object.scale[2] = 1/math.sin(FOV/2) #This is not working ? i tried SOH CAH TOA, not working idk why. 













$\endgroup$
  • $\begingroup$ NEVER MIND, i needed tan(), i got a bit messed up when choosing the hypothenuses. my bad my bad so the correct answer is replacing sin by tan in the last line. $\endgroup$ – DB3D Sep 25 '19 at 0:41
1
$\begingroup$

this script work pefectly

import bpy, bmesh, math
deleted = [ob for ob in bpy.context.scene.objects if ob.name =='SCATTER:[Active Cam Boolean]'];bpy.ops.object.delete({"selected_objects": deleted})

if bpy.context.scene.camera == None:
    bpy.ops.object.camera_add(align='VIEW',location=(0,0,0),rotation=(1.5708,0,0))
    bpy.context.scene.camera = bpy.context.view_layer.objects.active

CAM = bpy.context.scene.camera
bpy.context.view_layer.objects.active = CAM
FOV = bpy.context.object.data.angle
bpy.context.scene.cursor.location = CAM.location #

bpy.ops.mesh.primitive_cube_add(size=2)
BOOL = bpy.context.object
BOOL.rotation_euler=CAM.rotation_euler
BOOL.name = BOOL.data.name = "SCATTER:[Active Cam Boolean]"
BOOL.display_type = 'WIRE'
BOOL.hide_render = True
cy=BOOL.cycles_visibility ;cy.camera=cy.transmission=cy.diffuse=cy.scatter=cy.glossy=cy.shadow= False

bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='DESELECT')
mesh = bmesh.from_edit_mesh(BOOL.data)
for f in mesh.faces:
    if f.index ==5:
        f.select = True
bpy.ops.mesh.select_mode(type='EDGE') ; bpy.ops.mesh.select_mode(type='FACE')
bpy.ops.mesh.merge(type='CURSOR')
bpy.ops.object.editmode_toggle()

bpy.ops.object.transform_apply(location=0,rotation=0,scale=1)#=FFT
bpy.context.object.scale[2] = 1/math.tan(FOV/2)

CAM.select_set(state=True)
bpy.context.view_layer.objects.active = CAM
bpy.ops.object.parent_set(type='OBJECT', keep_transform=True)
for i in range(0,3): BOOL.lock_rotation[i] = BOOL.lock_location[i] = True

dimension_x = bpy.context.scene.render.resolution_x /1000
dimension_y = bpy.context.scene.render.resolution_y /1000
BOOL.dimensions.xy = (dimension_x,dimension_y) #to get correct ratio

if dimension_x>dimension_y:
    divider = 2/dimension_x
else:
    divider = 2/dimension_y
BOOL.scale.xy = (BOOL.scale.x*divider,BOOL.scale.y*divider)


$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.