check channels before constraints

49 views
Skip to first unread message

SquashnStretch net

unread,
Mar 25, 2023, 4:05:23 PM3/25/23
to Python Programming for Autodesk Maya
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)

Justin Israel

unread,
Mar 25, 2023, 5:20:47 PM3/25/23
to python_in...@googlegroups.com
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

--
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.

SquashnStretch net

unread,
Mar 25, 2023, 6:18:19 PM3/25/23
to Python Programming for Autodesk Maya
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

SquashnStretch net

unread,
Mar 26, 2023, 11:28:17 AM3/26/23
to Python Programming for Autodesk Maya
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'])

Justin Israel

unread,
Mar 26, 2023, 3:35:30 PM3/26/23
to python_in...@googlegroups.com
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


 

SquashnStretch net

unread,
Mar 27, 2023, 6:56:35 PM3/27/23
to Python Programming for Autodesk Maya
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!


SquashnStretch net

unread,
Mar 27, 2023, 7:21:49 PM3/27/23
to Python Programming for Autodesk Maya
Justin I ddnt notice that you rewrote my script! it is so ..beautifull that way! thanks, a lot to learn from it!

Justin Israel

unread,
Mar 27, 2023, 8:12:40 PM3/27/23
to python_in...@googlegroups.com


On Tue, 28 Mar 2023, 12:21 pm SquashnStretch net, <squashn...@gmail.com> 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. 

Reply all
Reply to author
Forward
0 new messages