当我使用 Python 脚本时 Blender 崩溃
Blender Crashes when i use Python Script
我有一个 python 脚本,当我在文本编辑器中 运行 时它工作正常。但我想为该脚本创建插件。我不是来自编码背景,所以在 python 方面没有太多知识。我看过一个 YouTube 视频 (https://www.youtube.com/watch?v=Y67eCfiqJQU&ab_channel=chocofur) 来创建插件,但它不起作用 Blender 在安装插件时冻结并执行它,因为我已经提到代码本身工作正常。
这是最终代码:
bl_info = {
"name": "New_Object",
"author": "Your Name Here",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "View3D > Add > Mesh > New Object",
"description": "Adds a new Mesh Object",
"warning": "",
"doc_url": "",
"category": "Add Mesh",
}
import bpy
import os
def main(context):
target_dir = "C:/Users/Arpit/Desktop/UV/"
selObj = []
for obj in bpy.context.selected_objects:
selObj.append(obj.name)
bpy.ops.object.select_all(action='TOGGLE')
i=0
while i < len(selObj):
obj = bpy.context.window.scene.objects[0]
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.select_all(action='TOGGLE')
full_file_name=target_dir + bpy.data.objects[selObj[i]].name
dirname = os.path.dirname(full_file_name)
if not os.path.exists(dirname):
os.makedirs(dirname)
bpy.ops.uv.export_layout(filepath=full_file_name, mode='PNG', size=(4096, 4096), opacity=0.6)
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='TOGGLE')
i+=1
class SimpleOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"
def execute(self, context):
main(context)
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(SimpleOperator.bl_idname, text=SimpleOperator.bl_label)
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
scene = context.scene
# Big render button
layout.label(text="Big Button:")
row = layout.row()
row.scale_y = 2.0
row.operator("object.simple_operator")
def register():
bpy.utils.register_class(LayoutDemoPanel)
bpy.utils.register_class(SimpleOperator)
bpy.types.VIEW3D_MT_object.append(menu_func)
def unregister():
bpy.utils.unregister_class(LayoutDemoPanel)
bpy.utils.unregister_class(SimpleOperator)
bpy.types.VIEW3D_MT_object.remove(menu_func)
if __name__ == "__main__":
register()
请指导我。主要代码在
定义主要(上下文):
其余为模板
我不能 运行 但我看到一些缩进错误 - 这可能会产生问题并且代码可能 运行 陷入无限循环并冻结程序。
您将 if
与 while
放在同一列中,因此它完成 while-loop
并在退出此循环后执行 rest。但是这个循环检查 i
退出,现在你在这个循环之外有 i += 1
所以它永远不会改变这个 i
.
如果您更改缩进,则 if
将在循环内并且 i += 1
也会在循环内,这应该可以解决冻结问题。
def main(context):
target_dir = "C:/Users/Arpit/Desktop/UV/"
selObj = []
for obj in bpy.context.selected_objects:
selObj.append(obj.name)
bpy.ops.object.select_all(action='TOGGLE')
# --- while-loop ---
i = 0
while i < len(selObj):
obj = bpy.context.window.scene.objects[0]
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.select_all(action='TOGGLE')
full_file_name = target_dir + bpy.data.objects[selObj[i]].name
dirname = os.path.dirname(full_file_name)
# inside `while`-loop
if not os.path.exists(dirname):
os.makedirs(dirname)
bpy.ops.uv.export_layout(filepath=full_file_name, mode='PNG', size=(4096, 4096), opacity=0.6)
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='TOGGLE')
i += 1
但坦率地说,我会用 for
-loop 来做,它不需要 i = 0
、i += 1
和 selObj[i]
,只需要 item
# --- for-loop ---
# without `i = 0`
for item in selObj:
obj = bpy.context.window.scene.objects[0]
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.select_all(action='TOGGLE')
# `item` instead of `selObj[i]`
full_file_name = target_dir + bpy.data.objects[ item ].name
dirname = os.path.dirname(full_file_name)
# inside `while`-loop
if not os.path.exists(dirname):
os.makedirs(dirname)
bpy.ops.uv.export_layout(filepath=full_file_name, mode='PNG', size=(4096, 4096), opacity=0.6)
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='TOGGLE')
# without `i += 1`
我有一个 python 脚本,当我在文本编辑器中 运行 时它工作正常。但我想为该脚本创建插件。我不是来自编码背景,所以在 python 方面没有太多知识。我看过一个 YouTube 视频 (https://www.youtube.com/watch?v=Y67eCfiqJQU&ab_channel=chocofur) 来创建插件,但它不起作用 Blender 在安装插件时冻结并执行它,因为我已经提到代码本身工作正常。 这是最终代码:
bl_info = {
"name": "New_Object",
"author": "Your Name Here",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "View3D > Add > Mesh > New Object",
"description": "Adds a new Mesh Object",
"warning": "",
"doc_url": "",
"category": "Add Mesh",
}
import bpy
import os
def main(context):
target_dir = "C:/Users/Arpit/Desktop/UV/"
selObj = []
for obj in bpy.context.selected_objects:
selObj.append(obj.name)
bpy.ops.object.select_all(action='TOGGLE')
i=0
while i < len(selObj):
obj = bpy.context.window.scene.objects[0]
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.select_all(action='TOGGLE')
full_file_name=target_dir + bpy.data.objects[selObj[i]].name
dirname = os.path.dirname(full_file_name)
if not os.path.exists(dirname):
os.makedirs(dirname)
bpy.ops.uv.export_layout(filepath=full_file_name, mode='PNG', size=(4096, 4096), opacity=0.6)
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='TOGGLE')
i+=1
class SimpleOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"
def execute(self, context):
main(context)
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(SimpleOperator.bl_idname, text=SimpleOperator.bl_label)
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
scene = context.scene
# Big render button
layout.label(text="Big Button:")
row = layout.row()
row.scale_y = 2.0
row.operator("object.simple_operator")
def register():
bpy.utils.register_class(LayoutDemoPanel)
bpy.utils.register_class(SimpleOperator)
bpy.types.VIEW3D_MT_object.append(menu_func)
def unregister():
bpy.utils.unregister_class(LayoutDemoPanel)
bpy.utils.unregister_class(SimpleOperator)
bpy.types.VIEW3D_MT_object.remove(menu_func)
if __name__ == "__main__":
register()
请指导我。主要代码在 定义主要(上下文): 其余为模板
我不能 运行 但我看到一些缩进错误 - 这可能会产生问题并且代码可能 运行 陷入无限循环并冻结程序。
您将 if
与 while
放在同一列中,因此它完成 while-loop
并在退出此循环后执行 rest。但是这个循环检查 i
退出,现在你在这个循环之外有 i += 1
所以它永远不会改变这个 i
.
如果您更改缩进,则 if
将在循环内并且 i += 1
也会在循环内,这应该可以解决冻结问题。
def main(context):
target_dir = "C:/Users/Arpit/Desktop/UV/"
selObj = []
for obj in bpy.context.selected_objects:
selObj.append(obj.name)
bpy.ops.object.select_all(action='TOGGLE')
# --- while-loop ---
i = 0
while i < len(selObj):
obj = bpy.context.window.scene.objects[0]
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.select_all(action='TOGGLE')
full_file_name = target_dir + bpy.data.objects[selObj[i]].name
dirname = os.path.dirname(full_file_name)
# inside `while`-loop
if not os.path.exists(dirname):
os.makedirs(dirname)
bpy.ops.uv.export_layout(filepath=full_file_name, mode='PNG', size=(4096, 4096), opacity=0.6)
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='TOGGLE')
i += 1
但坦率地说,我会用 for
-loop 来做,它不需要 i = 0
、i += 1
和 selObj[i]
,只需要 item
# --- for-loop ---
# without `i = 0`
for item in selObj:
obj = bpy.context.window.scene.objects[0]
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.select_all(action='TOGGLE')
# `item` instead of `selObj[i]`
full_file_name = target_dir + bpy.data.objects[ item ].name
dirname = os.path.dirname(full_file_name)
# inside `while`-loop
if not os.path.exists(dirname):
os.makedirs(dirname)
bpy.ops.uv.export_layout(filepath=full_file_name, mode='PNG', size=(4096, 4096), opacity=0.6)
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='TOGGLE')
# without `i += 1`