OpenMaya accessing shape node after creation

1,293 views
Skip to first unread message

Eric Thivierge

unread,
Dec 18, 2013, 1:34:33 PM12/18/13
to python_in...@googlegroups.com
Hey all,

Been lurking here for a long time but just now picking up some Maya programming for some rigging tools here at work. I've been a long time Softimage user and am used to the OM they have there so it's a bit of an uphill battle trying to make heads or tails of some of the stuff in OpenMaya.

Is this the simplest way to get to the shape node after it's created from the transform node?

# Sample Python
import maya.OpenMaya as om

dagModifier = om.MDagModifier()

locator = dagModifier.createNode('transform')
dagModifier.renameNode(locator, "myLocator")
dagModifier.createNode('locator', locator) # create locator shape under transform
dagModifier.doIt()

locatorDagPath = om.MDagPath()
locatorDag = om.MFnDagNode(locator)
locatorDag.getPath(locatorDagPath)
locatorDagPath.extendToShape()

shapeDag = om.MFnDagNode()
shapeDag.setObject(locatorDagPath.node())
shapeNode = om.MObject()
shapeNode = locatorDagPath.node()

dagModifier.renameNode(shapeNode, "myLocatorShape")
dagModifier.doIt()

--------------------------------------------
Eric Thivierge
http://www.ethivierge.com

br...@meljunky.com

unread,
Dec 18, 2013, 5:17:56 PM12/18/13
to python_in...@googlegroups.com
You could use extendToShape() method from the MDagPath Class

-brian
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAAjrnHvxetT_CMeQzqBqH-JaJnwDB2eqr6q5TKArb-82ZiC8Rg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Eric Thivierge

unread,
Dec 18, 2013, 5:34:54 PM12/18/13
to python_in...@googlegroups.com
Yeah I am in line 13. Just seems an insane amount of code just to get at the shape node.

--------------------------------------------
Eric Thivierge
http://www.ethivierge.com


Count Zer0

unread,
Dec 18, 2013, 6:26:50 PM12/18/13
to python_in...@googlegroups.com
That's why everybody uses maya.cmds and/or pymel.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Eric Thivierge

unread,
Dec 18, 2013, 7:33:05 PM12/18/13
to python_in...@googlegroups.com
Not much better there either no?

--------------------------------------------
Eric Thivierge
http://www.ethivierge.com


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/ab5e7a19-43e2-46ef-93b7-84231bc1d850%40googlegroups.com.

Emre Yilmaz

unread,
Dec 18, 2013, 8:00:51 PM12/18/13
to python_in...@googlegroups.com
Much better, unless I am misunderstanding your need:

import pymel.core as pm

# Create locator
locPN = pm.spaceLocator(n="myLocator")

# Get its shape node
locShapePN = locPN.getShape()

# Print out name of shape node
print locShapePN.name()

A lot (not all) of OpenMaya's functionality is integrated into pymel.  But whether pymel is a good path or not depends on what kind of tool you're writing.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Eric Thivierge

unread,
Dec 18, 2013, 8:32:36 PM12/18/13
to python_in...@googlegroups.com
Honestly I know it's much more concise in mel / pymel but it's not Object Oriented and I thought that OpenMaya would be more than it is. Super spoiled coming from Softimage...

Probably will bite the bullet and go pymel.

--------------------------------------------
Eric Thivierge
http://www.ethivierge.com


Justin Israel

unread,
Dec 18, 2013, 8:50:10 PM12/18/13
to python_in...@googlegroups.com
Unfortunately most of the OpenMaya Python API is derived syntactically from the original C++ counterpart. So you have to deal up front with a lot of C++ concepts like passing in pointer "helpers", the function sets, opaque handles, and non-pythonic iterators. But generally its going to be your more performant approach. PyMel would give you the convenience of wrapping OpenMaya and cmds into a more pythonic interface, at the expense of performance by introducing more overhead to accomplish this. Just depends on your needs. 


Jeremy YeoKhoo

unread,
Dec 19, 2013, 4:06:30 PM12/19/13
to python_in...@googlegroups.com
Hey Eric,

Hows it going?

