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. 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. But this loop check i
to exit and now you have i += 1
outside this loop so it never change this 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.
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 ---
# 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`
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.