Re: [Maya-Python] check channels before constraints

2023-03-27 Thread Justin Israel
On Tue, 28 Mar 2023, 12:21 pm SquashnStretch net, 
wrote:

> Justin I ddnt notice that you rewrote my script! it is so ..beautifull
> that way! thanks, a lot to learn from it!
>

No trouble at all! Happy to help out.
Don't hesitate to come back for more advice, as needed.


> Il giorno lunedì 27 marzo 2023 alle 23:56:35 UTC+1 SquashnStretch net ha
> scritto:
>
>> Thanks Justin for your time and advice.
>>
>> Yes I see the script is super complicated, my original one was mainly
>> inside separated functions but not using many loops actually, I need to
>> practice that, seems the right way...
>>
>> I'll try to work that way and im pretty sure I'll annoying you again ;)
>>
>> thanks!
>> F
>>
>> Il giorno domenica 26 marzo 2023 alle 20:35:30 UTC+1 Justin Israel ha
>> scritto:
>>
>>> Quick tip: If you are going to be sharing code longer than a couple
>>> lines, you might want to use something like a pastebin.com link, or
>>> similar, to retain the formatting and make it easier for others to read.
>>>
>>> Considering that most of the operations on your script apply to each
>>> individual selected object, it would make sense to run the entire thing
>>> under a for-loop. The current structure of the script is a bit confusing
>>> because it starts by operating on each item in the selection, builds up a
>>> new list, and then tries to correlate the new list back to the original
>>> selection. Another thing that is a bit harder to read at first is that you
>>> are storing the current selection as a list-of-lists. It would be more
>>> clear if you just store the single list.
>>>
>>> Check out this refactor of your script: https://pastebin.com/FcR1G32m
>>>
>>> I haven't tested it, so you can use it as a starting point. The idea is
>>> that you start with a main function that will get the current selection and
>>> do the up-front check. Then it will call do_constraint for each selected
>>> object. Within the do_constraint function, you only need to worry about
>>> that specific object. It is best if you avoid relying on manipulating the
>>> current selection to make your script work, and instead pass the target
>>> objects to the maya functions.
>>>
>>> Let me know if you need clarification on any part of that script.
>>>
>>> Justin
>>>
>>>
>>>
>>>
>>> On Mon, Mar 27, 2023 at 4:28 AM SquashnStretch net 
>>> wrote:
>>>
 Hello again, update
 I managed to make it work a bit better but still not as I need to...
 thanks to your suggestions I'm able now to check channels and create
 locators, constraints etc ...
 So I have a script that checks everything and if selections have both
 rotations and translations available, it runs the function properly; if
 there is something locked it stop and warn. And I can also have several
 objects selected.

 Now Im trying something more...
 I'd like the script to check what channel is available and act
 accordingly, if there is only translation, it contstraint just the
 translations, if only rotations available, it works only with that
 I ended up make it work by adding the skipRotation or skipTranslation
 flag
 but it works only selecting one object at a time, if I select several
 object in the scene where some has both T and R and some had just
 translation and no rotations, it fails for all the object but work only for
 the first one

 I guess I need a loop or another condition which checks inside my
 'userSel' variable but not sure how to do.
 Probably the problem is in the sel[0] call, which looks only on the
 first obj selected, I tried to add a loop but it works only with
 translation, seems that it doesnt connect the rotations

 anyways this is what I have so far which works only selecting obj 1 by
 1:

 # this works fine BUT 1 obj x time


 import maya.cmds as mc


 LocList = []

 userSel = []

 len(userSel)

 sel = []

 sel = mc.ls(sl=1)

 userSel.append(sel)


 if len(sel) != 0:

 _has_trans = mc.getAttr (sel[0]+'.translate', settable=True)

 _has_rot = mc.getAttr (sel[0]+'.rotate', settable=True)



 else :

 mc.warning('Please make a selection')



 # check if there is rotations

 # create a loc for ech selected obj and constraint to it


 if _has_rot ==1 and _has_trans ==1:

 for obj in sel:

 newLoc = mc.spaceLocator()

 newCon = mc.parentConstraint(obj, newLoc, mo=0)

 LocList.append(newLoc)



 elif _has_rot ==0 and _has_trans ==1:

 for obj in sel:

 newLoc = mc.spaceLocator()

 newCon = mc.parentConstraint(obj, newLoc, mo=0, sr=['x', 'y',
 'z'])

 LocList.append(newLoc)



 elif _has_rot ==1 and _has_trans ==0:

  

