Takanu / Capsule

(Blender 4.1) Universal batch export manager
https://takanu.itch.io/capsule-blender-addon
GNU General Public License v3.0
200 stars 13 forks source link

[Question] Rename multiple skeleton for each export list as "Armature" using Pack Script possible? #57

Closed Max-Vincent closed 6 months ago

Max-Vincent commented 7 months ago

Hello, i was wondering if i have multiple collections to be exported but with different armatures in each collections, how do i ensure that when exporting each collections it rename the armature as "Armature"?

i was guessing that Pack Script will run for each export list before exporting it, but when i use this script it didn't export the objects just a info that said it's successfully exported.

import bpy

# Deselect all objects first
bpy.ops.object.select_all(action='DESELECT')

# Select all armature objects in the scene
for obj in bpy.context.scene.objects:
    if obj.type == 'ARMATURE':
        obj.select_set(True)

# Get the selected objects
selected_objects = bpy.context.selected_objects

if selected_objects:
    for obj in selected_objects:
        # Set the armature's name to "Armature"
        obj.name = "Armature"
        print("Armature name set to 'Armature' for object:", obj.name)
else:
    print("No armature objects found in the scene.")
Wolfgang-IX commented 7 months ago

try this, from packscript_demos/packscript_override.py

import bpy

context = bpy.context

CAPStatus is the datablock that provides information on the current export.

(API explanation pending)

export_status = context.scene.CAPStatus

The first time the script is called the target_status will equal 'BEFORE_EXPORT'.

Use this if statement to make changes to the export.

if export_status.target_status == 'BEFORE_EXPORT':

# Use this to get the objects that Capsule wants to export
objects = export_status['target_input']
final_objects = []

for obj in objects:
    if obj.type == 'ARMATURE':
        obj.name = "Armature"
        final_objects.append(obj)

# Use this to provide Capsule with the objects you want to export
# THIS MUST CONTAIN SOMETHING
export_status['target_output'] = objects

The second time the script is called the target_status will equal 'AFTER_EXPORT'.

Use this to revert changes, delete created objects and clean up.

if export_status.target_status == 'AFTEREXPORT': for obj in objects: if obj.type == 'ARMATURE': if obj.name == "Armature": obj.name = "ARM"

# CAPStatus information will be cleared after is this run ready for
# the next export.
pass
Takanu commented 7 months ago

@Max-Vincent The example posted by @Wolfgang-IX is the correct format for Pack Scripts, though it's missing code to undo the changes made to the scene in the 'AFTER_EXPORT' portion of the template.

As of right now you need to both make the changes and undo them in the scene once the export is complete, though this may change in the future. It's really important to remember that Pack Scripts are editing the Blend file as it is, it's not currently possible to sandbox these changes without various compromises made elsewhere in the export process.

Max-Vincent commented 6 months ago

@Wolfgang-IX @Takanu thank you both of you, and the script works!