Skip to content
Snippets Groups Projects
test.py 4.95 KiB
import numpy as np
import sys
from coords import rotationAboutAxis

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 = []
        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

    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]))


class MayaBase(MayaObj):
    ...

class MayaAimConstraint(MayaObj):
    ...
        

# set ch [open /home/cmaffeo2/Downloads/ball.ma]


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].add_base( b ) # function updates base position?
        

    ## Assign base connectivity from aimCconstraints
    ...
    # base_positions = np.zeros( [ len(bases), 3 ] )
    base_positions = np.array( [b.position for b in bases] )
    for aim in aimConstraints:
        base = base_dict[ aim.parent ]
    

        

    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] 
}

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
	    # }
	}
    }    
}
"""