-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhier2x3d.blender.pl
146 lines (131 loc) · 3.7 KB
/
hier2x3d.blender.pl
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
#!/bin/perl
use strict;
use warnings;
# Genrerate HAnim humanoid from hierarchy
# $ARGV[0] -- hierarchy file
# $ARGV[1] -- python output
my %joints = ();
my $segments = "";
sub printPython {
my($prev) = @_; # $prev is $child
$prev->{line} =~ /([ |]*)(.*) : (.*)/;
my $parent_indent = length($1);
my $parent_joint = $2;
my $parent_segment = $3;
if ($joints{$parent_joint}) {
my $cenpj = $joints{$parent_joint};
my @cenpj = split(/[ ,\t]+/, $cenpj);
$cenpj = "$cenpj[0], $cenpj[2], $cenpj[1]";
# print OUTPUT " " x $prev->{indent}. '("'.$parent_joint.'", ('.$cenpj.'), (0, 0, 0)),'."\n";
foreach my $jnt (@{$prev->{children}}) { # go through child joints
&printPython($jnt);
if ($jnt->{joint} =~ m/_tarsal_interphalangeal_1$/) { # we need to take this out
# skip
} else {
$segments .= " " x $jnt->{indent}. '("'.$parent_joint.'", "'.$jnt->{joint}.'"),'."\n";
my $cencj = $joints{$jnt->{joint}};
my @cencj = split(/[ ,\t]+/, $cencj);
$cencj = "$cencj[0], $cencj[2], $cencj[1]";
print OUTPUT " " x $jnt->{indent}. '("'.$jnt->{joint}.'", ('.$cencj.'), ('.$cenpj.')),'."\n";
}
}
}
}
sub readHierarchy {
my $input = $ARGV[0];
print "$input\n";
open (HIER, "<$input");
my $prev = {};
$prev->{children} = ();
$prev->{indent} = -1;
foreach (<HIER>) {
my $line = $_;
chomp $line;
$line =~ /([ |]*)(.*) : (.*)/;
my $indent = length($1);
my $joint = $2;
my $segment = $3;
my $obj = {};
$obj->{line} = $line;
$obj->{indent} = $indent;
$obj->{joint} = $joint;
$obj->{segment} = $segment;
$obj->{children} = ();
# print "$line\n";
if ($obj->{indent} > $prev->{indent}) {
$obj->{parent} = $prev;
# print "$obj->{joint} parent 1 is $prev->{joint}\n";
} elsif ($obj->{indent} == $prev->{indent}) {
$obj->{parent} = $prev->{parent};
# print "$obj->{joint} parent 2 is $prev->{parent}->{joint}\n";
} else {
while ($obj->{indent} < $prev->{indent}) {
$prev = $prev->{parent};
}
$obj->{parent} = $prev->{parent};
# print "$obj->{joint} parent 3 is $prev->{joint}\n";
}
push @{$obj->{parent}->{children}}, $obj;
$prev = $obj;
}
while (0 < $prev->{indent}) {
$prev = $prev->{parent};
}
close(HIER);
return $prev;
}
open(TABLE, "sh sed.sh|");
while(<TABLE>) {
chomp;
my $joint = $_;
my $center = <TABLE>;
chomp $center;
$joints{$joint} = $center;
}
close(TABLE);
my $results = $ARGV[1];
print "$results\n";
open(OUTPUT, ">$results");
print OUTPUT << "HUMANHEADER";
import bpy
skeleton = bpy.data.objects.new("Armature", bpy.data.armatures.new("Armature"))
bpy.context.collection.objects.link(skeleton)
bpy.context.view_layer.objects.active = skeleton
skeleton.select_set(True)
bpy.ops.object.mode_set(mode='EDIT')
# Create joints
joints = [
HUMANHEADER
my $prev = &readHierarchy();
$prev->{line} =~ /([ |]*)(.*) : (.*)/;
my $root_indent = length($1);
my $cenrj = $joints{$prev->{joint}};
if ($cenrj) {
my @cenrj = split(/[ ,\t]+/, $cenrj);
$cenrj = "$cenrj[0], $cenrj[2], $cenrj[1]";
print OUTPUT " " x $root_indent. '("'.$prev->{joint}.'", ('.$cenrj.'), (0, 0, 0)),'."\n";
}
&printPython($prev);
print OUTPUT << "HUMANBODY";
]\n
for joint in joints:
joint_name, joint_start, joint_end = joint
bpy.ops.armature.bone_primitive_add(name=joint_name)
new_segment = skeleton.data.edit_bones[joint_name]
new_segment.head = joint_start
new_segment.tail = joint_end
# Connect joints
segments = [
HUMANBODY
print OUTPUT $segments;
print OUTPUT << "HUMANFOOTER";
]
for segment in segments:
parent_joint, child_joint = segment
parent = skeleton.data.edit_bones[parent_joint]
child = skeleton.data.edit_bones[child_joint]
child.parent = parent
# Exit edit mode
bpy.ops.object.mode_set(mode='OBJECT')
HUMANFOOTER
close(OUTPUT);