Mogie
Mogie

Reputation: 55

Maya Python on Renaming Nodes

I'm trying to get up to speed on Maya Python so I have been reading This book (http://www.maya-python.com/) There is an online pdf of it... I'm on a section (The For Statement) where I'm not getting the correct result but I am also not getting any errors. If anyone could look at this and give me an idea on whats causing the problem that'd be great.

So it creates 3 file nodes fine and it is supposed to rename all 3 nodes to have the prefix 'dirt_'. But it only renames 'file1' and not the other two nodes

Here is the process:

#The FOR statement

import maya.cmds;
def process_all_textures(**kwargs):
    pre = kwargs.setdefault('prefix', 'my_');
    textures = kwargs.setdefault('texture_nodes');
    new_texture_names = [];
    for texture in textures:
        new_texture_names.append(
        maya.cmds.rename(
        texture,
        '%s%s'%(pre, texture)
        )
        );
        return new_texture_names;

#create new Maya scene & list 3 file nodes  & print their names

maya.cmds.file(new=True, f=True);
textures = [];
for i in range(3):
    textures.append(
    maya.cmds.shadingNode(
    'file',
    asTexture=True
    )
    );
print(textures);

#pass new texture list to process_all_textures() func and print resulting names

new_textures = process_all_textures(
texture_nodes = textures,
prefix = 'dirt_'
);
print(new_textures);

[u'file1', u'file2', u'file3']
[u'dirt_file1']

Upvotes: 2

Views: 1650

Answers (1)

Andy Jazz
Andy Jazz

Reputation: 58553

The line return new_texture_names must be indented by four spaces (not by eight ones).

In your case return statement stops the function and immediately returns the value.

#The FOR statement
import maya.cmds as mc

def process_all_textures(**kwargs):
    pre = kwargs.setdefault('prefix', 'my_')
    textures = kwargs.setdefault('texture_nodes')
    new_texture_names = []

    for texture in textures:
        new_texture_names.append(mc.rename(texture,'%s%s'%(pre, texture)))

    return new_texture_names

#create new Maya scene & list 3 file nodes & print their names
mc.file(new=True,f=True)
textures = []

for i in range(3):
    textures.append(mc.shadingNode('file',asTexture=True))
print(textures)

#pass new texture list to process_all_textures() func and print resulting names
new_textures = process_all_textures(texture_nodes = textures,prefix = 'dirt_')
print(new_textures)

[u'file1', u'file2', u'file3']
[u'dirt_file1']

Upvotes: 1

Related Questions