Re: [Maya-Python] check channels before constraints

2023-03-27 Thread SquashnStretch net
Justin I ddnt notice that you rewrote my script! it is so ..beautifull that 
way! thanks, a lot to learn from it!

Il giorno lunedì 27 marzo 2023 alle 23:56:35 UTC+1 SquashnStretch net ha 
scritto:

> Thanks Justin for your time and advice.
>
> Yes I see the script is super complicated, my original one was mainly 
> inside separated functions but not using many loops actually, I need to 
> practice that, seems the right way...
>
> I'll try to work that way and im pretty sure I'll annoying you again ;)
>
> thanks!
> F 
>
> Il giorno domenica 26 marzo 2023 alle 20:35:30 UTC+1 Justin Israel ha 
> scritto:
>
>> Quick tip: If you are going to be sharing code longer than a couple 
>> lines, you might want to use something like a pastebin.com link, or 
>> similar, to retain the formatting and make it easier for others to read.
>>
>> Considering that most of the operations on your script apply to each 
>> individual selected object, it would make sense to run the entire thing 
>> under a for-loop. The current structure of the script is a bit confusing 
>> because it starts by operating on each item in the selection, builds up a 
>> new list, and then tries to correlate the new list back to the original 
>> selection. Another thing that is a bit harder to read at first is that you 
>> are storing the current selection as a list-of-lists. It would be more 
>> clear if you just store the single list.
>>
>> Check out this refactor of your script: https://pastebin.com/FcR1G32m
>>
>> I haven't tested it, so you can use it as a starting point. The idea is 
>> that you start with a main function that will get the current selection and 
>> do the up-front check. Then it will call do_constraint for each selected 
>> object. Within the do_constraint function, you only need to worry about 
>> that specific object. It is best if you avoid relying on manipulating the 
>> current selection to make your script work, and instead pass the target 
>> objects to the maya functions. 
>>
>> Let me know if you need clarification on any part of that script.
>>
>> Justin
>>
>>
>>  
>>
>> On Mon, Mar 27, 2023 at 4:28 AM SquashnStretch net  
>> wrote:
>>
>>> Hello again, update
>>> I managed to make it work a bit better but still not as I need to...
>>> thanks to your suggestions I'm able now to check channels and create 
>>> locators, constraints etc ...
>>> So I have a script that checks everything and if selections have both 
>>> rotations and translations available, it runs the function properly; if 
>>> there is something locked it stop and warn. And I can also have several 
>>> objects selected. 
>>>
>>> Now Im trying something more...
>>> I'd like the script to check what channel is available and act 
>>> accordingly, if there is only translation, it contstraint just the 
>>> translations, if only rotations available, it works only with that
>>> I ended up make it work by adding the skipRotation or skipTranslation 
>>> flag
>>> but it works only selecting one object at a time, if I select several 
>>> object in the scene where some has both T and R and some had just 
>>> translation and no rotations, it fails for all the object but work only for 
>>> the first one
>>>
>>> I guess I need a loop or another condition which checks inside my 
>>> 'userSel' variable but not sure how to do.
>>> Probably the problem is in the sel[0] call, which looks only on the 
>>> first obj selected, I tried to add a loop but it works only with 
>>> translation, seems that it doesnt connect the rotations
>>>
>>> anyways this is what I have so far which works only selecting obj 1 by 1:
>>>
>>> # this works fine BUT 1 obj x time
>>>
>>>
>>> import maya.cmds as mc
>>>
>>>
>>> LocList = []
>>>
>>> userSel = []
>>>
>>> len(userSel)
>>>
>>> sel = []
>>>
>>> sel = mc.ls(sl=1)
>>>
>>> userSel.append(sel)
>>>
>>>
>>> if len(sel) != 0:
>>>
>>> _has_trans = mc.getAttr (sel[0]+'.translate', settable=True)
>>>
>>> _has_rot = mc.getAttr (sel[0]+'.rotate', settable=True)
>>>
>>> 
>>>
>>> else :
>>>
>>> mc.warning('Please make a selection')
>>>
>>> 
>>>
>>> # check if there is rotations 
>>>
>>> # create a loc for ech selected obj and constraint to it
>>>
>>>
>>> if _has_rot ==1 and _has_trans ==1:
>>>
>>> for obj in sel:
>>>
>>> newLoc = mc.spaceLocator()
>>>
>>> newCon = mc.parentConstraint(obj, newLoc, mo=0)
>>>
>>> LocList.append(newLoc)
>>>
>>> 
>>>
>>> elif _has_rot ==0 and _has_trans ==1:
>>>
>>> for obj in sel:
>>>
>>> newLoc = mc.spaceLocator()
>>>
>>> newCon = mc.parentConstraint(obj, newLoc, mo=0, sr=['x', 'y', 
>>> 'z'])
>>>
>>> LocList.append(newLoc)
>>>
>>>  
>>>
>>> elif _has_rot ==1 and _has_trans ==0:
>>>
>>> for obj in sel:
>>>
>>> newLoc = mc.spaceLocator()
>>>
>>> newCon = mc.parentConstraint(obj, newLoc, mo=0, st=['x', 'y', 
>>> 'z'])
>>>
>>> LocList.append(newLoc)
>>>
>>>
>>> # select all new Loc 
>>>
>>> if len(sel) 

