Skip to content

Instantly share code, notes, and snippets.

@killiantimsit
Last active February 14, 2019 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killiantimsit/248eeab17e4785567513500984caf8fd to your computer and use it in GitHub Desktop.
Save killiantimsit/248eeab17e4785567513500984caf8fd to your computer and use it in GitHub Desktop.
MAXON Cinema 4D - Clean up joint's weights for AR compatibility
# MAXON Cinema 4D
# Clean up joint's weights for AR compatibility
#
# This script will set to zero the weights of all but 4
# joints/bones for each point in the selected Weight tag.
# As a result, the weights will be unnormalized. So you'll
# probably want to normalize all weights after using this
# script.
#
# NB: The weights table in the Weight Manager UI tab
# won't update automatically after using the script.
# Please click inside the Weight Manager tab to see
# the updated values.
import c4d
import operator
def main():
active_doc = c4d.documents.GetActiveDocument()
doc_sel = active_doc.GetSelection()
sel_len = len(doc_sel)
if sel_len != 1 or doc_sel[0].GetTypeName() != 'Weight' :
c4d.gui.MessageDialog('''Error: Please select a single Weight tag.
There is currently {0} object(s) selected.'''.format(sel_len))
return
weight_tag = doc_sel[0]
active_doc.StartUndo()
active_doc.AddUndo(c4d.UNDOTYPE_CHANGE, weight_tag)
mesh_obj = weight_tag.GetObject()
joint_count = weight_tag.GetJointCount()
for point_id in range(mesh_obj.GetPointCount()):
joint_weights = {joint_id: weight_tag.GetWeight(joint_id, point_id)
for joint_id in range(joint_count)}
new_weights = sorted(joint_weights.items(),
key=operator.itemgetter(1),
reverse=True)
for joint_id in range(4, joint_count):
joint_to_prune_id = new_weights[joint_id][0]
new_weights[joint_id] = (joint_to_prune_id, 0.0)
new_weights = dict(new_weights)
for joint_id in range(joint_count):
weight_tag.SetWeight(joint_id, point_id, new_weights[joint_id])
active_doc.EndUndo()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment