Asset Pipeline v2 #145
68
scripts-blender/addons/asset_pipeline_2/asset_suffix.py
Normal file
68
scripts-blender/addons/asset_pipeline_2/asset_suffix.py
Normal file
@ -0,0 +1,68 @@
|
||||
# ***** BEGIN GPL LICENSE BLOCK *****
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software Foundation,
|
||||
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
#
|
||||
# ***** END GPL LICENCE BLOCK *****
|
||||
#
|
||||
# (c) 2021, Blender Foundation - Paul Golter
|
||||
from typing import List, Dict, Union, Any, Set, Optional, Tuple, Generator
|
||||
|
||||
import bpy
|
||||
from bpy_extras.id_map_utils import get_id_reference_map, get_all_referenced_ids
|
||||
|
||||
from .utils import get_storage_of_id
|
||||
|
||||
DELIMITER = "."
|
||||
|
||||
|
||||
def remove_suffix_from_hierarchy(
|
||||
collection: bpy.types.Collection, delimiter: str = DELIMITER
|
||||
):
|
||||
"""Removes the suffix after a set delimiter from all datablocks
|
||||
referenced by a collection, itself included"""
|
||||
|
||||
ref_map = get_id_reference_map()
|
||||
datablocks = get_all_referenced_ids(collection, ref_map)
|
||||
datablocks.add(collection)
|
||||
for db in datablocks:
|
||||
if db.library:
|
||||
# Don't rename linked datablocks.
|
||||
continue
|
||||
try:
|
||||
db.name = delimiter.join(db.name.split(delimiter)[:-1])
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def add_suffix_to_hierarchy(collection: bpy.types.Collection, suffix_base: str):
|
||||
"""Add a suffix to the names of all datablocks referenced by a collection,
|
||||
itself included."""
|
||||
|
||||
suffix = f"{DELIMITER}{suffix_base}"
|
||||
|
||||
ref_map = get_id_reference_map()
|
||||
datablocks = get_all_referenced_ids(collection, ref_map)
|
||||
datablocks.add(collection)
|
||||
for db in datablocks:
|
||||
if db.library:
|
||||
# Don't rename linked datablocks.
|
||||
continue
|
||||
collision_db = get_storage_of_id(db).get(db.name + suffix)
|
||||
if collision_db:
|
||||
collision_db.name += f'{DELIMITER}OLD'
|
||||
try:
|
||||
db.name += suffix
|
||||
except:
|
||||
pass
|
93
scripts-blender/addons/asset_pipeline_2/utils.py
Normal file
93
scripts-blender/addons/asset_pipeline_2/utils.py
Normal file
@ -0,0 +1,93 @@
|
||||
# ***** BEGIN GPL LICENSE BLOCK *****
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software Foundation,
|
||||
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
#
|
||||
# ***** END GPL LICENCE BLOCK *****
|
||||
#
|
||||
# (c) 2021, Blender Foundation - Paul Golter
|
||||
|
||||
from typing import List, Dict, Union, Any, Set, Optional, Tuple, Generator
|
||||
|
||||
import bpy
|
||||
from bpy import types
|
||||
|
||||
ID_INFO = {
|
||||
(types.WindowManager, 'WINDOWMANAGER', 'window_managers'),
|
||||
(types.Scene, 'SCENE', 'scenes'),
|
||||
(types.World, 'WORLD', 'worlds'),
|
||||
(types.Collection, 'COLLECTION', 'collections'),
|
||||
(types.Armature, 'ARMATURE', 'armatures'),
|
||||
(types.Mesh, 'MESH', 'meshes'),
|
||||
(types.Camera, 'CAMERA', 'cameras'),
|
||||
(types.Lattice, 'LATTICE', 'lattices'),
|
||||
(types.Light, 'LIGHT', 'lights'),
|
||||
(types.Speaker, 'SPEAKER', 'speakers'),
|
||||
(types.Volume, 'VOLUME', 'volumes'),
|
||||
(types.GreasePencil, 'GREASEPENCIL', 'grease_pencils'),
|
||||
(types.Curve, 'CURVE', 'curves'),
|
||||
(types.LightProbe, 'LIGHT_PROBE', 'lightprobes'),
|
||||
(types.MetaBall, 'METABALL', 'metaballs'),
|
||||
(types.Object, 'OBJECT', 'objects'),
|
||||
(types.Action, 'ACTION', 'actions'),
|
||||
(types.Key, 'KEY', 'shape_keys'),
|
||||
(types.Sound, 'SOUND', 'sounds'),
|
||||
(types.Material, 'MATERIAL', 'materials'),
|
||||
(types.NodeTree, 'NODETREE', 'node_groups'),
|
||||
(types.Image, 'IMAGE', 'images'),
|
||||
(types.Mask, 'MASK', 'masks'),
|
||||
(types.FreestyleLineStyle, 'LINESTYLE', 'linestyles'),
|
||||
(types.Library, 'LIBRARY', 'libraries'),
|
||||
(types.VectorFont, 'FONT', 'fonts'),
|
||||
(types.CacheFile, 'CACHE_FILE', 'cache_files'),
|
||||
(types.PointCloud, 'POINT_CLOUD', 'pointclouds'),
|
||||
(types.Curves, 'HAIR_CURVES', 'hair_curves'),
|
||||
(types.Text, 'TEXT', 'texts'),
|
||||
# (types.Simulation, 'SIMULATION', 'simulations'),
|
||||
(types.ParticleSettings, 'PARTICLE', 'particles'),
|
||||
(types.Palette, 'PALETTE', 'palettes'),
|
||||
(types.PaintCurve, 'PAINT_CURVE', 'paint_curves'),
|
||||
(types.MovieClip, 'MOVIE_CLIP', 'movieclips'),
|
||||
(types.WorkSpace, 'WORKSPACE', 'workspaces'),
|
||||
(types.Screen, 'SCREEN', 'screens'),
|
||||
(types.Brush, 'BRUSH', 'brushes'),
|
||||
(types.Texture, 'TEXTURE', 'textures'),
|
||||
}
|
||||
|
||||
# Map datablock Python classes to their string representation.
|
||||
ID_CLASS_TO_IDENTIFIER: Dict[type, Tuple[str, int]] = dict(
|
||||
[(tup[0], (tup[1])) for tup in ID_INFO]
|
||||
)
|
||||
|
||||
# Map datablock Python classes to the name of their bpy.data container.
|
||||
ID_CLASS_TO_STORAGE_NAME: Dict[type, str] = dict(
|
||||
[(tup[0], (tup[2])) for tup in ID_INFO]
|
||||
)
|
||||
|
||||
|
||||
def get_fundamental_id_type(datablock: bpy.types.ID) -> Any:
|
||||
"""Certain datablocks have very specific types.
|
||||
This function should return their fundamental type, ie. parent class."""
|
||||
for id_type in ID_CLASS_TO_IDENTIFIER.keys():
|
||||
if isinstance(datablock, id_type):
|
||||
return id_type
|
||||
|
||||
|
||||
def get_storage_of_id(datablock: bpy.types.ID) -> 'bpy_prop_collection':
|
||||
"""Return the storage collection property of the datablock.
|
||||
Eg. for an object, returns bpy.data.objects.
|
||||
"""
|
||||
|
||||
fundamental_type = get_fundamental_id_type(datablock)
|
||||
return getattr(bpy.data, ID_CLASS_TO_STORAGE_NAME[fundamental_type])
|
Loading…
Reference in New Issue
Block a user