Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk® Maya® / Python / Getting data out of a scene file?
  RSS 2.0 ATOM  

Getting data out of a scene file?
Rate this thread
 
57508
 
Permlink of this thread  
avatar
  • Total Posts: 2
  • Joined: 26 July 2008 01:52 AM

OK, so I have a scene in Maya that contains a model and some animation information. What I would like to do is to get the vertex, normal, uv-coord, bone, and vertex weight data, and also possibly the animation information, although at the moment, I am mostly just interested in replicating different frames of the animation in a static pose.

I feel stupid, because after poring over the Maya 2012 python documentation for a day or so, I still have hardly any idea on how to go about doing this.

I realize that I have to get the appropriate nodes from the scene, so the ls() command does a nice job of getting me the node names. Then, I can use these names to iterate over the nodes to get the nodes of type ‘mesh’, which I am pretty sure are the ones containing the geometric information (at this point, I would be happy just to get triangle, normal, and texture coord information, bones and weights can come later). I intimate that I should be using the Maya API to somehow coerce this node into some kind of instantiation of MFnMesh, but this is the point where my comprehension breaks down. I can’t seem to get from node and name to actual geometry data.

If anyone can point me to a discussion of how to access the data from a scene using Python, I would be grateful. Otherwise, it will have to be plan B, which involves a Glock 27 and someone cleaning up an incredible mess from the wall of my cubicle.

Thanks,

David



Replies: 0
avatar

This is probably a little weird replying to my own post, but I have made some progress on getting information out, so I thought I would throw some snippets up on the off-chance someone else would find it useful.

Of course, if you are experienced with Maya Python, you realize by now that the problem that I was having was that I was not properly accessing the nodes in the DAG. So, the solution was to turn the names of the nodes, which are quite easy to get using maya.cmds.ls(), into actual objects that you can query. This, unfortunately, involves quite a bit of casting and subcasting, and seems like an awful lot of work, but it is what it is. Let me just put a couple snippets that helped me a bunch.

First, since I am working with names, I find that the easiest way to start the ball rolling is to use an MSelectionList. So, I have the following code

import maya.standalone
maya
.standalone.initialize(name='python')
import maya.cmd as cmds
import maya
.OpenMaya as OpenMaya

# this will give us the names of all the nodes in the scene
all_nodes cmds.ls()

# use an MSelectionList to drive our queries
s_list OpenMaya.MSelectionList()
for 
node in all_nodes:
    
# this gives us the node type
    
node_type cmds.nodeType(node)
    
    
# we can now do some selection and processing
    
if node_type == 'joint':
        print 
'Take a hit off this joint, dude!' # ok, bad pun

    # this ensures that the list will only have the single member
    
s_list.clear()

    
# add our current node to the list
    
s_list.add(node)

    
# get the depend node
    
depend_node OpenMaya.MObject()
    
s_list.getDependNode(0depend_node)

    
# let's say we are a mesh, let's get the vertex data
    
if node_type != 'mesh':
        continue

    
# select the mesh of interest
    
cmds.select(node)

    
# get the vertex data
    
vertex_data []
    num_verts 
cmds.polyEvaluate(vertex=True)
    
vertex_data [cmds.pointPosition("%s.vtx[%d]" % (nodei)) for i in range(num_verts)]

    
# now barf it out to the screen
    
print 'Vertices'
    
print str(vertex_data# in [x, y, z] format

So, that is an extremely simpleminded way of getting some information out of the scene, just in case anyone else was as confused as I was. I may be adding some more snippets to this later.



Replies: 0