Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Check if an object has preview smooth mode on and then getting the triangles of it

8 REPLIES 8
Reply
Message 1 of 9
bonelesschicken1
614 Views, 8 Replies

Check if an object has preview smooth mode on and then getting the triangles of it

bonelesschicken1
Participant
Participant

Like the title says, I was wondering how to check if an object is currently in smooth preview mode and if so, getting the triangles of that object. I'm working on a simple script that renames high and low poly meshes for me but I didn't consider the possibility of using smooth preview meshes, it currently gets the triangles of the mesh pre-smooth preview mode. 

0 Likes

Check if an object has preview smooth mode on and then getting the triangles of it

Like the title says, I was wondering how to check if an object is currently in smooth preview mode and if so, getting the triangles of that object. I'm working on a simple script that renames high and low poly meshes for me but I didn't consider the possibility of using smooth preview meshes, it currently gets the triangles of the mesh pre-smooth preview mode. 

Tags (2)
8 REPLIES 8
Message 2 of 9
brentmc
in reply to: bonelesschicken1

brentmc
Autodesk
Autodesk

Hi,

 

The mesh's displaySmoothMesh attribute controls whether the object is displaying the smooth mesh or base mesh.

getAttr pCubeShape1.displaySmoothMesh;

 

The mesh also has an outSmoothMesh attribute that can be used to access the smoothed mesh.

connectAttr pCubeShape1.outSmoothMesh pConeShape1.inMesh;


 

Brent McPherson
Principle Engineer

Hi,

 

The mesh's displaySmoothMesh attribute controls whether the object is displaying the smooth mesh or base mesh.

getAttr pCubeShape1.displaySmoothMesh;

 

The mesh also has an outSmoothMesh attribute that can be used to access the smoothed mesh.

connectAttr pCubeShape1.outSmoothMesh pConeShape1.inMesh;


 

Brent McPherson
Principle Engineer
Message 3 of 9

bonelesschicken1
Participant
Participant

What does connectAttr do in this case? I tried using .outSmoothMesh on a selected object but my console says that there is no 'outSmoothMesh' attribute.

 

import maya.cmds as cmds

selected = cmds.ls(selection=True)[0]
print('selected: ' + selected)

smooth = cmds.getAttr(selected + '.displaySmoothMesh')
print (smooth)

if (smooth == 2):
	print(str(selected.outSmoothMesh))
else:
	print("not smooth")

 

0 Likes

What does connectAttr do in this case? I tried using .outSmoothMesh on a selected object but my console says that there is no 'outSmoothMesh' attribute.

 

import maya.cmds as cmds

selected = cmds.ls(selection=True)[0]
print('selected: ' + selected)

smooth = cmds.getAttr(selected + '.displaySmoothMesh')
print (smooth)

if (smooth == 2):
	print(str(selected.outSmoothMesh))
else:
	print("not smooth")

 

Message 4 of 9
Kahylan
in reply to: bonelesschicken1

Kahylan
Advisor
Advisor

