├── .gitignore ├── images ├── .DS_Store ├── figure1c_eye_blink.gif ├── figure2b_eye_scale.gif ├── figure8_all_combined.gif ├── figure1a_lidupperweights.png ├── figure1b_lidlowerweights.png ├── figure2a_eyemoverweights.png ├── figure3b_outmesh_inmesh.png ├── figure7_layer_0_skin_speed.png ├── figure6_stacked_skins_profile.png ├── figure5_outmesh_connected_profile.png ├── figure4_disconnected_skinned_meshes.png ├── figure3a_gpu_points_before_connection.png └── figure3c_gpu_points_after_connection.png.png ├── README.md ├── LICENSE └── skin_merge.py /.gitignore: -------------------------------------------------------------------------------- 1 | .git 2 | .vscode 3 | .DS_Store 4 | *.pyc 5 | __pycache__ 6 | 7 | -------------------------------------------------------------------------------- /images/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kattkieru/medium_def_layers/HEAD/images/.DS_Store -------------------------------------------------------------------------------- /images/figure1c_eye_blink.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kattkieru/medium_def_layers/HEAD/images/figure1c_eye_blink.gif -------------------------------------------------------------------------------- /images/figure2b_eye_scale.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kattkieru/medium_def_layers/HEAD/images/figure2b_eye_scale.gif -------------------------------------------------------------------------------- /images/figure8_all_combined.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kattkieru/medium_def_layers/HEAD/images/figure8_all_combined.gif -------------------------------------------------------------------------------- /images/figure1a_lidupperweights.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kattkieru/medium_def_layers/HEAD/images/figure1a_lidupperweights.png -------------------------------------------------------------------------------- /images/figure1b_lidlowerweights.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kattkieru/medium_def_layers/HEAD/images/figure1b_lidlowerweights.png -------------------------------------------------------------------------------- /images/figure2a_eyemoverweights.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kattkieru/medium_def_layers/HEAD/images/figure2a_eyemoverweights.png -------------------------------------------------------------------------------- /images/figure3b_outmesh_inmesh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kattkieru/medium_def_layers/HEAD/images/figure3b_outmesh_inmesh.png -------------------------------------------------------------------------------- /images/figure7_layer_0_skin_speed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kattkieru/medium_def_layers/HEAD/images/figure7_layer_0_skin_speed.png -------------------------------------------------------------------------------- /images/figure6_stacked_skins_profile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kattkieru/medium_def_layers/HEAD/images/figure6_stacked_skins_profile.png -------------------------------------------------------------------------------- /images/figure5_outmesh_connected_profile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kattkieru/medium_def_layers/HEAD/images/figure5_outmesh_connected_profile.png -------------------------------------------------------------------------------- /images/figure4_disconnected_skinned_meshes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kattkieru/medium_def_layers/HEAD/images/figure4_disconnected_skinned_meshes.png -------------------------------------------------------------------------------- /images/figure3a_gpu_points_before_connection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kattkieru/medium_def_layers/HEAD/images/figure3a_gpu_points_before_connection.png -------------------------------------------------------------------------------- /images/figure3c_gpu_points_after_connection.png.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kattkieru/medium_def_layers/HEAD/images/figure3c_gpu_points_after_connection.png.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | Supporting files and figures for my Medium article, _Deformation Layering in Maya’s Parallel GPU World_. 3 | 4 | skin_merge.py is released under the MIT license. I'd appreciate a message if you find it useful, but that's not required. 5 | 6 | Mesh is from HippyDrome.com, and used only for educational purposes. 7 | 8 | 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The skin_merge.py source code file in this repository falls under the following license: 2 | 3 | Copyright 2017 Charles Warren Wardlaw. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | 11 | -------------------------------------------------------------------------------- /skin_merge.py: -------------------------------------------------------------------------------- 1 | 2 | from maya.api import OpenMaya as om2 3 | from maya.api import OpenMayaAnim as oma2 4 | import pymel.core as pm 5 | 6 | 7 | ## ====================================================================== 8 | def get_deform_shape( ob ): 9 | """ 10 | Gets the visible geometry shape regardless of whether or not 11 | the object is deformed or not. 12 | 13 | :param ob: The object to check. 14 | :returns: The object's deform shape. 15 | """ 16 | 17 | ob = pm.PyNode(ob) 18 | if ob.type() in ['nurbsSurface', 'mesh', 'nurbsCurve']: 19 | ob = ob.getParent() 20 | shapes = pm.PyNode(ob).getShapes() 21 | if len(shapes) == 1: 22 | return( shapes[0] ) 23 | else: 24 | real_shapes = [ x for x in shapes if not x.intermediateObject.get() ] 25 | return( real_shapes[0] if len(real_shapes) else None ) 26 | 27 | 28 | ## --------------------------------------------------------------------- 29 | def get_skin_cluster(ob): 30 | """ 31 | Find the first skinCluster on the object that actually effects it. 32 | When multiple deformers are layered, this means that the latest skin 33 | in the deform chain will be returned. 34 | 35 | In situations where multiple skins are layered through blendshapes, 36 | this function will only return the skin on the current mesh and not 37 | any of the blendshape sources. 38 | 39 | :param ob: The object whose skin you wish to return. 40 | :returns: A skinCluster object, or None. 41 | """ 42 | 43 | shape = get_deform_shape( ob ) 44 | if shape is None: 45 | return(None) 46 | skins = pm.ls( pm.listHistory( shape ), type='skinCluster' ) 47 | if len( skins ): 48 | for skin in skins: 49 | # log( '\t+ Processing %s...' % skin ) 50 | groupId = skin.input[0].groupId.inputs( plugs=True )[0] 51 | outputs = groupId.outputs(plugs=True) 52 | for outp in outputs: 53 | node = outp.node() 54 | if node == shape or node == ob: 55 | ## force neighbors! 56 | skin.weightDistribution.set(1) ## neighbors 57 | return( skin ) 58 | return( None ) 59 | 60 | 61 | ## --------------------------------------------------------------------- 62 | def log( msg, warn=False, error=False ): 63 | if warn and error: 64 | raise ValueError('Please specify only one of warn / error.') 65 | 66 | log_func = om.MGlobal.displayInfo 67 | if warn: 68 | log_func = om.MGlobal.displayWarning 69 | elif error: 70 | log_func = om.MGlobal.displayError 71 | 72 | log_func( msg ) 73 | 74 | 75 | ## ====================================================================== 76 | ## om2 utilities 77 | def get_mobject( name ): 78 | sel = om2.MGlobal.getSelectionListByName( name ) 79 | return sel.getDependNode(0) 80 | 81 | ## --------------------------------------------------------------------- 82 | def get_dag_path( name ): 83 | sel = om2.MGlobal.getSelectionListByName( name ) 84 | return sel.getDagPath(0) 85 | 86 | ## --------------------------------------------------------------------- 87 | def get_mfn_skin( skin_ob ): 88 | if isinstance( skin_ob, pm.PyNode ): 89 | skin_ob = get_mobject( skin_ob.longName() ) 90 | return oma2.MFnSkinCluster( skin_ob ) 91 | 92 | ## --------------------------------------------------------------------- 93 | def get_mfn_mesh( mesh_ob ): 94 | if isinstance( mesh_ob, pm.PyNode ): 95 | mesh_ob = get_mobject( mesh_ob.longName() ) 96 | return om2.MFnMesh( mesh_ob ) 97 | 98 | ## --------------------------------------------------------------------- 99 | def get_complete_components( mesh_ob ): 100 | assert( isinstance(mesh_ob, om2.MFnMesh) ) 101 | comp = om2.MFnSingleIndexedComponent() 102 | ob = comp.create( om2.MFn.kMeshVertComponent ) 103 | comp.setCompleteData( mesh_ob.numVertices ) 104 | return( ob ) 105 | 106 | ## ====================================================================== 107 | def move_skin( source, target ): 108 | source_shape = get_deform_shape( source ) 109 | source_dp = get_dag_path( source_shape.longName() ) 110 | source_skin = get_skin_cluster( source ) 111 | source_mfn = get_mfn_skin( source_skin ) 112 | source_mesh = get_mfn_mesh( get_deform_shape(source) ) 113 | components = get_complete_components( source_mesh ) 114 | 115 | weights, influence_count = source_mfn.getWeights( source_dp, components ) 116 | 117 | pm.select( cl=True ) 118 | target_skin = pm.deformer( target, type='skinCluster', n='MERGED__' + source_skin.name() )[0] 119 | 120 | ## copy over input values / connections 121 | bind_inputs = [ (x.inputs(plugs=True)[0] if x.isConnected() else None) for x in source_skin.bindPreMatrix ] 122 | bind_values = [ x.get() for x in source_skin.bindPreMatrix ] 123 | mat_inputs = [ (x.inputs(plugs=True)[0] if x.isConnected() else None) for x in source_skin.matrix ] 124 | mat_values = [ x.get() for x in source_skin.matrix ] 125 | 126 | for index, bind_value, mat_value in zip( xrange(influence_count), bind_values, mat_values ): 127 | target_skin.bindPreMatrix[index].set( bind_value ) 128 | target_skin.matrix[index].set( mat_value ) 129 | 130 | for index, bind_input, mat_input in zip( xrange(influence_count), bind_inputs, mat_inputs ): 131 | if bind_input: 132 | bind_input >> target_skin.bindPreMatrix[index] 133 | if mat_input: 134 | mat_input >> target_skin.matrix[index] 135 | 136 | ## copy over weights 137 | target_mfn = get_mfn_skin( target_skin ) 138 | target_mesh = get_mfn_mesh( get_deform_shape(target) ) 139 | target_dp = get_dag_path( get_deform_shape(target).longName() ) 140 | components = get_complete_components( target_mesh ) 141 | all_indices = om2.MIntArray( range(influence_count) ) 142 | 143 | target_mfn.setWeights( target_dp, components, all_indices, weights ) 144 | 145 | 146 | ## ====================================================================== 147 | ## main 148 | 149 | items = pm.selected() 150 | 151 | if len(items) == 2: 152 | move_skin( items[0], items[1] ) 153 | 154 | log( 155 | '+ SkinMerge: Complete. Merged skin from {} onto {}.' 156 | .format( *items ) 157 | ) 158 | 159 | else: 160 | log( "-- Please select a skinned mesh and a target mesh.", error=True ) 161 | 162 | --------------------------------------------------------------------------------