AttributeError:'module'对象没有'inputs'属性
我正在做一个Blender项目,需要安装一个插件。当我尝试在几何节点中添加这个插件时,出现了一个错误。我试着搜索了一些关于输入的相同错误,但没有找到任何相关的信息……这里是错误的截图。你们有什么建议可以解决这个问题吗?
代码:
import bpy
from bpy.props import IntProperty, EnumProperty
bl_info = {
"name": "Pick Node Generator",
"author": "Johnny Matthews",
"location": "Geometry Nodes Editor: Add Node Panel",
"version": (1, 0),
"blender": (3, 4, 1),
"description": "Add a customizable pick node",
"category": "Nodes"
}
def find_socket(list, type, name):
for s in list:
if s.type == type and s.name == name:
return s
def main(self, context):
space = context.space_data
if space.node_tree == None:
return
group = space.node_tree
cases = self.cases
type_enum = self.type
type = 'NodeSocket'+type_enum.capitalize()
if type_enum == "RGBA":
type = 'NodeSocketColor'
if type_enum == "BOOLEAN":
type = 'NodeSocketBool'
if type_enum == "VALUE":
type = 'NodeSocketFloat'
tree = bpy.data.node_groups.new("test", 'GeometryNodeTree')
tree.name = type_enum.capitalize() + ' Pick x'+str(cases)
socket_case = tree.inputs.new('NodeSocketInt', 'Switch')
for i in range(cases):
socket_case = tree.inputs.new(type, 'Case '+str(i+self.start)+":")
tree.inputs.new(type, 'Default ')
socket_output = tree.outputs.new(type, 'Output')
input = tree.nodes.new("NodeGroupInput")
input.location = (0, 0)
output = tree.nodes.new("NodeGroupOutput")
output.location = (700+(cases*200), 0)
compares = []
switches = []
for i in range(cases):
compares.append(tree.nodes.new("FunctionNodeCompare"))
compares[i].data_type = 'INT'
compares[i].hide = True
compares[i].name = 'Equals '+str(i+self.start)
compares[i].operation = 'EQUAL'
compares[i].inputs[3].default_value = i+self.start
compares[i].location = (200+((cases-i)*150), -i*250+(cases*250/2)-60)
tree.links.new(compares[i].inputs[2], input.outputs[0])
switches.append(tree.nodes.new("GeometryNodeSwitch"))
switches[i].location = (400+((cases-i)*150), -i*250+(cases*250/2))
if type_enum == 'VALUE':
switches[i].input_type = 'FLOAT'
else:
switches[i].input_type = type_enum
switch_type = 0
if type_enum in ('OBJECT', 'IMAGE', 'GEOMETRY', 'COLLECTION', 'TEXTURE', 'MATERIAL'):
switch_type = 1
tree.links.new(switches[i].inputs[switch_type], compares[i].outputs[0])
sock = find_socket(switches[i].inputs, type_enum, "True")
if sock != None:
tree.links.new(sock, input.outputs[i+1])
for i in range(cases):
if i == 0:
sock = find_socket(switches[i].outputs, type_enum, "Output")
if sock != None:
tree.links.new(output.inputs[0], sock)
else:
sock_o = find_socket(switches[i].outputs, type_enum, "Output")
sock_i = find_socket(switches[i-1].inputs, type_enum, "False")
tree.links.new(sock_i, sock_o)
sock_i = find_socket(switches[cases-1].inputs, type_enum, "False")
tree.links.new(sock_i, input.outputs[cases+1])
group = group.nodes.new('GeometryNodeGroup')
group.name = "Case Statement"
group.node_tree = tree
group.width = 150
return {'FINISHED'}
class PickNodeOperator(bpy.types.Operator):
"""Pick Node Generator"""
bl_idname = "node.swich_node"
bl_label = "Pick Node Generator"
bl_options = {'REGISTER', 'UNDO'}
cases: bpy.props.IntProperty(min=0, default=6, max=25, name="# of Cases")
start: bpy.props.IntProperty(default=0, name="Starting Value")
type: bpy.props.EnumProperty(name="Type", items=[
('VALUE', 'Float', "", 0),
('INT', 'Int', "", 1),
('BOOLEAN', 'Bool', "", 2),
('VECTOR', 'Vector', "", 3),
('RGBA', 'Color', "", 4),
('STRING', 'String', "", 5),
('GEOMETRY', 'Geometry', "", 6),
('OBJECT', 'Object', "", 7),
('COLLECTION', 'Collection', "", 8),
('TEXTURE', 'Texture', "", 9),
('MATERIAL', 'Material', "", 10),
('IMAGE', 'Image', "", 11)
], default='GEOMETRY')
@classmethod
def poll(cls, context):
space = context.space_data
return space.type == 'NODE_EDITOR'
def execute(self, context):
main(self, context)
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(PickNodeOperator.bl_idname,
text=PickNodeOperator.bl_label)
def register():
bpy.utils.register_class(PickNodeOperator)
bpy.types.NODE_MT_add.append(menu_func)
def unregister():
bpy.utils.unregister_class(PickNodeOperator)
bpy.types.NODE_MT_add.remove(menu_func)
if __name__ == "__main__":
register()
我试着把输入替换成Input,并尝试修复第40行的错误,但我对Blender插件编程不太在行 :P
1 个回答
1
我在Blender 3.4.1上没能重现这个错误,但在Blender 4.0.2上却能重现,这说明这个问题是特定版本的。通过查看Blender的更新日志,我们发现inputs
这个属性在Blender 3.6和4.0之间被移除了,这就是你遇到那个错误的原因。取而代之的是bpy.types.NodeTree.interface
,它有一个new_socket
的方法。因此,要让这个插件在Blender 4.0及以上版本上“更新”,你需要把所有的tree.inputs
和tree.outputs
替换成
tree.interface.new_socket(name = "the name", in_out='INPUT or OUTPUT',socket_type='socket type')
对于插件代码来说,这意味着要把第40到45行(从socket_case = tree.interface.new_socket(name = "Switch"...
到socket_output = tree.interface...
)替换成这段更新后的代码:
tree.name = type_enum.capitalize() + ' Pick x'+str(cases)
socket_case = tree.interface.new_socket(name = "Switch", in_out='INPUT',socket_type='NodeSocketInt')
for i in range(cases):
socket_case = tree.interface.new_socket(name = 'Case '+str(i+self.start)+":", in_out='INPUT',socket_type= type)
tree.interface.new_socket(name = "Default ", in_out='INPUT',socket_type=type)
socket_output = tree.interface.new_socket(name = "Output", in_out='OUTPUT',socket_type=type)
我确认在我新安装的Blender 4.0.2上这个方法是有效的。
看起来BlenderArtists Forum
上也有一个非常相似的问题,你可以去看看。
PS:一般来说,尽量避免在帖子中附上文字的图片,因为这样可能会引发一些麻烦。