It allows you to query the triangles of the smoothed mesh. The smoothed mesh will give you the unsmoothed triangle count if you use the polyEvaluate() command on it. When connecting the outSmoothMesh attribute (which is located on the shapenode, maybe thats why you couldn't find it.) to the inMesh of an other object, that object basically becomes the shape of the connected object so you can use polyEvaluate on it an get the correct number of triangles

0 Likes

It allows you to query the triangles of the smoothed mesh. The smoothed mesh will give you the unsmoothed triangle count if you use the polyEvaluate() command on it. When connecting the outSmoothMesh attribute (which is located on the shapenode, maybe thats why you couldn't find it.) to the inMesh of an other object, that object basically becomes the shape of the connected object so you can use polyEvaluate on it an get the correct number of triangles

Message 5 of 9

bonelesschicken1
Participant
Participant

Thank you for your reply, sorry for the delay.

 

Are there any other methods to achieve what I want without actually smoothing a mesh to get the tris amount? I've explored other possibilities like the heads up display's tris count but is there any way of accessing that number myself somehow? How is that calculated?

Also, when I tried to do cmds.connectAttr, it resulted in a really odd result - my mesh turned into a strange comet?

0 Likes

Thank you for your reply, sorry for the delay.

 

Are there any other methods to achieve what I want without actually smoothing a mesh to get the tris amount? I've explored other possibilities like the heads up display's tris count but is there any way of accessing that number myself somehow? How is that calculated?

Also, when I tried to do cmds.connectAttr, it resulted in a really odd result - my mesh turned into a strange comet?

Message 6 of 9
bonelesschicken1
in reply to: Kahylan

bonelesschicken1
Participant
Participant

Thank you for your reply, sorry for the delay. (Might be dubble post, whoops)

 

Are there any other methods to achieve what I want without actually smoothing a mesh to get the tris amount? I've explored other possibilities like the heads up display's tris count but is there any way of accessing that number myself somehow? How is that calculated?

Also, when I tried to do cmds.connectAttr, it resulted in a really odd result - my mesh turned into a strange comet?

Thank you for your reply, sorry for the delay. (Might be dubble post, whoops)

 

Are there any other methods to achieve what I want without actually smoothing a mesh to get the tris amount? I've explored other possibilities like the heads up display's tris count but is there any way of accessing that number myself somehow? How is that calculated?

Also, when I tried to do cmds.connectAttr, it resulted in a really odd result - my mesh turned into a strange comet?

Message 7 of 9
brentmc
in reply to: bonelesschicken1

brentmc
Autodesk
Autodesk

Hi,

 

I believe you could access this data using the Maya API MGeometryExtractor class. This is primarily meant for renderers but the primitiveCount method should give you the info you need.

https://help.autodesk.com/view/MAYAUL/2024/ENU/?guid=MAYA_API_REF_cpp_ref_class_m_h_w_render_1_1_m_g...

Note: I haven't tried this myself but that is essentially how the HUD gets this information.

Brent McPherson
Principle Engineer
0 Likes

Hi,

 

I believe you could access this data using the Maya API MGeometryExtractor class. This is primarily meant for renderers but the primitiveCount method should give you the info you need.

https://help.autodesk.com/view/MAYAUL/2024/ENU/?guid=MAYA_API_REF_cpp_ref_class_m_h_w_render_1_1_m_g...

Note: I haven't tried this myself but that is essentially how the HUD gets this information.

Brent McPherson
Principle Engineer
Message 8 of 9
bonelesschicken1
in reply to: brentmc

bonelesschicken1
Participant
Participant

How do I go about accessing the class? Using Python to write my script and I've tried OpenMaya but I don't think I can use it to instantiate MGeometryExtractor.

 
0 Likes

How do I go about accessing the class? Using Python to write my script and I've tried OpenMaya but I don't think I can use it to instantiate MGeometryExtractor.

 
Message 9 of 9
brentmc
in reply to: bonelesschicken1

brentmc
Autodesk
Autodesk

Hi,

 

Here is a quick and dirty sample script that should get you started.
Note: you need to use OpenMaya API v2.0 to access this class. ("import maya.api...")

 

import maya.cmds as cmds
import maya.api.OpenMaya as om
import maya.api.OpenMayaRender as omr

def printNumberOfTriangles(path, base):
    indexDesc = omr.MIndexBufferDescriptor(omr.MIndexBufferDescriptor.kTriangle, "indices", omr.MGeometry.kTriangles)
    positionDesc = omr.MVertexBufferDescriptor("POSITION", omr.MGeometry.kPosition, omr.MGeometry.kFloat, 3)
    requirements = omr.MGeometryRequirements()
    requirements.addVertexRequirement(positionDesc)
    requirements.addIndexingRequirement(indexDesc)
    meshType = 'Display'
    options = omr.MGeometryExtractor.kPolyGeom_Normal # Display mesh
    if base:
        meshType = 'Base'
        options = omr.MGeometryExtractor.kPolyGeom_BaseMesh # Base mesh
    extractor = omr.MGeometryExtractor(requirements, path, options)
    print('%s mesh has %d triangles' % (meshType, extractor.primitiveCount(indexDesc)))

cmds.file(new=True, force=True)
cmds.polyCube()
cmds.select('pCubeShape1')
cmds.setAttr('pCubeShape1.displaySmoothMesh', 2)
sList = om.MGlobal.getActiveSelectionList()
path = sList.getDagPath(0)
printNumberOfTriangles(path, True) # Base mesh
printNumberOfTriangles(path, False) # Display mesh

# Prints:
# Base mesh has 12 triangles
# Display mesh has 192 triangles

 

 

 

Brent McPherson
Principle Engineer

Hi,

 

Here is a quick and dirty sample script that should get you started.
Note: you need to use OpenMaya API v2.0 to access this class. ("import maya.api...")

 

import maya.cmds as cmds
import maya.api.OpenMaya as om
import maya.api.OpenMayaRender as omr

def printNumberOfTriangles(path, base):
    indexDesc = omr.MIndexBufferDescriptor(omr.MIndexBufferDescriptor.kTriangle, "indices", omr.MGeometry.kTriangles)
    positionDesc = omr.MVertexBufferDescriptor("POSITION", omr.MGeometry.kPosition, omr.MGeometry.kFloat, 3)
    requirements = omr.MGeometryRequirements()
    requirements.addVertexRequirement(positionDesc)
    requirements.addIndexingRequirement(indexDesc)
    meshType = 'Display'
    options = omr.MGeometryExtractor.kPolyGeom_Normal # Display mesh
    if base:
        meshType = 'Base'
        options = omr.MGeometryExtractor.kPolyGeom_BaseMesh # Base mesh
    extractor = omr.MGeometryExtractor(requirements, path, options)
    print('%s mesh has %d triangles' % (meshType, extractor.primitiveCount(indexDesc)))

cmds.file(new=True, force=True)
cmds.polyCube()
cmds.select('pCubeShape1')
cmds.setAttr('pCubeShape1.displaySmoothMesh', 2)
sList = om.MGlobal.getActiveSelectionList()
path = sList.getDagPath(0)
printNumberOfTriangles(path, True) # Base mesh
printNumberOfTriangles(path, False) # Display mesh

# Prints:
# Base mesh has 12 triangles
# Display mesh has 192 triangles

 

 

 

Brent McPherson
Principle Engineer

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report