python script

python script

flyssss
Contributor Contributor
2,420 Views
10 Replies
Message 1 of 11

python script

flyssss
Contributor
Contributor

Hello everybody I wrote a script that allows me to smart bake and kill constraints to a selected object in the scene. It works as intended, the only problem is that it doesnt remove the blendParent label node in the channel box.

this is the script:

#Bake and kill constraint
def FD_simple_SmartBake():
    #loc list
    userSel = []
    len(userSel)
    # get the user selection
    sel = mc.ls(sl=1)
    userSel.append(sel)
    
    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 obj in sel:
        mc.delete(cn=1)

FD_simple_SmartBake()    

if I just run the code to kill the constraint mc.delete(cn=1) without all the function, it works fine and remove the blendParent label from the channel box as well.... Something stupid I think but i dont understand why

Any help would be greatly appreciated

Thanks

Flys

0 Likes
Accepted solutions (1)
2,421 Views
10 Replies
Replies (10)
Message 2 of 11

Kahylan
Advisor
Advisor

Hi!

 

I'm not sure what the problem is, since it worked perfectly fine for me... It might depend on what kinds of values get blended, or if there are multiple Animation layers or something.

But if that is the only problem, you could also consider to forcefully delete the pairblend node on your selection.

 

for obj in sel:
   pb = mc.listConnections(s = True, d = True, type = "pairBlend")
   if pb != None:
       mc.delete(pb)

 

 

I hope this helps!

Message 3 of 11

flyssss
Contributor
Contributor

Hello Kahylan, 

thanks a lot for your reply.

 

Does it work for you ? the blendParent channel disappears from the channel box ? It doesn't do that for me ... the script works, the constraint actually is deleted but the channel is still there even if disabled ..

I think because executing first the bake, it puts keys on the bendParent node, so it doesn't delete it ....

Btw I'll try to delete as you suggested, I'll let you know

 

Thanks again!

Flys

0 Likes
Message 4 of 11

flyssss
Contributor
Contributor

but unfortunately also your tip doesnt work for me, I guess for the same problem the bake is performed first ... 

even because he problem is not the connection itself but only the blendParent name in the channel box...

 

thanks 😉

F

 

0 Likes
Message 5 of 11

Kahylan
Advisor
Advisor

could you upload a Screenshot of the connections to your object in the Node Editor before and after you run your script?

0 Likes
Message 6 of 11

flyssss
Contributor
Contributor

Sure, here we go.

The first img is before running the script: the constraint still active and of course the blendParent visible in the CB

flyssss_0-1651841636764.png

 

the 2nd img is after executing the script: the constraint is gone as expected but the label blendParent still visible in the CB even if inactive...

flyssss_1-1651841664453.png

thanks again for your help!

F

0 Likes
Message 7 of 11

flyssss
Contributor
Contributor

probably a solution could be to just delete the node blendParent somehow .... I just found a way to hide it but I want to totally delete it instead...

any suggestions would be greatly appreciated

 

thanks

0 Likes
Message 8 of 11

Kahylan
Advisor
Advisor

Oh, you were talking about a driver attribute, I thought you meant the pairBlend Node that is created when you have animation and constraints on the same attibute.

And this dissappears when you just run the delete constraint command? Thats strange, the Attribute staying is acctually the expected behaviour since it is custom.

I wouldn't really put deletion of an Attribute it into a tool to delete constraints, unless the whole setup can be created with the same script as well. And even then, I'd query if it exists first so you don't error out on a constraint that doesn't have that specific setup.

 

for obj in sel:
    if mc.attributeQuery("blendParent", n = obj, ex = True):
        mc.deleteAttr(obj, at = "blendParent")

 

I hope this helps!

 

 

0 Likes
Message 9 of 11

flyssss
Contributor
Contributor

Hello again and really thanks for your time.

 

I'll try to explain better what the problem is. 

Please refer to the previous 2 images....

 

In the example I have 1 pSphere constraint to a locator which drives the animation, image 1,

what I want to do is to bake the animation back to the sphere and kill the constraint to the locator, which the script does perfectly but the annoying thing is that I still see the blendParent attribute in the channel box, even if it is no more active(if I move the loc the sphere doesnt follow as expected). Image 2.

 

For instance if, before running the script, I just execute mc.delete(cn=1) to kill the constraint, it kills the constraint and remove the blendparent1 attribute from the channel box as well, this is what I would like to achieve in my main script.

 

From what I understood, the attribute blenparent doesn't go aways because the bake simulations happens before the mc.delete(cn=1 call...

 

hope this makes sense to you and thanks again

F

 

 

0 Likes
Message 10 of 11

Kahylan
Advisor
Advisor
Accepted solution

I understand, as I said, you can just delete blendParent1 using code after. The thing is, I would still use an Attribute Query command to detect if it is even there, because blendParent1 only gets created if there is a Constraint and Animation on the same object, so if that isn't the case the code will have problems if you dont do that. But this should work:

import maya.cmds as mc

#Bake and kill constraint
def FD_simple_SmartBake():
    #loc list
    userSel = []
    len(userSel)
    # get the user selection
    sel = mc.ls(sl=1)
    userSel.append(sel)
    
    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 obj in sel:
        for obj in sel:
            if mc.attributeQuery("blendParent1", n = obj, ex = True):
                mc.deleteAttr(obj, at = "blendParent1")
        mc.delete(cn=1)

FD_simple_SmartBake()   
0 Likes
Message 11 of 11

flyssss
Contributor
Contributor

you are a genius! this is working great!

 

Thanks Kahylan

0 Likes