top of page

Maya Python : Extract Faces and Apply Material to the Extracted Mesh Result

Using the time honored method of searching google for python snippets, I figured out how to solve a problem that's been bothering me since I started using maya.

Extracting faces returns a selection of both the initial mesh and the mesh result; but I want to apply further operations to the mesh result immediately.

I just started to learn about python, so converting some mel commands to python syntax is step1

In mel extractFace() ... in python it seemed easier to define a function.

def ExFace(a):

mc.ExtractFace(a)

ExFace(sel)

Some people refer to the imported maya commands as cmds. but I prefer mc.

So CenterPivot; becomes mc.CenterPivot(sel) where sel is the selection.

There are various commands to handle selections. This one makes a variable from the current selection: sel = mc.ls(sl=True)

It means run maya command 'list selected' effectively. The weird thing in Maya is it returns two meshes when you extract selected faces. To get the extracted faces, luckily, you just need to get Index 1 in the selection array.

sel_objs = mc.ls(sl = True)

mc.select(sel_objs[1], tgl = True, replace = True)

sel = mc.ls(sl=True)

The argument with the [1] is the last mesh in what's selected, and we're replacing the selection with that. This might not be true if your selection when running the script contains several objects. Most of the time, when I'm needing to run this script, I'm extracting faces from one mesh only.

This next part I found on HighEnd3D and it just adds on to the early part:

//a function to get a shader in the hypergraph

def getSG(shader=None):

if shader:

if mc.objExists(shader):

sg = mc.listConnections(shader, d=True, et=True, t='shadingEngine')

if sg:

return sg[0]

return None

Note the 4 spaces indents (which is how python replaces curly quotes)... but it can make pasting code a pain.

//a function to pick a shader by a name we input

def obtainShader(objList=None, shader=None):

shaderSG = getSG(shader)

if objList:

if shaderSG:

mc.sets(objList, e=True, forceElement=shaderSG)

//a function to let us input a shader name

def applyShader(shader=None):

if sel: //we'll run that previous function if we have a valid selection

obtainShader(sel, shader)

//run our function and enter the shader name of your choice (here, 'Blinn1' is just an example)

applyShader('Blinn1')

The whole thing you can paste into Python part of Script Editor:

import maya.cmds as mc

sel = mc.ls(sl=True)

def ExFace(a):

mc.ExtractFace(a)

ExFace(sel)

mc.CenterPivot(sel)

sel_objs = mc.ls(sl = True)

mc.select(sel_objs[1], tgl = True, replace = True)

sel = mc.ls(sl=True)

def getSG(shader=None):

if shader:

if mc.objExists(shader):

sg = mc.listConnections(shader, d=True, et=True, t='shadingEngine')

if sg:

return sg[0]

return None

def obtainShader(objList=None, shader=None):

shaderSG = getSG(shader)

if objList:

if shaderSG:

mc.sets(objList, e=True, forceElement=shaderSG)

def applyShader(shader=None):

if sel:

obtainShader(sel, shader)

applyShader('Blinn1') //replace Blinn1 with your shader name

As an extra tweak, if you want to finish with the objects involved selected instead of remaining at Face subobject level, add this at the end:

mc.select(sel_objs)

mc.selectedMode(object, tgl = True, replace = True)

Refs

Featured Posts
Recent Posts
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page