首页 > 解决方案 > Blender 2.8 错误消息:“NoneType”对象在更改“抽取”值时没有属性“修饰符”

问题描述

在 Blender 中更改Decimate值时,出现以下错误:

import bpy
class ReducePolygons(bpy.types.Operator):
   
    bl_idname = "reduce.polygons"
    bl_label = "Reduce Polygons of your Mesh"


    def execute(self, context):
        bpy.ops.object.modifier_add(type='DECIMATE')
        bpy.context.object.modifiers["Decimate"].decimate_type = 'UNSUBDIV'
        bpy.context.object.modifiers["Decimate"].iterations = 1
        bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Decimate")
        return {'FINISHED'}

class LayoutDemoPanel(bpy.types.Panel):
    """Creates a Panel in the scene context of the properties editor"""
    bl_label = "Layout Demo"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        layout = self.layout

        layout.label(text="Mesh Optimizations:")
        row = layout.row()
        row.scale_y = 2.0
        row.operator("reduce.polygons")


def register():
    bpy.utils.register_class(ReducePolygons)
    bpy.utils.register_class(LayoutDemoPanel)
    


def unregister():
    bpy.utils.unregister_class(ReducePolygons)
    bpy.utils.unregister_class(LayoutDemoPanel)
    


if __name__ == "__main__":
    register()

错误信息:

AttributeError: 'NoneType' object has no attribute 'modifiers'
location: <unknown location>:-1

标签: python

解决方案


尝试使用类似方法应用不同的修饰符时出现此错误。我不明白为什么修饰符不只是使用活动对象,但是如果您使用引用对象的变量来定义修饰符选项,它就会起作用。这是一个应该工作的 ClassPolygons:

类 ReducePolygons(bpy.types.Operator):

bl_idname = "reduce.polygons"
bl_label = "Reduce Polygons of your Mesh"

def execute(self, context):
    bpy.ops.object.modifier_add(type='DECIMATE')
    obj=bpy.context.active_object
    obj.modifiers["Decimate"].decimate_type = 'UNSUBDIV'
    obj.modifiers["Decimate"].iterations = 1
    bpy.ops.object.modifier_apply (modifier="Decimate")
    return {'FINISHED'}

我不知道 apply_as='DATA' 部分是否必要。根据文档,Blender 2.9 在 bpy.ops.object.modifier_apply 中只有一个类型选项,但是当我尝试使用它时,我得到一个“关键字“类型”无法识别的“错误”。


推荐阅读