Skip to content

Commit c65a366

Browse files
Merge pull request #254 from Simarilius-uk/main
Sector import cleanup/bug fixes, should be 2-3x faster now.
2 parents 509aee9 + 8f7f45a commit c65a366

File tree

16 files changed

+708
-513
lines changed

16 files changed

+708
-513
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@
1111
i_scene_cp77_gltf/.vs/i_scene_cp77_gltf/v17/.wsuo
1212
i_scene_cp77_gltf/.vs/slnx.sqlite
1313
i_scene_cp77_gltf/.vs/i_scene_cp77_gltf/v17/TestStore/0/000.testlog
14+
i_scene_cp77_gltf (2).zip
15+
profile.json
1416
/.idea/
1517

i_scene_cp77_gltf/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def register():
9595
register_materialtools()
9696

9797
for cls in classes:
98-
if cls.__name__ is "JSONTool": # this one is static
98+
if cls.__name__ == "JSONTool": # this one is static
9999
continue
100100
if not hasattr(bpy.types, cls.__name__):
101101
bpy.utils.register_class(cls)

i_scene_cp77_gltf/importers/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class CP77EntityImport(Operator,ImportHelper):
8888
description="Collector to put the imported entity in",
8989
default='',
9090
options={'HIDDEN'})
91-
91+
9292
def draw(self, context):
9393
cp77_addon_prefs = bpy.context.preferences.addons['i_scene_cp77_gltf'].preferences
9494
props = context.scene.cp77_panel_props
@@ -138,7 +138,7 @@ def execute(self, context):
138138
bob=self.filepath
139139
inColl=self.inColl
140140
#print('Bob - ',bob)
141-
importEnt(props.with_materials, bob, apps, excluded, self.include_collisions, self.include_phys, self.include_entCollider, inColl, props.remap_depot)
141+
importEnt(props.with_materials, bob, apps, excluded, self.include_collisions, self.include_phys, self.include_entCollider, inColl, props.remap_depot, meshes=None, mesh_jsons=None, escaped_path=None, app_path=None, anim_files=None, rigjsons=None)
142142

143143
return {'FINISHED'}
144144

@@ -263,10 +263,10 @@ def execute(self, context):
263263
props = context.scene.cp77_panel_props
264264
SetVulkanBackend(props.use_vulkan)
265265
SetCyclesRenderer(props.use_cycles, props.update_gi)
266-
266+
appearances=self.appearances.split(",")
267267
# turns out that multimesh import of an entire car uses a gazillion duplicates as well...
268268
JSONTool.start_caching()
269-
CP77GLBimport(self, props.with_materials, props.remap_depot, self.exclude_unused_mats, self.image_format, self.filepath, self.hide_armatures, self.import_garmentsupport, self.files, self.directory, self.appearances, self.scripting)
269+
CP77GLBimport( props.with_materials, props.remap_depot, self.exclude_unused_mats, self.image_format, self.filepath, self.hide_armatures, self.import_garmentsupport, self.files, self.directory, appearances, self.scripting)
270270
JSONTool.stop_caching()
271271

272272
return {'FINISHED'}

i_scene_cp77_gltf/importers/attribute_import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def manage_garment_support(existingMeshes, gltf_importer_data):
2020
gltf_importer = gltf_importer_data
2121
curMeshCount = 0
2222
for name in bpy.data.meshes.keys():
23-
if name in existingMeshes or "Icosphere" in name:
23+
if name in existingMeshes or "Icosphere" in name or'icosaédrica' in name:
2424
continue
2525
mesh = bpy.data.meshes[name]
2626

i_scene_cp77_gltf/importers/entity_import.py

Lines changed: 139 additions & 57 deletions
Large diffs are not rendered by default.

i_scene_cp77_gltf/importers/import_with_materials.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def disable_collection_by_name(collection_name):
5454
appearances = None
5555
collection = None
5656

57-
def CP77GLBimport(self, with_materials, remap_depot, exclude_unused_mats=True, image_format='png', filepath='', hide_armatures=True, import_garmentsupport=False, files=[], directory='', appearances=[], scripting=False):
57+
def CP77GLBimport( with_materials=False, remap_depot=False, exclude_unused_mats=True, image_format='png', filepath='', hide_armatures=True, import_garmentsupport=False, files=[], directory='', appearances=[], scripting=False):
5858
cp77_addon_prefs = bpy.context.preferences.addons['i_scene_cp77_gltf'].preferences
5959
context=bpy.context
6060