It really depends on what you need. Using OpenMaya (or python for that matter) doesnt necessarily make things faster in maya. It really depends on whether youre trying to make a plugin or youre executing a command. Using OpenMaya with with python uses a wrapper (SWIG) anyway, but there is a new python API 2.0, but Im not sure how much faster it is but its better in with the implementation of python's syntax.

If youre trying to get the shape node after creating a transform, then I would do....

import maya.cmds as cmds

myLoc= cmds.spaceLocator('')[0]
myLocShape= cmds.listRelatives(myLoc, s=1)[0]


But if youre creating a plugin or your own custom locator with a shape(that does something) then you have to use

import maya.OpenMayaMPx as OpenMayaMPx

class myClass(OpenMayaMPx.MPxLocatorNode):
    def __init__(OpenMayaMPx.MPxLocatorNode.__init__, self):

And etc..............

Write that plugin so it returns a shape node and then execute your command that created the plugin and node... And away you go, but I reckon thats not what you want to achieve.

-Jeremy

Eric Thivierge

unread,
Dec 19, 2013, 4:46:51 PM12/19/13
to python_in...@googlegroups.com
Hey Jer,

Not doing any really heavy lifting with it. I'm staying as far away from mel as possible and have decided to go the pymel route. I'm already much happier with that at the moment than mel and OpenMaya 2.0 as well. I would really just like a proper object model in Maya.

Thanks for the info.

Eric T.

--------------------------------------------
Eric Thivierge
http://www.ethivierge.com


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

damon shelton

unread,
Dec 19, 2013, 4:50:52 PM12/19/13
to python_in...@googlegroups.com
"""
while these techniques are not OOP they are still fully valid ways to perform these operations - Pymel is object oriented but is slower to execute (wrapper on wrapper on wrapper on wrapper) I am not a hater of pymel (i prefer OOP coding but autodesk didnt implement it that way 8(  ) but my code always comes down to speed and stability rather than just being concerned with it being easy to write and access as I write it. why i use a good python IDE ;)
"""
cmds.createNode('transform') - does not have a shape node
cmds.createNode('locator') - returns shapeNode only

#create a locator
loc = cmds.spaceLocator()[0]
locShape = cmds.listRelatives(loc, s=True)[0]

api 2.0 is showing great promise but is still missing a lot and the 2 api's dont play nice, spending  a lot of code to convert information but it has huge speed increases.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Count Zer0

unread,
Dec 20, 2013, 9:51:24 PM12/20/13
to python_in...@googlegroups.com
PyMEL is all OOP. Every maya node type is wrapped by a class and has all maya.cmds relevant to that object attached as methods to it. You can even create custom sub-classes that inherit all methods and attach your own.

We use it for 99.9% of our tools. It only slows down a lot when iterating over tons of objects; like mesh iteration or something like that (reason is it instantiates a new object for every single vert, this is cool because every vert is a MeshVertex object with a hundred handy methods on it, like .getNormal() etc. but this can be slow if you have a large mesh).

Enjoy OOP land in Maya! It's pretty damn fun and fast to code. Try setting up a nice IDE too for some awesome command completion, code inspection, real-time debugging, etc. (couple tips on my blog if you need it: artoftech.co)

-jason
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Eric Thivierge

unread,
Dec 21, 2013, 5:09:53 PM12/21/13
to python_in...@googlegroups.com

When I run a. addAttr() on a group object I don't get returned an attribute object... maybe they missed something in that return? It returns none.

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/fc8e0df3-9a02-4997-8124-db88956be470%40googlegroups.com.

Jeremy YeoKhoo

unread,
Dec 22, 2013, 8:24:11 PM12/22/13
to python_in...@googlegroups.com
maya.cmds doesnt return anything... Im not sure about using pymel though

Count Zer0

unread,
Dec 23, 2013, 10:41:05 PM12/23/13
to python_in...@googlegroups.com
PyMEL just wraps the maya.cmds.addAttr() as Jeremy mentions, which by default does not return anything.
You'll have to recast to an Attribute class manually like this:

myNode.addAttr('test')
myAttr = myNode.test

It's possible to hack pymel.core.general.addAttr() if you want to return the attribute as a class.

-jason

Eric Thivierge

unread,
Dec 24, 2013, 9:36:22 AM12/24/13
to python_in...@googlegroups.com

Thanks Jason will take a look at that.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages