0
$\begingroup$

Some background as to why I want to be able to do this: I've written a script that finds and replaces specific widgets with my own custom made ones (I read the names of existing objects inside the widget collection, and then replace all with a specific prefix by a custom mesh). Every time I want to regenerate my rig, and run the script to replace all the widgets, I also need to delete the original widgets, and purge the orphan data. This is because when you press the rigify "Generate rig" button, it will generate objects with the correct names, but the meshes themselves will be named .002 instead of .001, because all the orphan mesh data means they can't be named the original.

So, instead of manually deleting the widgets, purging the orphan data, selecting the mesh and generating the rig each time... Instead, if I could simply use select_set(True) on my metarig, and activate the "Generate Rig" from a script, then I could let my script handle everything from the the widget deletion, orphan purging, rig generation, and widget replacement, to eye parenting, driver assignment and bone constraints all in one, which would save me heaps of time!

Thanks in advance!

$\endgroup$
1
$\begingroup$

Well, I guess I'll be answering my own question! After reading up the barely commented documentation, I figured out how to do this. It's very likely that what I have done is very inefficient, but it works. Here's the code I concluded with!

RigGen = True #Just in case I don't want to replace the rig every time

#Define a deselect command for all selected items - it'll be useful later.

def DeselAll():
    for obj in bpy.context.selected_objects:
        obj.select_set(False)
DeselAll()

##Then, we delete the original widgets found in the "Widgets" collection
if RigGen == True:
    widg_del = bpy.data.collections["Widgets"].objects
    for obj in widg_del:
        bpy.data.objects.remove(obj)
    
    #Then, we removes all orphans stored in the meshes data block 
    for block in bpy.data.meshes:
        if block.users == 0:
            bpy.data.meshes.remove(block)
        
#At this point, the context has been changed, so we need to change it back to object mode
#Else, our rig generation will not work unless you hover your mouse over the 3d viewport.
    bpy.ops.object.mode_set(mode='OBJECT')
    cnt = bpy.context
    scn = bpy.context.scene
    mrig       = bpy.data.objects["Metarig"] #"Metarig" is your metarig object name 
    mrig.select_set(True)
    rigify.generate.generate_rig(cnt,mrig)

DeselAll()

It's slow, but it's faster than doing it all manually!

$\endgroup$
1
  • $\begingroup$ This is something that both blender and 3dsMax (expensive!) have in common. Barely useful documentation with few examples. $\endgroup$ – james_t Mar 20 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.