@@ -65,31 +65,29 @@ def CP77GLBimport(self, with_materials, remap_depot, exclude_unused_mats=True, i
6565
# obj = None
6666
start_time = time.time()
6767
if not scripting:
68-
loadfiles=self.files
68+
loadfiles=files
6969
else:
7070
f={}
71-
f['name']=os.path.basename(self.filepath)
71+
f['name']=os.path.basename(filepath)
7272
loadfiles=(f,)
73-
73+
glbname=os.path.basename(filepath)
7474
DepotPath=cp77_addon_prefs
75-
appearances=self.appearances.split(",")
75+
7676
if not cp77_addon_prefs.non_verbose:
77-
if ".anims.glb" in self.filepath:
77+
if ".anims.glb" in filepath:
7878
print('\n-------------------- Beginning Cyberpunk Animation Import --------------------')
79-
print(f"Importing Animations From: {os.path.basename(self.filepath)}")
79+
print(f"Importing Animations From: {glbname}")
8080

8181
else:
8282
print('\n-------------------- Beginning Cyberpunk Model Import --------------------')
8383
if with_materials==True:
84-
print(f"Importing: {os.path.basename(self.filepath)} with materials")
85-
print(f"Appearances to Import: {(', '.join(appearances))}")
84+
print(f"Importing: {glbname} with materials")
85+
print(f"Appearances to Import: ",appearances)
8686
else:
87-
print(f"Importing: {os.path.basename(self.filepath)}")
87+
print(f"Importing: {glbname}")
8888
# prevent crash if no directory supplied when using filepath
89-
if len(self.directory)>0 and not scripting:
90-
directory = self.directory
91-
else:
92-
directory = os.path.dirname(self.filepath)
89+
if len(directory)==0 or scripting:
90+
directory = os.path.dirname(filepath)
9391

9492

9593
file_names=[]
@@ -115,7 +113,7 @@ def CP77GLBimport(self, with_materials, remap_depot, exclude_unused_mats=True, i
115113
#Kwek: was tempted to do a try-catch, but that is just La-Z
116114
#Kwek: Added another gate for materials
117115
for f in loadfiles:
118-
filename=os.path.splitext(f['name'])[0]
116+
filename=os.path.splitext(os.path.splitext(f['name'])[0])[0]
119117
filepath = os.path.join(directory, f['name'])
120118
vers = bpy.app.version
121119
if vers[0] == 4 and vers[1] >= 2 and vers[1] < 4:
@@ -125,12 +123,12 @@ def CP77GLBimport(self, with_materials, remap_depot, exclude_unused_mats=True, i
125123
elif vers[0] == 4 and vers[1] >= 5:
126124
gltf_importer = glTFImporter(filepath, { "files": None, "loglevel": 0, "import_pack_images" :True, 'import_unused_materials' :False, "merge_vertices" :False, "import_shading" : 'NORMALS', "bone_heuristic":heuristic, "guess_original_bind_pose" : False, "import_user_extensions": "",'disable_bone_shape':octos, 'bone_shape_scale_factor':1.0, 'import_scene_as_collection':True, 'import_scene_extras':True, 'import_select_created_objects':True, 'import_merge_material_slots':False})
127125
else:
128-
gltf_importer = glTFImporter(filepath, { "files": None, "loglevel": 0, "import_pack_images" :True, "merge_vertices" :False, "import_shading" : 'NORMALS', "bone_heuristic":heuristic, "guess_original_bind_pose" : False, "import_user_extensions": "",'disable_bone_shape':octos,})
126+
gltf_importer = glTFImporter(filepath, { "files": None, "loglevel": 0, "import_pack_images" :True, "merge_vertices" :False, "import_shading" : 'NORMALS', "bone_heuristic":heuristic, "guess_original_bind_pose" : False, "import_user_extensions": "",'disable_bone_shape':octos, 'import_select_created_objects':True,})
129127
gltf_importer.read()
130128
gltf_importer.checks()
131129
existingMeshes = bpy.data.meshes.keys()
132130

133-
current_file_base_path = os.path.splitext(filepath)[0]
131+
current_file_base_path = os.path.join(os.path.dirname(filepath),filename)
134132
has_material_json = os.path.exists(current_file_base_path + ".Material.json")
135133

136134
existingMaterials = bpy.data.materials.keys()
@@ -229,9 +227,14 @@ def CP77GLBimport(self, with_materials, remap_depot, exclude_unused_mats=True, i
229227
validmats={}
230228
# fix the app names as for some reason they have their index added on the end.
231229
if len(json_apps) > 0:
230+
232231
appkeys=[k for k in json_apps.keys()]
233232
for i,k in enumerate(appkeys):
234233
json_apps[k[:-1*len(str(i))]]=json_apps.pop(k)
234+
235+
# save the json_apps to the collection so that we can use it later
236+
collection['json_apps']=json.dumps(json_apps)
237+
235238
#appearances = ({'name':'short_hair'},{'name':'02_ca_limestone'},{'name':'ml_plastic_doll'},{'name':'03_ca_senna'})
236239
#if appearances defined populate valid mats with the mats for them, otherwise populate with everything used.
237240
if len(appearances)>0 and 'ALL' not in appearances:
@@ -354,6 +357,7 @@ def import_mats(BasePath, DepotPath, exclude_unused_mats, existingMeshes, gltf_i
354357
for name in names:
355358

356359
bpy.data.meshes[name].materials.clear()
360+
# we're not getting the materials from the json, but from the glTF importer data
357361
extras = gltf_importer.data.meshes[counter].extras
358362

359363
# morphtargets don't have material names. Just use all of them.
@@ -368,6 +372,7 @@ def import_mats(BasePath, DepotPath, exclude_unused_mats, existingMeshes, gltf_i
368372
materialNames = extras["materialNames"]
369373

370374
# remove duplicate material names (why does "extras" end up with 10k "decals" entries when I import the maimai?)
375+
# Sim - because of a bug in wkit I'd assume mana
371376
materialNames = list(dict.fromkeys(materialNames))
372377

373378
# Kwek: I also found that other material hiccups will cause the Collection to fail
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Import a mlsetup file
2+
# needs a mesh to apply to so a plane is created
3+
import bpy
4+
import os
5+
import json
6+
from ..material_types.multilayered import Multilayered
7+
8+
def import_mlsetup_and_create_plane():
9+
# Prompt user for .mlsetup.json file
10+
from bpy_extras.io_utils import ImportHelper
11+
class ImportMLSetupOperator(bpy.types.Operator, ImportHelper):
12+
bl_idname = "import_scene.mlsetup"
13+
bl_label = "Import MLSetup and Create Plane"
14+
filename_ext = ".json"
15+
filter_glob: bpy.props.StringProperty(default="*.mlsetup.json", options={'HIDDEN'})
16+
17+
def execute(self, context):
18+
filepath = self.filepath
19+
# Load the mlsetup JSON
20+
with open(filepath, 'r') as f:
21+
mlsetup_data = json.load(f)
22+
# Extract paths
23+
base_path = os.path.dirname(filepath)
24+
image_format = 'PNG' # Or detect from JSON or user input
25+
proj_path = base_path # Or set as needed
26+
27+
# Create the material using Multilayered
28+
multilayered = Multilayered(base_path, image_format, proj_path)
29+
mat_name = os.path.splitext(os.path.basename(filepath))[0]
30+
mat = bpy.data.materials.new(mat_name)
31+
mat.use_nodes = True
32+
multilayered.create(mlsetup_data["Data"], mat)
33+
34+
# Create a plane and assign the material
35+
bpy.ops.mesh.primitive_plane_add(size=2)
36+
plane = bpy.context.active_object
37+
if plane.data.materials:
38+
plane.data.materials[0] = mat
39+
else:
40+
plane.data.materials.append(mat)
41+
42+
# Ensure 0-1 UV mapping
43+
if not plane.data.uv_layers:
44+
plane.data.uv_layers.new(name="UVMap")
45+
uv_layer = plane.data.uv_layers.active.data
46+
# Set UVs for a quad (plane)
47+
uv_coords = [(0,0), (1,0), (1,1), (0,1)]
48+
for i, loop in enumerate(plane.data.loops):
49+
uv_layer[loop.index].uv = uv_coords[i % 4]
50+
51+
return {'FINISHED'}
52+
53+
bpy.utils.register_class(ImportMLSetupOperator)
54+
bpy.ops.import_scene.mlsetup('INVOKE_DEFAULT')

0 commit comments

Comments
 (0)