RCG: data base names #253
4 changed files with 77 additions and 93 deletions
|
@ -28,10 +28,8 @@ sys.path.append('/nix/store/ip49yhfl2l8gphylj4i43kdpq5qnq93i-bpy-4.1.0/lib/pytho
|
|||
sys.path.append('/media/disk/robossembler/project/collab/150-cg-render-assets/rcg_pipeline')
|
||||
sys.path.append('/nix/store/x1rqfn240xn6m6p6077gxfqxdxxj1cmc-python3.11-numpy-1.26.4/lib/python3.11/site-packages')
|
||||
import rcg_pipeline
|
||||
project_dir = '/path/to/<my_freecad_project>'
|
||||
parts_tree_path = '/path/to/<my_assembly>_tree_version.json'
|
||||
libs_data_path = '/path/to/<my_freecad_project>_libs_data.json'
|
||||
project_dir = '/path/to/<my_project_dir>'
|
||||
|
||||
rcg_pipeline.libs.generate_libs_database(project_dir)
|
||||
rcg_pipeline.render_asset.build_render_asset(parts_tree_path, libs_data_path, project_dir)
|
||||
rcg_pipeline.render_asset.build_render_assets(project_dir)
|
||||
```
|
||||
|
|
|
@ -42,7 +42,6 @@ __license__ = 'GPL-3'
|
|||
#__all__ = ['libs', 'render_asset', 'rcg_full_pipeline']
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
from . import libs
|
||||
from . import render_asset
|
||||
|
@ -55,13 +54,9 @@ def rcg_full_pipeline(project_dir):
|
|||
'''
|
||||
'''
|
||||
# 1 generate libs
|
||||
libs_data_path = libs.generate_libs_database(project_dir)
|
||||
libs.generate_libs_database(project_dir)
|
||||
|
||||
# 2 build render assets
|
||||
parts_tree_names = [
|
||||
data for data in os.listdir(project_dir)
|
||||
if data.endswith('tree_version.json')]
|
||||
for parts_tree_name in parts_tree_names:
|
||||
parts_tree_path = os.path.join(project_dir, parts_tree_name)
|
||||
render_asset.build_render_asset(parts_tree_path, libs_data_path, project_dir)
|
||||
render_asset.build_render_assets(project_dir)
|
||||
|
||||
return True
|
||||
|
|
|
@ -52,16 +52,9 @@ logger = logging.getLogger(__name__)
|
|||
|
||||
def generate_libs_database(project_dir):
|
||||
''' Generate Blender LIBS database from PARTs database. '''
|
||||
parts_data_name = [
|
||||
data for data in os.listdir(project_dir)
|
||||
if data.endswith('_parts_data.json')]
|
||||
if len(parts_data_name) != 1:
|
||||
raise Exception(
|
||||
'No database! Only single supported! Check %s directory' % project_dir)
|
||||
parts_data_path = os.path.join(project_dir, parts_data_name[0])
|
||||
|
||||
data_name = parts_data_name[0].split('_parts_data.json')[0]
|
||||
|
||||
parts_data_path = os.path.join(project_dir, 'parts.json')
|
||||
if not os.path.exists(parts_data_path):
|
||||
raise Exception('No parts database found! Check %s directory' % project_dir)
|
||||
with open(parts_data_path, encoding='utf-8') as data:
|
||||
parts_data = json.load(data)
|
||||
|
||||
|
@ -200,7 +193,7 @@ def generate_libs_database(project_dir):
|
|||
)
|
||||
|
||||
# write db file
|
||||
libs_data_path = os.path.join(project_dir, data_name + '_libs_data.json')
|
||||
libs_data_path = os.path.join(project_dir, 'libs.json')
|
||||
with open(libs_data_path, 'w', encoding='utf-8') as libs_data_file:
|
||||
json.dump(libs_data, libs_data_file, ensure_ascii=False, indent=4)
|
||||
logger.info('Database saved successfully to %s!', libs_data_path)
|
||||
|
|
|
@ -65,74 +65,64 @@ def assembly_builder(item, libs_data, libs_data_dir, collection=None, parent=Non
|
|||
'''
|
||||
if not collection:
|
||||
collection = bpy.context.scene.collection
|
||||
|
||||
item_obj = None
|
||||
|
||||
if item['type'] == 'LOCATOR':
|
||||
item_obj = bpy.data.objects.new(item['name'], None)
|
||||
item_obj.empty_display_type = 'CUBE'
|
||||
item_obj.empty_display_size = 0.01
|
||||
collection.objects.link(item_obj)
|
||||
|
||||
elif item['type'] == 'LCS':
|
||||
item_obj = bpy.data.objects.new(item['name'], None)
|
||||
item_obj.empty_display_type = 'ARROWS'
|
||||
item_obj.empty_display_size = round(random.uniform(0.05, 0.15), 3)
|
||||
item_obj.show_in_front = True
|
||||
collection.objects.link(item_obj)
|
||||
|
||||
elif item['type'] == 'PART':
|
||||
# link clones
|
||||
if item.get('base_name'):
|
||||
item_data = [
|
||||
data for data in libs_data
|
||||
if data['type'] == 'OBJECT'
|
||||
if data['name'] == item['base_name']]
|
||||
if not item_data:
|
||||
logger.error('No %s in database!', item['name'])
|
||||
return False
|
||||
# if there is local base_named object in scene -> rename it
|
||||
item_data = [
|
||||
data for data in libs_data
|
||||
if data['type'] == 'OBJECT'
|
||||
if data['name'] == item['base_name']]
|
||||
if not item_data:
|
||||
logger.error('No %s in database!', item['base_name'])
|
||||
return False
|
||||
# if there is local base_named object in scene -> rename it
|
||||
local_obj = None
|
||||
# for clones
|
||||
if item['base_name'] != item['name']:
|
||||
local_obj = [
|
||||
loc for loc in bpy.data.objects
|
||||
if loc.name == item['base_name']
|
||||
if not loc.library]
|
||||
if local_obj:
|
||||
local_obj[0].name += '_loc'
|
||||
item_file_path = os.path.join(libs_data_dir, item_data[0]['path'])
|
||||
# TODO already linked
|
||||
bpy.ops.wm.link(
|
||||
filepath=item_file_path,
|
||||
directory=os.path.join(item_file_path, 'Collection'),
|
||||
filename=item['base_name'],
|
||||
relative_path=True,
|
||||
do_reuse_local_id=True,
|
||||
active_collection=True,
|
||||
)
|
||||
item_obj = bpy.data.objects[item['base_name']]
|
||||
item_file_path = os.path.join(libs_data_dir, item_data[0]['path'])
|
||||
# TODO already linked
|
||||
bpy.ops.wm.link(
|
||||
filepath=item_file_path,
|
||||
directory=os.path.join(item_file_path, 'Collection'),
|
||||
filename=item['base_name'],
|
||||
relative_path=True,
|
||||
do_reuse_local_id=True,
|
||||
active_collection=True,
|
||||
)
|
||||
item_obj = bpy.data.objects[item['base_name']]
|
||||
# for clones
|
||||
if item['base_name'] != item['name']:
|
||||
item_obj.name = item['name']
|
||||
# rename local back
|
||||
if local_obj:
|
||||
local_obj[0].name = item['base_name']
|
||||
# link unical
|
||||
else:
|
||||
item_data = [
|
||||
data for data in libs_data
|
||||
if data['type'] == 'OBJECT'
|
||||
if data['name'] == item['name']]
|
||||
if not item_data:
|
||||
logger.error('No %s in database!', item['name'])
|
||||
return False
|
||||
item_file_path = os.path.join(libs_data_dir, item_data[0]['path'])
|
||||
bpy.ops.wm.link(
|
||||
filepath=item_file_path,
|
||||
directory=os.path.join(item_file_path, 'Collection'),
|
||||
filename=item['name'],
|
||||
relative_path=True,
|
||||
do_reuse_local_id=True,
|
||||
active_collection=True,
|
||||
)
|
||||
item_obj = bpy.data.objects[item['name']]
|
||||
item_obj.empty_display_type = 'PLAIN_AXES'
|
||||
item_obj.empty_display_size = 0.01
|
||||
|
||||
else:
|
||||
logger.error('Unknown object type %s of %s', item['type'], item['name'])
|
||||
return False
|
||||
|
||||
item_obj.location = Vector(item['pose'][0]['loc_xyz']) * 0.001
|
||||
item_obj.rotation_mode = 'QUATERNION'
|
||||
item_obj.rotation_quaternion = [
|
||||
|
@ -197,42 +187,50 @@ def assembly_rebuilder():
|
|||
return default_origin
|
||||
|
||||
|
||||
def build_render_asset(parts_tree_path, libs_data_path, project_dir):
|
||||
def build_render_assets(project_dir):
|
||||
'''
|
||||
'''
|
||||
# start from stratch
|
||||
bpy.ops.wm.read_homefile()
|
||||
remove_collections_with_objects()
|
||||
cleanup_orphan_data()
|
||||
|
||||
with open(parts_tree_path, encoding='utf-8') as data:
|
||||
parts_tree_item = json.load(data)
|
||||
libs_data_path = os.path.join(project_dir, 'libs.json')
|
||||
if not os.path.exists(libs_data_path):
|
||||
raise Exception('No libs database found! Check %s directory' % project_dir)
|
||||
with open(libs_data_path, encoding='utf-8') as data:
|
||||
libs_data = json.load(data)
|
||||
trees_path = os.path.join(project_dir, 'trees.json')
|
||||
if not os.path.exists(trees_path):
|
||||
raise Exception('No trees database found! Check %s directory' % project_dir)
|
||||
with open(trees_path, encoding='utf-8') as data:
|
||||
tree_item_list = json.load(data)
|
||||
|
||||
# create redner collection
|
||||
render_collection = bpy.data.collections.new(parts_tree_item['name'])
|
||||
bpy.context.scene.collection.children.link(render_collection)
|
||||
active_collection = recursive_layer_collection(
|
||||
bpy.context.view_layer.layer_collection,
|
||||
render_collection.name)
|
||||
bpy.context.view_layer.active_layer_collection = active_collection
|
||||
for tree_item in tree_item_list:
|
||||
# start from stratch
|
||||
bpy.ops.wm.read_homefile()
|
||||
remove_collections_with_objects()
|
||||
cleanup_orphan_data()
|
||||
|
||||
# build original hierarchy
|
||||
assembly_builder(parts_tree_item, libs_data, project_dir, render_collection)
|
||||
# rebuild to LCS hierarchy
|
||||
assembly_rebuilder()
|
||||
# create redner collection
|
||||
render_collection = bpy.data.collections.new(tree_item['name'])
|
||||
bpy.context.scene.collection.children.link(render_collection)
|
||||
active_collection = recursive_layer_collection(
|
||||
bpy.context.view_layer.layer_collection,
|
||||
render_collection.name)
|
||||
bpy.context.view_layer.active_layer_collection = active_collection
|
||||
|
||||
# render assets dir
|
||||
render_assets_dir = os.path.join(project_dir, 'assets', 'render')
|
||||
if os.path.exists(render_assets_dir):
|
||||
shutil.rmtree(render_assets_dir)
|
||||
os.makedirs(render_assets_dir)
|
||||
else:
|
||||
os.makedirs(render_assets_dir)
|
||||
# build original hierarchy
|
||||
assembly_builder(tree_item, libs_data, project_dir, render_collection)
|
||||
# rebuild to LCS hierarchy
|
||||
assembly_rebuilder()
|
||||
|
||||
blend_path = os.path.join(render_assets_dir, parts_tree_item['name'] + '.blend')
|
||||
bpy.ops.wm.save_as_mainfile(filepath=blend_path)
|
||||
logger.info('Render asset %s generated!', parts_tree_item['name'])
|
||||
# render assets dir
|
||||
render_assets_dir = os.path.join(project_dir, 'assets', 'render')
|
||||
if os.path.exists(render_assets_dir):
|
||||
shutil.rmtree(render_assets_dir)
|
||||
os.makedirs(render_assets_dir)
|
||||
else:
|
||||
os.makedirs(render_assets_dir)
|
||||
|
||||
return blend_path
|
||||
blend_path = os.path.join(render_assets_dir, tree_item['name'] + '.blend')
|
||||
bpy.ops.wm.save_as_mainfile(filepath=blend_path)
|
||||
logger.info('Render asset %s generated!', tree_item['name'])
|
||||
|
||||
logger.info('%s Render Assets was generated!', len(tree_item_list))
|
||||
return render_assets_dir
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue