You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3D渲染中Camera类实例化报错:init()遇意外关键字参数'fov'

问题原因与解决办法

错误根源

  1. 重复参数传递冲突:你调用add_Camera时传入了fov、aspect_ratio、up_vector,这些参数会被方法的**kwargs收集;但add_Camera内部调用Camera构造函数时,已经显式传递了这三个参数的固定值,导致参数被重复传递给Camera.__init__,触发冲突错误。
  2. Camera类逻辑冗余:Camera.__init__同时定义了fov和field_of_view参数,代码中虽赋值了self.fov = fov,但计算视锥体宽度时却使用field_of_view的默认值90°,导致你传入的fov完全不起作用,属于隐性bug。

解决办法

方法1:修改add_Camera实现,支持参数透传与默认值

让add_Camera优先使用外部传入的参数,未传入时用默认值,避免重复传递:

class Scene():
    # 其他代码不变
    def add_Camera(self, look_from, look_at, **kwargs):
        # 设置默认参数,允许外部传入值覆盖
        camera_defaults = {
            'up_vector': vec3(0.0, 1.0, 0.0),
            'fov': 60.0,
            'aspect_ratio': 16.0 / 9.0
        }
        camera_defaults.update(kwargs)
        self.camera = Camera(look_from, look_at, **camera_defaults)

方法2:修复Camera类的冗余参数问题

删除多余的field_of_view参数,统一用fov计算视锥体,确保参数生效:

class Camera():
    def __init__(self, look_from, look_at, up_vector, fov, aspect_ratio, 
                 screen_width=400, screen_height=300, aperture=0., focal_distance=1.):
        self.fov = fov
        self.aspect_ratio = aspect_ratio
        self.up_vector = up_vector
        self.screen_width = screen_width
        self.screen_height = screen_height
        # 可选:若希望屏幕宽高覆盖传入的aspect_ratio,保留此行;否则删除
        # self.aspect_ratio = float(screen_width) / screen_height
        self.look_from = look_from
        self.look_at = look_at
        # 使用传入的fov计算视锥体宽度
        self.camera_width = np.tan(fov * np.pi/180 / 2.) * 2.
        self.camera_height = self.camera_width / self.aspect_ratio
        self.cameraFwd = (look_at - look_from).normalize()
        self.cameraRight = (self.cameraFwd.cross(vec3(0.,1.,0.))).normalize()
        self.cameraUp = self.cameraRight.cross(self.cameraFwd)
        self.lens_radius = aperture / 2.
        self.focal_distance = focal_distance
        self.x = np.linspace(-self.camera_width/2., self.camera_width/2., self.screen_width)
        self.y = np.linspace(self.camera_height/2., -self.camera_height/2., self.screen_height)
        xx,yy = np.meshgrid(self.x,self.y)
        self.x = xx.flatten()
        self.y = yy.flatten()

方法3:调用时移除重复参数

若想保留add_Camera的固定参数设置,调用时仅传递必要的自定义参数:

My_Scene.add_Camera(
    look_from = vec3(0*np.sin(angle), 0, 0*np.cos(angle) -1.5 ),
    look_at = vec3(0, 0, 0),
    screen_width = 300,
    screen_height = 300
)

内容的提问来源于stack exchange,提问作者FrostDream

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 11:02:10