Spaces:
Running
on
A10G
Running
on
A10G
File size: 8,161 Bytes
ff0340e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
import bpy, random
import os
import sys
import pdb
import math
from mathutils import Vector
def gc():
for i in range(10): bpy.ops.outliner.orphans_purge()
def clear():
[bpy.data.objects.remove(bpy.data.objects[x]) for x in list(bpy.data.objects.keys())]
gc()
def importVrm(importVrmPath):
old_objs = set(bpy.context.scene.objects)
result = bpy.ops.import_scene.vrm(filepath=importVrmPath)
return [x for x in set(bpy.context.scene.objects)-old_objs if x.type=="ARMATURE"][0]
def importFbx(importFbxPath):
old_objs = set(bpy.context.scene.objects)
result = bpy.ops.import_scene.fbx(filepath=importFbxPath)
return list(set(bpy.context.scene.objects)-old_objs)[0]
def get_keyframes(obj_list):
keyframes = []
for obj in obj_list:
anim = obj.animation_data
if anim is not None and anim.action is not None:
for fcu in anim.action.fcurves:
for keyframe in fcu.keyframe_points:
x, y = keyframe.co
if x not in keyframes:
keyframes.append(int(x))
return keyframes
def retarget(source_armature,target_armature):
bpy.context.view_layer.objects.active = source_armature
bpy.context.scene.source_rig=source_armature.name
bpy.context.scene.target_rig=target_armature.name
bpy.ops.arp.build_bones_list()
bpy.ops.arp.import_config(filepath=os.path.abspath("remap_mixamo.bmap"))
bpy.ops.arp.auto_scale()
keyframes=get_keyframes([source_armature])
bpy.ops.arp.retarget(frame_end=int(max(keyframes)))
def look_at(obj_camera, point):
direction = point - obj_camera.location
rot_quat = direction.to_track_quat('-Z', 'Y')
obj_camera.rotation_euler = rot_quat.to_euler()
def render_4_views(folder, origin = (0, 0, 0)):
bpy.context.scene.render.film_transparent = True
bpy.context.scene.render.resolution_x = 768
bpy.context.scene.render.resolution_y = 768
camera_positions = {
'front': (0, -2.5, 0.5),
'back': (0, 2.5, 0.5),
'left': (-2.5, 0, 0.5),
'right': (2.5, 0, 0.5),
}
camera_data = bpy.data.cameras.new(name='MyCamera')
camera_data.angle = math.radians(40)
camera_object = bpy.data.objects.new('MyCamera', camera_data)
bpy.context.collection.objects.link(camera_object)
bpy.context.scene.camera = camera_object
camera = bpy.data.objects['MyCamera']
for angle, position in camera_positions.items():
camera.location = Vector(position) + Vector(origin)
look_at(camera, Vector(origin))
bpy.context.scene.render.filepath = f'{folder}/{angle}.png'
bpy.ops.render.render(write_still=True)
def changeApose(armature):
bones = armature.pose.bones
if "J_Bip_L_UpperArm" in bones:
L_arm_name = "J_Bip_L_UpperArm"
R_arm_name = "J_Bip_R_UpperArm"
L_leg_name = "J_Bip_L_UpperLeg"
R_leg_name = "J_Bip_R_UpperLeg"
elif "θ
δΈ_L.002" in bones:
L_arm_name = "θ
δΈ_L.002"
R_arm_name = "θ
δΈ_R.002"
L_leg_name = "ε€ͺγγ_L.001"
R_leg_name = "ε€ͺγγ_R.001"
elif "Left arm" in bones:
L_arm_name = "Left arm"
R_arm_name = "Right arm"
L_leg_name = "Left leg"
R_leg_name = "Right leg"
elif "upper_arm.L" in bones:
L_arm_name = "upper_arm.L"
R_arm_name = "upper_arm.R"
L_leg_name = "upper_leg.L"
R_leg_name = "upper_leg.R"
elif "LeftArm" in bones:
L_arm_name = "LeftArm"
R_arm_name = "RightArm"
L_leg_name = "LeftUpLeg"
R_leg_name = "RightUpLeg"
elif "Arm_L" in bones:
L_arm_name = "Arm_L"
R_arm_name = "Arm_R"
L_leg_name = "UpLeg_L"
R_leg_name = "UpLeg_R"
elif "mixamorig:LeftArm" in bones:
L_arm_name = "mixamorig:LeftArm"
R_arm_name = "mixamorig:RightArm"
L_leg_name = "mixamorig:LeftUpLeg"
R_leg_name = "mixamorig:RightUpLeg"
elif "UpperArm_L" in bones:
L_arm_name = "UpperArm_L"
R_arm_name = "UpperArm_R"
L_leg_name = "UpperLeg_L"
R_leg_name = "UpperLeg_R"
else:
import pdb; pdb.set_trace()
if L_arm_name in bones:
bones[L_arm_name].rotation_mode = "XYZ"
bones[L_arm_name].rotation_euler = (-math.pi / 4, 0.0, 0.0)
bones[L_arm_name].keyframe_insert(data_path="rotation_euler",frame=0)
if R_arm_name in bones:
bones[R_arm_name].rotation_mode = "XYZ"
bones[R_arm_name].rotation_euler = (-math.pi / 4, 0.0, 0.0)
bones[R_arm_name].keyframe_insert(data_path="rotation_euler",frame=0)
if L_leg_name in bones:
bones[L_leg_name].rotation_mode = "XYZ"
bones[L_leg_name].rotation_euler = (-math.pi / 30, 0.0, 0.0)
bones[L_leg_name].keyframe_insert(data_path="rotation_euler",frame=0)
if R_leg_name in bones:
bones[R_leg_name].rotation_mode = "XYZ"
bones[R_leg_name].rotation_euler = (-math.pi / 30, 0.0, 0.0)
bones[R_leg_name].keyframe_insert(data_path="rotation_euler",frame=0)
def move_origin_to_center(obj):
local_bbox_center = 0.125 * sum((Vector(b) for b in obj.bound_box), Vector())
scale_factor = max(obj.dimensions)
return local_bbox_center
#print(local_bbox_center)
#local_bbox_center = 0.125 * sum((Vector(b) for b in obj.bound_box), Vector())
#global_bbox_center = obj.matrix_world @ local_bbox_center
# for cur_obj in bpy.context.scene.objects:
# if cur_obj.type != "MESH":
# continue
# print(cur_obj.name, cur_obj.type)
# import pdb; pdb.set_trace()
# global_bbox_center = local_bbox_center @ cur_obj.matrix_world
# cur_obj.location -= global_bbox_center
#cur_obj.scale /= scale_factor
#obj.scale /= max(obj.dimensions)
# # bpy.ops.object.select_all(action='DESELECT')
# # cur_obj.select_set(True)
# # bpy.context.view_layer.objects.active = cur_obj
# # bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS')
def export(armature,exportFileNamePattern,apose=False,origin=None):
bpy.ops.object.select_all(action='DESELECT')
[x.select_set(True) for x in armature.children if(x.type=="MESH")]
if apose:
changeApose(armature)
os.makedirs(folder + "/apose",exist_ok=True)
bpy.ops.wm.obj_export(filepath=folder + "/apose.obj",export_animation=True,start_frame=0,end_frame=0,
export_selected_objects=True,export_materials=False,export_colors=False,export_uv=False,export_normals=False)
render_4_views(folder + "/apose", origin)
else:
keyframes = get_keyframes([armature])
#rand_frame = int(random.choice(keyframes))
os.makedirs(folder + "/pose",exist_ok=True)
bpy.ops.wm.obj_export(filepath=folder + "/pose.obj",export_animation=True,start_frame=0,end_frame=0,
export_selected_objects=True,export_materials=False,export_colors=False,export_uv=False,export_normals=False)
render_4_views(folder + "/pose", origin)
def exportAnimatedMesh(importVrmPath,importFbxPath,folder,apose):
clear()
human=importVrm(importVrmPath)
# resize human
if apose:
origin = move_origin_to_center(human)
export(human, folder, True, origin)
else:
anim = importFbx(importFbxPath)
retarget(anim, human)
origin = move_origin_to_center(human)
export(human, folder, False, origin)
#bpy.data.objects.remove(anim)
#gc()
#bpy.data.objects.remove(human)
#gc()
if(__name__=="__main__"):
argv = sys.argv
if("--" in argv):
argv = argv[argv.index("--") + 1:]
importVrmPath, importFbxPath, folder, apose=argv
else:
raise Exception("no args")
print("importVrmPath:", importVrmPath)
exportAnimatedMesh(importVrmPath, importFbxPath, folder, int(apose)) |