The problem that I see is that you are doing your counter on the inner
loop. This means you will end up getting name clashes for each "geo" in
"all_geos". That is, your defined naming pattern is:  TEST_<NUM>_GRP
While you are making sure to increment the name for each inner loopitem,
you will start over at the next geo and then Maya's own unique naming logic
is helping you out by appending a number to the name. If you were to use a
unique counter for the outer loop (at the start of your function), this
would end up with a different outcome.

Futhermore, since you are doing your own internal counter, this does not
prevent you from still having that number added to the end when you run
this script more than once, since you are likely to clash again with groups
from the previous run.

If you really want to be able to have custom naming with a custom number
increment, then you may want to try your own unique naming function.
Something like this?

def uniqueNamePattern(base="object"):
    i = 1
    customRepl = base.format('x') != base

    if customRepl:
        name = base.format()
    else:
        name = base

    while cmds.objExists(name):
        i += 1
        if customRepl:
            name = base.format()
        else:
            name = "{0}{1}".format(base, i)

    return name

cmds.group(name=uniqueNamePattern("test_{0}_GRP"))

​

Justin

On Tue, Aug 23, 2016 at 12:58 PM likage <[email protected]> wrote:

> I am trying to do some grouping with the usage of counters, such that it
> will increment accordingly.. I apologize if my title gives the wrong
> impression but...
>
> Anyway, when I have 2 or more selections and as I execute my code, instead
> of seeing groups with naming such as - TEST_1_GRP, TEST_2_GRP, TEST_3_GRP
> etc., I am getting TEST_1_GRP, TEST_1_GRP1, TEST_1_GRP2 as my results...
>
> I tried placing some of the line for the counter portion around (like
> within/ before the for sentence), I am getting nowhere..
>
> Below is my code:
> def fix_shapes():
>     all_geos = cmds.ls(sl = True)
>     for geo in all_geos:
>         shapes = cmds.listRelatives(geo, fullPath=True, shapes=True)
>
>         if len(shapes) == 1:
>             continue
>
>         new_listing = []
>         listing.append(shapes[:1])
>         # pop out the first shape, since we don't have to fix it
>         multi_shapes = shapes[1:]
>         for multi_shape in multi_shapes:
>             new_transform = cmds.duplicate(multi_shape, parentOnly=True)
>             new_geos = cmds.parent(multi_shape, new_transform, addObject=
> True, shape=True)
>             listing.append(new_geos)
>
>
>             # remove the shape from its original transform
>             cmds.parent(multi_shape, removeObject=True, shape=True)
>
>         # counter to 'version' up new_geos group naming
>         counter = 0
>         new_group_name = cmds.group(em=True, name = 'TEST_' + str(counter
> + 1) + '_GRP')
>
>         for item in new_listing:
>             counter = counter + 1
>             new_geos_parent_name = cmds.listRelatives(item, parent = True)
>             cmds.parent(new_geos_parent_name, new_group_name)
>
> Greatly appreciate for any advice..
>
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/fde473f3-2f99-459c-82dc-f92061d65f4f%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/fde473f3-2f99-459c-82dc-f92061d65f4f%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1CwOCpV%3D9s0F8ohGb5NtNKFPAacpvq7tUGAiaWqyH_9g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to