Before you read anything below, read this list first
import maya.cmds as cmds; cmds.sphere()
import maya.mel as mel; skin = mel.eval('findRelatedSkinCluster "'+geoName+'"')
import maya.OpenMaya as om import maya.OpenMayaAnim as oma aim_dir = cmds.xform(myLoc, q=1, t=1, ws=1) aim_vec = om.MVector(*aim_dir) rot_quaternion = myObj_axis_vec.rotateTo(aim_vec) # myObj_axis_vec is set as x axis direction myObj_fnTransform.rotateBy(rot_quaternion) # myObj_fnTransform is set as the transform api function object
import maya.api.OpenMaya as om2 aim_dir = cmds.xform(myLoc, q=1, t=1, ws=1) aim_vec = om2.MVector(aim_dir) # can take python list automatically # also, no need MScriptUtil for pointer to value stuff # can directly return value like normal python # also, sometimes faster than api 1.0
color1R = nAttr.create( “color1R”, “c1r”,MFnNumericData::kFloat); color1G = nAttr.create( “color1G”, “c1g”,MFnNumericData::kFloat); color1B = nAttr.create( “color1B”, “c1b”,MFnNumericData::kFloat); color1 = nAttr.create( “Sides”, “c1”, color1R, color1G,color1B); nAttr.setStorable(true); nAttr.setUsedAsColor(true); nAttr.setDefault(1.0f, 1.0f, 1.0f); addAttribute(color1R); addAttribute(color1G); addAttribute(color1B); addAttribute(color1); attributeAffects(color1R, color1); attributeAffects(color1G, color1); attributeAffects(color1B, color1); attributeAffects(color1, aOutColor);
note: shader=material
shadingNode -asShader blinn; // same as: createNode blinn; connectAttr blinn1.message defaultShaderList1.shaders;
sets -renderable true -noSurfaceShader true -empty -name blinn1SG; connectAttr -f blinn1.outColor blinn1SG.surfaceShader;
shadingNode -asShader lightFog; sets -renderable true -noSurfaceShader true -empty -name lightFog1SG; connectAttr -f lightFog1.outColor lightFog1SG.volumeShader;
shadingNode -asShader displacementShader; sets -renderable true -noSurfaceShader true -empty -name displacementShader1SG; connectAttr -f displacementShader1.displacement displacementShader1SG.displacementShader;
shadingNode -asTexture checker; // means: 1. createNode checker; 2. add to multilister // if with option checked "With New Texture Placement" shadingNode -asUtility place2dTexture; connectAttr place2dTexture1.outUV checker1.uv; // if with option checked "As Projection" shadingNode -asTexture projection; shadingNode -asUtility place3dTexture; connectAttr place3dTexture1.wim[0] projection1.pm; connectAttr checker1.outColor projection1.image; // else with option checked "as stencil" shadingNode -asTexture stencil; shadingNode -asUtility place2dTexture; connectAttr place2dTexture2.outUV stencil1.uv; connectAttr checker1.outColor stencil1.image; // ------ 3d tex shadingNode -asUtility place3dTexture; connectAttr place3dTexture2.wim[0] brownian1.pm;
virtual MPxGeometryIterator* geometryIteratorSetup(MObjectArray&, MObject&, bool ); virtual bool acceptsGeometryIterator( bool writeable ); virtual bool acceptsGeometryIterator( MObject&,bool, bool );
set MAYA_MODULE_PATH=Z:/example_external_folder/maya/modules/2014;%MAYA_MODULE_PATH% set MAYA_SHELF_PATH=Z:/example_external_folder/maya/shelves/2014;%MAYA_SHELF_PATH% set MAYA_PRESET_PATH=Z:/example_external_folder/maya/presets/2014;%MAYA_PRESET_PATH% set MAYA_PLUG_IN_PATH=Z:/example_external_folder/maya/plug-ins/2014;%MAYA_PLUG_IN_PATH% set MAYA_PLUG_IN_LIST=fbxmaya.mll;objExport.mll;%MAYA_PLUG_IN_LIST% set MAYA_SCRIPT_PATH=Z:/example_external_folder/maya/scripts/2014;%MAYA_SCRIPT_PATH% set MAYA_SCRIPT_PATH=Z:/example_external_folder/maya/scripts/2014/custom2;%MAYA_SCRIPT_PATH% set MAYA_SCRIPT_PATH=Z:/example_external_folder/maya/scripts/2014/custom3;%MAYA_SCRIPT_PATH% set PYTHONPATH=Z:/example_external_folder/maya/Python/2014/Lib;%PYTHONPATH% set PYTHONPATH=Z:/example_external_folder/maya/tools/2014;%PYTHONPATH% "C:\path_to_maya\maya.exe" -c "python(\"execfile('Z:/path_to_custom_script_to_run_on_start/custom.py')\")"
maya flags
-v | version number |
---|---|
-batch | start in batch mode |
-prompt | non-gui mode |
-proj [dir] | start in define project |
-command [mel] | start with defined mel command |
-script [mel] | start with mel script file |
-recover | recovers last file |
-render [file] | render defined file |
-archive [file] | list required or related files to archive a scene |
-help | display flags help |
-noAutoloadPlugins | don't load any plugin |
cmds.displayColor('locator', 17, c=1, dormant=1) # 17, gold color # active=1 if for active color
allWindows = cmds.lsUI(windows=1) toClose_win_list = [x for x in allWindows if x not in ['CommandWindow','ConsoleWindow','MayaWindow','ColorEditor'] ] for each in toClose_win_list : cmsd.deleteUI(each)
cmds.selectType(allObjects=0) # query by cmds.selectType(allObjects=1, q=1) cmds.selectType(locator=1)
cmds.select(objList) cmds.createDisplayLayer(name="objList_layer", number=1, nr=1) # create layer from selection cmds.setAttr(layer + ".displayType", 2) # 0 normal, 1 template, 2 reference
cmds.displaySmoothness(['guide_R_path', 'guide_L_path'],divisionsU=3, divisionsV=3, pointsWire=16, pointsShaded=4, polygonObject=3)
mel name | python name | description |
---|---|---|
$gChannelBoxName | mainChannelBox | channel box |
$gCommandReporter | cmdScrollFieldReporter1 | script editor result panel |
$gMainWindow | MayaWindow | maya main window |
graph1HyperShadeEd | graph1HyperShadeEd | material hypershade |
nodeEditorPanel2NodeEditorEd | nodeEditorPanel2NodeEditorEd | node editor |
cmds.getPanel(wf=1)
cmds.getPanel(up=1)
cmds.lsUI(type='tabLayout') # list all maya ui tab layout object cmds.lsUI(windows=1) # list all window # trick cmds cmds.lsUI(controlLayouts=1, l=1) layoutType = cmds.objectTypeUI('AErootLayout') childs = cmds.layout('AErootLayout', q=1, childArray=1)
# most maya layout has a -childArray flag to get child cmds.tabLayout("compEdTab",q=1,childArray=1) # get UI type cmds.objectType("compEdTabChild5") # then continue call with the object's related cmd # to get parent cmds.tabLayout("compEdTab",q=1,parent=1)
cmds.window('MayaWindow', e=1, titleBar=(not cmds.window('MayaWindow', q=1, titleBar=1))) # hide title bar (true no UI by: press ctrl+space; then run this cmd) newT="Autodesk Maya 2009 x64 Unlimited: untitled | cool" cmds.window('MayaWindow', e=1, title=newT) # mel name as $gMainWindow
if cmds.window('outlinerPanel1Window',ex=1): cmds.deleteUI('outlinerPanel1Window') else: mel.eval('OutlinerWindow')
# ref: http://cgbreakdown.com/Blog/tools-and-scripts-maya-undo import maya.mel as mel import maya.cmds as cmds # nextFrame ('Alt+.') cmds.undoInfo(stateWithoutFlush=0) mel.eval('playButtonStepForward') cmds.undoInfo(stateWithoutFlush=1) # previousFrame ('Alt+,') cmds.undoInfo(stateWithoutFlush=0) mel.eval('playButtonStepBackward') cmds.undoInfo(stateWithoutFlush=1) # nextKey ('.') cmds.undoInfo(stateWithoutFlush=0) cmds.currentTime(cmds.findKeyframe(timeSlider=1,which='next'), e=1) cmds.undoInfo(stateWithoutFlush=1) # previousKey (',') cmds.undoInfo(stateWithoutFlush=0) cmds.currentTime(cmds.findKeyframe(timeSlider=1,which='previous'), e=1) cmds.undoInfo(stateWithoutFlush=1) # firstKey cmds.undoInfo(stateWithoutFlush=0) cmds.currentTime(cmds.findKeyframe(which='first'), e=1) cmds.undoInfo(stateWithoutFlush=1) # lastKey cmds.undoInfo(stateWithoutFlush=0) cmds.currentTime(cmds.findKeyframe(which='last'), e=1) cmds.undoInfo(stateWithoutFlush=1)
cmds.undoInfo(openChunk=1) # your scripting block here cmds.undoInfo(closeChunk=1)
mel.eval('global string $gChannelBoxName; $temp=$gChannelBoxName;')
main_cb = 'mainChannelBox' next_state = not cmds.channelBox(main_cb, q=1, nn=1) cmds.channelBox(main_cb, e=1, ln=next_state, nn=next_state)
cmdPanel = 'cmdScrollFieldReporter1' next_state = not cmds.cmdScrollFieldReporter(cmdPanel, q=1, eac=1) cmds.cmdScrollFieldReporter(cmdPanel, e=1, eac=next_state) # clear echo cmds.cmdScrollFieldReporter(cmdPanel, e=1, clear=1)
if cmds.cmdScrollFieldExecuter(ui_name, q=1, ex=1): cmds.deleteUI(ui_name) mui = cmds.cmdScrollFieldExecuter(ui_name, st='python', sth=0, sln=1)# width=200, height=100) selected_text = cmds.cmdScrollFieldExecuter(mui, q=1, selectedText=1) # change selected or insert text cmds.cmdScrollFieldExecuter(mui, e=1, insertText='#==== '+selected_text+' ====') # set search text and search next cmds.cmdScrollFieldExecuter(mui, e=1, searchDown=1, searchWraps=1, searchMatchCase=0, searchString=text) res = cmds.cmdScrollFieldExecuter(mui, q=1, searchAndSelect=1) # 1 or 0 # go to line number cmds.cmdScrollFieldExecuter(mui, e=1, currentLine=lineNum) # get script editor whole text script_text = cmds.cmdScrollFieldExecuter(mui, q=1, t=1)
mui = cmds.cmdScrollFieldReporter(ui_name)#, width=200, height=100) # clear history cmds.cmdScrollFieldReporter(mui, e=1, clr=1)
mel.eval('ComponentEditor') # active smooth skin tab tab_name_list = cmds.tabLayout("compEdTab", q=1, tabLabel=1) for i in range(len(tab_name_list)): if tab_name_list[i] == "Smooth Skins": cmds.tabLayout("compEdTab",e=1, selectTabIndex=i+1)
for each in cmds.ls(sl=1): cmds.hyperGraph("graph1HyperShadeEd", e=1, addDependNode=each ) cmds.hyperGraph("graph1HyperShadeEd", e=1, addBookmark=1) newBookmark = cmds.hyperGraph("graph1HyperShadeEd", q=1, bookmarkName=1) # return recent created mark cmds.setAttr(newBookmark+".description", "my bookmark note", type="string")
for each in cmds.ls(sl=1): cmds.nodeEditor('nodeEditorPanel2NodeEditorEd',e=1, addNode=each) cmds.getPanel(scriptType="nodeEditorPanel") # add NodeEditorEd at the end to get the nodeEditor object cmds.nodeEditor('nodeEditorPanel2NodeEditorEd', q=1, getNodeList=1)
import maya.mel as mel mel.eval('setChannelLongName(!`channelBox -query -nn $gChannelBoxName`)')
def process_func(data): print(data) cmds.button( command = lambda *args: process_func('one')
# get panel by name whichPanel = cmds.getPanel(withLabel='Graph Editor') cmds.scriptedPanel(whichPanel, e=1, tor=1) wndName = whichPanel + "Window"; # the window name
Note: from Maya 2017 onwards, you need to duplicate default maya hotkey to be able to make changes, it is under Windows > Settings > Hotkey Editor
cmds.hotkey(factorySettings=1)
import maya.cmds as cmds # cmds.hotkey(factorySettings=1) cmds.hotkey(autoSave=0) # -- toggle select mode g_shiToggleMode=0 def toggleSelectMode(curMode): global g_shiToggleMode print([g_shiToggleMode, curMode]) if g_shiToggleMode != curMode: if curMode == 1: cmds.SelectVertexMask() elif curMode == 2: cmds.SelectEdgeMask() elif curMode == 3: cmds.SelectFacetMask() elif curMode == 0: cmds.SelectToggleMode() g_shiToggleMode = curMode else: cmds.SelectToggleMode() g_shiToggleMode = 0 for i in range(1,3+1): cmds.nameCommand('NC_shiToggleMode_'+str(i), ann='NC_shiToggleMode_'+str(i), c='python("toggleSelectMode({0})")'.format(i)) cmds.hotkey(k=str(i), ctl=1, n='NC_shiToggleMode_'+str(i)) # -- viewport cmds.nameCommand('NC_view_tWireframe', ann='NC_view_tWireframe', c='python("cmds.modelEditor(cmds.getPanel(up=1), e=1, wireframeOnShaded=(1-cmds.modelEditor(cmds.getPanel(up=1), q=1, wireframeOnShaded=1)))")') cmds.hotkey(k='f', ctl=1, alt=1, n='NC_view_tWireframe') # -- windows cmds.nameCommand('NC_win_outliner', ann='NC_win_outliner', c="OutlinerWindow;") cmds.hotkey(k='4', ctl=1, n='NC_win_outliner') # -- script editor cmds.nameCommand('NC_win_scriptEditor', ann='NC_win_scriptEditor', c="ScriptEditor") cmds.hotkey(k='x', ctl=1, alt=1, n='NC_win_scriptEditor') #-- hyperShade cmds.nameCommand('NC_win_hyperShade', ann='NC_win_hyperShade', c="HypershadeWindow") cmds.hotkey(k='s', ctl=1, alt=1, n='NC_win_hyperShade') #-- graph editor cmds.nameCommand('NC_win_graphEditor', ann='NC_win_graphEditor', c="GraphEditor") cmds.hotkey(k='g', ctl=1, alt=1, n='NC_win_graphEditor') #-- custom cmds.nameCommand('NC_fun01', ann='NC_fun01', c='python("fun01")') cmds.hotkey(k='d', ctl=1, alt=1, n='NC_fun01')
# ref: http://bindpose.com/custom-global-hotkey-maya/ # qt import try: from PySide import QtGui, QtCore import PySide.QtGui as QtWidgets import shiboken except ImportError: try: from PySide2 import QtCore, QtGui, QtWidgets import shiboken2 as shiboken except ImportError: pass import maya.cmds as cmds import maya.OpenMayaUI as omui hotkey = {} mayaWindow = shiboken.wrapInstance(long(omui.MQtUtil.mainWindow()), QtWidgets.QWidget) def shortcut_process(keyname): if keyname == 'toggleWire': if 'modelPanel' in cmds.getPanel(wf=1): print('Done Something') else: # give key event to maya hotkey[keyname].setEnabled(0) e = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_F, QtCore.Qt.ControlModifier | QtCore.Qt.AltModifier) QtCore.QCoreApplication.postEvent(mayaWindow , e) cmds.evalDeferred(partial(hotkey[keyname].setEnabled, 1)) else: pass # shortcut hotkey['toggleWire'] = QtWidgets.QShortcut(QtGui.QKeySequence('Ctrl+Alt+F'), mayaWindow) hotkey['toggleWire'].setContext(QtCore.Qt.ApplicationShortcut) hotkey['toggleWire'].activated.connect(partial(shortcut_process, 'toggleWire'))
Python Maya API reference
common api page offical reference page
om.MDagPath:
selectionList = om.MSelectionList() selectionList.add(mesh_name) mesh_dagPath = om.MDagPath() selectionList.getDagPath(0, mesh_dagPath) print(mesh_dagPath.partialPathName()) # if done correct, it will give same name as mesh_name
geo_obj.hasFn(om.MFn.kDagNode) # check if it is Dag node, then it can have a dag path geo_dagNode = om.MFnDagNode(geo_obj) # now it is a DagNode, then get DagPath from DagNode print( geo_dagNode.partialPathName() ) # since DagNode also has name function geo_dagPath = om.MDagPath() geo_dagNode.getPath(geo_dagPath) print( geo_dagPath.partialPathName() ) # proof geo_dagPath work correctly
geo_dagPath = om.MDagPath() geo_dagNode.getPath(geo_dagPath) print( geo_dagPath.partialPathName() ) # proof geo_dagPath work correctly
om.MObject:
geo_transform_ojbect = geo_transform_dagPath.node() geo_transform_ojbect.hasFn(om.MFn.kTransform) # true, then can safely cast to MFnTransform
selectionList = om.MSelectionList() selectionList.add(skinName) skinClusterObj = om.MObject() selectionList.getDependNode(0,skinClusterObj) skinClusterObj.hasFn(om.MFn.kSkinClusterFilter) # use for testing become cast to the object function
MFnDagNode:
int = MDagPath.childCount() = MFnDagNode.childCount() MObject = MDagPath.child(index) = MFnDagNode.child(index) int = MDagPath.pathCount() = MFnDagNode.pathCount() # node only can return itself while path can return segment str = MDagPath.getPath(MDagPath, i) = MFnDagNode.getPath(MDagPath) str = MDagPath.fullPathName() = MFnDagNode.fullPathName() str = MDagPath.partialPathName() = MFnDagNode.partialPathName()
selectionList = om.MSelectionList() selectionList.add(mesh_name) mesh_dagPath = om.MDagPath() selectionList.getDagPath(0, mesh_dagPath) mesh_dagNode = om.MFnDagNode(mesh_dagPath)
my_dagNode = om.MFnDagNode(my_dagPath)
my_obj.hasFn(om.MFn.kDagNode) my_dagNode = om.MFnDagNode(my_obj)
import maya.OpenMaya as om selected = om.MSelectionList() om.MGlobal.getActiveSelectionList(selected) dagPath = om.MDagPath() selected.getDagPath(0,dagPath) print(dagPath.fullPathName()) # current full path name print(dagPath.partialPathName()) # current node name # get shape count countUtil = om.MScriptUtil() countUtil.createFromInt(0) tmpIntPtr = countUtil.asUintPtr() # unsigned int dagPath.numberOfShapesDirectlyBelow(tmpIntPtr) print(countUtil.getUint(tmpIntPtr)) print(dagPath.childCount()) # another alternative method # get its Shape as MDagPath dagPath.extendToShape() # MDagPath: change to shape path, will error if has no shape or more than 1 shape dagPath.extendToShapeDirectlyBelow(0) # same as above; but won't error when more than 1 shape print(dagPath.fullPathName()) # shape, if already shape, also return itself # get itself as Node dagObj = dagPath.node() # MObject dagObj.hasFn(om.MFn.kDagNode) # check if its DAG node, not DG node dagNode = om.MFnDagNode(dagObj) # use function to handle this MObject print(dagNode.fullPathName()) # get its Transform as Node dagObj = dagPath.transform() # MObject: return its transform MObject, if already transform, it return itself dagNode = om.MFnDagNode(dagObj) print(dagNode.fullPathName()) # transform node full dag path
my_DGNode = OpenMaya.MFnDependencyNode(myObj) print(my_DGNode.name())
selectionList = OpenMaya.MSelectionList() selectionList.add(my_name) my_obj = OpenMaya.MObject() selectionList.getDependNode(0,my_obj) my_obj.hasFn(om.MFn.kDependencyNode) # check before convert my_dgNode = om.MFnDependencyNode(my_obj)
my_obj = my_dagPath.node() my_obj.hasFn(om.MFn.kDependencyNode) # check before convert my_dgNode = om.MFnDependencyNode(my_obj)
my_dagPath = my_dagNode.getPath() my_obj = my_dagPath.node() my_obj.hasFn(om.MFn.kDependencyNode) # check before convert my_dgNode = om.MFnDependencyNode(my_obj)
other common class:
Array in MPlug:
my_arraPlugy.evaluateNumElements() print(my_arraPlugy.numElements())
Concept of Dynamic array - a pre-defined-size list with pre-loaded empty items
string $nameArray[10]; $nameArray[5]="objA"; // at place 5 in nameArray print("\n-------start sparse array--------\n"); print $nameArray; // null null null null null objA print("-------end----------"); $logical_index = stringArrayFind( "objA", 0, $nameArray ); // result: 5 print("\nlogical index of 'objA': "+ $logical_index); print("\nsize: "+size($nameArray)); // size of array currently: 6 print("\n9th: "+$nameArray[8]); // null print("\n11th: "+$nameArray[10]); // null print("\nsize: "+size($nameArray)); // size of array currently: 6 string $nameArray_autoNonSparse_means_noEmpty[]; for($each in $nameArray){ if($each != ""){ $cur_index = size($nameArray_autoNonSparse_means_noEmpty); $nameArray_autoNonSparse_means_noEmpty[$cur_index] = $each; } } print("\n-------start no sparse array--------\n"); print($nameArray_autoNonSparse_means_noEmpty); print("-------end----------"); $physical_index = stringArrayFind( "objA", 0, $nameArray_autoNonSparse_means_noEmpty ); // result: 0 print("\nphysical index of 'objA': "+$physical_index+"\n"); // clear from memory clear($nameArray);
The Physical index of joint in the influence list
py_var = 0 # your type, say Unsign Int util = om.MScriptUtil() util.createFromInt(py_var) # same as your type util_ptr = util.asUintPtr() # same as your type api_obj.api_function_return_a_value(util_ptr) # use pointer to hold the result value py_var = util.getUint(util_ptr) # same as your type print(py_var) # new value from API
/*
rot: rot vector around another vector by a degree
rad_to_deg, deg_to_rad
*/
# ref: https://groups.google.com/forum/#!topic/python_inside_maya/-Yp8WfyTkPA # MVector: rotateBy (MVector::Axis axis, const double angle) # get a MVector result by rotate one Vector around default x,y,z axis by certain degree # MVector: rotateBy (const MQuaternion &) const # case 1: get a MVector result by rotate one Vector around another Vector by certain degree res_vector = mainVector.rotateBy( maya.OpenMaya.MQuaternion(math.radians(rotDegree), aimVector) ) # case 2: get a MVector result by rotate one Vector by a swing motion defined by start vector to end vector res_vector = mainVector.rotateBy( maya.OpenMaya.MQuaternion(MotionStartVector, MotionEndVector, optionalPercentage ) )
import maya.OpenMaya as om import math def getLocalVecToWorldSpaceAPI(obj, vec=om.MVector.xAxis): # ref: http://forums.cgsociety.org/showthread.php?t=1124527 # take obj's local space vec to workspace, default object x axis selList = om.MSelectionList() selList.add(obj) nodeDagPath = om.MDagPath() selList.getDagPath(0, nodeDagPath) matrix = nodeDagPath.inclusiveMatrix() vec = (vec * matrix).normal() return vec.x, vec.y, vec.z # define objects # a locator reference that we are aligning our pCube object to # of course, you dont need a refLoc, you can use Vector from other data as well # here locator is just a visual example refLoc = 'locator_1' obj = 'pCube1' # a cube we are aligning # grab object transform function lib selectionList = om.MSelectionList() selectionList.add( obj ) dagPath = om.MDagPath() selectionList.getDagPath(0, dagPath) fnTransform = om.MFnTransform(dagPath) # STEP 1: align Y axis first refLoc_normal_y = getLocalVecToWorldSpaceAPI(refLoc, om.MVector.yAxis) aim_vec_y = om.MVector(*refLoc_normal_y) # break array into 3 parameters obj_normal_y = getLocalVecToWorldSpaceAPI(obj, om.MVector.yAxis) obj_vec_y = om.MVector(*obj_normal_y) rot_quaternion = obj_vec_y.rotateTo(aim_vec_y) # absolute version #fnTransform.setRotationQuaternion( rot_quaternion.x, rot_quaternion.y, rot_quaternion.z, rot_quaternion.w ) # relative transform on top of existing obj transform fnTransform.rotateBy(rot_quaternion) # STEP 2: align Z axis refLoc_normal_z = getLocalVecToWorldSpaceAPI(refLoc, om.MVector.zAxis) aim_vec_z = om.MVector(*refLoc_normal_z) obj_normal_z = getLocalVecToWorldSpaceAPI(obj, om.MVector.zAxis) obj_vec_z = om.MVector(*obj_normal_z) rot_quaternion = obj_vec_z.rotateTo(aim_vec_z) fnTransform.rotateBy(rot_quaternion)
#general pos = cmds.xform(obj, q=1, t=1, ws=1) # loc pos = cmds.getAttr(loc+'.worldPosition')[0] # world position pos = cmds.pointPosition(loc) # world position #cv - local position from origin at creation time cmds.getAttr('my_curve.cv[1]')[0] cmds.pointPosition('my_curve.cv[1]',l=1) # vtx - cmds.getAttr('pCube1.vtx[1]')[0] # local offset value since creation cmds.getAttr("pCube1.pnts[1]")[0] # local offset value since creation cmds.pointPosition('pCube1.vtx[0]',l=1) cmds.setAttr("pCubeShape1.pnts[5].pntx", 0) cmds.setAttr("pCubeShape1.pnts[5].pnty", 0) cmds.setAttr("pCubeShape1.pnts[5].pntz", 0) # vtx - local position relative to pivot # loc, vtx, cv, uv, point - global position pos = cmds.pointPosition(pointObj)
cmds.makeIdentity(cmds.ls(sl=1),apply=1,t=1,r=1,s=1,n=0)
selected = om.MSelectionList() om.MGlobal.getActiveSelectionList(selected)
def getVtxSelectionSoft_OpenMaya(): # ref: http://around-the-corner.typepad.com/adn/2013/05/support-soft-selection-with-custom-mpxcomponentshape.html startTime = cmds.timerX() # 0.13s > no string 0.07s # soft selection softSelectionList = om.MSelectionList() softSelection = om.MRichSelection() om.MGlobal.getRichSelection(softSelection) softSelection.getSelection(selectionList) selectionPath = om.MDagPath() selectionObj = om.MObject() iter = om.MItSelectionList( selectionList, om.MFn.kMeshVertComponent) #elements = [] id_list = [] weights = [] node_list = [] while not iter.isDone(): iter.getDagPath( selectionPath, selectionObj ) selectionPath.pop() #Grab the parent of the shape node node = selectionPath.fullPathName() compFn = om.MFnSingleIndexedComponent(selectionObj) getWeight = lambda i: compFn.weight(i).influence() if compFn.hasWeights() else 1.0 node_list.append(node) for i in range(compFn.elementCount()): #elements.append('{0}.vtx[{1}]'.format(node, compFn.element(i))) id_list.append(compFn.element(i)) weights.append(getWeight(i)) iter.next() totalTime = cmds.timerX(startTime=startTime) print("\nTotal Time: {0}".format(totalTime)) return list(set(node_list)), id_list, weights
def createVtxSelection_cmds(objName, id_list): startTime = cmds.timerX() # 0.039s result_sel = ['{0}.vtx[{1}]'.format(objName, i) for i in id_list] totalTime = cmds.timerX(startTime=startTime) print("\nTotal Time: {0}".format(totalTime)) return result_sel def createVtxSelection_OpenMaya(objName, id_list): # ref: http://discourse.techart.online/t/maya-api-components/3400/3 startTime = cmds.timerX() # 0.009s sel = om.MSelectionList() sel.add(objName) mdag = om.MDagPath() sel.getDagPath(0, mdag) util = om.MScriptUtil() util.createFromList(id_list, len(id_list)) ids_ptr = util.asIntPtr() ids = om.MIntArray(ids_ptr, len(id_list)) compFn = om.MFnSingleIndexedComponent() components = compFn.create( om.MFn.kMeshVertComponent ) compFn.addElements(ids) result_sel = om.MSelectionList() result_sel.add(mdag, components) totalTime = cmds.timerX(startTime=startTime) print("\nTotal Time: {0}".format(totalTime)) return result_sel def getVtxSelection_cmd(l=0): startTime = cmds.timerX() # 0.18s selected = cmds.ls(sl=1, fl=1, l=l) # len(selected) id_list = [x.split('[',1)[-1][:-1] for x in selected] totalTime = cmds.timerX(startTime=startTime) print("\nTotal Time: {0}".format(totalTime)) return selected[0].split['.'][0], id_list def getVtxSelection_OpenMaya(): startTime = cmds.timerX() # 0.11s > no comp iter: 0.039 # normal selection normalSelectionList = om.MSelectionList() om.MGlobal.getActiveSelectionList(normalSelectionList) selectionPath = om.MDagPath() selectionObj = om.MObject() indexList = om.MIntArray() iter = om.MItSelectionList( normalSelectionList, om.MFn.kMeshVertComponent ) #normalSelectionElements = [] # len(normalSelectionElements) node_list = [] id_list = [] while not iter.isDone(): iter.getDagPath( selectionPath, selectionObj ) selectionPath.pop() node = selectionPath.fullPathName() compFn = om.MFnSingleIndexedComponent(selectionObj) # flatten the component selection compFn.getElements(indexList) id_list.extend(list(indexList)) node_list.append(node) ''' for i in range(compFn.elementCount()): normalSelectionElements.append('{0}.vtx[{1}]'.format(node, compFn.element(i))) id_list.append(compFn.element(i)) ''' iter.next() totalTime = cmds.timerX(startTime=startTime) print("\nTotal Time: {0}".format(totalTime)) return list(set(node_list)), id_list
# mel: DeleteHistory; cmds.DeleteHistory() # show poly vertex # mel: setPolygonDisplaySettings("verts"); cmds.polyOptions(r=1, activeObjects=1, dv=1) # mel: ToggleFaceNormalDisplay; cmds.ToggleFaceNormalDisplay() # mel: ToggleBackfaceGeometry; cmds.ToggleBackfaceGeometry() # mel: ReversePolygonNormals; cmds.ReversePolygonNormals() # hard edge # mel: SoftPolyEdgeElements(0); cmds.polySoftEdge(a=0) # show border # mel: ToggleBorderEdges; cmds.ToggleBorderEdges() # dialog to change edge width display # mel: ChangeEdgeWidth; cmds.ChangeEdgeWidth() # show UV window # mel: TextureViewWindow; cmds.TextureViewWindow(); # mel: CenterPivot; cmds.CenterPivot() # turn on / off symmetric modeling ''' symmetricModelling -e -symmetry true;symmetricModelling -e -symmetry false; symmetricModelling -e -about "world"; symmetricModelling -e -about "object"; symmetricModelling -e -axis "x" ''' cmds.symmetricModelling(e=1, symmetry=1) cmds.symmetricModelling(e=1, symmetry=0) # face selection to edge border selection # mel: select -r `polyListComponentConversion -ff -te -bo`; cmds.select(cmds.polyListComponentConversion(ff=1, te=1, bo=1),r=1) # selection to shell # mel: ConvertSelectionToShell; cmds.ConvertSelectionToShell() # selection to create layer ''' string $tmpname[]=`ls -sl`; createDisplayLayer -name ($tmpname[0]+"_Lyr") -number 1 -nr; ''' selected = cmds.ls(sl=1) if len(selected)>0: cmds.createDisplayLayer(name=selected[0]+'_layer', number=1, nr=1) # remove from layers # mel: editDisplayLayerMembers -noRecurse "defaultLayer" `ls -sl` cmds.editDisplayLayerMembers("defaultLayer", cmds.ls(sl=1), noRecurse=1) #### curve ##### # show CV # mel: ToggleCVs; cmds.ToggleCVs() # show editable point # mel: ToggleEditPoints; cmds.ToggleEditPoints() # rebuild curve dialog # mel: RebuildCurveOptions; cmds.RebuildCurveOptions() # detach surface by edge or isopam # mel: DetachCurve; cmds.DetachCurve() # detach/attach curve by cut # mel: CutCurve; AttachCurve; cmds.CutCurve() cmds.AttachCurve()
Vertex check in API way
vtx_cnt = cmds.polyEvaluate(geoName, v=1)
selected = om.MSelectionList() om.MGlobal.getActiveSelectionList(selected) dagPath = om.MDagPath() selected.getDagPath(0,dagPath) print(dagPath.fullPathName()) # transform dagPath.extendToShape() # .extendToShapeDirectlyBelow() if dont want to get child of child; will error if has no shape print(dagPath.fullPathName()) # shape, if shape, also return itself dagPath.transform() # transform mesh = om.MFnMesh(dagPath.node()) # MObject to MeshFunction print(mesh.numVertices()) # vtx count
Vertex Check
# test empty geo shape all_geo_list = cmds.ls(type='mesh') zeroVtx_list = [] for each in all_geo_list: if cmds.polyEvaluate(each, v=1) == 0: zeroVtx_list.append(each) print(zeroVtx_list)
Face Check and Correct
shape_list = cmds.ls(type='mesh') for shape in shape_list: cmds.setAttr(shape+".doubleSided", 1)
Curve related
def getCurveInfo(curveObj, index=2): d = cmds.getAttr(curveObj+'.degree') p = cmds.getAttr(curveObj+'.cv[:]') p_new = [ [numClean(y, index) for y in x] for x in p] tmpInfo = cmds.createNode( 'curveInfo' ) cmds.connectAttr( curveObj + '.worldSpace', tmpInfo+'.inputCurve' ) k = cmds.getAttr( tmpInfo + '.knots[*]' ) cmds.delete(tmpInfo) return (d, p_new, k) def numClean(fnum, index=2): rule = "{0:."+str(index)+"f}" cnum = float(rule.format(fnum)) if cnum.is_integer(): cnum = int(cnum) return cnum def setCurveInfo(curveObj, curveInfo): # input: dict - d, p; p ; d,p,k if not isinstance(curveInfo, dict) or 'p' not in curveInfo.keys(): print('require curveInfo in dict of d,p,k or d,p or p') return if all(x in curveInfo.keys() for x in ['d','p','k']): return cmds.curve(curveObj,r=1,d=curveInfo['d'], p=curveInfo['p'], k=curveInfo['k']) elif all(x in curveInfo.keys() for x in ['d','p']): return cmds.curve(curveObj,r=1,d=curveInfo['d'], p=curveInfo['p']) else: return cmds.curve(curveObj,r=1,d=1, p=curveInfo['p']) def buildCurveInfo(curveInfo, n='tmpCurve'): # input: dict - d, p; p ; d,p,k if not isinstance(curveInfo, dict) or 'p' not in curveInfo.keys(): print('require curveInfo in dict of d,p,k or d,p or p') return if all(x in curveInfo.keys() for x in ['d','p','k']): return cmds.curve(n=n,d=curveInfo['d'], p=curveInfo['p'], k=curveInfo['k']) elif all(x in curveInfo.keys() for x in ['d','p']): return cmds.curve(n=n,d=curveInfo['d'], p=curveInfo['p']) else: return cmds.curve(n=n,d=1, p=curveInfo['p'])
#txt = 'curve -d 1 -p 0 0 1 -p 0 0.5 0.866025 -p 0 0.866025 0.5 -p 0 1 0 -p 0 0.866025 -0.5 -p 0 0.5 -0.866025 -p 0 0 -1 -p 0 -0.5 -0.866025 -p 0 -0.866025 -0.5 -p 0 -1 0 -p 0 -0.866025 0.5 -p 0 -0.5 0.866025 -p 0 0 1 -p 0.707107 0 0.707107 -p 1 0 0 -p 0.707107 0 -0.707107 -p 0 0 -1 -p -0.707107 0 -0.707107 -p -1 0 0 -p -0.866025 0.5 0 -p -0.5 0.866025 0 -p 0 1 0 -p 0.5 0.866025 0 -p 0.866025 0.5 0 -p 1 0 0 -p 0.866025 -0.5 0 -p 0.5 -0.866025 0 -p 0 -1 0 -p -0.5 -0.866025 0 -p -0.866025 -0.5 0 -p -1 0 0 -p -0.707107 0 0.707107 -p 0 0 1 -k 0 -k 1 -k 2 -k 3 -k 4 -k 5 -k 6 -k 7 -k 8 -k 9 -k 10 -k 11 -k 12 -k 13 -k 14 -k 15 -k 16 -k 17 -k 18 -k 19 -k 20 -k 21 -k 22 -k 23 -k 24 -k 25 -k 26 -k 27 -k 28 -k 29 -k 30 -k 31 -k 32' # maya float number cleaner def numClean(fnum, index=2): rule = "{0:."+str(index)+"f}" cnum = float(rule.format(fnum)) if cnum.is_integer(): cnum = int(cnum) return cnum def curve_mel2py(txt): rest, k = txt.split('-k',1) ks = [int(x.strip()) for x in k.split('-k')] rest, p = rest.split('-p',1) ps = [ x.strip().split() for x in p.split('-p') ] ps = [ [numClean(float(y)) for y in x] for x in ps] d = int(rest.split('-d',1)[-1].strip()) result = "created = cmds.curve(d={0}, p={1}, k={2})".format(d, str(ps), str(ks)) return result
lineCurve = cmds.curve(p=pointList, d=1) nurbsCurve = cmds.fitBspline(lineCurve,ch=0,tol=0.01) cmds.delete(lineCurve)
# ref: http://www.fevrierdorian.com/blog/post/2011/09/27/Quickly-retrieve-vertex-positions-of-a-Maya-mesh-(English-Translation) def getVtxSelection_OpenMaya(): startTime = cmds.timerX() # 0.11s > no comp iter: 0.039 # normal selection normalSelectionList = om.MSelectionList() om.MGlobal.getActiveSelectionList(normalSelectionList) selectionPath = om.MDagPath() selectionObj = om.MObject() indexList = om.MIntArray() iter = om.MItSelectionList( normalSelectionList, om.MFn.kMeshVertComponent ) #normalSelectionElements = [] # len(normalSelectionElements) node_list = [] id_list = [] while not iter.isDone(): iter.getDagPath( selectionPath, selectionObj ) selectionPath.pop() node = selectionPath.fullPathName() compFn = om.MFnSingleIndexedComponent(selectionObj) # flatten the component selection compFn.getElements(indexList) id_list.extend(list(indexList)) node_list.append(node) ''' for i in range(compFn.elementCount()): normalSelectionElements.append('{0}.vtx[{1}]'.format(node, compFn.element(i))) id_list.append(compFn.element(i)) ''' iter.next() totalTime = cmds.timerX(startTime=startTime) print("\nTotal Time: {0}".format(totalTime)) return list(set(node_list)), id_list def getVtxPos_cmd( shapeNode, sl=0 ): # full path, duplicate name proof startTime = cmds.timerX() pos_list = [] index_list = [] if sl==0: # 0.885 #index_list = cmds.getAttr( shapeNode+".vrts", multiIndices=True ) # 0.0089 index_list = range( cmds.polyEvaluate(shapeNode,v=1) ) # 0.007 else: # 0.244 shapeNode, index_list = getVtxSelection_OpenMaya() # 0.01 shapeNode = shapeNode[0] for i in index_list: #i=0 pos = cmds.xform( '{0}.vtx[{1}]'.format(shapeNode, i), q=1, t=1, ws=1) pos_list.append( pos ) totalTime = cmds.timerX(startTime=startTime) print("\nTotal Time: {0}".format(totalTime)) return index_list, pos_list def setVtxPos_cmd( shapeNode, pos_data, sl=0 ): startTime = cmds.timerX() cmds.undoInfo(openChunk=1) index_list, pos_list = pos_data if sl== 0: # 4.38 for i in range(len(index_list)): cmds.xform('{0}.vtx[{1}]'.format(shapeNode, index_list[i]), t=pos_list[i], ws=1) else: # 5.29 sel_index_list = getVtxSelection_OpenMaya()[1] if not set(sel_index_list).issubset(set(index_list)): print('selection is not subset of stored plmp data.') return for id in sel_index_list: i = index_list.index(id) cmds.xform('{0}.vtx[{1}]'.format(shapeNode, id), t=pos_list[i], ws=1) cmds.undoInfo(closeChunk=1) totalTime = cmds.timerX(startTime=startTime) print("\nTotal Time: {0}".format(totalTime)) def getVtxPos_OpenMaya( shapeNode, sl=0 ): # full path, duplicate name proof startTime = cmds.timerX() pos_list = [] index_list = [] if sl==0: # 0.18 selectionList = om.MSelectionList() selectionList.add(shapeNode) dagPath = om.MDagPath() selectionList.getDagPath(0, dagPath) mesh_fn = om.MFnMesh(dagPath) meshPointArray = om.MPointArray() mesh_fn.getPoints(meshPointArray, om.MSpace.kWorld) index_list = range(meshPointArray.length()) for i in index_list: pos_list.append( [meshPointArray[i][0], meshPointArray[i][1], meshPointArray[i][2]] ) else: # 0.04 selectionList = om.MSelectionList() om.MGlobal.getActiveSelectionList(selectionList) selectionPath = om.MDagPath() selectionCompObj = om.MObject() iter_sel = om.MItSelectionList( selectionList, om.MFn.kMeshVertComponent ) while not iter_sel.isDone(): iter_sel.getDagPath( selectionPath, selectionCompObj ) iter_vtx = om.MItMeshVertex(selectionPath,selectionCompObj) while not iter_vtx.isDone(): pnt = iter_vtx.position(om.MSpace.kWorld) index_list.append( iter_vtx.index() ) pos_list.append([pnt.x, pnt.y, pnt.z]) iter_vtx.next() iter_sel.next() totalTime = cmds.timerX(startTime=startTime) print("\nTotal Time: {0}".format(totalTime)) return index_list, pos_list def setVtxPos_OpenMaya(shapeNode, pos_data, sl=0): startTime = cmds.timerX() cmds.undoInfo(openChunk=1) index_list, pos_list = pos_data selectionList = om.MSelectionList() selectionList.add(shapeNode) dagPath = om.MDagPath() selectionList.getDagPath(0, dagPath) mesh_fn = om.MFnMesh(dagPath) if sl==0: # 0.069 # set one by one - 4.22 #for i in range(len(index_list)): # mesh_fn.setPoint(index_list[i], om.MPoint(*pos_list[i]) ) # get all and get all - 0.069 meshPointArray = om.MPointArray() mesh_fn.getPoints(meshPointArray, om.MSpace.kWorld) for i in range(len(index_list)): meshPointArray.set(index_list[i],*pos_list[i] ) mesh_fn.setPoints(meshPointArray, om.MSpace.kWorld) else: # 0.53 sel_index_list = getVtxSelection_OpenMaya()[1] if not set(sel_index_list).issubset(set(index_list)): print('selection is not subset of stored plmp data.') return meshPointArray = om.MPointArray() mesh_fn.getPoints(meshPointArray, om.MSpace.kWorld) for id in sel_index_list: i = index_list.index(id) meshPointArray.set(id,*pos_list[i] ) mesh_fn.setPoints(meshPointArray, om.MSpace.kWorld) cmds.undoInfo(closeChunk=1) totalTime = cmds.timerX(startTime=startTime) print("\nTotal Time: {0}".format(totalTime))
cmds.hyperShade(testGeo, assign='refMat')
cmds.shadingNode('lambert', n='refMat', asShader=1)
cmds.setAttr('{0}.transparency'.format(testMat), 0.5, 0.5, 0.5, type="double3")
nodeList = cmds.ls(type=['unknown', 'unknownDag', 'unknownTransform']) #nodeToDelete = nodeList[0] def deleteIfNotReferenced( nodeList ): if not isinstance(nodeList, (list, tuple)): nodeList = [nodeList] for nodeToDelete in nodeList: if cmds.objExists(nodeToDelete) and not cmds.reference(nodeToDelete, q=1, isNodeReferenced=1): isLocked = cmds.lockNode(nodeToDelete, q=1, lock=1)[0] if not isLocked: cmds.delete( nodeToDelete )
obj_transform | .worldMatrix |
mesh_list = cmds.ls('sw2_*',type='mesh',l=1) for each_mesh in mesh_list: info = each_mesh.rsplit('|',1) if len(info )==2: cmds.rename(each_mesh, info[-1].replace('sw2_','')) else: print(each_mesh) obj_list = cmds.ls('sw2_*',type='transform',l=1) for each_obj in obj_list: info = each_obj.rsplit('|',1) if len(info)==2: cmds.rename(each_obj, info[-1].replace('sw2_','')) else: print(each_obj)
alias_target_weight_list = cmds.aliasAttr( final_bs, q=1 ) # ['body_BST', 'weight[0]'] target_list = alias_target_weight_list[::2] # all the alias target names weight_attr_list = alias_target_weight_list[1::2] # all the weight attribute list
attach_vtx_id = 16 # use same weight as on vtx[16] cur_skin = cmds.skinCluster(cur_bones, cur_geo, tsb=1, n=cur_geo+'_skinCluster')[0] cmds.setAttr( cur_skin + ".skinningMethod", 1) vtx_weight = cmds.skinPercent(cur_skin, cur_geo+'.vtx[{0}]'.format(attach_vtx_id), q=1, v=1) cmds.skinPercent(cur_skin, cur_geo+'.vtx[:]', tv=zip(cur_bones,vtx_weight))
cur_vtx_list = cmds.ls(sl=1, fl=1) if len(cur_vtx_list)>0: if '.vtx[' in cur_vtx_list[0]: cur_skin = weightAPI.findRelatedSkinCluster( cur_vtx_list[0].split('.')[0] ) affect_boneList = cmds.skinPercent(cur_skin, cur_vtx_list, q=1, ignoreBelow=0.01, t=None)
cmds.skinCluster(cur_skin, e=1, selectInfluenceVerts=bone_name)
cmds.getAttr('ball.vtx[0]')[0] # those value in channelbox CV, and component editor cmds.polyMoveVertex(localTranslate=(0, 0, 0)) # clear local values
cmds.getAttr(cur_bs+'.it[0].itg[0].tw[0]') # cmds method extremely slow even for a ball # target 1 : weight per vtx cmds.getAttr('blendShape1.inputTarget[0].inputTargetGroup[0].targetWeights[240]') cmds.getAttr('blendShape1.inputTarget[0].inputTargetGroup[0].targetWeights')[0] # get full list of weights # target 2 : weight per vtx cmds.getAttr('blendShape1.inputTarget[0].inputTargetGroup[1].targetWeights[240]') # set target 2: weight per vtx cmds.setAttr('blendShape1.inputTarget[0].inputTargetGroup[1].targetWeights[241]', .8) # get current loaded paint weight data cmds.getAttr(cur_bs+'.it[0].pwt[11]') #vtx 11 at current paint data # get current base (the final mixing global per vtx weight) cmds.getAttr(cur_bs+'.it[0].bw[11]') #vtx 11 at current paint data
import maya.OpenMaya as om import maya.OpenMayaAnim as oma selectionList = om.MSelectionList() selectionList.add(cur_bs) bs_obj = om.MObject() selectionList.getDependNode(0,bs_obj) bs_obj.hasFn(om.MFn.kBlendShape) bs_fn = oma.MFnBlendShapeDeformer(bs_obj) # blendshape object fn # current result object obj_list = om.MObjectArray() bs_fn.getBaseObjects(obj_list) print(obj_list.length()) # 1, only one blendshape object, even if it has more targets bs_base_obj = obj_list[0]
nWeights = bs_fn.numWeights() # number of blend targets indexList = om.MIntArray() # get logic index of blend targets, since target can be remove from array, and become sparse bs_fn.weightIndexList(indexList) w_info_list=[] # weight attr info w_plug = bs_fn.findPlug('weight') for index in indexList: cur_plug = w_plug.elementByLogicalIndex(index) cur_plug_alias = bs_fn.plugsAlias(cur_plug) cur_plug_w = bs_fn.weight(index) w_info_list.append([cur_plug_alias, cur_plug_w]) print(w_info_list)
inputTargetArrayPlug = bs_fn.findPlug('inputTarget') inputTargetPlug = inputTargetArrayPlug.elementByLogicalIndex(0) # base_model # base weight list baseWeightsArrayPlug = inputTargetPlug.child(1) # .inputTarget[0].baseWeights vtxCnt = cmds.polyEvaluate(base_geo,v=1) base_weight_list = [1]*vtxCnt # default is 1 for j in range(baseWeightsArrayPlug.numElements()): cur_plug = baseWeightsArrayPlug.elementByPhysicalIndex(j) # same as baseWeightsArrayPlug[j] logicalIndex = cur_plug.logicalIndex() base_weight_list[logicalIndex] = cur_plug.asFloat() # update from sparse array data # following method may fail if you start maya fresh and read the scene ''' base_weight_list = [] for j in range(baseWeightsArrayPlug.numElements()): # fresh start will cause numElements become 0 or sparse count again, since not in datablock memory yet base_weight_list.append( baseWeightsArrayPlug.elementByLogicalIndex(j).asFloat() ) ''' # even this will not get the full index list baseWeightsArrayPlug.evaluateNumElements() print(baseWeightsArrayPlug.numElements())
inputTargetGroupArrayPlug = inputTargetPlug.child(0) target_weight_data = [] for index in indexList: # i=0 inputTargetGroupPlug = inputTargetGroupArrayPlug.elementByLogicalIndex(index) # inputTargetGroupPlug.name() targetWeightsArrayPlug = inputTargetGroupPlug.child(1) # .targetWeights target_weight_list = [1]*vtxCnt for j in range(targetWeightsArrayPlug.numElements()): # j=0 cur_plug = targetWeightsArrayPlug.elementByPhysicalIndex(j) logicalIndex = cur_plug.logicalIndex() target_weight_list[logicalIndex] = cur_plug.asFloat() target_weight_data.append(target_weight_list)
* API: Geometry result deformed vertex list position info
pos_list = [] geo_iter = om.MItGeometry(obj_list[0]) while not geo_iter.isDone(): p = geo_iter.position() pos_list.append((p.x, p.y, p.z)) geo_iter.next()
target_data = [] def geoObject_to_posList_OpenMaya(geo_object): # MObject as input geo_iter = om.MItGeometry(geo_object) pos_list = [] while not geo_iter.isDone(): pnt = geo_iter.position() pos_list.append([pnt.x, pnt.y, pnt.z]) geo_iter.next() return pos_list for index in indexList: # i = 0 target_object_data = [] target_list = om.MObjectArray() bs_fn.getTargets(bs_base_obj, index, target_list) # target_list.length() #1, ideally one blend target only have one object target_cnt = target_list.length() if target_cnt > 1: # more than one object case for j in range(target_cnt ): target_object_data.append( geoObject_to_posList_OpenMaya(target_list[j]) ) else: # normal one object case if target_cnt == 0: # no longer object link to it, get it from base # switch all shape off and only leave it on old_w_list = [] for n in indexList: old_w_list.append( [ n, bs_fn.weight(n) ] ) # backup bs_fn.setWeight(n, 0) bs_fn.setWeight(n, 1) # get object data target_object_data.append( geoObject_to_posList_OpenMaya(bs_base_obj) ) # from backup for n, w in old_w_list: bs_fn.setWeight(n, w) elif target_cnt == 1: target_object_data.append( geoObject_to_posList_OpenMaya(target_list[0]) ) # - target_data.append(target_object_data) # locator creation def pos_to_loc(pos_list, prefix='tmp'): for i,pos in enumerate(pos_list): cur_loc = cmds.spaceLocator(n='loc_{0}_{1}'.format(prefix,i))[0] cmds.xform(cur_loc, t=pos) for i,targets in enumerate(target_data): pos_to_loc(targets[0], 'tgt'+str(i))
Import and Export animation with atom plugin
# select controls to load animation, then cmds.file("D:/path_to_file/pose_001.atom", i=1, type="atomImport")
Import and Export animation with Maya built-in file import export
cmds.file("D:/path_to_scene/animation_node_only_v001.ma", f=1, typ="mayaAscii", es=1)
cmds.file("D:/path_to_scene/animation_node_only_v002.ma", f=1, typ="mayaAscii", exportSelectedAnim=1)
Get all Animation Curve
for animCurve_type in ['animCurveTA', 'animCurveTL', 'animCurveTU']: curve_list = cmds.ls( type = animCurve_type ) for each_curve in curve_list: cur_connection_out = each_curve + ".output" destinations = cmds.connectionInfo(cur_connection_out, destinationFromSource=1) if len(destinations) == 0: # optional process for unconnected animation node cmds.delete(each_curve) else: cur_node_attr = destinations[0] cur_node, cur_attr = cur_node_attr.split('.') # optional get transform node cur_transform = '' if cmds.nodeType(cur_node) == 'transform': cur_transform = cur_node else: try_parent = cmds.listRelatives(cur_node, p=1) if try_parent: cur_transform = cmds.listRelatives(cur_node, p=1)[0] # optional unlock attribute old_lock = cmds.getAttr(cur_node_attr, l=1) cmds.setAttr(cur_node_attr, l=0) # optional process here cmds.delete(cur_node_attr, staticChannels=1, unitlessAnimationCurves=0, hierarchy="", controlPoints=0, shape=1) # lock back cmds.setAttr(cur_node_attr, lock= old_lock)
Clean Static Animation on object
cmds.delete(cur_node_attr, staticChannels=1, unitlessAnimationCurves=0, hierarchy="", controlPoints=0, shape=1)
Bake Animation
cmds.bakeResults(animation_curve_node,t=(bake_start,bake_end))
cmds.bakeResults(object_node,t=(bake_start,bake_end))
# get all abc node all_abc_nodes = cmds.ls(type='AlembicNode') # check current file path for each in all_abc_nodes: print('------------------') print('For node: {0}'.format(each)) print(cmds.getAttr(each+'.fn')) print(cmds.getAttr(each+'.fns'))
all_abc_nodes = cmds.ls(type='AlembicNode') for each in all_abc_nodes: print('------------------') print('Fixing node: {0}'.format(each)) cur_abc_path = cmds.getAttr(each+'.fn') cmds.setAttr('{0}.fns'.format(each), 1, cur_abc_path, type='stringArray')
import maya.OpenMayaUI as omui import maya.OpenMaya as om import maya.cmds as cmds obj = cmds.ls(sl=1, fl=1)[0] # get first vtx or object, assume you have selected sth pos = om.MPoint(obj)
dagPath = om.MDagPath() # dag path holder omui.M3dView().active3dView().getCamera(dagPath) # get info cam_name = om.MFnDagNode(dagPath.transform()).fullPathName() # get cam name
omui.M3dView().active3dView().portWidth() omui.M3dView().active3dView().portHeight()
''' ========================================== 2019.05.21 by Shining Ying shiningdesign[with]live.com make move tool aim to camera (with object/vtx selection) ========================================== ''' import maya.cmds as cmds import maya.OpenMaya as om import math def getLocalVecToWorldSpaceAPI(obj, vec=om.MVector.xAxis): # ref: http://forums.cgsociety.org/showthread.php?t=1124527 # take obj's local space vec to workspace, default object x axis selList = om.MSelectionList() selList.add(obj) nodeDagPath = om.MDagPath() selList.getDagPath(0, nodeDagPath) matrix = nodeDagPath.inclusiveMatrix() vec = (vec * matrix).normal() return vec.x, vec.y, vec.z def alignObjectToVector(obj, axis, aim_dir, up_guide_dir = None, side_axis=None, side_guide_dir = None ): # grab object transform function lib selectionList = om.MSelectionList() selectionList.add( obj ) dagPath = om.MDagPath() selectionList.getDagPath(0, dagPath) fnTransform = om.MFnTransform(dagPath) # axis dict axis_dict = {'x':om.MVector.xAxis, 'y':om.MVector.yAxis, 'z':om.MVector.zAxis} obj_axis_dir = getLocalVecToWorldSpaceAPI(obj, axis_dict[axis]) obj_axis_vec = om.MVector(*obj_axis_dir) aim_vec = om.MVector(*aim_dir) rot_quaternion = obj_axis_vec.rotateTo(aim_vec) fnTransform.rotateBy(rot_quaternion) # optional 2nd axis setup if up_guide_dir != None and side_axis != None: # based on: aim_vec info up_guide_vec = om.MVector(*up_guide_dir) # make sure aim is not parallel to up_guide_vec if up_guide_vec.isParallel(aim_vec): # then align side x to provided backup side axis if side_guide_dir != None: alignObjectToVector(obj, side_axis, side_guide_dir) else: side_vec = up_guide_vec ^ aim_vec # side dir alignObjectToVector(obj, side_axis, (side_vec.x,side_vec.y,side_vec.z )) cur_selection = cmds.ls(sl=1) #fixCam= 'camera1' fixCam = '' cmds.setToolTo('moveSuperContext') # $gMove cur_cam = cmds.modelEditor(cmds.playblast(activeEditor=1), q=1, camera=1) if fixCam !='': cur_cam = fixCam print('Aim Camera: {0}'.format(cur_cam)) camPos = cmds.xform(cur_cam, q=1, t=1, ws=1) objPos = cmds.manipMoveContext('Move', q=1, position=1) # only work for 1st time selection, then it got 1/10 objPos2 = cmds.xform(cur_selection[0],q=1,t=1,ws=1 ) # override with actual measure if (objPos[0] - objPos2[0])**2 > 0.000001: objPos_maya = objPos objPos = objPos2 print('Maya make mistake on move tool position, I correct it from {0} to {1}'.format(str(objPos_maya),str(objPos2))) aim_dir = [a-b for a,b in zip(camPos,objPos)] up_ref_dir = getLocalVecToWorldSpaceAPI(cur_cam, vec=om.MVector.yAxis) # loc tmp_loc = cmds.spaceLocator(n='tmp_move_loc')[0] cmds.xform(tmp_loc, t=objPos, ws=1) # align alignObjectToVector(tmp_loc, 'z', aim_dir, up_guide_dir = up_ref_dir, side_axis='x') rot_val = cmds.xform(tmp_loc, q=1, ro=1) cmds.delete(tmp_loc) # back to old selection cmds.select(cur_selection) # set tool cmds.setToolTo('moveSuperContext') cmds.manipMoveContext( 'Move', e=1, activeHandle=2, m=6, orientAxes=[math.radians(a) for a in rot_val]) ''' ========================================== 2019.05.21 by Shining Ying shiningdesign[with]live.com make move tool paralell to camera (with object/vtx selection) ========================================== ''' cur_selection = cmds.ls(sl=1) #fixCam= 'camera1' fixCam = '' cmds.setToolTo('moveSuperContext') # $gMove cur_cam = cmds.modelEditor(cmds.playblast(activeEditor=1), q=1, camera=1) if fixCam !='': cur_cam = fixCam print('Aim Camera: {0}'.format(cur_cam)) # loc tmp_loc = cmds.spaceLocator(n='tmp_move_loc')[0] # align cmds.parentConstraint(cur_cam,tmp_loc) rot_val = cmds.xform(tmp_loc, q=1, ro=1) cmds.delete(tmp_loc) # back to old selection cmds.select(cur_selection) # set tool cmds.setToolTo('moveSuperContext') cmds.manipMoveContext( 'Move', e=1, activeHandle=2, m=6, orientAxes=[math.radians(a) for a in rot_val])
cmds.select('defaultRenderGlobals') # get current scene name cur_scene = cmds.file(q=1, sn=1, shn=1) if cur_scene == '': cur_scene = 'untitled' print('Not Name Yet') else: cur_scene = cur_scene.rsplit('.',1)[0] # get render global output name, update to custom name format cur_img_prefix = cmds.getAttr('defaultRenderGlobals.imageFilePrefix') if '<Scene>' in cur_img_prefix: result_txt = cur_img_prefix.replace('<Scene>', cur_scene.rsplit('_',1)[0]) cmds.setAttr('defaultRenderGlobals.imageFilePrefix',result_txt , type='string' )
image_ext = 'png' cmds.setAttr("defaultRenderGlobals.imageFormat", 32) # png scene_path = os.path.dirname(cmds.file(q=True, sceneName=True)) dir_path_info = [scene_path, 'images'] render_root_path = os.path.join(*dir_path_info ).replace('\\','/') cmds.setAttr("defaultRenderGlobals.imageFilePrefix", render_root_path +'/%s', type='string' ) # set as name.####.ext format cmds.setAttr("defaultRenderGlobals.animation",1) # render multi frame cmds.setAttr("defaultRenderGlobals.outFormatControl",0) # use ext cmds.setAttr("defaultRenderGlobals.putFrameBeforeExt",1) # use frame num cmds.setAttr("defaultRenderGlobals.periodInExt",1) # use .ext cmds.setAttr("defaultRenderGlobals.extensionPadding",4) # use padding # set width cmds.setAttr("defaultResolution.w",1920) # use padding cmds.setAttr("defaultResolution.h",1080) # use padding
cmds.renderSettings(firstImageName=1, fullPath=1)
cmds.workspace(q=1, rootDirectory=True) # root proj dir, holding the scene/image/etc folders cmds.workspace(q=1, dir=1) # scene folder full-path # scene path and scene name cmds.file(q=True, sceneName=True) cmds.file(q=True, sceneName=True, shortName=True) # segment subpath cmds.workspace(fr=1,q=1) # list of segment sub names cmds.workspace(fr=['images', 'images']) # set the segment subpath cmds.workspace(fr=['depth', 'renderData/depth']) # set the segment subpath cmds.workspace(fileRuleEntry='depth') # get the segment path
import datetime; time_text = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'); import maya.cmds as cmds; out_path = cmds.renderSettings(firstImageName=1, fullPath=1)[0]; scene_path = cmds.file(q=True, sceneName=True); parent_dir = os.path.dirname(out_path); info_file=parent_dir+'.info'; os.path.exists(os.path.dirname(info_file)) or os.mkdir(os.path.dirname(info_file)); f = open(info_file,'w'); f.writelines([scene_path,'\n',time_text]); f.close(); print('Render Info written to {0}'.format(info_file))
python("import datetime;time_text = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S');import maya.cmds as cmds;out_path = cmds.renderSettings(firstImageName=1, fullPath=1)[0]; parent_dir = os.path.dirname(out_path);info_file=parent_dir+'.info';os.path.exists(os.path.dirname(info_file)) or os.mkdir(os.path.dirname(info_file));scene_path = cmds.file(q=True, sceneName=True);f = open(info_file,'w');f.writelines([scene_path,'\\n',time_text]);f.close();print('Render Info written to '.format(info_file))")
# bake scene name into mel cmd, as render will create a another tmp scene file, which cause get scene name different in script scene_path = cmds.file(q=1, sceneName=True) pre_mel = r'''python("import datetime;time_text = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S');import maya.cmds as cmds;out_path = cmds.renderSettings(firstImageName=1, fullPath=1)[0]; parent_dir = os.path.dirname(out_path);info_file=parent_dir+'.info';os.path.exists(os.path.dirname(info_file)) or os.mkdir(os.path.dirname(info_file));f = open(info_file,'w');f.writelines(['{0}','\\n',time_text]);f.close();print('Render Info written to {0}'.format(info_file))")'''.format(scene_path) # set as per render layer instead pre scene, since each folder has different name cmds.setAttr("defaultRenderGlobals.preRenderLayerMel",pre_mel, type="string")
import maya.mel as mel print(mel.eval('pwd'))
cmds.workspace(dir='d:/proj/current')
cmds.file(f=1,new=1)
cmds.file(filePath, o=True) cmds.file(renameToSave=1) # prevent overwrite save after open
cmds.file(filePath, i=True)
cmds.file(filePath.replace('\\', '/'), r=True, namespace=ns) allRefNode = cmds.ls( references=1) cur_node = allRefNode[0] # get reference used namespace - :MyNameSpace cur_ns= cmds.referenceQuery(cur_node, namespace=1) # get reference used file - C:/user/myfile.ma cur_path = cmds.referenceQuery(cur_node, f=1) # get reference used file name - myfile.ma cur_name = cmds.referenceQuery(cur_node, f=1, shn=1)
mel.eval('MLdeleteUnused')
cmds.file(q=1, sn=1) # filepath cmds.file(q=1, sn=1, un=1) # filepath in variable format cmds.file(q=1, sn=1, shn=1) # filename cmds.file(q=1, sn=1, wcn=1) # withoutCopyNumber
cmds.file(q=1, modified=1) cmds.file(save=1)
# you need to rename first before you can save as if ext.lower() == ".ma": cmds.file(rename = filePath) cmds.file(f=1, save=1, type="mayaAscii") save_done = 1 elif ext.lower() == ".mb": cmds.file(rename = filePath) cmds.file(f=1, save=1, type="mayaBinary") save_done = 1
if format_info == "ma": if self.uiList['publish_selected_check'].isChecked(): cmds.file(filePath, f=1, type="mayaAscii", pr=1, exportSelected=1) else: cmds.file(filePath, f=1, type="mayaAscii", pr=1, exportAll=1) else: if self.uiList['publish_selected_check'].isChecked(): cmds.file(filePath, f=1, type="mayaBinary", pr=1, exportSelected=1) else: cmds.file(filePath, f=1, type="mayaBinary", pr=1, exportAll=1)
global string $gShelfTopLevel; string $currentShelf = `tabLayout -query -selectTab $gShelfTopLevel`; setParent $currentShelf; // - step 2: prepare path, name, icon, cmd string $name="test"; string $ver="v1.0"; string $icon="test.png"; string $cmd ="ls -sl"; // - step 3: create shelf btn shelfButton -c $cmd -ann $name -l $name -imageOverlayLabel $ver -overlayLabelColor 0 0 0 -overlayLabelBackColor 0 0 0 0.0 -image $icon -image1 $icon -sourceType "mel"; shelfButton -c $cmd -ann $name -l $name -imageOverlayLabel $ver -overlayLabelColor 0 0 0 -overlayLabelBackColor 1 1 1 0.5 -image $icon -image1 $icon -sourceType "mel"; string $curBtn = `shelfButton -parent $currentShelf -c $cmd -mi "reload" "print ok" -ann $name -l $name -imageOverlayLabel $ver -overlayLabelColor 0 0 0 -overlayLabelBackColor 0 0 0 0.0 -image $icon -image1 $icon -sourceType "mel"`; string $curPop = `popupMenu -parent $curBtn -button 1 -ctl 1`; // sh, alt, ctl menuItem -p $curPop -l "A A A" -c "print A"; menuItem -p $curPop -l "Reload" -c "print reload";
import maya.cmds as cmds import maya.mel as mel shelf_holder = mel.eval('$tmpVar=$gShelfTopLevel') # get tab holder shelves = cmds.tabLayout(shelf_holder, q=1, selectTab=1) # get selected tab shelves = cmds.tabLayout(shelf_holder, q=1, childArray=1) # names of the layout's immediate children. cmds.shelfTabLayout(e=1, selectTabIndex=3, shelf_holder) # change active tab by index cmds.shelfTabLayout(shelf_holder, e=1, selectTab = shelf_name) # change active tab by name cmds.shelfLayout(shelf_name, ex=True) # check shelf exists by name shelf_name = mel.eval('addNewShelfTab %s' % shelf_name) # add shelf mel.eval('deleteShelfTab %s' % shelf_name) # delete shelf cmds.saveAllShelves(shelf_holder) # save all shelfs
USER_SCRIPT_PATH1=C:/myscripts/whatever/path1 USER_SCRIPT_PATH2=C:/myscripts/whatever/path2 MAYA_SCRIPT_PATH=$MAYA_SCRIPT_PATH;$USER_SCRIPT_PATH1;USER_SCRIPT_PATH2 MAYA_MODULE_PATH = D:\MayaModule\mgear_3.5.1 MAYA_PLUG_IN_PATH = D:\whatever\plugin PYTHONPATH = D:\whatever\scripts
print("\nLoading Pipeline from userSetup.py\n")
print("\nLoading Pipeline from userSetup.mel\n"); $path1 = "C:/myscripts/whatever/path1"; $path2 = "C:/myscripts/whatever/path2"; string $newScriptPath = $path1 + ";" + $path2 + ";" +`getenv "MAYA_SCRIPT_PATH"`; putenv "MAYA_SCRIPT_PATH" $newScriptPath; // or source other config script $script1 = "C:/myscripts/whatever/script1.mel"; $script2 = "C:/myscripts/whatever/script2.mel"; source $script1; source $script2;
// bash alias proj1="export MAYA_SCRIPT_PATH=path/to/scripts:$MAYA_SCRIPT_PATH; maya;" // dos set MAYA_MODULE_PATH=R:/Pipeline/maya/modules; "C:/Program Files/Autodesk/Maya2020/bin/maya.exe" -c "python(\"print('Loading Pipeline from Launch Cmd')\")"
import maya.cmds as cmds import maya.mel as mel import os root_path = r'__________fbx______folder_path' fbx_list = [x for x in os.listdir(root_path ) if x.endswith('.fbx') ] prefix = 'rock_' # prefix for new name for each_fbx in fbx_list: #each_fbx = fbx_list[0] name = each_fbx.split('.',1)[0] # get file name (eg. rockName_1.fbx) id = name.rsplit('_',1)[-1] # get file index number # new file cmds.file(f=1,new=1) cmds.file(os.path.join(root_path, each_fbx), i=1) mel.eval('MLdeleteUnused') anim_list = cmds.ls(type='animCurveTA')+cmds.ls(type='animCurveTL')+cmds.ls(type='animCurveTU') if len(anim_list)>0: cmds.delete(anim_list) # mesh mesh_list = [ cmds.listRelatives(x,p=1)[0] for x in cmds.ls(type='mesh')] new_geo = cmds.rename(mesh_list[0], prefix+str(id)+'_geo') # assume single geo in scene cmds.scale(0.1,0.1,0.1,new_geo) cmds.xform(new_geo, t=(0,0,0),ro=[0,0,0]) cmds.makeIdentity(new_geo, s=1,apply=1) # material mat_node = cmds.shadingNode('lambert', asShader=1, n=prefix+str(id)+'_mat') cmds.select(new_geo) cmds.hyperShade(new_geo, assign=mat_node) cmds.sets(new_geo, e=1, forceElement=mat_node+'SG') # texture file_node = [x for x in cmds.ls(type='file') if x.startswith('Map')][0] # assume the file node named "Map***" new_file = cmds.rename(file_node, prefix+str(id)+'_map') cmds.connectAttr(new_file+'.ot', mat_node+'.it') cmds.connectAttr(new_file+'.oc', mat_node+'.c') # save mel.eval('MLdeleteUnused') cmds.file(rename = os.path.join(root_path, name+'.mb')) cmds.file(f=1, save=1, type='mayaBinary') # ask for verify as it runs res = cmds.confirmDialog(title="Confirm", message="Next?", button=["Yes", "No"], defaultButton="Yes", cancelButton="No", dismissString="No") if res == 'No': break
mel_path = r"D:\Dev\Pipeline\PyMaya\maya_3rd_tool\someMelScript.mel" mel.eval('source "{0}"'.format(mel_path.replace('\\','/')))
# this enable external python tool to ask maya to run a command with variable in maya's scope cmds.python('cmds.select({0})'.format('my_variableName_in_maya'))
# open port in maya import maya.cmds as cmds port = 7001 open_already = 0 try: cmds.commandPort(name=":{0}".format(port), sourceType="python") except: open_already = 1 if open_already == 1: print('Port already open') # send to Maya from external python import socket host = '127.0.0.1' # localhost port = 7001 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((host, port)) client.send('print "cool from external"') # close port in maya cmds.commandPort(name=":7001", close=1)
if not cmds.pluginInfo('myPlugin.py', query=True, loaded=True): cmds.loadPlugin('myPlugin.py')
cmds.undoInfo(openChunk=True) # after end of the operation and before error rise, put cmds.undoInfo(closeChunk=True)
plugin_list = sorted(cmds.pluginInfo(q=1,listPlugins=1)) plugin_list_inUse = sorted(cmds.pluginInfo(q=1,pluginsInUse=1))
oldplugins = cmds.unknownPlugin(q=True, list=True) for plugin in oldplugins: cmds.unknownPlugin(plugin, remove=True)
import sys;print(sys.version) import struct;print( 8 * struct.calcsize("P"))
# check Maya python version and bit import sys;print(sys.version) import struct;print( 8 * struct.calcsize("P")) # check Maya python included path import sys; for each in sys.path: print each # maya's local addition python package place: # example on windows: C:\Program Files\Autodesk\Maya2015\Python\lib\site-packages # or add my downloaded PyQt4 install python, # PyQt4 windows binary link (you can install/extra to any place, just link like below): # http://www.riverbankcomputing.com/software/pyqt/download sys.path.append('D:\z_sys\App_CG\Python27x64\Lib\site-packages') from PyQt4 import QtGui,QtCore
from PySide import QtGui,QtCore
import maya.cmds as cmds from sys import platform import sys import os def getOS(): ''' get os ''' if platform.startswith('win'): return 'windows' elif platform.startswith('darwin'): return 'mac' return 'linux' def getZ(): ''' get z_sys path ''' z_sys=os.getenv('z_sys') z_sys = z_sys.strip() z_sys = z_sys.replace("\\","/") # fix windows path return z_sys sys.path.append(z_sys+"/xTool/resMaya/py") # much better than source
Environment of Python in Maya
import sys sys.path.append("/xTool/resMaya/py") # much better than eval("source "+$scriptName+".mel")
import maya.cmds as cmds from sys import platform import os def getOS(): if platform.startswith('win'): return 'windows' elif platform.startswith('darwin'): return 'mac' return 'linux' def getZ(): ''' get z_sys path ''' # mel, about -os "nt", "win64", "mac", "linux" and "linux64" checkOS = getOS() #if($checkOS == "windows") $z_sys=system("echo %z_sys%"); # windows #else $z_sys=system("printf $z_sys") # linux, osx (bash,csh) echo do the name but with new line z_sys=os.getenv('z_sys') ## remove new line, mel #$a=stringToStringArray($z_sys,"\r"); // windows echo use \r newline #$aS=stringArrayToString($a,""); #$a=stringToStringArray($aS,"\n"); // linux echo use \n newline #$z_sys=$a[0]; z_sys = z_sys.strip() #$z_sys = substituteAllString($z_sys, "\\", "/"); // to normal directory format for windows z_sys = z_sys.replace("\\","/") # make sure system environment got defined if(z_sys!=""): print z_sys return z_sys
word="awesome" print("This is %s" % word) print("This is "+word)
for i in range (1,10): string="string"+str(i)
import maya.cmds maya.cmds.select("ball01",type="mesh") maya.cmds.ls(sl=True)
import myModule myModule.myFunc() # reload reload(myModule)
import maya.mel maya.mel.eval("ls -sl")
string $list[]=python("['a','b','c']"); size($list)
import maya.cmds as cmds
# method 1, filterExpand childs = cmds.listRelatives(root, ad=1, f=1 ) allgeo = cmds.filterExpand(childs, selectionMask=12) # method 2, naming based cmds.select(root) cmds.select(hierarchy=1) tAll=cmds.ls(sl=1,type='transform') allgeo=[] for each in tAll: if(each.endswith('_geo')): allgeo.append(each) #mel:if(`gmatch $each "*_geo"`) $selected[size($selected)]=$each;
selected_attr_list = cmds.channelBox("mainChannelBox",q=1, sma=1)
s="""Long Text 2nd line; """ if(cmds.objExists('base_prefs_null.notes')==False): cmds.addAttr('base_prefs_null',dt='string',ln='notes',sn='nts') cmds.setAttr('base_prefs_null.notes', s, type="string")
bulletGeoGrp=cmds.group(n='bulletGeoGrp',em=1)
cmds.sets([c_start, c_end],add='LocatorSet')
# get parentConstraint source selected = cmds.ls(sl=1)[0] links = cmds.listConnections(selected+'.tx' , type='parentConstraint',s=1) if links is None: links = [] print(links)
def findRelatedSkinCluster( skinObject ): #ref:http://takkun.nyamuuuu.net/blog2/en/archives/592 skinShape = None skinShapeWithPath = None hiddenShape = None hiddenShapeWithPath = None cpTest = cmds.ls( skinObject, typ="controlPoint" ) if len( cpTest ): skinShape = skinObject else: rels = cmds.listRelatives( skinObject ) for r in rels : cpTest = cmds.ls( "%s|%s" % ( skinObject, r ), typ="controlPoint" ) if len( cpTest ) == 0: continue io = cmds.getAttr( "%s|%s.io" % ( skinObject, r ) ) if io: continue visible = cmds.getAttr( "%s|%s.v" % ( skinObject, r ) ) if not visible: hiddenShape = r hiddenShapeWithPath = "%s|%s" % ( skinObject, r ) continue skinShape = r skinShapeWithPath = "%s|%s" % ( skinObject, r ) break if len( skinShape ) == 0: if len( hiddenShape ) == 0: return None else: skinShape = hiddenShape skinShapeWithPath = hiddenShapeWithPath clusters = cmds.ls( typ="skinCluster" ) for c in clusters: geom = cmds.skinCluster( c, q=True, g=True ) for g in geom: if g == skinShape or g == skinShapeWithPath: return c return None
cmds.setKeyframe(testGeo+'.v',time=10, value=False) cmds.setKeyframe(testGeo+'.v',time=11, value=True) # just key the channel cmds.setKeyframe(testGeo+'.v')
# delete all keys cmds.cutKey('locator1.t',cl=1) # clear clear without copy to clipboard # delete some of keys # 1. get key frame list key_list = cmds.keyframe('locator1.tx', q=1) key_list = cmds.keyframe('locator1', q=1, at='tx') # current key value cur_v = cmds.getAttr('locator1.tx', time=1)
startFrame = cmds.playbackOptions(q=1, min=1) endFrame = cmds.playbackOptions(q=1, max=1) for f in range(int(startFrame), int(endFrame+1)): cmds.currentTime(f)
#============================== # creation particle system #============================== mName='bullet' mE_subfix='Emiiter' mP_subfix='Particle' mI_subfix='Instancer' mInMesh_subfix='_InMesh' mFt_subfix='_turbulenceField' mC_loc_subfix='_loc' mC_curve_subfix='_guideCurve' mC_null_subfix='_null' # node subfix mN_mdl_subfix='_mdl' #multDoubleLinear #---------------------- # emitter and particle #---------------------- mE=cmds.createNode('pointEmitter',n=mName+mE_subfix) mP=cmds.createNode('particle',n=mName+mP_subfix) # connect dynamic cmds.connectDynamic(mP,em=mE) cmds.connectAttr('time1.outTime',mP+'.cti') # set initial value e_conf={'emitterType':0,'rate':24,'scaleRateByObjectSize':0,'dx':0,'dz':1,'speed':150} for attr, v in e_conf.items(): cmds.setAttr(mE+'.'+attr, v) #---------------------- # instance #---------------------- mI=cmds.createNode('instancer',n=mName+mI_subfix) cmds.connectAttr(mP+'.instanceData[0].instancePointData', mI+'.inputPoints') # instance Mesh mInMesh=cmds.polyCylinder(n=mName+mInMesh_subfix,ax=[1,0,0]) mMesh=mInMesh[0] mShape=mInMesh[1] cmds.setAttr(mMesh+'.v',0) cmds.connectAttr(mMesh+'.matrix', mI+'.inputHierarchy[0]') cmds.particleInstancer(mP, e=1, name=mI, aimDirection='velocity') #---------------------- # field #---------------------- mFt=cmds.createNode('turbulenceField',n=mName+mFt_subfix) cmds.connectDynamic(mP,f=mFt) ft_conf={'mag':10,'att':0.75,'maxDistance':10} for attr, v in ft_conf.items(): cmds.setAttr(mFt+'.'+attr, v) cmds.expression(s="phy=time*4",o=mFt,ae=1,uc='all')
import math #----------------------------- # aim vector to rotation value #----------------------------- # rotation aimDir=cmds.particle(pName,q=1,at='velocity', order=i) d=math.sqrt(aimDir[0]*aimDir[0]+aimDir[1]*aimDir[1]+aimDir[2]*aimDir[2]) rx=math.degrees(math.asin(aimDir[1]/d))*-1 ry=0 if(aimDir[2]>0): # check +z normal condition, result (-90,90) ry=math.degrees(math.atan(aimDir[0]/aimDir[2])) elif(aimDir[2]==0): # check z=0 xy panel, which rotateX=+-90 condition -90 or 90 or 0 if(aimDir[0]>0): # +xy panel condition ry=90 elif(aimDir[0]<0): # -xy panel condition ry=-90 elif(aimDir[0]==0): # 0=xy panel ry=0 else: # for easy reading only ry=0 elif(aimDir[2]<0): # check -z +-90 condition (-90,-180) and (90,180) and 180 if(aimDir[0]>0): # +xy panel condition ry=math.degrees(math.atan(aimDir[0]/aimDir[2]))+180 elif(aimDir[0]<0): # -xy panel condition ry=math.degrees(math.atan(aimDir[0]/aimDir[2]))-180 elif(aimDir[0]==0): # 0=xy panel condition ry=180 else: # for easy reading only ry=0 else: # for easy reading only ry=0 rz=0 cmds.xform(tGeo,ro=[rx,ry,rz],a=1)
let A (x,y) to A1 (x1,y1) turns degree of a in respect of origin. in Polar coordinate, so, A (o,R), A1 (o1,R) from A1, we have x1=R*cos(o1) y1=R*sin(o1) as o1=o+a, resulting o1 from turn o in a degree, we have x1=R*cos(o1)=R*cos(o+a) y1=R*sin(o1)=R*sin(o+a) based on formula of cos(A+B)=cosA*cosB-sinA*sinB; sin(A+B)=sinA*cosB+cosA*sinB; x1=R*cos(o1)=R*cos(o+a)=R*cos(o)*cos(a)-R*sin(o)*sin(a) y1=R*sin(o1)=R*sin(o+a)=R*sin(o)*cos(a)+R*cos(o)*sin(a) as in Polar coordinate, the R*cos(o) is actually x, R*sin(o) is actually y, so we have x1=x*cos(a)-y*sin(a) y1=y*cos(a)+x*sin(a)=x*sin(a)+y*cos(a) in matrix form, it is same is the question above, Done.
import maya.cmds as cmds from maya.OpenMaya import MVector # function: make selected points into a sphere/circle based on the defined center. points=cmds.ls(sl=1, fl=1) center=cmds.ls(sl=1)[0] pointVectors = [] for pnt in points: v = MVector(*cmds.pointPosition(pnt)) pointVectors.append(v) v_center = MVector(*cmds.pointPosition(center)) pointDistances = [] for v in pointVectors: pointDistances.append((v - v_center).length()) minRadius = min(pointDistances) for pnt,v in zip(points, pointVectors): new_v = (v - v_center).normal()*minRadius + v_center cmds.xform(pnt, ws=1, t=(new_v.x, new_v.y, new_v.z))