def image_copy_guess(filepath, objects): # 'filepath' is the path we are writing to. import shutil from bpy_extras import object_utils image = None for obj in objects: image = object_utils.object_image_guess(obj) if image is not None: break if image is not None: imagepath = bpy.path.abspath(image.filepath, library=image.library) if os.path.exists(imagepath): filepath_noext = os.path.splitext(filepath)[0] ext = os.path.splitext(imagepath)[1] imagepath_dst = filepath_noext + ext print(f"copying texture: {imagepath!r} -> {imagepath_dst!r}") try: shutil.copy(imagepath, imagepath_dst) except: import traceback traceback.print_exc()
def save_object(fw, global_matrix, scene, obj, use_mesh_modifiers, use_color, color_type, use_uv, path_mode, copy_set): assert (obj.type == 'MESH') if use_mesh_modifiers: is_editmode = (obj.mode == 'EDIT') if is_editmode: bpy.ops.object.editmode_toggle() me = obj.to_mesh(scene, True, 'PREVIEW') bm = bmesh.new() bm.from_mesh(me) if is_editmode: bpy.ops.object.editmode_toggle() else: me = obj.data if obj.mode == 'EDIT': bm_orig = bmesh.from_edit_mesh(me) bm = bm_orig.copy() else: bm = bmesh.new() bm.from_mesh(me) # triangulate first so tessellation matches the view-port. bmesh.ops.triangulate(bm, faces=bm.faces) bm.transform(global_matrix * obj.matrix_world) # default empty material_colors = [] uv_image = None if use_color: if color_type == 'VERTEX': if bm.loops.layers.color.active is None: # fallback to material color_type = 'MATERIAL' if color_type == 'MATERIAL': if not me.materials: use_color = False else: material_colors = [ "%.2f %.2f %.2f " % (m.diffuse_color[:] if m else (1.0, 1.0, 1.0)) for m in me.materials ] assert (color_type in {'VERTEX', 'MATERIAL'}) if use_uv: if bm.loops.layers.uv.active is None: use_uv = False uv_image = object_utils.object_image_guess(obj, bm=bm) if uv_image is None: use_uv = False save_bmesh(fw, bm, use_color, color_type, material_colors, use_uv, uv_image, path_mode, copy_set) bm.free()
def save_object(fw, global_matrix, scene, obj, use_mesh_modifiers, use_color, color_type, use_uv, path_mode, copy_set): assert obj.type == "MESH" if use_mesh_modifiers: is_editmode = obj.mode == "EDIT" if is_editmode: bpy.ops.object.editmode_toggle() me = obj.to_mesh(scene, True, "PREVIEW", calc_tessface=False) bm = bmesh.new() bm.from_mesh(me) if is_editmode: bpy.ops.object.editmode_toggle() else: me = obj.data if obj.mode == "EDIT": bm_orig = bmesh.from_edit_mesh(me) bm = bm_orig.copy() else: bm = bmesh.new() bm.from_mesh(me) bm.transform(global_matrix * obj.matrix_world) bmesh.ops.triangulate(bm, faces=bm.faces, use_beauty=True) # default empty material_colors = [] uv_image = None if use_color: if color_type == "VERTEX": if bm.loops.layers.color.active is None: # fallback to material color_type = "MATERIAL" if color_type == "MATERIAL": if not me.materials: use_color = False else: material_colors = [ "%.2f %.2f %.2f " % (m.diffuse_color[:] if m else (1.0, 1.0, 1.0)) for m in me.materials ] assert color_type in {"VERTEX", "MATERIAL"} if use_uv: if bm.loops.layers.uv.active is None: use_uv = False uv_image = object_utils.object_image_guess(obj, bm=bm) if uv_image is None: use_uv = False save_bmesh(fw, bm, use_color, color_type, material_colors, use_uv, uv_image, path_mode, copy_set) bm.free()
def image_copy_guess(filepath, objects): # 'filepath' is the path we are writing to. import shutil from bpy_extras import object_utils image = None for obj in objects: image = object_utils.object_image_guess(obj) if image is not None: break if image is not None: imagepath = bpy.path.abspath(image.filepath, library=image.library) if os.path.exists(imagepath): filepath_noext = os.path.splitext(filepath)[0] ext = os.path.splitext(imagepath)[1] imagepath_dst = filepath_noext + ext print("copying texture: %r -> %r" % (imagepath, imagepath_dst)) try: shutil.copy(imagepath, imagepath_dst) except: import traceback traceback.print_exc()
def save_object(fw, global_matrix, scene, obj, use_mesh_modifiers, use_color, color_type, use_uv, path_mode, copy_set): assert(obj.type == 'MESH') if use_mesh_modifiers: is_editmode = (obj.mode == 'EDIT') if is_editmode: bpy.ops.object.editmode_toggle() me = obj.to_mesh(scene, True, 'PREVIEW', calc_tessface=False) bm = bmesh.new() bm.from_mesh(me) if is_editmode: bpy.ops.object.editmode_toggle() else: me = obj.data if obj.mode == 'EDIT': bm_orig = bmesh.from_edit_mesh(me) bm = bm_orig.copy() else: bm = bmesh.new() bm.from_mesh(me) # triangulate first so tessellation matches the view-port. bmesh.ops.triangulate(bm, faces=bm.faces) bm.transform(global_matrix * obj.matrix_world) # default empty material_colors = [] uv_image = None if use_color: if color_type == 'VERTEX': if bm.loops.layers.color.active is None: # fallback to material color_type = 'MATERIAL' if color_type == 'MATERIAL': if not me.materials: use_color = False else: material_colors = [ "%.2f %.2f %.2f " % (m.diffuse_color[:] if m else (1.0, 1.0, 1.0)) for m in me.materials] assert(color_type in {'VERTEX', 'MATERIAL'}) if use_uv: if bm.loops.layers.uv.active is None: use_uv = False uv_image = object_utils.object_image_guess(obj, bm=bm) if uv_image is None: use_uv = False save_bmesh(fw, bm, use_color, color_type, material_colors, use_uv, uv_image, path_mode, copy_set) bm.free()