import os
import sys
import bpy
import math
def main_CUI(arg_filepathlist = ["C:\\WORK\\Models\\FBXImport.fbx"]):
blender_addon_enable()
for filepath in arg_filepathlist:
if os.path.exists(filepath) == False:
continue
target_dirpath = os.path.dirname(filepath)
target_filename = os.path.splitext(os.path.basename(filepath))[0]
target_fileextension = os.path.splitext(filepath)[1]
print("dir:" + target_dirpath)
print("name:" + target_filename)
print("ext:" + target_fileextension)
delete_all_objects()
import_result = import_file(
arg_filedir = target_dirpath,
arg_filename = target_filename,
arg_fileextension = target_fileextension
)
if import_result == False:
continue
joinmesh_joinmat_transapply(arg_texturedir = target_dirpath)
export_result = export_file(
arg_filedir = target_dirpath,
arg_filename = target_filename,
arg_fileextension = ".glb"
)
blendfilepath = target_dirpath + "\\" + target_filename + "_export.blend"
bpy.ops.wm.save_as_mainfile(filepath = blendfilepath)
print("Export blend:" + blendfilepath)
print("Export Result:" + str(export_result))
return
def main_GUI():
texture_dir = get_blend_dirpath()
if texture_dir == "":
print("save .blend Project, plase")
return
joinmesh_joinmat_transapply(arg_texturedir = texture_dir)
return
def test():
return
def blender_addon_enable():
bpy.ops.wm.addon_enable(module = "io_scene_gltf2")
return
def joinmesh_joinmat_transapply(arg_texturedir = "C:\\WORK\\Texture"):
joinob_name = "JoinObject"
texture_dir = arg_texturedir
join_objects_mesh(arg_objectname = joinob_name)
joint_material_texture(arg_objectname = joinob_name, arg_texturedir = texture_dir)
apply_object_transform(arg_objectname = joinob_name)
return
def get_blend_dirpath():
filepath = bpy.data.filepath
if filepath == "":
return ""
dirpath = os.path.dirname(filepath)
return dirpath
def transmove(arg_vector = (0.0,0.0,0.0)):
for ob in bpy.context.scene.objects:
if ob.type == 'MESH':
move_vector = arg_vector
add_object_transform(
arg_objectname = ob.name,
arg_location = move_vector
)
return
def delete_all_objects():
for item in bpy.context.scene.objects:
bpy.context.scene.objects.unlink(item)
for item in bpy.data.objects:
bpy.data.objects.remove(item)
for item in bpy.data.meshes:
bpy.data.meshes.remove(item)
for item in bpy.data.materials:
bpy.data.materials.remove(item)
return
def import_file(
arg_filedir = "C:\\WORK\\Models",
arg_filename = "ImportModel",
arg_fileextension = ".fbx"):
import_result = False
if arg_fileextension == ".fbx":
fbx_filepath = arg_filedir + "\\" + arg_filename + arg_fileextension
print("Import FBX:" + fbx_filepath)
import_file_fbx(fbx_filepath)
import_result = True
elif arg_fileextension == ".ifc":
ifc_filepath = arg_filedir + "\\" + arg_filename+arg_fileextension
print("Import IFC:" + ifc_filepath)
import_file_ifc(ifc_filepath)
import_result = True
else:
unknown_filepath = arg_filedir + "\\" + arg_filename + arg_fileextension
print("Unknown file format:" + unknown_filepath)
return import_result
def import_file_ifc(arg_filepath = "D:\\BlenderDir\\Script\\Import.ifc"):
bpy.ops.wm.addon_enable(module = "io_import_scene_ifc")
bpy.ops.import_scene.ifc(
filepath=arg_filepath,
use_names=True,
process_relations=False,
blender_booleans=False
)
return
def import_file_fbx(arg_filepath = "D:\\BlenderDir\\Script\\Import.fbx"):
bpy.ops.import_scene.fbx(
filepath = arg_filepath,
use_manual_orientation = False,
global_scale = 1.0,
bake_space_transform = False,
use_custom_normals = True,
use_image_search = True,
use_alpha_decals = False,
decal_offset = 0.0,
use_anim = True,
anim_offset = 1.0,
use_custom_props = True,
use_custom_props_enum_as_string = True,
ignore_leaf_bones = False,
force_connect_children = False,
automatic_bone_orientation = False,
primary_bone_axis = 'Y',
secondary_bone_axis = 'X',
use_prepost_rot = True
)
return
def export_file(
arg_filedir = "C:\\WORK\\Models",
arg_filename = "ExportModel",
arg_fileextension = ".fbx"):
export_result = False
if arg_fileextension == ".fbx":
fbx_filepath = arg_filedir + "\\" + arg_filename + arg_fileextension
print("Export FBX:" + fbx_filepath)
export_file_fbx(fbx_filepath)
export_result = True
elif arg_fileextension == ".glb":
glb_filepath = arg_filedir + "\\" + arg_filename + arg_fileextension
print("Export GLB:" + glb_filepath)
export_file_glb(glb_filepath)
export_result = True
else:
unknown_filepath = arg_filedir + "\\" + arg_filename + arg_fileextension
print("Unknown file format:" + unknown_filepath)
return export_result
def export_file_fbx(arg_filepath = "D:\\BlenderDir\\Script\\Export.fbx"):
bpy.ops.export_scene.fbx(
filepath = arg_filepath,
version = 'BIN7400',
use_selection = False,
global_scale = 1.0,
apply_unit_scale = True,
apply_scale_options = 'FBX_SCALE_NONE',
bake_space_transform = False,
object_types = {'ARMATURE', 'MESH'},
use_mesh_modifiers = True,
use_mesh_modifiers_render = True,
mesh_smooth_type = 'OFF',
use_mesh_edges = False,
use_tspace = False,
use_custom_props = False,
add_leaf_bones = True,
primary_bone_axis = 'Y',
secondary_bone_axis = 'X',
use_armature_deform_only = False,
armature_nodetype = 'NULL',
bake_anim = True,
bake_anim_use_all_bones = True,
bake_anim_use_nla_strips = True,
bake_anim_use_all_actions = True,
bake_anim_force_startend_keying = True,
bake_anim_step = 1.0,
bake_anim_simplify_factor = 1.0,
path_mode = 'AUTO',
embed_textures = False,
batch_mode = 'OFF',
use_batch_own_dir = True,
use_metadata = True
)
return
def export_file_glb(arg_filepath = "D:\\BlenderDir\\Script\\Export.glb"):
bpy.ops.export_scene.glb(
filepath = arg_filepath,
export_copyright = '',
export_embed_buffers = False,
export_embed_images = False,
export_strip = False,
export_indices = 'UNSIGNED_INT',
export_force_indices = False,
export_texcoords = True,
export_normals = True,
export_tangents = True,
export_materials = True,
export_colors = True,
export_cameras = False,
export_camera_infinite = False,
export_selected = False,
export_layers = True,
export_extras = False,
export_yup = True,
export_apply = False,
export_animations = True,
export_frame_range = True,
export_frame_step = 1,
export_move_keyframes = True,
export_force_sampling = False,
export_current_frame = True,
export_skins = True,
export_bake_skins = False,
export_morph = True,
export_morph_normal = True,
export_morph_tangent = True,
export_lights = False,
export_displacement = False,
will_save_settings = False
)
return
def add_object_transform(
arg_objectname = "Default",
arg_location = (0,0,0),
arg_rotation = (0,0,0),
arg_scale = (1,1,1)):
selectob = bpy.context.scene.objects[arg_objectname]
selectob.location.x += arg_location[0]
selectob.location.y += arg_location[1]
selectob.location.z += arg_location[2]
selectob.rotation_euler.x += 2 * math.pi / 360 * arg_rotation[0]
selectob.rotation_euler.y += 2 * math.pi / 360 * arg_rotation[1]
selectob.rotation_euler.z += 2 * math.pi / 360 * arg_rotation[2]
selectob.scale.x *= arg_scale[0]
selectob.scale.y *= arg_scale[1]
selectob.scale.z *= arg_scale[2]
return
def join_objects_mesh(arg_objectname = ""):
for ob in bpy.context.scene.objects:
if ob.type == 'MESH':
ob.select = True
bpy.context.scene.objects.active = ob
else:
ob.select = False
bpy.ops.object.join()
if len(arg_objectname):
bpy.context.scene.objects.active.name = arg_objectname
return
def apply_object_transform(
arg_objectname = "Default",
arg_location = True,
arg_rotation = True,
arg_scale = True):
for ob in bpy.context.scene.objects:
ob.select = False
selectob = bpy.context.scene.objects[arg_objectname]
selectob.select = True
bpy.ops.object.transform_apply(
location = arg_location,
rotation = arg_rotation,
scale = arg_scale)
selectob.select = False
return
def clear_object_transform(
arg_objectname = "Default",
arg_location = True,
arg_rotation = True,
arg_scale = True):
for ob in bpy.context.scene.objects:
ob.select = False
selectob = bpy.context.scene.objects[arg_objectname]
selectob.select = True
if arg_location:
bpy.ops.object.location_clear(clear_delta = False)
if arg_rotation:
bpy.ops.object.rotation_clear(clear_delta = False)
if arg_scale:
bpy.ops.object.scale_clear(clear_delta = False)
selectob.select = False
return
def joint_material_texture(
arg_objectname = "Default",
arg_texturedir = "C:\\WORK\\Texture"):
joinname = arg_objectname
duplicateame = "OptimumModel"
duplicate_target_object(arg_objectname = joinname, arg_dupname = duplicateame)
remove_materialslot_object(arg_objectname = duplicateame)
delete_UVmap_mesh(arg_objectname = duplicateame)
uv_smart_project(arg_objectname = duplicateame)
imagedirpath = arg_texturedir
imagename = "BakeTexture"
bake_texture_select2active(
arg_sourcename = joinname,
arg_destinationname = duplicateame,
arg_savefilepath = imagedirpath,
arg_savefilename = imagename)
new_material_textureset(
arg_objectname = duplicateame,
arg_applyfilepath = imagedirpath,
arg_applyfilename = imagename)
unlink_object(arg_objectname = joinname)
rename_target_object(arg_objectname = duplicateame, arg_rename = joinname)
return
def rename_target_object(
arg_objectname = "Default",
arg_rename = ""):
renameob = bpy.data.objects[arg_objectname]
renameob.name = arg_rename
return
def duplicate_target_object(
arg_objectname = "Default",
arg_dupname = ""):
for ob in bpy.context.scene.objects:
ob.select = False
selectob = bpy.context.scene.objects[arg_objectname]
selectob.select = True
bpy.ops.object.duplicate_move(
OBJECT_OT_duplicate = None,
TRANSFORM_OT_translate = None)
selectob.select = False
if len(arg_dupname) > 0:
duplicated_objectname = arg_objectname + ".001"
duplicatedob = bpy.data.objects[duplicated_objectname]
duplicatedob.name = arg_dupname
return
def remove_materialslot_object(arg_objectname = "Default"):
selectob = bpy.context.scene.objects[arg_objectname]
for loop_index in range(len(selectob.material_slots)):
selectob.active_material_index = 0
bpy.ops.object.material_slot_remove()
return
def delete_UVmap_mesh(arg_objectname = "Default"):
selectob = bpy.context.scene.objects[arg_objectname]
if selectob.type == 'MESH':
dataname = selectob.data.name
targetmesh = bpy.data.meshes[dataname]
for loop_index in range(len(targetmesh.uv_textures)):
targetmesh.uv_textures.active_index = 0
bpy.ops.mesh.uv_texture_remove()
return
def uv_smart_project(arg_objectname = "Default"):
selectob = bpy.context.scene.objects[arg_objectname]
bpy.context.scene.objects.active = selectob
bpy.ops.object.mode_set(mode = 'EDIT', toggle = False)
bpy.ops.mesh.select_all(action = 'SELECT')
bpy.ops.uv.smart_project(
angle_limit = 66,
island_margin = 0,
user_area_weight = 0,
use_aspect = True,
stretch_to_bounds = True)
bpy.ops.object.mode_set(mode = 'OBJECT', toggle = False)
return
def bake_texture_select2active(
arg_sourcename = "Default",
arg_destinationname = "Default",
arg_savefilepath = "C:\\UnityChan\\Models\\UnityChanShader\\Texture",
arg_savefilename = "NewTexture"):
for ob in bpy.context.scene.objects:
ob.select = False
destinationob = bpy.context.scene.objects[arg_destinationname]
bpy.context.scene.objects.active = destinationob
bpy.ops.object.mode_set(mode = 'EDIT', toggle = False)
bpy.ops.mesh.select_all(action = 'SELECT')
bakeimage = bpy.data.images.new(name = arg_savefilename, width = 1024, height = 1024, alpha = True)
savepath = arg_savefilepath + "\\" + arg_savefilename + ".png"
bakeimage.filepath_raw = savepath
bakeimage.file_format = 'PNG'
bakeimage.save()
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
area.type='IMAGE_EDITOR'
for space in area.spaces:
if space.type == 'IMAGE_EDITOR':
space.image = bakeimage
sourceob = bpy.context.scene.objects[arg_sourcename]
sourceob.select = True
destinationob.select = True
bpy.data.scenes['Scene'].render.bake_type = 'TEXTURE'
bpy.context.scene.render.use_bake_selected_to_active = True
bpy.ops.object.bake_image()
bakeimage.save()
for area in bpy.context.screen.areas:
if area.type == 'IMAGE_EDITOR':
area.type = 'VIEW_3D'
bpy.ops.object.mode_set(mode = 'OBJECT', toggle = False)
return
def new_material_textureset(
arg_objectname = "Default",
arg_applyfilepath = "C:\\UnityChan\\Models\\UnityChanShader\\Texture",
arg_applyfilename = "NewTexture"):
selectob = bpy.context.scene.objects[arg_objectname]
bpy.context.scene.objects.active = selectob
new_material = bpy.data.materials.new(arg_applyfilename)
bpy.ops.object.material_slot_add()
bpy.context.object.active_material = new_material
new_texture = bpy.data.textures.new(arg_applyfilename, type = 'IMAGE')
new_texture_slot = new_material.texture_slots.add()
new_texture_slot.texture = new_texture
allpy_filepath = arg_applyfilepath + "\\" + arg_applyfilename + ".png"
apply_image = bpy.data.images.load(filepath = allpy_filepath)
new_texture.image = apply_image
return
def unlink_object(arg_objectname = "Default"):
selectob=bpy.context.scene.objects[arg_objectname]
bpy.context.scene.objects.unlink(selectob)
return
if bpy.app.background:
if __name__ == "__main__":
args = sys.argv
if 2 <= len(args):
print('--------------------------- START ---------------------------')
args.pop(0)
print(args)
main_CUI(args)
print('--------------------------- END ---------------------------')
else:
main_GUI()