def create_locator(name): pm.select(None) global THU_MFT_LOCATOR_MATERIAL_NAME, THU_MFT_LOCATOR_ATTR try: material = pm.PyNode(THU_MFT_LOCATOR_MATERIAL_NAME) except: material = pm.createSurfaceShader( 'lambert', name=THU_MFT_LOCATOR_MATERIAL_NAME)[0] material.color.set((1, 0, 0)) material.transparency.set((0.8, 0.8, 0.8)) loc_name = '%s_%s' % (name, THU_MFT_LOCATOR_ATTR) try: locator_trans = pm.PyNode(loc_name) except: locator_trans = pm.polyPlane(name=loc_name, sw=1, sh=1)[0] pm.sets(material.shadingGroups()[0], edit=True, forceElement=locator_trans) if (not locator_trans.hasAttr(THU_MFT_LOCATOR_ATTR)): locator_trans.addAttr(THU_MFT_LOCATOR_ATTR, dt='string') locator_trans.setAttr(THU_MFT_LOCATOR_ATTR, name) return locator_trans
def create_locator( name ) : pm.select( None ) global THU_MFT_LOCATOR_MATERIAL_NAME, THU_MFT_LOCATOR_ATTR try : material = pm.PyNode( THU_MFT_LOCATOR_MATERIAL_NAME ) except : material = pm.createSurfaceShader( 'lambert', name=THU_MFT_LOCATOR_MATERIAL_NAME )[0] material.color.set( ( 1, 0, 0 ) ) material.transparency.set( ( 0.8, 0.8, 0.8 ) ) loc_name = '%s_%s' % ( name, THU_MFT_LOCATOR_ATTR ) try : locator_trans = pm.PyNode( loc_name ) except : locator_trans = pm.polyPlane( name=loc_name, sw=1, sh=1 )[0] pm.sets( material.shadingGroups()[0], edit=True, forceElement=locator_trans ) if( not locator_trans.hasAttr( THU_MFT_LOCATOR_ATTR ) ) : locator_trans.addAttr( THU_MFT_LOCATOR_ATTR, dt='string' ) locator_trans.setAttr( THU_MFT_LOCATOR_ATTR, name ) return locator_trans
def create_blinn(self): """ creates a blinn for observing how light interacts with the surface of an object """ oSel = pm.ls(sl=True) self.normal_shader = pm.shadingNode('blinn', asShader=True) self.normal_shading_group = pm.sets(renderable=True, noSurfaceShader=True, empty=True) pm.connectAttr('%s.outColor' % self.normal_shader, '%s.surfaceShader' % self.normal_shading_group) for objs in oSel: pm.select(objs) pm.hyperShade(assign='%s' % self.normal_shader) pm.select(oSel)
def create_mesh( xml_path, name, locator=None ) : pm.select( None ) tree = et.parse( xml_path ) root_keys = tree.getroot().keys() if( not 'imagePath' in root_keys ) : pm.error( 'The XML file does not appear to be a Texture Packer XML' ) texture_size = ( tree.getroot().get( 'width' ), tree.getroot().get( 'height' ) ) sprite = tree.find( ".//*[@n='%s']" % ( name ) ) material = create_material( xml_path ) if( not len( material.shadingGroups() ) ) : pm.error( 'Material %s is not connected to a Shading Group. Aborting.' ) plane_name = __get_filename_noext( name ) + '_G' if( locator ) : plane_name = '%s_%s' % ( locator.name(), plane_name ) try : plane_trans = pm.PyNode( plane_name ) except : # v = pm.datatypes.Vector( float(sprite.get('w')) / 100.0, float(sprite.get('h')) / 100.0 ) # v = v.normal() w_scale = float(sprite.get('w')) / 100.0 h_scale = float(sprite.get('h')) / 100.0 w_h = w_scale / h_scale h_w = h_scale / w_scale if( w_h > h_w ) : wp = 1.0 / w_h # print wp w_scale = 1.0 h_scale = 1.0 / w_h # print 'w_h', w_scale, h_scale else : hp = 1.0 / h_w # print hp h_scale = 1.0 w_scale = 1.0 / h_w # print w_h, h_w # print 'h_w', w_scale, h_scale plane_trans = pm.polyPlane( name=plane_name, sw=1, sh=1, w=w_scale, h=h_scale )[0] plane_shape = plane_trans.getShape() if( not plane_trans.hasAttr( THU_MFT_SPRITE_ATTR ) ) : plane_trans.addAttr( THU_MFT_SPRITE_ATTR, dt='string' ) plane_trans.setAttr( THU_MFT_SPRITE_ATTR, name.replace( '.png', '' ) ) pm.sets( material.shadingGroups()[0], edit=True, forceElement=plane_trans ) sx = ( float( sprite.get( 'x' ) ) / float( texture_size[0] ) ) sy = 1 - ( float( sprite.get( 'y' ) ) / float( texture_size[1] ) ) sw = ( float( sprite.get( 'w' ) ) / float( texture_size[0] ) ) sh = ( float( sprite.get( 'h' ) ) / float( texture_size[1] ) ) uv_positions = ( ( sx, sy - sh ), ( sx + sw, sy - sh ), ( sx, sy ), ( sx + sw, sy ) ) for uv, uv_position in zip( plane_shape.uvs, uv_positions ) : pm.polyEditUV( uv, r=False, u=uv_position[0], v=uv_position[1] ) if( locator ) : # print locator plane_trans.setParent( locator ) plane_trans.setTranslation( ( 0, 0, 0 ) ) plane_trans.setRotation( ( 0, 0, 0 ) ) plane_trans.setScale( ( 1, 1, 1 ) ) locator_bounds = locator.getBoundingBox() plane_bounds = plane_trans.getBoundingBox() if( plane_bounds.width() > plane_bounds.height() ) : pass else : s = locator_bounds.height() / plane_bounds.height() # plane_trans.setScale( (s, s, s) ) pm.select( plane_trans ) return plane_trans
def splitByMaterials( mesh, outputGrp ) : materials = mesh.listConnections(type=pm.nodetypes.ShadingEngine) print mesh.name() materials = list(set(materials)) for material in materials : # print '----- ' + material.name() __st(1) faces = material.members( flatten=True ) outmesh = None for facelist in faces : if( type( facelist ) is pm.Mesh ) : facelist = facelist.f[:] shape = facelist.node() if( shape == mesh ) : __st(6) duplicate = facelist.node().getTransform().duplicate()[0] if( duplicate is None ) : pm.warning( "%s could not be duplicated" % mesh.name() ) continue duplicate.setParent(outputGrp) dupfacelist = facelist.indices() dupallfaces = duplicate.f[:].indices() deletefacelist = duplicate.f[facelist.indices()] __et(6, 'initTime') __st(2) # deletefacelist = [ duplicate.f[face] for face in dupallfaces if face not in dupfacelist ] pm.select(deletefacelist) pm.runtime.InvertSelection() # deletefacelist = pm.ls(sl=True, fl=True) __et(2, 'obtainFaceListTime') __st(3) pm.delete() __et(3, 'deleteFaceTime') # pm.filterExpand(dupfacelist, sm=34) __st(4) # pm.mel.select( duplicate.f[:] ) # pm.hyperShade(assign=material) pm.sets( material, fe=duplicate.f[:] ) __et(4, 'assignMaterialTime') outmesh = duplicate __st(5) try: outmeshGrp = pm.PyNode( '%s|%s' % ( OUTPUT_GRP_NAME, material.name() + '_split' ) ) except (pm.MayaNodeError) : outmeshGrp = pm.group(name=material.name() + '_split', parent=pm.PyNode(OUTPUT_GRP_NAME), empty=True) outmesh.setParent(outmeshGrp) __et(5, 'tidyUpTime') __et( 1, material.name() + ' done' )
def create_mesh(xml_path, name, locator=None): pm.select(None) tree = et.parse(xml_path) root_keys = tree.getroot().keys() if (not 'imagePath' in root_keys): pm.error('The XML file does not appear to be a Texture Packer XML') texture_size = (tree.getroot().get('width'), tree.getroot().get('height')) sprite = tree.find(".//*[@n='%s']" % (name)) material = create_material(xml_path) if (not len(material.shadingGroups())): pm.error('Material %s is not connected to a Shading Group. Aborting.') plane_name = __get_filename_noext(name) + '_G' if (locator): plane_name = '%s_%s' % (locator.name(), plane_name) try: plane_trans = pm.PyNode(plane_name) except: # v = pm.datatypes.Vector( float(sprite.get('w')) / 100.0, float(sprite.get('h')) / 100.0 ) # v = v.normal() w_scale = float(sprite.get('w')) / 100.0 h_scale = float(sprite.get('h')) / 100.0 w_h = w_scale / h_scale h_w = h_scale / w_scale if (w_h > h_w): wp = 1.0 / w_h # print wp w_scale = 1.0 h_scale = 1.0 / w_h # print 'w_h', w_scale, h_scale else: hp = 1.0 / h_w # print hp h_scale = 1.0 w_scale = 1.0 / h_w # print w_h, h_w # print 'h_w', w_scale, h_scale plane_trans = pm.polyPlane(name=plane_name, sw=1, sh=1, w=w_scale, h=h_scale)[0] plane_shape = plane_trans.getShape() if (not plane_trans.hasAttr(THU_MFT_SPRITE_ATTR)): plane_trans.addAttr(THU_MFT_SPRITE_ATTR, dt='string') plane_trans.setAttr(THU_MFT_SPRITE_ATTR, name.replace('.png', '')) pm.sets(material.shadingGroups()[0], edit=True, forceElement=plane_trans) sx = (float(sprite.get('x')) / float(texture_size[0])) sy = 1 - (float(sprite.get('y')) / float(texture_size[1])) sw = (float(sprite.get('w')) / float(texture_size[0])) sh = (float(sprite.get('h')) / float(texture_size[1])) uv_positions = ((sx, sy - sh), (sx + sw, sy - sh), (sx, sy), (sx + sw, sy)) for uv, uv_position in zip(plane_shape.uvs, uv_positions): pm.polyEditUV(uv, r=False, u=uv_position[0], v=uv_position[1]) if (locator): # print locator plane_trans.setParent(locator) plane_trans.setTranslation((0, 0, 0)) plane_trans.setRotation((0, 0, 0)) plane_trans.setScale((1, 1, 1)) locator_bounds = locator.getBoundingBox() plane_bounds = plane_trans.getBoundingBox() if (plane_bounds.width() > plane_bounds.height()): pass else: s = locator_bounds.height() / plane_bounds.height() # plane_trans.setScale( (s, s, s) ) pm.select(plane_trans) return plane_trans