0
$\begingroup$

I need to mute all Transformation,Copy Location,Copy Rotation,and Copy Scale constraints to get pure rest_length number of Stretch To constraint, but there are lots of constraints so I need to use this python script.

I am using this python script. It resets selected bones' transformation and mutes Transformation,Copy Location,Copy Rotation,and Copy Scale constraints, and saves processed bones' names and constraints' names.After that it resets all rest_length of Stretch To constraints and re-activate Transformation,Copy Location,Copy Rotation,and Copy Scale constraints again.

But this just mutes Transformation,Copy Location,Copy Rotation,and Copy Scale and enable them all again. Seems Stretch Constraints are untouched. Do you know why?

import bpy

context = bpy.context
for b in context.selected_pose_bones_from_active_object:
    b.matrix_basis = Matrix() 
    
names =  set()
for b in bpy.context.selected_pose_bones:
    for c in b.constraints: 
        if c.type in ["TRANSFORM", "COPY_ROTATION", "COPY_LOCATION","COPY_SCALE"]:
            c.mute = True
            names.add(b.name)
            names.add(c.name)

for b in bpy.context.selected_pose_bones:     
    for c in b.constraints: 
            if c.type == "STRETCH_TO":
                c.rest_length = 0.0
             
for b in bpy.context.selected_pose_bones:  
    if b.name in names:
        for c in b.constraints: 
            if c.type in ["TRANSFORM", "COPY_ROTATION", "COPY_LOCATION","COPY_SCALE"]:
                if c.name in names:
                    c.mute = False
$\endgroup$

1 Answer 1

0
$\begingroup$

this works fine. Seems bpy.ops.screen.frame_jump() needs to be done before resetting Stretch to constraint.

import bpy
from mathutils import Matrix

# Reset Bones Transformation
context = bpy.context
for b in context.selected_pose_bones_from_active_object:
    b.matrix_basis = Matrix() 

# Mute other constraints and save names
names = set()
for b in bpy.context.selected_pose_bones:
    for c in b.constraints: 
        if c.type in ["TRANSFORM", "COPY_ROTATION", "COPY_LOCATION","COPY_SCALE"]:
            c.mute = True
            names.add(b.name)
            names.add(c.name)

# Update the constraints by jumping to the current frame
bpy.ops.screen.frame_jump()

# Reset rest length of stretch to constraints
for b in bpy.context.selected_pose_bones:     
    for c in b.constraints: 
        if c.type == "STRETCH_TO":
            c.rest_length = 0.0

# Mute constraints with saved names
for b in bpy.context.selected_pose_bones:  
    if b.name in names:
        for c in b.constraints: 
            if c.type in ["TRANSFORM", "COPY_ROTATION", "COPY_LOCATION","COPY_SCALE"]:
                if c.name in names:
                    c.mute = True

# Update the constraints by jumping to the current frame
bpy.ops.screen.frame_jump()

# Unmute constraints with saved names
for b in bpy.context.selected_pose_bones:  
    if b.name in names:
        for c in b.constraints: 
            if c.type in ["TRANSFORM", "COPY_ROTATION", "COPY_LOCATION","COPY_SCALE"]:
                if c.name in names:
                    c.mute = False
$\endgroup$
2
  • $\begingroup$ This is an interesting way to do this, but I wonder how essential the muting function is. For my own uses I have a muscle deform rig that has a lot of stretch-tos and I'd like to have a button that pops up when the rig is in rest mode and I can just reset all of them at once. $\endgroup$ Apr 8 at 20:23
  • $\begingroup$ Sometimes other constraints changes bone's position so muting them all is needed. $\endgroup$
    – Yokomizo
    May 2 at 7:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .