Skip to content

Commit

Permalink
Initial refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mottosso committed Apr 27, 2016
1 parent e87dffd commit f45eaef
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 258 deletions.
60 changes: 57 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,58 @@
sansapp
=======
### Autodesk Maya Scenefile Parser

An Experimental Batch Scene Parser
Parse Binary and Ascii Maya scene files with Python.

Supports Maya 8.5-2017.

**Alternatives**

- [cgkit](https://github.com/yamins81/cgkit/blob/master/cgkit/mayabinary.py)
- [westernx/mayatools](https://github.com/westernx/mayatools/blob/master/mayatools/binary.py)


<br>
<br>
<br>

### Usage

Parsing either an `.ma` of `.mb` involves subclassing a parser superclass and implementing various handles, such as when a node or attribute creation event takes place.

```python
from maya_scenefile_parser import MayaAsciiParser, MayaBinaryParser

nodes = dict()

ext = os.path.splitext(path)[1]
try:
Base, mode = {
".ma": (MayaAsciiParser, "r"),
".mb": (MayaBinaryParser, "rb"),
}[ext]
except KeyError:
raise RuntimeError("Invalid maya file: %s" % path)

class Parser(Base):
def on_create_node(self, nodetype, name, parent):
self.current_node = (name, parent, nodetype)

def on_set_attr(self, name, value, type):
if name not in ("nts", "notes"):
return

if self.current_node not in nodes:
nodes[self.current_node] = {}

nodes[self.current_node][name] = value

print("{name} = {value} ({type})".format(**locals()))

with open(path, mode) as f:
parser = Parser(f)
parser.parse()

for node, attrs in nodes.iteritems():
for key, value in attrs.iteritems():
print("{node}.{key} = {value}".format(**locals()))

```
130 changes: 0 additions & 130 deletions docs/maya_iff.md

This file was deleted.

2 changes: 0 additions & 2 deletions maya/__init__.py

This file was deleted.

8 changes: 8 additions & 0 deletions maya_scenefile_parser/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# API
from .ascii import MayaAsciiParser
from .binary import MayaBinaryParser

__all__ = [
"MayaAsciiParser",
"MayaBinaryParser"
]
48 changes: 42 additions & 6 deletions maya/ascii.py → maya_scenefile_parser/ascii.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from common import *
import json

from . import common


class MayaAsciiError(ValueError):
pass


class MayaAsciiParserBase(MayaParserBase):
class MayaAsciiParserBase(common.MayaParserBase):
def __init__(self):
self.__command_handlers = {
"requires": self._exec_requires,
Expand Down Expand Up @@ -75,7 +77,6 @@ def _exec_create_node(self, args):

name = None
parent = None
shared = False

argptr = 1
while argptr < len(args):
Expand All @@ -87,16 +88,50 @@ def _exec_create_node(self, args):
parent = args[argptr + 1]
argptr += 2
elif arg in ("-s", "--shared"):
shared = True
argptr += 1
else:
raise MayaAsciiError, "Unexpected argument: %s" % arg
raise MayaAsciiError("Unexpected argument: %s" % arg)

self.on_create_node(nodetype, name, parent)

def _exec_set_attr(self, args):
pass
name = args.pop(0)[1:]
attrtype = None
value = None

argptr = 1
while argptr < len(args):
arg = args[argptr]
if arg in ("-type", "--type"):
attrtype = args[argptr + 1]
value = args[argptr + 2:]
argptr += 2
else:
# FIXME this is a catch-all; explicitly support flags
argptr += 1

if not value:
value = args[-1]

if not attrtype:
# Implicitly convert between Python types
# FIXME this isn't particularly safe?
types = {
str: "string",
float: "double",
int: "integer"
}

try:
attrtype = types[type(json.loads(value))]

except KeyError:
attrtype = "string"

except ValueError:
attrtype = types.get(type(value), "string")

self.on_set_attr(name, value, attrtype)

class MayaAsciiParser(MayaAsciiParserBase):

Expand Down Expand Up @@ -190,3 +225,4 @@ def __parse_command_lines(self, lines):

# Done tokenizing arguments, call command handler
self.exec_command(command, args)

Loading

0 comments on commit f45eaef

Please sign in to comment.