test.py 4.66 KB
Newer Older
cmaffeo2's avatar
cmaffeo2 committed
1
2
3
import numpy as np
import sys
from coords import rotationAboutAxis
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

class MayaObj():
    def __init__(self, maya_lines):
        self.name, self.parent_name = MayaObj._parse_first_maya_line( maya_lines[0] )

    def _parse_first_maya_line(l):
        ## createNode vHelix -n "helix_1";
        name = parent = None
        vals = l.split()
        for i in range(len(vals)):
            if vals[i] == "-n": name = vals[i+1]
            if vals[i] == "-p": parent = vals[i+1]
        return name,parent

class MayaHelix(MayaObj):
    def __init__(self, maya_lines):
        MayaObj.__init__(self, maya_lines)
        self.bases = []
cmaffeo2's avatar
cmaffeo2 committed
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
        self.position = None
        self.orientation = None

        self._parse_maya_lines(maya_lines)
        
    def _parse_maya_lines(self, maya_lines):
"""
createNode vHelix -n "helix_30";
setAttr ".t" -type "double3" 9.24809 21.4638 -12.465 ;
setAttr ".r" -type "double3" 28.99971393816978 2.2177410772603801 87.637950315471372 ;
setAttr ".dh" yes;
"""
        for l in maya_lines:
            vals = l.split()
            if vals[0] == "setAttr":
                if vals[1] == '".t"':
                    self.position = np.array([float(a) for a in vals[4:7]])
                elif vals[1] == '".r"':
                    # self.eulerAngles = np.array([float(a) for a in vals[4:7]]) # defined here
                    ax,ay,az = [float(a) for a in vals[4:7]] 
                    Rx = rotationAboutAxis( [1,0,0], ax, normalizeAxis=False )
                    Ry = rotationAboutAxis( [0,1,0], ay, normalizeAxis=False )
                    Rz = rotationAboutAxis( [0,0,1], az, normalizeAxis=False )
                    self.orientation = Rx.dot(Ry.dot(Rz)) # TODO: check
                    rotXYZ = np.array()

            for i in range(len(vals)):
            if vals[i] == "-n": name = vals[i+1]
            if vals[i] == "-p": parent = vals[i+1]
        return name,parent
52

cmaffeo2's avatar
cmaffeo2 committed
53
54
55
56
    def print_xyz(self,file_handle = sys.stdout):
        for b in self.bases:
            position = b.position.dot( self.orientation ) + self.position # TODO: double check
            print("%s %f %f %f\n" % (b.name[0], position[0], position[1], position[2]))
57
58


cmaffeo2's avatar
cmaffeo2 committed
59
60
class MayaBase(MayaObj):
    ...
61

cmaffeo2's avatar
cmaffeo2 committed
62
63
64
class MayaAimConstraint(MayaObj):
    ...
        
65

cmaffeo2's avatar
cmaffeo2 committed
66
# set ch [open /home/cmaffeo2/Downloads/ball.ma]
67
68


cmaffeo2's avatar
cmaffeo2 committed
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
def parse_maya_file(maya_file):

    helices = dict()
    bases = []
    aimConstraints = []

    ## Parse through .ma file
    with open(maya_file) as fh:
        linesBuffer = []
        for line in fh:
            if line[0] == "createNode":
                ## Create a new object
                if len(linesBuffer) > 0:
                    objType = linesBuffer[0].split()[1]
                    if objType == "vHelix":
                        h = MayaHelix( linesBuffer ) )
                        helices[h.name] = h
                    elif objType == "HelixBase":
                        bases.append( MayaBase( linesBuffer ) )
                    elif objType == "aimConstraint":
                        aimConstraints.append( MayaAimConstraint( linesBuffer ) )
                ## Clear lines buffer
                linesBuffer = []
            ## Extend lines buffer
            linesBuffer.append(line)    
        
    ## Crawl through bases, attach to parent helix
    for b in bases:
        helix_name = b.parent
        helices[helix_name].bases.append(b)

    return helices

if __name__ == "main":
    helices = parse_maya_file("/home/cmaffeo2/Downloads/bunny.ma")
    ...


""" TCL CODE

proc getNextVector {ch} {
    gets $ch line
    set line [split $line]
    lrange $line 5 7
}
proc eulerToRot {euler} {
    lassign $euler x y z
    transmult  [transaxis z $z] [transaxis y $y] [transaxis x $x] 
}
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

while {[gets $ch line] >= 0} {
    set args [lassign [split $line] cmd obj]
    if {$cmd == "createNode"} {
	if {$obj == "HelixBase" && $drawBase} {
	    set drawBase 1
	    # set v [vecscale 10 [getNextVector $ch]] 
	    set v [coordtrans $R [vecscale 10 [getNextVector $ch]]]
	    # set v [coordtrans [transmult [trans_from_offset [vecscale 10 [getNextVector $ch]] $R ] {0 0 0}]]
	    graphics $ID sphere $v radius 2
	} elseif {$obj == "vHelix"} {
	    set drawBase 1
	    # createNode vHelix -n "helix_1";
	    # setAttr ".t" -type "double3" -16.9433 11.8494 25.2659 ;
	    # setAttr ".r" -type "double3" 42.338094401101131 222.16004220698011 5.8192508587610412 ;
	    # setAttr ".dh" yes;
		
	    set v [vecscale 10 [getNextVector $ch]] 
	    # set r [vecscale [expr {90.0/acos(0)}] [getNextVector $ch]]
	    set r [getNextVector $ch]
	    set R [eulerToRot $r]	    
	    set R [transmult [trans_from_offset $v] $R ]

	    # if {$helixCount == 2} {
	    # 	break
	    # } else {
	    # 	incr helixCount
	    # }
	}
    }    
}
cmaffeo2's avatar
cmaffeo2 committed
149
"""