Instantiating A Maya Node In Pymel (PyMel)
· Def:-
To instantiate a Maya node as PyNode using PyMel. There are two ways two instantiate it.
· Implicit:- using PyNode class .
· Explicit:- finding the nodeType of Maya node and instantiate it using its respective PyMel node class.
· Tip:-
If majorly(>= 90%) single type of nodes are get used in the code, we can explicitly define equivalent PyMel instance rather than using Implicit way.
This way we will save our-self from look ups PyNode class does to create a node, eg. it validates the nodeType of maya node before creating the
instance out of it.
This technique can be visualized equivalent to except statement where we explicitly define the exception class(if we know it before hand).
· Code:-
import pymel.core as pm
# some maya node name
nodeName = 'null1'
# explicit node type
expNodeTyp = 'transform'
# resulting pyNode
py_node = ''
# maya node type matches explicit node type
if pm.nodeType(nodeName) in expNodeTyp:
# explicity create a node
py_node = pm.nt.Transform(nodeName)
else:
# otherwise, use PyNode class
py_node = pm.PyNode(nodeName)
· Note:-
Don't over do(don't check multiple types explicitly) the technique as it make code inefficient, instead use PyNode class.
eg.
import pymel.core as pm
# some maya node name
nodeName = 'null1'
# explicit node type, over doing of the types
expNodeTyp = ['transform', 'mesh', 'camera']
# resulting pyNode
py_node = ''
# maya node type matches explicit node type
# transform node
if pm.nodeType(nodeName) == expNodeTyp[0]:
# explicity create a node
py_node = pm.nt.Transform(nodeName)
# polygon shape node
elif pm.nodeType(nodeName) == expNodeTyp[1]:
# explicity create a node
py_node = pm.nt.Mesh(nodeName)
# camera node
elif pm.nodeType(nodeName) == expNodeTyp[2]:
# explicity create a node
py_node = pm.nt.Camera(nodeName)
else:
# otherwise, use PyNode class
py_node = pm.PyNode(nodeName)
To instantiate a Maya node as PyNode using PyMel. There are two ways two instantiate it.
· Implicit:- using PyNode class .
· Explicit:- finding the nodeType of Maya node and instantiate it using its respective PyMel node class.
· Tip:-
If majorly(>= 90%) single type of nodes are get used in the code, we can explicitly define equivalent PyMel instance rather than using Implicit way.
This way we will save our-self from look ups PyNode class does to create a node, eg. it validates the nodeType of maya node before creating the
instance out of it.
This technique can be visualized equivalent to except statement where we explicitly define the exception class(if we know it before hand).
· Code:-
import pymel.core as pm
# some maya node name
nodeName = 'null1'
# explicit node type
expNodeTyp = 'transform'
# resulting pyNode
py_node = ''
# maya node type matches explicit node type
if pm.nodeType(nodeName) in expNodeTyp:
# explicity create a node
py_node = pm.nt.Transform(nodeName)
else:
# otherwise, use PyNode class
py_node = pm.PyNode(nodeName)
· Note:-
Don't over do(don't check multiple types explicitly) the technique as it make code inefficient, instead use PyNode class.
eg.
import pymel.core as pm
# some maya node name
nodeName = 'null1'
# explicit node type, over doing of the types
expNodeTyp = ['transform', 'mesh', 'camera']
# resulting pyNode
py_node = ''
# maya node type matches explicit node type
# transform node
if pm.nodeType(nodeName) == expNodeTyp[0]:
# explicity create a node
py_node = pm.nt.Transform(nodeName)
# polygon shape node
elif pm.nodeType(nodeName) == expNodeTyp[1]:
# explicity create a node
py_node = pm.nt.Mesh(nodeName)
# camera node
elif pm.nodeType(nodeName) == expNodeTyp[2]:
# explicity create a node
py_node = pm.nt.Camera(nodeName)
else:
# otherwise, use PyNode class
py_node = pm.PyNode(nodeName)
HTML Comment Box is loading comments...