framework/cg/blender/remesh/lowpoly_setup.py

86 lines
2.9 KiB
Python

# -*- 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.
Create lowpoly shells from parts collections.
'''
__version__ = '0.1'
import logging
import sys
import bpy
import math
from blender.utils.generative_modifiers import shell_remesher
from blender.utils.object_converter import convert_mesh_to_mesh
from blender.utils.object_relations import parenting
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
# COLLECTIONS NAMIG CONVENTION
parts_col_name = 'Import Parts'
lcs_col_name = 'Import LCS'
hierarchy_col_name = 'Import Hierarchy'
lowpoly_col_name = 'Lowpoly Parts'
# LCS POINT'S SUFFIXES CONVENTION
inlet = '_in'
outlet = '_out'
root = '_root'
# CG ASSETS SUFFIXES CONVENTION
hightpoly = '_hp'
lowpoly = '_lp'
render = '_render'
def parts_to_shells(hightpoly_part_names):
''' Create lowpoly shells from parts collections. '''
logger.info('Lowpoly shells creation launched...')
lowpoly_col = bpy.data.collections.new(lowpoly_col_name)
bpy.context.scene.collection.children.link(lowpoly_col)
for part_name in hightpoly_part_names:
# generate lowpoly objects from part collections
lowpoly_name = ('{}{}'.format(part_name, lowpoly))
lowpoly_mesh = bpy.data.meshes.new(lowpoly_name)
lowpoly_obj = bpy.data.objects.new(lowpoly_name, lowpoly_mesh)
bpy.context.view_layer.update()
part_inlet = bpy.data.objects.get('{}{}'.format(part_name, inlet))
lowpoly_obj.matrix_world = part_inlet.matrix_world.copy()
parenting(part_inlet, lowpoly_obj)
lowpoly_col.objects.link(lowpoly_obj)
shell_remesher(lowpoly_obj, 'remesh_nodes', 'robossembler')
part_col = bpy.data.collections[('{}{}'.format(part_name, hightpoly))]
lowpoly_obj.modifiers['remesh_nodes']['Input_0'] = part_col
remesh_voxel = lowpoly_obj.modifiers.new('remesh_voxel', type='REMESH')
remesh_voxel.mode = 'VOXEL'
remesh_voxel.voxel_size = 0.001
remesh_sharp = lowpoly_obj.modifiers.new('remesh_sharp', type='REMESH')
remesh_sharp.mode = 'SHARP'
remesh_sharp.octree_depth = 7
decimate = lowpoly_obj.modifiers.new('decimate', type='DECIMATE')
decimate.decimate_type = 'COLLAPSE'
decimate.ratio = 0.1
# apply all modifiers to mesh
parenting(part_inlet, convert_mesh_to_mesh(lowpoly_obj))
logger.info('Lowpoly shells created successfully!')
return lowpoly_col.objects