Moving collections up and down layer hierarchy using python in Render Setup

Moving collections up and down layer hierarchy using python in Render Setup

licenses
Explorer Explorer
1,634 Views
5 Replies
Message 1 of 6

Moving collections up and down layer hierarchy using python in Render Setup

licenses
Explorer
Explorer

Pretty much as it says in the title, does anyone know how to move collections up and down layer hierarchy using python in Render Setup?

 

Reason I ask is so that I can blow away the spurious '_untitled' collection that gets generated when you save a scene and do not have the 'master scene' selected to view.  If the '_untitled' collection is not at the bottom of the hierarchy when you delete it, everything below gets removed too!

 

Any help appreciated.

 

Thanks

 

Craig

0 Likes
Accepted solutions (1)
1,635 Views
5 Replies
Replies (5)
Message 2 of 6

cheng_xi_li
Autodesk Support
Autodesk Support

Hi,

 

I tried to reproduce this issue, but I am not sure about "If the '_untitled' collection is not at the bottom of the hierarchy when you delete it, everything below gets removed too!".

 

The _untitled_ collection is created when master layer isn't selected and creating objects in the scene. I tried to remove the _untitled_ later. It removes the children below it as expected. Are you trying to preseve the children? Could you give me more details?

 

BTW: In Maya 2018 Update 3, there is a new option to disable creating _untitled_ automatically.

 

Yours,

Li

0 Likes
Message 3 of 6

licenses
Explorer
Explorer

Hi Li, thanks for the reply.

 

My point about preserving the children is that, it's not just children that are removed, it's anything that is lower down in the layer list.

 

Say I had 3 layers, beauty, fur, shadows and we're currently viewing the beauty layer, I hit 'Save' and an empty '_untitled_' collection is created above an existing collection 'bty_Environment' in this layer (see image 1), when I run cmds.delete('_untitled_') as a post save cleanup, 'bty_Environment' is deleted too.

 

MayaRenderSetupIssue.PNG

 

If I was to move '_untitled_' to the bottom of the layer heirarchy (see image 2) and run cmds.delete('_untitled_') - then only that empty collection is removed, so in order to do this as an auto post save cleanup, I need to work out a way of moving the _untitled_ collection down, using python without human interaction.

 

MayaRenderSetupIssue2.PNG

 

I am aware that this has been addressed in 2018 but we cannot currently move to that version until the pipeline has been fully moved across to support it.

 

Thanks

 

0 Likes
Message 4 of 6

licenses
Explorer
Explorer

Hi Li, thanks for the reply.

 

My point about preserving the children is that, it's not just children that are removed, it's anything that is lower down in the layer list.

 

Say I had 3 layers, beauty, fur, shadows and we're currently viewing the beauty layer, I hit 'Save' and an empty '_untitled_' collection is created above an existing collection 'bty_Environment' in this layer (see image 1), when I run cmds.delete('_untitled_') as a post save cleanup, 'bty_Environment' is deleted too.

 

MayaRenderSetupIssue.PNG

 

If I was to move '_untitled_' to the bottom of the layer heirarchy (see image 2) and run cmds.delete('_untitled_') - then only that empty collection is removed, so in order to do this as an auto post save cleanup, I need to work out a way of moving the _untitled_ collection down, using python without human interaction.

 

MayaRenderSetupIssue2.PNG

 

I am aware that this has been addressed in 2018 but we cannot currently move to that version until the pipeline has been fully moved across to support it.

 

Thanks

 

0 Likes
Message 5 of 6

cheng_xi_li
Autodesk Support
Autodesk Support
Accepted solution

Hi,

 

I remembered renderSetup's data is related to each other, so you can't just use delete command.

 

There are some APIs provided for renderSetup in Python, you could use APIs to detach the collections and delete them like below:

 

import maya.app.renderSetup.model.collection as collection
import maya.app.renderSetup.model.renderLayer as renderLayer
import maya.app.renderSetup.model.renderSetup as renderSetup
import maya.cmds as cmds

rs = renderSetup.instance()  

layers = rs.getRenderLayers()

for layer in layers:
    collections  = layer.getCollections()
    for collection in collections:
        name = collection.name()
        if "_untitled_" in name:           
            layer.detachCollection(collection)
            cmds.delete(name)

It should remove all collections named with "_untitled_*" directly under the renderLayers.

 

I can't reproduce the way you've mentioned with Maya 2017 Update 5. So, I tested with _untitled_ collections created when active layer wasn't master layer. Using delete will remove the collection below like you mentioned. The script was working fine with that(remove all _untitled_ layers without deleting other collections below them)

 

Yours,

Li

Message 6 of 6

licenses
Explorer
Explorer

Amazing, thanks Li!

 

It was - detachCollection(collection) that I was looking for, works perfectly.

 

Regards

Craig

0 Likes