PyMel Object-Oriented Duplicate

I am trying to duplicate an object.
Here is an example:

obj6 = obj1[0].duplicate(name="object_6"); 

print obj6;
#Result: [nt.Transform(u'object_6')]

print obj1;
#Result: [nt.Transform(u'object_1'),nt.MakeNurbSphere(u'makeNurbSphere1')]

I also used the following command to duplicate the object, getting the same results.

obj6 = pm.duplicate("object_1", name = "object_6");

My question is the following:
Can i duplicate an object in an object-oriented way and at the same time duplicate the shape node, instead of only the transform node?

I think you are asking for a way to get the shape node in the Return right?

When I run your code, it returns [nt.Transform(u’object_6’)] like you say, but the shape node gets duplicated as well.

If you need to access the shape node, you can use obj6[0].getShape()

Does this work for your needs?

Edit: Added [0] to obj6, since it returns a list.


import pymel.core as pm
obj1 = pm.PyNode('pSphere1')
obj6 = pm.duplicate(obj1, name = "object_6");
print obj6[0].getShape();

[QUOTE=Thios;13824]

My question is the following:
Can i duplicate an object in an object-oriented way and at the same time duplicate the shape node, instead of only the transform node?[/QUOTE]


"""
Try this, duplicates a transform and shape.
A bit naive, but if you're in a sitch where you can make assumptions, it's not horrible.
Note that not all PyNodes implement duplicate, so it's probably not a good general use case
"""
obj1_xf, obj1_shape = pm.polySphere()
obj6 = obj1_xf.duplicate()


Duplicating the node transforms the shapeNode as well.

If you only want specifically the shapeNode, you can get it all in one line:

obj6Shape = pm.duplicate(pm.PyNode('pCube1'), name = "object_6")[0].getShape()

[QUOTE=Thios;13824]I am trying to duplicate an object.
Here is an example:

obj6 = obj1[0].duplicate(name="object_6"); 

print obj6;
#Result: [nt.Transform(u'object_6')]

print obj1;
#Result: [nt.Transform(u'object_1'),nt.MakeNurbSphere(u'makeNurbSphere1')]

I also used the following command to duplicate the object, getting the same results.

obj6 = pm.duplicate("object_1", name = "object_6");

My question is the following:
Can i duplicate an object in an object-oriented way and at the same time duplicate the shape node, instead of only the transform node?[/QUOTE]

The pymel duplicate function will duplicate all children of the selected object by default. The Shape node is the child of the transform node. When you duplicate the transform in this case the shape will also get duplicated. But the returning list from the duplicate function will only contain the transform. So you have to access the children of the transform to get the shape. Hope this code clarifies.



import pymel.core as pmc
pmc.newFile(force=True)

cubeXForm, cubeShape = pmc.polyCube() 
print "cubeXform: %s  cubeShape: %s"%(cubeXForm, cubeShape)

#result cubeXform: pCube1  cubeShape: polyCube1

myCube = pmc.duplicate(cubeXForm, un=True)
print myCube[0].getChildren()

#result [nt.Mesh(u'pCubeShape2')]