Friday, 8 May 2026

insert string before string, Python script for Maya

generated through ChatGPT.. mel solutions seemed broken.
This will make a popup window, you put in your desired string and the one you want to put it in front of. Whatever you have selected gets processed

# =======================================================
# Maya Python Script
# Insert String BEFORE Another String
# on Multiple Selected Transform Nodes
# =======================================================

import maya.cmds as cmds


# -------------------------------------------------------
# Rename Logic
# -------------------------------------------------------
def insert_before_rename_execute(*args):

    # Get selected transforms
    sel = cmds.ls(selection=True, type="transform")

    if not sel:
        cmds.warning("Please select one or more transform nodes.")
        return

    # Get UI values
    insert_str = cmds.textField(
        "insertStringField",
        q=True,
        text=True
    )

    before_str = cmds.textField(
        "beforeStringField",
        q=True,
        text=True
    )

    # Validation
    if not before_str:
        cmds.warning("Please enter a target string.")
        return

    renamed_count = 0

    # Process all selected objects
    for obj in sel:

        # Skip objects that don't contain target string
        if before_str not in obj:
            print(
                'Skipped "{}" (target string not found)'.format(obj)
            )
            continue

        # Find insertion point
        index = obj.find(before_str)

        # Build new name
        new_name = (
            obj[:index] +
            insert_str +
            obj[index:]
        )

        # Rename object
        renamed = cmds.rename(obj, new_name)

        print(
            'Renamed: {} -> {}'.format(obj, renamed)
        )

        renamed_count += 1

    print(
        "\nFinished. {} object(s) renamed.\n".format(
            renamed_count
        )
    )


# -------------------------------------------------------
# UI
# -------------------------------------------------------
def insert_before_tool_ui():

    window_name = "insertBeforeToolWin"

    # Delete existing window
    if cmds.window(window_name, exists=True):
        cmds.deleteUI(window_name)

    # Create window
    cmds.window(
        window_name,
        title="Insert String Before",
        widthHeight=(400, 180)
    )

    cmds.columnLayout(
        adjustableColumn=True,
        rowSpacing=10
    )

    cmds.text(
        label="Rename Selected Transform Nodes"
    )

    cmds.separator(style="in")

    # Insert string
    cmds.text(
        label="String To Insert:",
        align="left"
    )

    cmds.textField("insertStringField")

    # Before string
    cmds.text(
        label="Insert BEFORE This String:",
        align="left"
    )

    cmds.textField("beforeStringField")

    cmds.separator(style="in")

    # Execute button
    cmds.button(
        label="Rename Selected Objects",
        height=40,
        command=insert_before_rename_execute
    )

    cmds.showWindow(window_name)


# -------------------------------------------------------
# Launch
# -------------------------------------------------------
insert_before_tool_ui()

Monday, 3 April 2023

Thursday, 16 March 2023

padding numbers in maya - mel

 python - Number/Version Paddings in Maya - Stack Overflow
don't know why I've never really had to do this before heh. Watch out for the backticks. The python bit really wants the ' and not the `, but then the mel bit really wants the `. sigh.
Also hilarious that you have to use a python function for this.

$padding = 3;
$num = 5;
string $pad = `python ("'%0"+$padding+"d' % "+$num)`;
// Results is: 005

Thursday, 9 March 2023

selecting with wildcards

Don't know why I always forget this, but here's a mel script to select things with wildcards...
select -r "ballShape*";

Monday, 27 February 2023

deleting uv sets maya

 Maya 2020 UV Sets infuriating - need help : Maya (reddit.com)

sometimes you need to kill uv maps, you have to move the one you want to delete so it isn't at the top of the list - it can only be moved if you use the UV Toolkit mode - right at the bottom of the tabs there'll be a UV Sets rollout that lets you do so & delete the offending sets. VERY SHITTY.



Friday, 24 February 2023

python way of making the bones based on cvs

from cgtalk forum poster - Darksuit

CGTalk | Auto-create joint chain along spline? (cgsociety.org)

just in case - 

changing the orientation on the joints should be easy enough look to the script for

this line

cmds.joint(currentJoint,e=True,zso=True,oj="xyz")

change the oj=“xyz” to the orientation you need… more than likely you are looking for “xzy”.

The import maya.cmds as cmds line imports the MEL script commands to python. There are some differences, but there is a lot of good documentation, and a number of references out there.

