I have a brief prior knowledge of the Python language - at the start of the academic year I took an 8 week course run by Code First Girls on the fundamentals of Python. During this course I worked with a recipe API to create a small program which returned recipes filtered by values defined by the user. I've been aware of the ability to execute code within Maya for a while, however I hadn't actually tried it out yet - I was intimidated by the script editor and didn't know where to start.
Searching YouTube for a tutorial to start me off, something was apparent - the majority of videos outlining Maya-Python were from at least 8 years ago, so they were outdated to me now. Yes, the functionality of some of this code could've still worked, but with how fast technology changes I didn't want to hedge my bets on a video that could just further confuse me.
I opted for cgside's video "Bottom pivot python script in Maya" (cgside, 2022). The fact the pivot isn't automatically set to the bottom of the object in Maya is something that drives me crazy when opening the application, so I was excited to learn how to negate this.
In the video, cgside opens up the script editor and starts writing code in the script editor and I have no clue why he's writing the code he is and what it means. This won't do for this module - I'm not going to blindly copy the code from a tutorial and then claim I've learnt something because this wouldn't be true. I would also be cheating myself out of knowledge, so it's an ultra no-go.
So, I hopped over to the official Maya Python documentation (Autodesk, 2023a) to try and decipher what witchcraft is going on here. In Maya, there are 2 different languages that can be used for scripting - MEL (Maya Embedded Language) and Python. I'm choosing to execute Python because I have prior experience with it and it's more general use than the bespoke MEL. Commands can be entered directly into the Python tab, or an existing .py (python) file can be loaded into the tab. I see that in the tutorial, Python is being typed directly into the script editor for execution. Great, that clears some stuff up.
Searching through the documentation I finally found the Maya python command documentation (Autodesk, 2023b) - brilliant! This lists all of the commands you can call within your Python script to interact with Maya. Therefore I spent the rest of the video typing up code and line-by-line referencing the command documentation to gain an understanding of what was going on.
Here's my final piece of code created from the video, which sets the pivot to the bottom centre of the object and aligns the object onto the Y axis.
def bottomPivot():
# string holding all selected objects
# ls = all objs in scene
# sl = selected bool
selection = cmds.ls(sl=1)
if selection == []:
cmds.warning("selection is empty")
for obj in selection:
# cmds.scale(10, 10, 10, r = 1)
# cmds.setAttr([obj + '.scaleZ'] 10)
# xform = sets transformation nodes
# cp = centre pivot bool
cmds.xform(obj, cp=1)
# bb = bounding box bool
# ws = worldspace bool
# q query mode bool
bounding_box = cmds.xform(obj, q=1, bb=1, ws=1)
# setting bounding box values to separate variables
# y_min = bottom middle obj coord
x_min, y_min, z_min, x_max, y_max, z_max = bounding_box
# a = absolute trans bool
# scale and rotate object pivot to y_min coordinate
# move y bool
cmds.move(y_min, [obj + ".scalePivot", obj + ".rotatePivot"], y=1, a=1)
# sets y coord of obj to 0
cmds.move(y_min * -1, obj, r=1, y=1)
bottomPivot()
Around half of this code isn't actual code, instead it's comments that I wrote at the time to help me understand what is going on with each line of code.
I will also add here that Chad Vernon's page "Python Scripting for Maya Artists" (Vernon, 2023) was a godsend, as it helped me understand how to use the documentation to my advantage and recapped me on the basics of coding.
I start off by defining a function called bottomPivot() which means when bottomPivot() is called, all of my code is executed.
cmds. is used to call maya python commands within the script. All of the items within the scene which have selected (sl) set to true (1) are written to the variable 'selection'. If the variable selection is empty, a warning message is output that says "selection is empty".
selection = cmds.ls(sl=1)
if selection == []:
cmds.warning("selection is empty")
I then open a for loop which loops through each selected item in the scene.
for obj in selection:
The xform command is called which relates to transformation of the object. 'cp' stands for centre pivot, which is set to true.
cmds.xform(obj, cp=1)
The xform command is called again but in query mode (q = 1), meaning you're retrieving information from Maya. The worldspace (ws = true) coordinates of the bounding box (bb) are written to the variable bounding_box.
bounding_box = cmds.xform(obj, q=1, bb=1, ws=1)
The coordinates within bounding_box are extracted to independent variables that respectively hold the min and max xyz values of the bounding box.
x_min, y_min, z_min, x_max, y_max, z_max = bounding_box
The move command is called which has quite a few parameters. y = 1 means we're moving the object in the y axis, and a = 1 means we're performing an absolute transformation rather than a relative one. This scales and rotates the pivot to the y_min value, which is the bottom of the object/bounding box.
cmds.move(y_min, [obj + ".scalePivot", obj + ".rotatePivot"], y=1, a=1)
Almost there, the move command is called again which moves the object in the y axis (y = 1) with a relative transformation (r = 1) by the current relative y coordinate multiplied by -1. This sets the object on the y axis since if the y coordinate is currently 6, it will be moved by -6, and 6 + -6 = 0.
cmds.move(y_min * -1, obj, r=1, y=1)
Finally, we call the bottomPivot() function which means all the above code is executed when the script is run.
bottomPivot()
And the script is finished! This is what it looks like in practice - you can see I docked the script in the 'custom' tab for easy access to incorporate it into my workflow.
It was intimidating starting with scripting in Maya, but now I'm a bit more used to the interface I feel more comfortable using it.
Autodesk (2023a) Maya Help | Python, Python. Available at: https://help.autodesk.com/view/MAYAUL/2024/ENU/?guid=GUID-703B18A2-89E5-48A8-988A-1ED815D5566F (Accessed: 7 December 2023)
Autodesk (2023b) Maya Python Commands Index. Available at: https://help.autodesk.com/view/MAYAUL/2024/ENU/?guid=__CommandsPython_index_html (Accessed: 7 December 2023)..
cgside (2022) Bottom pivot python script in Maya, YouTube. Available at: https://www.youtube.com/watch?v=vkx5IvXeNkQ&list=PL42gZMHWYcvjs5SfnoV4X6ocuSnFQsLwS&index=82&t=137s (7 December 2023).
Vernon, C. (2023) Python scripting for maya artists, Python Scripting for Maya Artists | Chad Vernon. Available at: https://www.chadvernon.com/python-scripting-for-maya-artists/#python-in-maya (Accessed: 7 December 2023).
Comentários