Changing defaults of a node type

142 views
Skip to first unread message

Szabolcs

unread,
Mar 30, 2017, 7:20:16 AM3/30/17
to Python Programming for Autodesk Maya
Hi,

I'd like to change the default attribute values of a certain node type, but because of an existing asset library I can't simply change the defaults in the plugin, because that would break existing Maya scenes. I tried using OpenMaya.MDGMessage.addNodeAddedCallback() to setup a callback to change the attributes after node creation and it works ok, but the problem is that the callback executes not only when a new node is created by hand (or script) but also when a scene file is loaded / imported / referenced. I could disable the callback for scene load using another callback (yuck!) but it sounds like its leading into a mess of callbacks fighting with each other. I'm also not super happy about having a callback that examines the node type of every created node either, I'm afraid that it would slow down some scripts.

So the question is: is there a bulletproof solution for doing something like this? Or are there any tricks to check the reason of the addNodeAddedCallback being called? Or should I simply hijack createNode and shadingNode commands and wrap them into custom code (for both mel and python)? Any suggestion is much appreciated!

Cheers,
Szabolcs

Marcus Ottosson

unread,
Mar 30, 2017, 8:03:49 AM3/30/17
to python_in...@googlegroups.com

Here’s my 2 cents. I’d be explicit about it.

from maya import cmds

def myCreateNode(node):
    nodes = {
        "joint": {
            "tx": 1.0,
            "ty": 10.0,
            "ry": 5.0
        },
        "mesh": {
            "overrideEnabled": 1,
            "overrideColor": 6,
        }
    }

    overrides = nodes.get(node, {})
    node = cmds.createNode(node)

    for attribute, value in overrides.items():
        cmds.setAttr(node + "." + attribute, value)

    return node

myCreateNode("joint")
myCreateNode("mesh")

Nicolas Chaverou

unread,
Mar 30, 2017, 9:50:14 AM3/30/17
to python_in...@googlegroups.com
Hey,

We're having the same mechanism here.
But that means you need to educate all your users to use that override function instead of the default Maya createNode

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCA_FC-6iK3EVhYCXTYw8CTKttfrjJNm92izuCVroOfXA%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Szabolcs

unread,
Mar 30, 2017, 10:21:50 AM3/30/17
to Python Programming for Autodesk Maya
Thanks guys, but the main problem is that we have (and probably will have) lots of scripts that call createNode and shadingNode commands both from MEL and Python. In our case we'd like to override the attributes of shading nodes, so there are already at least 4-5 different Maya UI scripts that can create them from the GUI. I could track these down and inject our housekeeping code and override the node creation commands too, *but* it would require constant migration of these scripts to new Maya versions and also monitoring user scripts. Not the direction I would like to go if there is another option...

Cheers,
Szabolcs

Ian Waters

unread,
Mar 30, 2017, 10:26:18 AM3/30/17
to python_in...@googlegroups.com
Hey,
You can run that check without a callback:

from maya import OpenMaya as om

if not om.MFileIO.isReadingFile():
change some stuff...

That would avoid callback spaghetti :)
Best,
Ian

--
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/2bd95713-2224-4b8f-8dfb-cf74b0f275a4%40googlegroups.com.

Szabolcs

unread,
Mar 30, 2017, 11:32:16 AM3/30/17
to Python Programming for Autodesk Maya
Ian, you rock!

OpenMaya.MFileIO.isReadingFile() handles all the cases that I need (open, reference, import) and OpenMaya.MDGMessage.addNodeAddedCallback() accepts an explicit node type as a second argument, so the callback system won't call my script to inspect every single node. Great! The only remaining minor gotcha is that duplicating a node of this type will change the attributes, but thats something the users can probably live with.

Thanks a lot!

Cheers,
Szabolcs


Just for the archives:
--
import maya.OpenMaya as OpenMaya
import maya.cmds as cmds

def nodeCreationCB(node, info):
    if OpenMaya.MFileIO.isReadingFile():
        return

    mayaNodeName = OpenMaya.MFnDependencyNode(node).name()
    cmds.setAttr(mayaNodeName + '.attr', 1)

cbid = OpenMaya.MDGMessage.addNodeAddedCallback(nodeCreationCB, 'myNodeType')
--


On Thursday, March 30, 2017 at 4:26:18 PM UTC+2, Ian Waters wrote:
Hey,
You can run that check without a callback:

from maya import OpenMaya as om

if not om.MFileIO.isReadingFile():
change some stuff...

That would avoid callback spaghetti :)
Best,
Ian

On 30 Mar 2017, at 12:20, Szabolcs <szab...@impresszio.hu> wrote:

Hi,

I'd like to change the default attribute values of a certain node type, but because of an existing asset library I can't simply change the defaults in the plugin, because that would break existing Maya scenes. I tried using OpenMaya.MDGMessage.addNodeAddedCallback() to setup a callback to change the attributes after node creation and it works ok, but the problem is that the callback executes not only when a new node is created by hand (or script) but also when a scene file is loaded / imported / referenced. I could disable the callback for scene load using another callback (yuck!) but it sounds like its leading into a mess of callbacks fighting with each other. I'm also not super happy about having a callback that examines the node type of every created node either, I'm afraid that it would slow down some scripts.

So the question is: is there a bulletproof solution for doing something like this? Or are there any tricks to check the reason of the addNodeAddedCallback being called? Or should I simply hijack createNode and shadingNode commands and wrap them into custom code (for both mel and python)? Any suggestion is much appreciated!

Cheers,
Szabolcs

--
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.

Szabolcs

unread,
Mar 30, 2017, 11:50:06 AM3/30/17
to Python Programming for Autodesk Maya
I'm not proud of the solution, but when Maya duplicates a node, by the time the creation callback executes the node has a standard prefix, "__PrenotatoPerDuplicare". So I simply check for this string in the node name to avoid modifying duplicated nodes. Not nice but works. Although now I'm really curious about the Maya lore where this funky prefix comes from...  :)

Cheers,
sz.
Reply all
Reply to author
Forward
0 new messages