I’ve been hitting my head on this for a while. I create a mesh, assign a material, reparent it a bit, and it shows up green in the viewport as if there’s no shader. I have to unassign and reassign the material or reload the scene to get it to work. Anyone have any clues? I’m not sure if I’m assigning the material incorrectly or missing some step in creating the mesh.
If I change the joint to a transform, remove any of the reparenting, or insert the cmds.refresh to draw the viewport it stops happening (but not reliably enough to use as a workaround). It doesn’t always happen the first time, but it always happens if I make a new scene and run it again.
from maya import cmds, OpenMaya as om
def create_mesh():
vertex_array = om.MPointArray()
verts = (
(0,0,0),(10,0,0),(10,10,0),(0,10,0),
(0,0,50),(10,0,50),(10,10,50),(0,10,50))
vertex_array.setLength(len(verts))
for idx, vert in enumerate(verts):
point = om.MPoint(*vert)
vertex_array.set(point, idx)
# Create the index lists for faces.
faces = om.MIntArray()
vertex_counts_array = om.MIntArray()
polys = [[0,1,2,3],[4,5,6,7]]
for poly_idx, poly in enumerate(polys):
vertex_counts_array.append(len(poly))
for idx, vertex_idx in enumerate(poly):
faces.append(vertex_idx)
# Create the shape.
meshFn = om.MFnMesh()
meshMObj = meshFn.create(vertex_array.length(), vertex_counts_array.length(), vertex_array, vertex_counts_array, faces)
return meshFn.name()
def go():
node1 = cmds.createNode('transform')
node2 = cmds.createNode('joint', p=node1)
maya_shape = create_mesh()
maya_transform = cmds.listRelatives(maya_shape, parent=True)[0]
# Doesn't help:
# cmds.select(maya_transform)
# cmds.hyperShade(assign='initialShadingGroup')
cmds.sets(maya_shape, edit=True, forceElement='initialShadingGroup')
cmds.parent(maya_transform, node2)
# cmds.refresh()
cmds.parent(node2, w=True)
go()