Reinnard Dinata
Reinnard Dinata

Reputation: 43

How to add selected objects to renderLayerSetup in Maya 2017 with Python?

I'm trying to make a Python code to render only selected objects in Maya, and want to know how to add selected objects to renderLayerSetup in Maya 2017 ?

I've tried to use some code I found

import maya.app.renderSetup.model.renderSetup as renderSetup
rs = renderSetup.instance()
test = rs.createRenderLayer('render')
scene_Assets = test.createCollection('scene_Assets')
scene_Assets.getSelector().setPattern('name')

but this code required me to use the objects name to add to the collection

I expecting the output to be able to add objects to collection without having to rename all the names.

Upvotes: 0

Views: 1195

Answers (2)

Sigrid Mohn
Sigrid Mohn

Reputation: 21

Bit of an old thread, just stumbled across it as I am googling around for solutions to my own little headache and figured I could probably help. (If anyone has any tips on how to get a hold of the collection objects within a collection please throw me a message. The pattern I can find but not the ones that have been added through add or drag and drop.)

Anyway I am actually a bit new to programming so this might not be the most optimal solution, but it works in Maya 2018.6 (the above answer didn't work for me), so it might help someone:

import maya.app.renderSetup.model.renderSetup as renderSetup
import maya.cmds as mc

rs = renderSetup.instance()

#taking the selection and bring it through a for loop to format it.
selectionLi = mc.ls(sl=1)
selection=""
for each in selectionLi:
    selection += each +", "

test = rs.createRenderLayer('render')
scene_Assets = test.createCollection('scene_Assets')
scene_Assets.getSelector().setPattern(str(selection))

Upvotes: 2

g2m.agent
g2m.agent

Reputation: 180

this is my code, it works in Maya 2019.1

import maya.app.renderSetup.model.override as override
import maya.app.renderSetup.model.selector as selector
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

# 連接 render setup,如無,則新建
rs = renderSetup.instance()

# 連接 render layer,如無,則新建
try: rl = rs.getRenderLayer("previewLayer") # 如成功:返回實例;如失敗:拋出異常
except: rl = rs.createRenderLayer("previewLayer")

# 連接對象集合 collection,如無,則新建
try: c1 = rl.getCollectionByName("previewCollection")
except:  c1 = rl.createCollection("previewCollection")

# 連接選擇器 selector
sl = c1.getSelector()

# staticSelection
ss = sl.staticSelection

# add the selection to list
ss.add(cmds.ls(sl=1))

# remove all static selection from the list
ss.remove(ss.asList())

Upvotes: 0

Related Questions