Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Smooth Mesh Preview reminder

Smooth Mesh Preview reminder

dg3duy
Collaborator Collaborator
317 Views
2 Replies
Message 1 of 3

Smooth Mesh Preview reminder

dg3duy
Collaborator
Collaborator

I'm looking for a script that remembers the state of the objects, if you have on or off the option
Smooth Mesh Preview

If it has it turned on, the problem is to turn back on the objects that had this option activated, and the objects that were deactivated to keep it deactivated.
Keep the initial state of your configuration in that area.

0 Likes
Accepted solutions (1)
318 Views
2 Replies
Replies (2)
Message 2 of 3

Kahylan
Advisor
Advisor
Accepted solution

Hi!

 

The problem isn't really getting the information, because every object is storing it in the "displaySmoothMesh" attribute. The problem is how you store it. A very simple way to do that, is to just create a dummy node that stores the information in an attribute so the dummy node can be found later and the smoothstate restored.

Here is a python script that does this by using a script node to do so (this is a script that I just wrote to demonstrate this, so there is obviously a lot of room for improvement):

import maya.cmds as mc
import ast

def unsmoothAllObjects():
    
    #create storing node
    if mc.objExists("storeSubdiv_ScriptNode"):
        storeNode = "storeSubdiv_ScriptNode"
    else:
        storeNode = mc.createNode("script",n = "storeSubdiv_ScriptNode" )
        mc.addAttr(storeNode, ln = "subdivisionData", dt = "string")
    
    #getting all meshes in scene    
    meshes = mc.ls(type = "mesh")
    
    statesDict = {}
    #storing smoothmeth preview status in dict
    for m in meshes:
        state = mc.getAttr('{0}.displaySmoothMesh'.format(m))
        
    
        statesDict[m] = state
    
        mc.setAttr('{0}.displaySmoothMesh'.format(m), 0)
    #storing dict in attribute on storing Node
    mc.setAttr('{0}.subdivisionData'.format(storeNode), statesDict, type= "string" )
    
def returnSmoothState():
    #getting attribute from storing Node
    strD = mc.getAttr("storeSubdiv_ScriptNode.subdivisionData")
    #converting attribute string to dict
    dict = ast.literal_eval(strD)
    #restoring Smoothmesh status
    for k in dict:
        if mc.objExists(k):
            mc.setAttr('{0}.displaySmoothMesh'.format(k), dict[k])

  the fuctions for creating the Node, storing the values and unsmoothing all objects in the scene and the command for restoring the smoothstate can be triggered as follows:

unsmoothAllObjects()
returnSmoothState()

 

There are also other ways to permanently store variables each with their benefits/downsides. They can for example be bound to a UI, stored using option variables/global variables or exported into a separate file.

 

I hope it helps!

Message 3 of 3

dg3duy
Collaborator
Collaborator
Thank you very much! it is exactly what I was looking for and it works really well!
0 Likes