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

Support for merging all geometry in a collection on export? #35

Closed MattGraczyk closed 2 years ago

MattGraczyk commented 2 years ago

Hey there more of a question about support for a new feature. I use Capsule a lot for work and it covers a lot of my needs but this is one thing I haven't quite found a solution for yet.

Would it be possible for a feature to be added that would apply a Join operation on all geometry that's tagged for export in a single collection? Manually doing this for complex props is a major pain as you have to undo or not save your export geometry if you want to keep things non-destructive.

Thanks for the wonderful tool, Capsule is probably the only export tool I've found for blender that actually makes sense for an environment artist 3d asset pipeline.

Takanu commented 2 years ago

Hey!

I build environments myself and it's definitely a pain, but it's partly why I've built a beta feature in Capsule called Pack Scripts. You can tick this on in the Addon Preferences menu under "Extra Settings" and a new Pack Script property will appear for editing with any Object or Collection Export. Be warned that you'll need to know a bit of Python scripting in order to use it (this video seems like the best for learning the key things you'll need to understand for making a Pack Script)

A Pack Script is a python script that is called when an Object or Collection is just about to export and it's my current strategy for giving people more tools while avoiding the trap of having to make and maintain specific features that only certain types of artists need and making the interface harder to work with as a result. A Pack Script only needs a few lines of code and it means you can get Capsule to complete the export in exactly the way you want it to.

I currently use these extensively to realize and export Geometry Node-based objects without having to make any manual merges and to perform basic automatic lightmap operations.

A basic explanation of how they work can be found here - https://github.com/Takanu/Capsule/blob/master/Pack%20Scripts/Override%20Demo.py

You can also check out this script I made below for ideas, which duplicates and makes real any Geometry Node objects that contain instances for a better illustration of how I use them. Copy the script below into a new Text Object inside Blender and you can assign it as a Pack Script to an Object or Collection export.

I hope this helps, let me know if you have any issues using the feature and I'll help as best I can.


# ////////////////////////////////////////////////
# CAPSULE PACK SCRIPT DEMO
#
#
# Pack Scriptslet you adjust the objects that are about to export in any way you wish.
# The Blender Python API lets you do 99.9% of the things you could do using it's interface
# which make Pack Scripts a great way to remove any tedious custom setups you might
# need to perform on an export.
#
# IMPORTANT INFO
#
# - Create and test your scripts without Capsule first so you check they do what
# you expect them to.
# 
#
# -  If some of your exports have the Origin Point set to anything other than
# 'Scene', when this script is run the entire scene will have moved in 3D space.  
# This is how Capsule ensures that an object's origin point is where you want it.
#
#

import bpy
context = bpy.context

# CAPStatus is the datablock that provides information on the current export.
export_status = context.scene.CAPStatus

# ////////////////////////////////////////////////
# BEFORE EXPORT

# This is called just before Capsule will save Export Targets to a file.
if export_status.target_status == 'BEFORE_EXPORT':

    # Use this to get an array of objects that Capsule wants to export for the current
    # export operation.
    objects = export_status['target_input']
    final_export_targets = []
    target_layer = context.layer_collection

    for target in objects:

        # ////////////////////////////////////////////////////
        # REALIZE INSTANCES OF TARGET

        # Just select the current object
        bpy.ops.object.select_all(action='DESELECT')
        context.view_layer.objects.active = target
        target.select_set(state=True)

        # Duplicate and make the selected object real!
        bpy.ops.object.duplicates_make_real()

        # The selection will be the original object and all realized objects.
        # We just have to deselect the original target to add the realized
        # objects to our final export array.
        target.select_set(state=False)
        final_export_targets += context.selected_objects

    # Use this to provide Capsule with the objects you want to export
    # THIS MUST CONTAIN AT LEAST ONE OBJECT!
    export_status['target_output'] = final_export_targets

# ////////////////////////////////////////////////
# AFTER EXPORT

if export_status.target_status == 'AFTER_EXPORT':

    # this is called just after exporting, clean up the operations you
    # performed here.

    # CAPStatus information will be cleared after is this run ready for
    # the next export.

    # Delete all the export objects we created
    for export in export_status['target_output']:

        name = export.name
        objs = bpy.data.objects
        objs.remove(objs[name], do_unlink=True)
MattGraczyk commented 2 years ago

Hey thanks for the reply! I can see how this would be helpful and I might look into it more with our technical artist but we use so many big source files that contain large individual sets of props I don't think it would be convenient to script everything in each one.

I think I'll probably look for an end of pipeline solution within Unity itself to allow for merging and renaming of an imported mesh so I can keep my modifiers and work files the way I need them.

Thanks again! I wouldn't be able to work effectively without Capsule and I'm very happy that someone is making blender tools that actually make sense for a real team production.

Takanu commented 2 years ago

With this system you don't have to hand-script for each export, you just add the one script that performs the merge operations to your exports and it will execute the same script for each individual export. If i get time I'll try and make an example script for you that performs the merge operation.

No problem though, I'm really happy to hear that it's working well for you!