python - 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
解决方案
尝试使用类似方法应用不同的修饰符时出现此错误。我不明白为什么修饰符不只是使用活动对象,但是如果您使用引用对象的变量来定义修饰符选项,它就会起作用。这是一个应该工作的 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 中只有一个类型选项,但是当我尝试使用它时,我得到一个“关键字“类型”无法识别的“错误”。
推荐阅读
- php - 有没有从 Laravel 应用程序进行 Moodle 单点登录的方法?
- python - Python + Pandas + dataframe + Csv:将计算值存储在列的末尾
- python - 使用 plt.imshow() 创建叠加频谱图
- java - Kafka Streams:如何获取 SessionWindow 的第一条和最后一条记录?
- python-3.x - 合并 2 列上的两个数据框
- react-native - 如何在移动浏览器上打开的 Highcharts 工具提示中添加链接?
- r - 在 R 中的特定条件下过滤数据集
- android - @Ignore 和不可变字段的问题
- python - 如何在需要之前停止用户输入?
- lstm - ValueError:维度 0 的切片索引 0 超出范围