>From what I can see in your last code example and your description of the
problem you are seeing, is the issue due to the selection changing after
you run the add_attribute_and_create_group() operation? Because unless I am
missing something specific to your select, it seems to work fine as long as
I reselect the "hand" again before running the button. If that is the case,
then you can make sure to reset the selection to the original, after the
creation of your group transforms changes the selection:

https://pastebin.com/YSm8gPb8

If that is not what you are seeing as the problem, maybe you can provide a
simple scene as a sample, and exact steps to reproduce the problem using
this last code example?

On Sun, Jun 25, 2023 at 11:21 PM SquashnStretch net <
squashnstret...@gmail.com> wrote:

> Hello Justin,
> here is the prev version with no globals, i ddnt connect the attributes
> here because i wanted to solde the parentConstraint first...:
>
> import maya.cmds as mc
>
> def add_attribute_and_create_group(*args):
>     attribute_name = mc.textField(attribute_field, query=True, text=True)
>     selected_objects = mc.ls(selection=True)
>
>     created_groups = []
>
>     for selected_object in selected_objects:
>         if not mc.objExists("{0}.{1}".format(selected_object,
> attribute_name)):
>             mc.addAttr(selected_object, ln=attribute_name, at="double",
> min=0, max=1, dv=0, keyable=True)
>
>         group_name = selected_object + "_spaceGrp"
>         if not mc.objExists(group_name):
>             new_group = mc.group(selected_object, name=group_name)
>             mc.xform(new_group, centerPivots=True)
>             created_groups.append(new_group)
>
>     print('Returned object is:', created_groups)
>     return created_groups
>
> def create_parent_constraint(*args):
>     selected_objects = mc.ls(selection=True)
>     if not selected_objects:
>         print("Nessun oggetto selezionato.")
>         return
>
>     groups = mc.ls("*_spaceGrp")
>     if not groups:
>         print("Nessun gruppo creato.")
>         return
>
>     for selected_object in selected_objects:
>         mc.parentConstraint(selected_object, groups[0],
> maintainOffset=True)
>         print("ParentConstraint creato tra", selected_object, "e",
> groups[0])
>
>
> # Creazione della finestra
> window = mc.window(title="Add Attribute")
> mc.columnLayout(adjustableColumn=True)
> attribute_field = mc.textField(text="")
> mc.button(label="Esegui", command=add_attribute_and_create_group)
> mc.button(label="Parent", command=create_parent_constraint)
> mc.showWindow(window)
>
> Thanks for taking a look
> F
>
> Il giorno ven 23 giu 2023 alle ore 20:05 Justin Israel <
> justinisr...@gmail.com> ha scritto:
>
>>
>>
>> On Sat, 24 Jun 2023, 5:13 am SquashnStretch net, <
>> squashnstret...@gmail.com> wrote:
>>
>>> actually that was the version with globals, I tried many ways but I get
>>> the same results.....
>>> thanks
>>>
>>
>> Can you share your attempt without globals?
>>
>>
>>> Il giorno venerdì 23 giugno 2023 alle 17:41:28 UTC+1 SquashnStretch net
>>> ha scritto:
>>>
>>>> Hello Everyone,
>>>> I went back to my attempt on adding new attribute to every rig, like I
>>>> want hand can be constraint to the head and / or to the Body, Hips, etc ...
>>>> If I add al the attributes that I want and I go to the other hand or
>>>> leg etc and add new attributes, everything's fine,
>>>> the problem happens when I go back to the first hand for instance, and
>>>> try to add new attribute, then the script fails because it picks the wrong
>>>> elements to perform the constraints ...
>>>>
>>>> I tried not to use global variables but I cant figure out how to fix
>>>> it. I would like to have this little code/snippet that once I add the new
>>>> attribute and create the constraint etc, it would  clear all the variables
>>>> or what it needs, in order to avoid conflict
>>>>
>>>> Here's the script which create a GUI with a text field where to enter
>>>> name of the attribute we want to add and after we select an object (an hand
>>>> for instance), it add the name etc,
>>>> with the second button we select what it need to follow....
>>>>
>>>> https://pastebin.com/raw/cdqeJCx0
>>>>
>>>> thanks in advance
>>>> Flys
>>>>
>>> --
>>> 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+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/python_inside_maya/212298a6-ebf7-4ad9-9cf6-f96050987f5cn%40googlegroups.com
>>> <https://groups.google.com/d/msgid/python_inside_maya/212298a6-ebf7-4ad9-9cf6-f96050987f5cn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
>> 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+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0ehT%3DxShVAvELyt29m8WbiarDyc1QGb0PdU6%3D2cx9o_A%40mail.gmail.com
>> <https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0ehT%3DxShVAvELyt29m8WbiarDyc1QGb0PdU6%3D2cx9o_A%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/CAE8PYnC%2BSoJPrFa%3Djt%2Bih9bx%2BvLU2X_Djx33dDE66mXdNwx6yg%40mail.gmail.com
> <https://groups.google.com/d/msgid/python_inside_maya/CAE8PYnC%2BSoJPrFa%3Djt%2Bih9bx%2BvLU2X_Djx33dDE66mXdNwx6yg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2pgqQbeAbDVLWv8_gOt06YNnT9RGfmG9pxdeWO%3DUqG5A%40mail.gmail.com.

Reply via email to