[英]Blender Crashes when i use Python Script
I have a python script which works fine but it is not working Blender freezes as install the addon and Execute it as I have mentioned the code itself works fine.我有一个运行良好的 python 脚本,但它无法运行 Blender 在安装插件时冻结并执行它,因为我已经提到代码本身运行良好。 Here Is the Final code:这是最终代码:
def main(context):
# You should change this varibale in "here" to match your own directory path
# use '/' or '\\' for hirecacy
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
I can't run it but I see some mistakes with indentations - and this can make problem and code may run in endless loop and freeze program.我无法运行它,但我看到一些缩进错误 - 这可能会产生问题,代码可能会在无限循环中运行并冻结程序。
You put if
in the same column as while
so it finishs while-loop
and rest is executed after exiting this loop.您将if
放在与while
相同的列中,以便它完成while-loop
并在退出此循环后执行 rest。 But this loop check i
to exit and now you have i += 1
outside this loop so it never change this i
.但是这个循环检查i
退出,现在你在这个循环之外有i += 1
所以它永远不会改变这个i
。
If you change indentations then if
will be inside loop and i += 1
will be also inside loop and this should resolve problem with freezing.如果您更改缩进,则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
But frankly I would do it with for
-loop and it wouldn't need i = 0
, i += 1
and selObj[i]
but only item
但坦率地说,我会用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`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.