as for reorienting the joints. change the following line i=1 in the last block to i=0 that will start the orientation at the base joint instead of the second joint. A lot of times I leave the base joint aligned to world for my own uses.




 import maya.cmds as cmds


# array of selected objects

sel = cmds.ls( selection=True )


# name of the curve to convert

lastSelect = int(len(sel)) -1

curveSel = sel[lastSelect]


# print lastSelect

#print curveSel


#find the number of CVs (Degree + Spans)

numCVs = int(cmds.getAttr (curveSel+'.degree'))+ (cmds.getAttr (curveSel+'.spans'))

#print numCVs


# selection must be clear

cmds.select(clear=True)


#For each CV in the Curve create a joint.


i=0

while 1:

    if not ( i < numCVs ):

        break

    cvPosition = (cmds.getAttr (curveSel+'.cv['+(str(i))+']'))

    if (i == 0):

    cmds.joint(n=curveSel+'_jointRoot',p = cvPosition[0])

    if (i == (numCVs-1)):

    cmds.joint(n=curveSel+'_jointEnd', p = cvPosition[0])

    if(i>0)and(i != numCVs-1):

    cmds.joint(n=curveSel+'_joint_'+(str(i)),p = cvPosition[0])

    i+=1

 

# reorient the joints    

i=1

while 1:

if not (i < numCVs-1):

break

#find the name of the current Joint

currentJoint = (curveSel+'_joint_'+(str(i)))

cmds.joint(currentJoint,e=True,zso=True,oj="xyz")

i+=1


joints along curve script mel

 nnJointsOnCurve for Maya - Free Character Scripts / Plugins Downloads for Maya (highend3d.com)

JUST in case this vanishes... puts joints on a curve. v useful and simple

/////////////////////////////// nnJointsOnCurve /////////////////////////////////


//                              Scripted by -Nilesh Jadhav

//                                                         27-07-2010

/////////////////////////////////////////////////////////////////////////////////////

// -------------- This script generates joint chain on selected Curve ---------------

//////////////////////////////////////////////////////////////////////////////////////

//

////////////////////////// UI Designing for nnJointOnCurve /////////////////////////


if (`window -exists curveJoint`)

deleteUI "curveJoint";

    

    window -t "nnJointsOnCurve" -w 200 -h 200 -mnb 1 -mxb 0 -s 1 curveJoint;


columnLayout -adj 1 -columnAttach "both" 1 -rowSpacing 10 -columnWidth 250;


    intFieldGrp -l"Number of Joints" numberInt;

    checkBoxGrp -l"Rebuild the Curve" rebuildCheck;

    text -l "Select the Curve and press the Button" -fn "smallBoldLabelFont" ;

    button -l"<< Add Joint Chain on Curve >>" -h 30 -c ("nnJointsOnCurve()") goButton;

    text -l"Scripted by - Nilesh Jadhav" -bgc 1 1 1;


showWindow curveJoint;




/////////////////////////////////////  Procedure ////////////////////////////////////////////////


global proc nnJointsOnCurve()

{


    string $sel []=`ls -sl`;

    string $curve =$sel [0];

    string $check =`checkBoxGrp -q -v1 rebuildCheck`;

if ($check==1 )

{

rebuildCurve -rt 0 -s 25;


}

else

{

}

    string $lcr []=`spaceLocator`;

    string $loc =$lcr [0];


    string $path =`pathAnimation  -stu 1 -etu 1000 -f on  $curve $loc `; 

    int $numbers []=`intFieldGrp -q -v numberInt`;


    float $locPos []=`xform -q -ws  -t $loc`;

    int $divFacter =1000 / $numbers [0];


select -cl;

$i=`currentTime -q`;

            for ($i=1; $i<1000; $i=$i+$divFacter) //--------- Loop to make joint chain

                {




        currentTime $i;

        float $lcPos []=`xform -q  -ws -t locator1`;


        string $joints =`joint -a -p $lcPos [0] $lcPos [1] $lcPos [2]`;


}

string $selJoint []=`ls -sl`;

string $jnt =$selJoint [0]; 



    $i =1;

            for ( $i=1; $i<$numbers[0]+1; $i++) //--------- loop for getting 1st joint

                {

                    pickWalk -d up;

                }    

    

    joint -e -oj xyz -secondaryAxisOrient yup -ch -zso;



delete $path;

delete $loc;

currentTime -e 1;

}