Re: [Maya-Python] check channels before constraints

2023-03-27 Thread SquashnStretch net
Thanks Justin for your time and advice.

Yes I see the script is super complicated, my original one was mainly 
inside separated functions but not using many loops actually, I need to 
practice that, seems the right way...

I'll try to work that way and im pretty sure I'll annoying you again ;)

thanks!
F 

Il giorno domenica 26 marzo 2023 alle 20:35:30 UTC+1 Justin Israel ha 
scritto:

> Quick tip: If you are going to be sharing code longer than a couple lines, 
> you might want to use something like a pastebin.com link, or similar, to 
> retain the formatting and make it easier for others to read.
>
> Considering that most of the operations on your script apply to each 
> individual selected object, it would make sense to run the entire thing 
> under a for-loop. The current structure of the script is a bit confusing 
> because it starts by operating on each item in the selection, builds up a 
> new list, and then tries to correlate the new list back to the original 
> selection. Another thing that is a bit harder to read at first is that you 
> are storing the current selection as a list-of-lists. It would be more 
> clear if you just store the single list.
>
> Check out this refactor of your script: https://pastebin.com/FcR1G32m
>
> I haven't tested it, so you can use it as a starting point. The idea is 
> that you start with a main function that will get the current selection and 
> do the up-front check. Then it will call do_constraint for each selected 
> object. Within the do_constraint function, you only need to worry about 
> that specific object. It is best if you avoid relying on manipulating the 
> current selection to make your script work, and instead pass the target 
> objects to the maya functions. 
>
> Let me know if you need clarification on any part of that script.
>
> Justin
>
>
>  
>
> On Mon, Mar 27, 2023 at 4:28 AM SquashnStretch net  
> wrote:
>
>> Hello again, update
>> I managed to make it work a bit better but still not as I need to...
>> thanks to your suggestions I'm able now to check channels and create 
>> locators, constraints etc ...
>> So I have a script that checks everything and if selections have both 
>> rotations and translations available, it runs the function properly; if 
>> there is something locked it stop and warn. And I can also have several 
>> objects selected. 
>>
>> Now Im trying something more...
>> I'd like the script to check what channel is available and act 
>> accordingly, if there is only translation, it contstraint just the 
>> translations, if only rotations available, it works only with that
>> I ended up make it work by adding the skipRotation or skipTranslation flag
>> but it works only selecting one object at a time, if I select several 
>> object in the scene where some has both T and R and some had just 
>> translation and no rotations, it fails for all the object but work only for 
>> the first one
>>
>> I guess I need a loop or another condition which checks inside my 
>> 'userSel' variable but not sure how to do.
>> Probably the problem is in the sel[0] call, which looks only on the first 
>> obj selected, I tried to add a loop but it works only with translation, 
>> seems that it doesnt connect the rotations
>>
>> anyways this is what I have so far which works only selecting obj 1 by 1:
>>
>> # this works fine BUT 1 obj x time
>>
>>
>> import maya.cmds as mc
>>
>>
>> LocList = []
>>
>> userSel = []
>>
>> len(userSel)
>>
>> sel = []
>>
>> sel = mc.ls(sl=1)
>>
>> userSel.append(sel)
>>
>>
>> if len(sel) != 0:
>>
>> _has_trans = mc.getAttr (sel[0]+'.translate', settable=True)
>>
>> _has_rot = mc.getAttr (sel[0]+'.rotate', settable=True)
>>
>> 
>>
>> else :
>>
>> mc.warning('Please make a selection')
>>
>> 
>>
>> # check if there is rotations 
>>
>> # create a loc for ech selected obj and constraint to it
>>
>>
>> if _has_rot ==1 and _has_trans ==1:
>>
>> for obj in sel:
>>
>> newLoc = mc.spaceLocator()
>>
>> newCon = mc.parentConstraint(obj, newLoc, mo=0)
>>
>> LocList.append(newLoc)
>>
>> 
>>
>> elif _has_rot ==0 and _has_trans ==1:
>>
>> for obj in sel:
>>
>> newLoc = mc.spaceLocator()
>>
>> newCon = mc.parentConstraint(obj, newLoc, mo=0, sr=['x', 'y', 
>> 'z'])
>>
>> LocList.append(newLoc)
>>
>>  
>>
>> elif _has_rot ==1 and _has_trans ==0:
>>
>> for obj in sel:
>>
>> newLoc = mc.spaceLocator()
>>
>> newCon = mc.parentConstraint(obj, newLoc, mo=0, st=['x', 'y', 
>> 'z'])
>>
>> LocList.append(newLoc)
>>
>>
>> # select all new Loc 
>>
>> if len(sel) != 0:
>>
>> for loc in LocList:
>>
>> mc.select (loc, add=1)
>>
>> start = mc.playbackOptions(q=True, min=True)
>>
>> end = mc.playbackOptions(q=True, max=True)
>>
>> mc.bakeResults(sm=1, sr=1, t=(start, end))
>>
>> #else:
>>
>> #mc.warning('Please make a selection')
>>
>>
>> for loc in LocList:
>>
>> mc.select (loc, add=1)
>>
>> mc.delete(cn=1)
>>
>>
>> # reverse 

