framework/cg/blender/processing/midpoly_setup.py

79 lines
2.8 KiB
Python
Raw Normal View History

2023-10-16 12:21:34 +00:00
# -*- coding: utf-8 -*-
# Copyright (C) 2023 Ilia Kurochkin <brothermechanic@gmail.com>
#
# 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 3 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.
'''
DESCRIPTION.
Basic mesh processing for asset pipeline.
'''
__version__ = '0.1'
import logging
import bpy
import math
from blender.utils.object_relations import parenting
from blender.utils.remove_collections import remove_collections
from blender.utils.mesh_tools import collect_less_volume_objs
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
# COLLECTIONS NAMIG CONVENTION
parts_col_name = 'Parts'
lcs_col_name = 'LCS'
hierarchy_col_name = 'Hierarchy'
midpoly_col_name = 'Midpoly'
lowpoly_col_name = 'Lowpoly'
# LCS POINT'S SUFFIXES CONVENTION
inlet = '_in'
outlet = '_out'
root = '_root'
# CG ASSETS SUFFIXES CONVENTION
hightpoly = '_hp'
midpoly = 'mp'
lowpoly = '_lp'
render = '_render'
def hightpoly_collections_to_midpoly(part_names):
''' Convert part's collecttions to single objects. '''
for part_name in part_names:
midpoly_name = '_'.join((part_name, midpoly))
midpoly_mesh = bpy.data.meshes.new(midpoly_name)
midpoly_obj = bpy.data.objects.new(midpoly_name, midpoly_mesh)
bpy.context.view_layer.update()
part_inlet = bpy.data.objects.get('{}{}'.format(part_name, inlet))
midpoly_obj.matrix_world = part_inlet.matrix_world.copy()
parenting(part_inlet, midpoly_obj)
midpoly_parts_col = bpy.data.collections['_'.join((parts_col_name, midpoly))]
midpoly_parts_col.objects.link(midpoly_obj)
for col in midpoly_parts_col.children.keys():
if part_name in col:
bpy.ops.object.select_all(action='DESELECT')
exclude_objs = collect_less_volume_objs(
bpy.data.collections[col].objects, min_volume=2.0e-06)
for obj in bpy.data.collections[col].objects:
if obj not in exclude_objs:
obj.select_set(state=True)
midpoly_obj.select_set(state=True)
bpy.context.view_layer.objects.active = midpoly_obj
bpy.ops.object.join()
bpy.ops.object.shade_smooth(use_auto_smooth=True)
break
midpoly_parts_col.name = midpoly_col_name
for col in midpoly_parts_col.children.keys():
remove_collections(col)
return logger.info('Setup of %s midpoly meshes is finished!', len(part_names))