Re: [Maya-Python] Re: Python Maya add new attribute

2023-07-27 Thread Justin Israel
No worries. Happy to help.

On Thu, 27 Jul 2023, 11:47 pm SquashnStretch net, 
wrote:

> I guess I forgot to reply to this Justin, sorry and thank you very much
> for taking the time to explain perfectly ;)
>
> Thanks
> F
>
> Il giorno gio 13 lug 2023 alle ore 10:48 Justin Israel <
> justinisr...@gmail.com> ha scritto:
>
>>
>>
>> On Thu, 13 Jul 2023, 8:55 pm SquashnStretch net, <
>> squashnstret...@gmail.com> wrote:
>>
>>> Thanks Justin,
>>> yes the problem is when I go back and forth between arms to add new
>>> attribute, the code sometimes gets confused :)
>>> I'll try your version..
>>> I have another question.. Does it make sense to wrap these kind of codes
>>> into a class or wouldn't change much ?
>>>
>>
>> The benefit of a class is that you can bind behaviour with state. So your
>> functions become methods. And each instance of the class can track the
>> state between method calls. As an example, a single invocation might create
>> objects and store what it created. Another method could operate on what was
>> previously created. But then a second instance of this class would be an
>> entirely new process that would not become confused with the previous one.
>> That is the trouble with globals. You end up sharing state across many
>> independent actions.
>> Mayas scene selection state can also be thought of as a global. Sometimes
>> code tries to rely too much on the current selection which can change,
>> instead of passing inputs and receiving outputs, between functions.
>>
>> Classes don't solve every problem though. Sometimes it can be better to
>> just try to make your functions more "pure" and pass in the required inputs
>> to perform the work, and return outputs.
>>
>>
>>> Thanks
>>> F
>>>
>>> Il giorno lun 26 giu 2023 alle ore 05:49 Justin Israel <
>>> justinisr...@gmail.com> ha scritto:
>>>
 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> 

Re: [Maya-Python] Re: Python Maya add new attribute

2023-07-27 Thread SquashnStretch net
I guess I forgot to reply to this Justin, sorry and thank you very much for
taking the time to explain perfectly ;)

Thanks
F

Il giorno gio 13 lug 2023 alle ore 10:48 Justin Israel <
justinisr...@gmail.com> ha scritto:

>
>
> On Thu, 13 Jul 2023, 8:55 pm SquashnStretch net, <
> squashnstret...@gmail.com> wrote:
>
>> Thanks Justin,
>> yes the problem is when I go back and forth between arms to add new
>> attribute, the code sometimes gets confused :)
>> I'll try your version..
>> I have another question.. Does it make sense to wrap these kind of codes
>> into a class or wouldn't change much ?
>>
>
> The benefit of a class is that you can bind behaviour with state. So your
> functions become methods. And each instance of the class can track the
> state between method calls. As an example, a single invocation might create
> objects and store what it created. Another method could operate on what was
> previously created. But then a second instance of this class would be an
> entirely new process that would not become confused with the previous one.
> That is the trouble with globals. You end up sharing state across many
> independent actions.
> Mayas scene selection state can also be thought of as a global. Sometimes
> code tries to rely too much on the current selection which can change,
> instead of passing inputs and receiving outputs, between functions.
>
> Classes don't solve every problem though. Sometimes it can be better to
> just try to make your functions more "pure" and pass in the required inputs
> to perform the work, and return outputs.
>
>
>> Thanks
>> F
>>
>> Il giorno lun 26 giu 2023 alle ore 05:49 Justin Israel <
>> justinisr...@gmail.com> ha scritto:
>>
>>> 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