Re: [Maya-Python] check channels before constraints

2023-03-26 Thread Justin Israel
Quick tip: If you are going to be sharing code longer than a couple lines,
you might want to use something like a pastebin.com link, or similar, to
retain the formatting and make it easier for others to read.

Considering that most of the operations on your script apply to each
individual selected object, it would make sense to run the entire thing
under a for-loop. The current structure of the script is a bit confusing
because it starts by operating on each item in the selection, builds up a
new list, and then tries to correlate the new list back to the original
selection. Another thing that is a bit harder to read at first is that you
are storing the current selection as a list-of-lists. It would be more
clear if you just store the single list.

Check out this refactor of your script: https://pastebin.com/FcR1G32m

I haven't tested it, so you can use it as a starting point. The idea is
that you start with a main function that will get the current selection and
do the up-front check. Then it will call do_constraint for each selected
object. Within the do_constraint function, you only need to worry about
that specific object. It is best if you avoid relying on manipulating the
current selection to make your script work, and instead pass the target
objects to the maya functions.

Let me know if you need clarification on any part of that script.

Justin




On Mon, Mar 27, 2023 at 4:28 AM SquashnStretch net <
squashnstret...@gmail.com> wrote:

> Hello again, update
> I managed to make it work a bit better but still not as I need to...
> thanks to your suggestions I'm able now to check channels and create
> locators, constraints etc ...
> So I have a script that checks everything and if selections have both
> rotations and translations available, it runs the function properly; if
> there is something locked it stop and warn. And I can also have several
> objects selected.
>
> Now Im trying something more...
> I'd like the script to check what channel is available and act
> accordingly, if there is only translation, it contstraint just the
> translations, if only rotations available, it works only with that
> I ended up make it work by adding the skipRotation or skipTranslation flag
> but it works only selecting one object at a time, if I select several
> object in the scene where some has both T and R and some had just
> translation and no rotations, it fails for all the object but work only for
> the first one
>
> I guess I need a loop or another condition which checks inside my
> 'userSel' variable but not sure how to do.
> Probably the problem is in the sel[0] call, which looks only on the first
> obj selected, I tried to add a loop but it works only with translation,
> seems that it doesnt connect the rotations
>
> anyways this is what I have so far which works only selecting obj 1 by 1:
>
> # this works fine BUT 1 obj x time
>
>
> import maya.cmds as mc
>
>
> LocList = []
>
> userSel = []
>
> len(userSel)
>
> sel = []
>
> sel = mc.ls(sl=1)
>
> userSel.append(sel)
>
>
> if len(sel) != 0:
>
> _has_trans = mc.getAttr (sel[0]+'.translate', settable=True)
>
> _has_rot = mc.getAttr (sel[0]+'.rotate', settable=True)
>
>
>
> else :
>
> mc.warning('Please make a selection')
>
>
>
> # check if there is rotations
>
> # create a loc for ech selected obj and constraint to it
>
>
> if _has_rot ==1 and _has_trans ==1:
>
> for obj in sel:
>
> newLoc = mc.spaceLocator()
>
> newCon = mc.parentConstraint(obj, newLoc, mo=0)
>
> LocList.append(newLoc)
>
>
>
> elif _has_rot ==0 and _has_trans ==1:
>
> for obj in sel:
>
> newLoc = mc.spaceLocator()
>
> newCon = mc.parentConstraint(obj, newLoc, mo=0, sr=['x', 'y', 'z'])
>
> LocList.append(newLoc)
>
>
>
> elif _has_rot ==1 and _has_trans ==0:
>
> for obj in sel:
>
> newLoc = mc.spaceLocator()
>
> newCon = mc.parentConstraint(obj, newLoc, mo=0, st=['x', 'y', 'z'])
>
> LocList.append(newLoc)
>
>
> # select all new Loc
>
> if len(sel) != 0:
>
> for loc in LocList:
>
> mc.select (loc, add=1)
>
> start = mc.playbackOptions(q=True, min=True)
>
> end = mc.playbackOptions(q=True, max=True)
>
> mc.bakeResults(sm=1, sr=1, t=(start, end))
>
> #else:
>
> #mc.warning('Please make a selection')
>
>
> for loc in LocList:
>
> mc.select (loc, add=1)
>
> mc.delete(cn=1)
>
>
> # reverse connection
>
> if _has_rot ==1 and _has_trans ==1:
>
> for idx, item in enumerate(LocList):
>
> ctl = item
>
> makeParentCons = mc.parentConstraint(ctl, userSel[0][idx], mo=True,
> w=1)
>
> elif _has_rot ==0 and _has_trans ==1:
>
> for idx, item in enumerate(LocList):
>
> ctl = item
>
> makeParentCons = mc.parentConstraint(ctl, userSel[0][idx], mo=True,
> w=1, sr=['x', 'y', 'z'])
>
> elif _has_rot ==1 and _has_trans ==0:
>
> for idx, item in enumerate(LocList):
>
> ctl = item
>
> makeParentCons = mc.parentConstraint(ctl, userSel[0][idx], mo=True,
> w=1, st=['x', 'y', 

Re: [Maya-Python] check channels before constraints

2023-03-26 Thread SquashnStretch net
Hello again, update
I managed to make it work a bit better but still not as I need to...
thanks to your suggestions I'm able now to check channels and create 
locators, constraints etc ...
So I have a script that checks everything and if selections have both 
rotations and translations available, it runs the function properly; if 
there is something locked it stop and warn. And I can also have several 
objects selected. 

Now Im trying something more...
I'd like the script to check what channel is available and act accordingly, 
if there is only translation, it contstraint just the translations, if only 
rotations available, it works only with that
I ended up make it work by adding the skipRotation or skipTranslation flag
but it works only selecting one object at a time, if I select several 
object in the scene where some has both T and R and some had just 
translation and no rotations, it fails for all the object but work only for 
the first one

I guess I need a loop or another condition which checks inside my 'userSel' 
variable but not sure how to do.
Probably the problem is in the sel[0] call, which looks only on the first 
obj selected, I tried to add a loop but it works only with translation, 
seems that it doesnt connect the rotations

anyways this is what I have so far which works only selecting obj 1 by 1:

# this works fine BUT 1 obj x time


import maya.cmds as mc


LocList = []

userSel = []

len(userSel)

sel = []

sel = mc.ls(sl=1)

userSel.append(sel)


if len(sel) != 0:

_has_trans = mc.getAttr (sel[0]+'.translate', settable=True)

_has_rot = mc.getAttr (sel[0]+'.rotate', settable=True)



else :

mc.warning('Please make a selection')



# check if there is rotations 

# create a loc for ech selected obj and constraint to it


if _has_rot ==1 and _has_trans ==1:

for obj in sel:

newLoc = mc.spaceLocator()

newCon = mc.parentConstraint(obj, newLoc, mo=0)

LocList.append(newLoc)



elif _has_rot ==0 and _has_trans ==1:

for obj in sel:

newLoc = mc.spaceLocator()

newCon = mc.parentConstraint(obj, newLoc, mo=0, sr=['x', 'y', 'z'])

LocList.append(newLoc)

 

elif _has_rot ==1 and _has_trans ==0:

for obj in sel:

newLoc = mc.spaceLocator()

newCon = mc.parentConstraint(obj, newLoc, mo=0, st=['x', 'y', 'z'])

LocList.append(newLoc)


# select all new Loc 

if len(sel) != 0:

for loc in LocList:

mc.select (loc, add=1)

start = mc.playbackOptions(q=True, min=True)

end = mc.playbackOptions(q=True, max=True)

mc.bakeResults(sm=1, sr=1, t=(start, end))

#else:

#mc.warning('Please make a selection')


for loc in LocList:

mc.select (loc, add=1)

mc.delete(cn=1)


# reverse connection

if _has_rot ==1 and _has_trans ==1:

for idx, item in enumerate(LocList):

ctl = item

makeParentCons = mc.parentConstraint(ctl, userSel[0][idx], mo=True, w=1)

elif _has_rot ==0 and _has_trans ==1:

for idx, item in enumerate(LocList):

ctl = item

makeParentCons = mc.parentConstraint(ctl, userSel[0][idx], mo=True, 
w=1, sr=['x', 'y', 'z'])

elif _has_rot ==1 and _has_trans ==0:

for idx, item in enumerate(LocList):

ctl = item

makeParentCons = mc.parentConstraint(ctl, userSel[0][idx], mo=True, 
w=1, st=['x', 'y', 'z'])
Il giorno sabato 25 marzo 2023 alle 22:18:19 UTC SquashnStretch net ha 
scritto:

> that's great Justin, yes with settable I can what is available, now I'll 
> try to check with a condition.I'll let you know if im able to :D
>
> thanks
> F
>
> Il giorno sabato 25 marzo 2023 alle 21:20:47 UTC Justin Israel ha scritto:
>
>> The getAttr function can tell you if an attribute is locked, or even 
>> "settable":
>>
>> https://help.autodesk.com/cloudhelp/2022/ENU/Maya-Tech-Docs/CommandsPython/getAttr.html
>>
>> On Sun, Mar 26, 2023 at 9:05 AM SquashnStretch net  
>> wrote:
>>
>>> Hello everyone,
>>> I wrote (with a lot of difficulties :) my first python script where I 
>>> wanted to create a locator constraint to any selected animated object, bake 
>>> the anim and reverse the constraint , basically to easily change space if 
>>> needed .
>>> the script works well but only if the animated object has translate and 
>>> rotate channels available, if they are locked or hidden, the script fails.
>>>
>>> Basically I dont know how to check first which channel is available and 
>>> based on the result, constraint only available channels
>>> hope it makes sense to you 
>>>
>>> this is what I have so far and thanks in advance for any helps!
>>> 
>>> import maya.cmds as mc
>>>
>>> #loc list
>>> userSel = []
>>> LocList = []
>>> len(userSel)
>>> # get the user selection
>>> sel = mc.ls(sl=1)
>>> userSel.append(sel)
>>>
>>> # create a loc for ech selected obj and constraint to it
>>> for obj in sel:
>>> if len(sel)!=0:
>>> newLoc = mc.spaceLocator()
>>> newCon = mc.parentConstraint(obj, newLoc, 

Re: [Maya-Python] check channels before constraints

2023-03-25 Thread SquashnStretch net
that's great Justin, yes with settable I can what is available, now I'll 
try to check with a condition.I'll let you know if im able to :D

thanks
F

Il giorno sabato 25 marzo 2023 alle 21:20:47 UTC Justin Israel ha scritto:

> The getAttr function can tell you if an attribute is locked, or even 
> "settable":
>
> https://help.autodesk.com/cloudhelp/2022/ENU/Maya-Tech-Docs/CommandsPython/getAttr.html
>
> On Sun, Mar 26, 2023 at 9:05 AM SquashnStretch net  
> wrote:
>
>> Hello everyone,
>> I wrote (with a lot of difficulties :) my first python script where I 
>> wanted to create a locator constraint to any selected animated object, bake 
>> the anim and reverse the constraint , basically to easily change space if 
>> needed .
>> the script works well but only if the animated object has translate and 
>> rotate channels available, if they are locked or hidden, the script fails.
>>
>> Basically I dont know how to check first which channel is available and 
>> based on the result, constraint only available channels
>> hope it makes sense to you 
>>
>> this is what I have so far and thanks in advance for any helps!
>> 
>> import maya.cmds as mc
>>
>> #loc list
>> userSel = []
>> LocList = []
>> len(userSel)
>> # get the user selection
>> sel = mc.ls(sl=1)
>> userSel.append(sel)
>>
>> # create a loc for ech selected obj and constraint to it
>> for obj in sel:
>> if len(sel)!=0:
>> newLoc = mc.spaceLocator()
>> newCon = mc.parentConstraint(obj, newLoc, mo=0)
>> LocList.append(newLoc)
>> else:
>> mc.warning('Please make a selection') 
>>
>> # select all new Loc 
>> if len(sel) != 0:
>> for loc in LocList:
>> mc.select (loc, add=1)
>> start = mc.playbackOptions(q=True, min=True)
>> end = mc.playbackOptions(q=True, max=True)
>> mc.bakeResults(sm=1, sr=1, t=(start, end))
>> else:
>> mc.warning('Please make a selection')
>>
>> for loc in LocList:
>> mc.select (loc, add=1)
>> mc.delete(cn=1)
>>
>> # reverse connection
>> for idx, item in enumerate(LocList):
>> ctl = item
>> makeParentCons = mc.parentConstraint(ctl, userSel[0][idx], mo=True, w=1)
>>
>> -- 
>> 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/5c5853a4-ec08-40fa-bfea-2bb39ee333d5n%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
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/80dfe6e0-0e63-4429-90c7-c61946007841n%40googlegroups.com.


Re: [Maya-Python] check channels before constraints

2023-03-25 Thread Justin Israel
The getAttr function can tell you if an attribute is locked, or even
"settable":
https://help.autodesk.com/cloudhelp/2022/ENU/Maya-Tech-Docs/CommandsPython/getAttr.html

On Sun, Mar 26, 2023 at 9:05 AM SquashnStretch net <
squashnstret...@gmail.com> wrote:

> Hello everyone,
> I wrote (with a lot of difficulties :) my first python script where I
> wanted to create a locator constraint to any selected animated object, bake
> the anim and reverse the constraint , basically to easily change space if
> needed .
> the script works well but only if the animated object has translate and
> rotate channels available, if they are locked or hidden, the script fails.
>
> Basically I dont know how to check first which channel is available and
> based on the result, constraint only available channels
> hope it makes sense to you 
>
> this is what I have so far and thanks in advance for any helps!
> 
> import maya.cmds as mc
>
> #loc list
> userSel = []
> LocList = []
> len(userSel)
> # get the user selection
> sel = mc.ls(sl=1)
> userSel.append(sel)
>
> # create a loc for ech selected obj and constraint to it
> for obj in sel:
> if len(sel)!=0:
> newLoc = mc.spaceLocator()
> newCon = mc.parentConstraint(obj, newLoc, mo=0)
> LocList.append(newLoc)
> else:
> mc.warning('Please make a selection')
>
> # select all new Loc
> if len(sel) != 0:
> for loc in LocList:
> mc.select (loc, add=1)
> start = mc.playbackOptions(q=True, min=True)
> end = mc.playbackOptions(q=True, max=True)
> mc.bakeResults(sm=1, sr=1, t=(start, end))
> else:
> mc.warning('Please make a selection')
>
> for loc in LocList:
> mc.select (loc, add=1)
> mc.delete(cn=1)
>
> # reverse connection
> for idx, item in enumerate(LocList):
> ctl = item
> makeParentCons = mc.parentConstraint(ctl, userSel[0][idx], mo=True, w=1)
>
> --
> 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/5c5853a4-ec08-40fa-bfea-2bb39ee333d5n%40googlegroups.com
> 
> .
>

-- 
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/CAPGFgA0ozdWmGhWeu3RfAt%2Bk-gWeRvBhfgNs%2BM7iOmutWD%2Besw%40mail.gmail.com.