How to select and delete only child joints in python or mel?

How to select and delete only child joints in python or mel?

amaterasu-qbb
Collaborator Collaborator
1,763 Views
4 Replies
Message 1 of 5

How to select and delete only child joints in python or mel?

amaterasu-qbb
Collaborator
Collaborator

I started this question to write a useful command for this. Question by Questioner 

About how to select and delete only child joints.
I answered.

 

use the removeJoint command. 

import maya.cmds as cmds

cmds.removeJoint()

If you write a complex command, you could select all the joints and run it to select and remove only the child joints, but I can't think of a command like that. I am sorry. 

 

and I searched.

Code source: https://tech-art.online/maya-python-recursive/

from maya import cmds

# 階層構造の末端ジョイントを取得する再帰関数
def getEndJoints(root):
    end_joints = []
    children = cmds.listRelatives(root, c=True, type="joint")
    if children is None:
        return [root]
    for child in children:
        end_joints.extend(getEndJoints(child))
    return  end_joints

end_joints = getEndJoints("root")
cmds.select(end_joints)

The command wanted is,

  1. Duplicate and erase joints of children with the same name
  2. Ability to find and remove terminal child joints from long joints

The code of the command source must have the root name as root.
or else I can select children's joints.

image.png

 

Thank you for help.

0 Likes
Accepted solutions (1)
1,764 Views
4 Replies
Replies (4)
Message 2 of 5

Kahylan
Advisor
Advisor
Accepted solution

Hi!

 

I did it slightly different, since I'm not too fond of functions calling themselves. But this should give you the result you want:

import maya.cmds as mc

def deleteEndJoints(roots = [], delete = False):
    """
    This function selects and deletes all the end joints of a given root joint.
    kwargs: roots (list of strings, default empty List), if roots is empty selection will be used,
    delete (boolean, default False), if delete is set to True endjoints will be deleted.
    """

    if roots == []:
        roots = mc.ls(sl= True, l = True)

    mc.select(cl = True)
        
    for r in roots:
        children = mc.listRelatives(r, ad= True, typ = "joint", pa = True) or []
        if children == []:
            mc.select(r, add = True)
            
        for c in children:
            if mc.listRelatives(c, ad= True) == None:
                mc.select(c, add= True)
   
    if delete == True:
        mc.delete()
 
deleteEndJoints()

You have two keyword arguments which can be used like this:

# if you want to delete the end joints of current selection
deleteEndJoints(delete= True)

#if you want to provide the root by name
#note that it needs to be provided as a list even if it is a single root
deleteEndJoints(roots = ["root"])
deleteEndJoints(roots = ["bunny_root", "fox_root"])

#if you want to delete end joints of provided roots
deleteEndJoints(roots = ["bunny_root", "fox_root"], delete = True)

 

I hope it helps!

Message 3 of 5

amaterasu-qbb
Collaborator
Collaborator
Hello. Sorry for the late reply. you are a genius! This is exactly the code that the questioner and I needed. Thank you! Can I also quote you on the Japanese forum? to help people.

It was necessary to create def first. I didn't even think about it. I will study more by looking at the Technical Documentation. Thank you very much.
Message 4 of 5

Kahylan
Advisor
Advisor

Happy to help!
Sure you can quote me on the japanese Forum 🙂

Message 5 of 5

amaterasu-qbb
Collaborator
Collaborator

I have definitely quoted you. Please let me know if there is anything wrong. The link is below.

https://forums.autodesk.com/t5/maya-ri-ben-yu/zijointodakewo-xiaosu-fang-fawo-jiaoetekudasai/m-p/116... 

I want to spread your power as much as possible. Thank you very much.😊