[CG Pipeline] Refactor

This commit is contained in:
brothermechanic 2023-11-13 13:07:33 +00:00 committed by Igor Brylyov
parent 6538f70d54
commit b3612d8655
23 changed files with 634 additions and 645 deletions

View file

@ -18,7 +18,7 @@ DESCRIPTION.
- Setup LCS points.
- Apply Bledner scene transforms.
'''
__version__ = '0.1'
__version__ = '0.2'
import collections
import logging
@ -26,43 +26,22 @@ import random
import bpy
from blender.utils.object_transforms import apply_transforms
from blender.import_cad.import_hierarchy import (fc_placement,
hierarchy)
hierarchy_list)
from blender.import_cad.import_materials import (assign_materials,
assign_black)
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
# COLLECTIONS NAMIG CONVENTION
part_col_name = 'Parts'
lcs_col_name = 'LCS'
hierarchy_col_name = 'Hierarchy'
lowpoly_col_name = 'Lowpoly'
# LCS POINT'S SUFFIXES CONVENTION
inlet = '_in'
outlet = '_out'
root = '_root'
# CG ASSETS SUFFIXES CONVENTION
hightpoly = '_hp'
lowpoly = '_lp'
render = '_render'
scene_scale = 0.001
blackbody_mat_name = 'Robossembler_Black_Body'
def json_to_blend(js_data):
def json_to_blend(js_data, **cg_config):
''' Reads JSON data and creates Blender scene '''
part_collection = bpy.data.collections.new(part_col_name)
bpy.context.scene.collection.children.link(part_collection)
lcs_collection = bpy.data.collections.new(lcs_col_name)
lcs_collection = bpy.data.collections.new(cg_config['lcs_col_name'])
bpy.context.scene.collection.children.link(lcs_collection)
hierarchy_collection = bpy.data.collections.new(hierarchy_col_name)
bpy.context.scene.collection.children.link(hierarchy_collection)
parts_collection = bpy.data.collections.new(cg_config['parts_col_name'])
bpy.context.scene.collection.children.link(parts_collection)
fc_file = list(js_data.keys())[0]
@ -90,7 +69,7 @@ def json_to_blend(js_data):
bmesh.from_pydata(verts, edges, faces)
bmesh.update()
bobj = bpy.data.objects.new(js_obj, bmesh)
part_collection.objects.link(bobj)
parts_collection.objects.link(bobj)
if bobj:
fc_placement(bobj,
@ -102,11 +81,10 @@ def json_to_blend(js_data):
apply_transforms(bobj, scale=True)
# construct assembly hierarchy
hierarchy_objs = hierarchy(bobj,
js_data[fc_file][js_obj]['hierarchy'],
scene_scale)
hierarchy_objs = hierarchy_list(
bobj, js_data[fc_file][js_obj]['hierarchy'], scene_scale)
for hierarchy_obj in hierarchy_objs:
hierarchy_collection.objects.link(hierarchy_obj)
parts_collection.objects.link(hierarchy_obj)
imported_objects['objs_hierarchy'].append(hierarchy_obj.name)
# one material for the whole object
@ -119,35 +97,6 @@ def json_to_blend(js_data):
assign_black(bobj)
imported_objects['objs_background'].append(bobj.name)
# losted root lcs inlet workaround
if imported_objects['objs_lcs']:
root_lcs = None
for obj_name in imported_objects['objs_lcs']:
if obj_name.endswith(root):
root_lcs = bpy.data.objects[obj_name]
break
if root_lcs:
root_inlet_name = '{}{}'.format(root_lcs.name.split(root)[0], inlet)
if not bpy.data.objects.get(root_inlet_name):
root_inlet = bpy.data.objects.new(root_inlet_name, None)
root_inlet.empty_display_type = 'ARROWS'
root_inlet.empty_display_size = 0.1
root_inlet.show_in_front = True
root_inlet.location = root_lcs.location
root_inlet.rotation_euler = root_lcs.rotation_euler
root_inlet.parent = root_lcs.parent
lcs_collection.objects.link(root_inlet)
imported_objects['objs_lcs'].append(root_inlet.name)
logger.info('Root Inlet LCS object created!')
else:
logger.info('Root Inlet LCS object already exists!')
else:
logger.info('Lost Root LCS object!')
else:
logger.info('No LCS objects found!')
# TODO
# update do not dork
logger.info('Generated %s objects without errors',
len(sum(list(imported_objects.values()), [])))
return imported_objects