Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:empriselab/RCareWorld into main
Browse files Browse the repository at this point in the history
  • Loading branch information
YoruCathy committed Apr 7, 2024
2 parents 36fc717 + 62a1116 commit 4d76329
Show file tree
Hide file tree
Showing 72 changed files with 969 additions and 870 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

# Visual Studio cache directory
.vs/
.vscode/

# Gradle cache directory
.gradle/
Expand Down Expand Up @@ -230,4 +231,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
#.idea/
16 changes: 10 additions & 6 deletions planning/urdf_gen/python_script/combine2fbx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import sys
import os


def combine2fbx(from_path, to_path, hier_file):
# load all the ply object
# and combine to a fbx
# hier file: this object -> parent

for item in os.listdir(from_path):
bpy.ops.import_scene.obj(filepath=from_path+"/"+item)
bpy.ops.import_scene.obj(filepath=from_path + "/" + item)

objs = bpy.data.objects
objs.remove(objs["Cube"], do_unlink=True)
Expand All @@ -20,14 +21,17 @@ def combine2fbx(from_path, to_path, hier_file):
this_object, parent, _ = line.strip().split(" ")
if parent != "None":
try:
objs[this_object].parent = objs[parent]
objs[this_object].matrix_parent_inverse = objs[parent].matrix_world.inverted()
objs[this_object].parent = objs[parent]
objs[this_object].matrix_parent_inverse = objs[
parent
].matrix_world.inverted()
except KeyError:
print("Warning: Virtual link exists!")

bpy.ops.export_scene.fbx(filepath=to_path)


argv = sys.argv
argv = argv[argv.index("--") + 1:]
argv = argv[argv.index("--") + 1 :]
print(argv)
combine2fbx(argv[0], argv[1], argv[2])
combine2fbx(argv[0], argv[1], argv[2])
67 changes: 49 additions & 18 deletions planning/urdf_gen/python_script/extract_parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
import argparse
import os


def parse_obj_with_group(obj_name):
vlines = []
vnlines = []
glines = {}
with open(obj_name) as f:
for line in f:
if line.startswith("v "): # it is vertex line
if line.startswith("v "): # it is vertex line
vlines.append(line)
if line.startswith("vn "): # it is normal line
if line.startswith("vn "): # it is normal line
vnlines.append(line)
if line.startswith("g"):
g_name = line.strip().split(" ")[-1]
Expand All @@ -30,15 +31,15 @@ def parse_obj_with_group(obj_name):
parser.add_argument("-o", "--output_dir", type=str, default="smpl_parts")
parser.add_argument("--hier", type=str)
args = parser.parse_args()
#obj_name = "SMPL_f_unityDoubleBlends_lbs_10_scale5_207_v1.0.0.obj"
#obj_name = "SMPL_f_unityDoubleBlends_lbs_10_scale5_207_v1.0.0_groups.obj"
# obj_name = "SMPL_f_unityDoubleBlends_lbs_10_scale5_207_v1.0.0.obj"
# obj_name = "SMPL_f_unityDoubleBlends_lbs_10_scale5_207_v1.0.0_groups.obj"
obj_name = args.object_name

output_dir = args.output_dir

if not os.path.exists(output_dir):
os.makedirs(output_dir)

hier_path = args.hier
hier_dict = {}
co_vertex_dict = {}
Expand All @@ -47,12 +48,12 @@ def parse_obj_with_group(obj_name):
child, parent, _ = line.strip().split()
if parent != "None" and parent != "f_avg_root":
hier_dict[child] = parent

vlines, vnlines, glines = parse_obj_with_group(obj_name)
all_counter = 0
face_id_group_mapping = {}
part_v_seq_new_index_mapping = {}
for part_name in glines.keys(): # for each part
for part_name in glines.keys(): # for each part
v_seq = []
vn_seq = []
for line in glines[part_name]:
Expand All @@ -62,7 +63,7 @@ def parse_obj_with_group(obj_name):
v3i, vt3i, vn3i = face3.split("/")
v_seq.append([int(v1i), int(v2i), int(v3i)])
vn_seq.append([int(vn1i), int(vn2i), int(vn3i)])

v_seq_unique = np.unique(v_seq)
vn_seq_unique = np.unique(vn_seq)

Expand All @@ -71,16 +72,16 @@ def parse_obj_with_group(obj_name):
v_seq_new_index_mapping = {}
vn_seq_new_index_mapping = {}
for i in range(len(v_seq_unique)):
v_seq_new_index_mapping[v_seq_unique[i]] = i+1
v_seq_new_index_mapping[v_seq_unique[i]] = i + 1
for i in range(len(vn_seq_unique)):
vn_seq_new_index_mapping[vn_seq_unique[i]] = i + 1
part_v_seq_new_index_mapping[part_name] = v_seq_new_index_mapping
with open(output_dir+"/"+part_name+".obj", "w") as fout:

