Duplicate SG on Selected

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
  02 February 2013
[SOLVED] Duplicate SG on Selected

I have a tonne of objects sharing a few shaders. I want to duplicate the shading networks and assign a unique copy of it to each object.

Currently I'm attempting to do this by processing the list of objects one by one, selecting the assigned SG, duplicating it and reassigning it.

I'm using the pymel hyperShade(dup=True) option to duplicate the SG, which is fine, but it doesn't return the name of the duplicated SG and there's no reliable way for me to predict what it'll be as shader naming conventions are loose. So I'm stalled at the point of reassigning the duplicated shading network to the object.

import pymel.core as pm selObj = pm.selected()
for object in selObj:
    oldSG = pm.listConnections(object.getShape(), type='shadingEngine')[0]
    newSG = pm.hyperShade(dup=True)
    #newSG is None

Any ideas?

Last edited by faultymoose : 02 February 2013 at 11:06 PM.
  02 February 2013
I can do something hokey like rename the original shader temporarily to something random that's not in use, duplicate, then search for originalName1 and reassign it that way, but I am also looking for a clean non-hacky solution.

  02 February 2013
Is there a reason why you use the hypershade duplicate command over the normal duplicate command?


Using the upstreamNodes option it should duplicate all the nodes connecting up to the shading group node. This command also allows you to rename your duplicate.

Hope this helps

Cheers Jake.
  02 February 2013
No reason at all :/ It just never occurred to me to use regular old duplicate. Excuse me while I facepalm...


  02 February 2013
Hm... Actually the duplicate command doesn't return the duplicated object either, still forcing me to rename it to something unique so I can find it to assign. For this purpose, pymel.duplicate and pymel.hypershade(dup=True) do exactly the same thing.

EDIT: According to the doc it should be returning the duplicates. No idea why it isn't. Will reply if I solve it.

  02 February 2013
i don't use pyMel much, but try using the maya.cmds duplicate command instead.

From what I understand about pyMel, you are dealing with python objects, not return values. So you probably need to ask the result of a pyMel duplication for some attribute.

using cmds.duplicate('lambert2') returns ['lambert3']
  02 February 2013
pm.duplicate() returns a list of the duplicated objects. Since I prefer pymel to maya.cmds, objects are what I like, but if you need the names, then..

 dups = pm.duplicate('lambert'2)
 for d in dups:
  	 print d.name()

or if you know you are duplicating a single thing, you could do
dupName = pm.duplicate('lambert'2)[0].name()

  02 February 2013
@David: I also like the objects Working with another OOP language and then coming back to MEL makes my head hurt! But pymel.duplicate is returning an empty list for me.

You can reproduce it fairly easily:

#Create a shader. I just tested with a Lambert but I've had the problem with others.
#Select the shading group node
import pymel.core as pm
print pm.duplicate(un=True)

It successfully duplicates the shading network, but returns an empty list.

EDIT: Just tested with an explicit object name reference and it works as expected.

import pymel.core as pm
print pm.duplicate('nameOfShadingGroup', un=True)
#pm.duplicate(ObjReference, un=True) also works

That successfully returns a list of duplicated nodes. I was using duplicate implicitly with the current selection, but I'll re-write my script to specify the node or node name explicitly.

Thanks all for your replies!

As an aside: I wonder if there's a reason an implicit duplicate doesn't return the duplicated nodes?

Last edited by faultymoose : 02 February 2013 at 11:05 PM.
  02 February 2013
Yeah that seems odd. As you said, easily worked around...

  02 February 2013
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 05:01 PM.

Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.