with open(output_dir + "/" + part_name + ".obj", "w") as fout:
for vi in v_seq_unique:
fout.write(vlines[vi-1])
fout.write(vlines[vi - 1])
for vni in vn_seq_unique:
fout.write(vnlines[vni-1])
fout.write(vnlines[vni - 1])
for line in glines[part_name]:
head, face1, face2, face3 = line.strip().split()
v1i, vt1i, vn1i = face1.split("/")
Expand All @@ -92,12 +93,42 @@ def parse_obj_with_group(obj_name):
new_vn1i = vn_seq_new_index_mapping[int(vn1i)]
new_vn2i = vn_seq_new_index_mapping[int(vn2i)]
new_vn3i = vn_seq_new_index_mapping[int(vn3i)]
fout.write(head+" "+str(new_v1i)+"//"+str(new_vn1i)+" "+str(new_v2i)+"//"+str(new_vn2i)+" "+str(new_v3i)+"//"+str(new_vn3i)+"\n")

fout.write(
head
+ " "
+ str(new_v1i)
+ "//"
+ str(new_vn1i)
+ " "
+ str(new_v2i)
+ "//"
+ str(new_vn2i)
+ " "
+ str(new_v3i)
+ "//"
+ str(new_vn3i)
+ "\n"
)

with open(hier_path.replace("hier", "covertex"), "w") as f:
for key in hier_dict.keys():
intersect_ids = face_id_group_mapping[key].intersection(face_id_group_mapping[hier_dict[key]])
intersect_ids = face_id_group_mapping[key].intersection(
face_id_group_mapping[hier_dict[key]]
)
selected_id = int(list(intersect_ids)[0])
current_child = int(part_v_seq_new_index_mapping[key][selected_id])
current_parent = int(part_v_seq_new_index_mapping[hier_dict[key]][selected_id])
f.write(key+" "+hier_dict[key]+" "+str(selected_id)+" "+str(current_child)+" "+str(current_parent)+"\n")
current_parent = int(
part_v_seq_new_index_mapping[hier_dict[key]][selected_id]
)
f.write(
key
+ " "
+ hier_dict[key]
+ " "
+ str(selected_id)
+ " "
+ str(current_child)
+ " "
+ str(current_parent)
+ "\n"
)
25 changes: 15 additions & 10 deletions planning/urdf_gen/python_script/extract_vertex_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import os


def find_max_group(weights):
max_weight = 0
max_index = 0
Expand All @@ -12,6 +13,7 @@ def find_max_group(weights):
max_index = i
return max_index


def extractVertexGroup(from_path, to_path, hier_path):
to_dir = "/".join(to_path.split("/")[:-1])
res_dir = "/".join(hier_path.split("/")[:-1])
Expand All @@ -24,24 +26,27 @@ def extractVertexGroup(from_path, to_path, hier_path):
objs.remove(objs["Cube"], do_unlink=True)
objs.remove(objs["Camera"], do_unlink=True)
objs.remove(objs["Light"], do_unlink=True)

bpy.ops.import_scene.fbx(filepath=from_path)
print(bpy.data.armatures.keys())
real_armature = bpy.data.armatures['SMPLX-female']
real_armature = bpy.data.armatures["SMPLX-female"]
print(real_armature.bones.keys())

with open(hier_path, "w") as f:
for key in real_armature.bones.keys():
if real_armature.bones[key].parent is None:
f.write(key+" None 0\n")
elif real_armature.bones[key].parent.name.find("root")!=-1:
f.write(key+" "+real_armature.bones[key].parent.name+" 0\n")
f.write(key + " None 0\n")
elif real_armature.bones[key].parent.name.find("root") != -1:
f.write(key + " " + real_armature.bones[key].parent.name + " 0\n")
else:
f.write(key+" "+real_armature.bones[key].parent.name+" 1\n")
f.write(key + " " + real_armature.bones[key].parent.name + " 1\n")

bpy.ops.export_scene.obj(
filepath=to_path, keep_vertex_order=True, use_vertex_groups=True
)

bpy.ops.export_scene.obj(filepath=to_path, keep_vertex_order=True,use_vertex_groups=True)

argv = sys.argv
argv = argv[argv.index("--") + 1:]
argv = argv[argv.index("--") + 1 :]

extractVertexGroup(argv[0], argv[1], argv[2])
extractVertexGroup(argv[0], argv[1], argv[2])
Loading

0 comments on commit 4d76329

Please sign in to comment.