Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move more json loading to jsontool.py #160

Merged
merged 5 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions i_scene_cp77_gltf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
print('-------------------- Cyberpunk IO Suite Starting--------------------')
print()
print('')
from .cyber_prefs import *
from .cyber_props import *
import bpy
Expand Down Expand Up @@ -96,8 +96,9 @@ def register():
if not hasattr(bpy.types, cls.__name__):
bpy.utils.register_class(cls)
load_icons()

print('-------------------- Cyberpunk IO Suite Finished--------------------')
print('')
print('-------------------- Cyberpunk IO Suite Has Started--------------------')
print('')

def unregister():
unregister_scriptman()
Expand Down
3 changes: 2 additions & 1 deletion i_scene_cp77_gltf/exporters/mlsetup_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import os
import numpy as np
import copy
from ..main.common import openJSON, createOverrideTable
from ..jsontool import openJSON
from ..main.common import createOverrideTable


##################################################################################################################
Expand Down
2 changes: 1 addition & 1 deletion i_scene_cp77_gltf/importers/import_with_materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from io_scene_gltf2.io.imp.gltf2_io_gltf import glTFImporter
from io_scene_gltf2.blender.imp.gltf2_blender_gltf import BlenderGlTF
from ..main.setup import MaterialBuilder
from ..main.common import json_ver_validate, UV_by_bounds
from ..main.bartmoss_functions import UV_by_bounds
from .import_from_external import *
from .attribute_import import manage_garment_support
from ..cyber_props import add_anim_props
Expand Down
82 changes: 61 additions & 21 deletions i_scene_cp77_gltf/jsontool.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def normalize_paths(data):
data = data.replace('\\',os.sep)
return data


def json_ver_validate(json_data):
if 'Header' not in json_data:
return False
Expand All @@ -29,14 +28,14 @@ def json_ver_validate(json_data):
return False
return True


def load_json(file_path):
with open(file_path, 'r') as file:
data = json.load(file)
normalize_paths(data)
return data

def jsonload(filepath):

if not filepath.endswith('.json'):
raise ValueError("This is not a json, what are you doing?")
cp77_addon_prefs = bpy.context.preferences.addons['i_scene_cp77_gltf'].preferences
Expand All @@ -52,33 +51,41 @@ def jsonload(filepath):
data=load_json(filepath)
if json_ver_validate(data) == False:
if not cp77_addon_prefs.non_verbose:
print(f"invalid anims.json found at: {filepath} this plugin requires the use of the latest version of Wolvenkit")
show_message(f"invalid anims.json found at: {filepath} this plugin requires the use of the latest version of Wolvenkit")
print(f"invalid anims.json found at: {filepath} this plugin requires jsons generated using the latest version of Wolvenkit")
show_message(f"invalid anims.json found at: {base_name} this plugin requires jsons generated using the latest version of Wolvenkit")
# Do something for .anims.json

case _ if base_name.endswith('.app.json'):
if not cp77_addon_prefs.non_verbose:
print(f"Processing: {base_name}")
data=load_json(filepath)
if json_ver_validate(data) == False:
if not cp77_addon_prefs.non_verbose:
print(f"invalid app.json found at: {filepath} this plugin requires the use of the latest version of Wolvenkit")
show_message(f"invalid app.json found at: {filepath} this plugin requires the use of the latest version of Wolvenkit")
# Do something for .app.json
print(f"invalid app.json found at: {filepath} this plugin requires jsons generated using the latest version of Wolvenkit")
show_message(f"invalid app.json: {base_name} this plugin requires jsons generated using the latest version of Wolvenkit")
# Do something for .app.json

case _ if base_name.endswith('.ent.json'):
if not cp77_addon_prefs.non_verbose:
print(f"Processing: {base_name}")
data=load_json(filepath)
if json_ver_validate(data) == False:
if not cp77_addon_prefs.non_verbose:
print(f"attempted import of invalid ent.json from: {filepath} this plugin requires the use of the latest version of Wolvenkit")
show_message(f"attempted import of invalid ent.json from: {filepath} this plugin requires the use of the latest version of Wolvenkit")
print(f"attempted import of invalid ent.json from: {filepath} this plugin requires jsons generated using the latest version of Wolvenkit")
show_message(f"attempted import of invalid ent.json: {base_name} this plugin requires jsons generated using the latest version of Wolvenkit")
return 'CANCELLED'
# Do something for .ent.json

case _ if base_name.endswith('.mesh.json'):
if not cp77_addon_prefs.non_verbose:
print(f"Processing: {base_name}")
data=load_json(filepath)
# Do something for .mesh.json
if json_ver_validate(data) == False:
if not cp77_addon_prefs.non_verbose:
print(f"invalid mesh.json found at: {filepath} this plugin requires jsons generated using the latest version of Wolvenkit")
show_message(f"found invalid mesh.json: {base_name} this plugin requires jsons generated using the latest version of Wolvenkit")
# Do something for .mesh.json
#
case _ if base_name.endswith('.Material.json'):
if not cp77_addon_prefs.non_verbose:
print(f"Processing: {base_name}")
Expand All @@ -91,62 +98,95 @@ def jsonload(filepath):
if not cp77_addon_prefs.non_verbose:
print('Building shaders')
# Do something for .material.json

case _ if base_name.endswith('.mlsetup.json'):
if not cp77_addon_prefs.non_verbose:
print(f"Processing: {base_name}")
data=load_json(filepath)
if json_ver_validate(data) == False:
if not cp77_addon_prefs.non_verbose:
print(f"invalid mlsetup.json found at: {filepath} import will continue but shaders may be incorrectly set up for this mesh")
show_message(f"invalid mlsetup.json found at: {filepath} import will continue but shaders may be incorrectly setup for this mesh")
show_message(f"invalid mlsetup.json: {base_name} import will continue but shaders may be incorrectly setup for this mesh")
# Do something for .mlsetup.json

case _ if base_name.endswith('.mltemplate.json'):
if not cp77_addon_prefs.non_verbose:
print(f"Processing: {base_name}")
data=load_json(filepath)
if json_ver_validate(data) == False:
if not cp77_addon_prefs.non_verbose:
print(f"invalid mltemplate.json found at: {filepath} import will continue but shaders may be incorrectly set up for this mesh")
show_message(f"invalid mltemplate.json found at: {filepath} import will continue but shaders may be incorrectly setup for this mesh")
show_message(f"invalid mltemplate.json: {base_name} import will continue but shaders may be incorrectly setup for this mesh")
# Do something for .mlsetup.json

case _ if base_name.endswith('.phys.json'):
if not cp77_addon_prefs.non_verbose:
print(f"Processing: {base_name}")
data=load_json(filepath)
if json_ver_validate(data) == False:
if not cp77_addon_prefs.non_verbose:
print(f"invalid phys.json found at: {filepath} import may continue but .phys colliders will not be imported")
show_message(f"invalid phys.json found at: {filepath} import may continue but .phys colliders will not be imported")
show_message(f"invalid phys.json: {base_name} import may continue but .phys colliders will not be imported")
# Do something for .phys.json

case _ if base_name.endswith('.streamingsector.json'):
if not cp77_addon_prefs.non_verbose:
print(f"Processing: {base_name}")
data=load_json(filepath)
if json_ver_validate(data) == False:
if not cp77_addon_prefs.non_verbose:
print(f"invalid streamingsector.json found at: {filepath} this plugin requires jsons generated with the current version of Wolvenkit")
show_message(f"invalid streamingsector.json found at: {filepath} this plugin requires jsons generated with the current version of Wolvenkit")
# Do something for .streamingsector.json
print(f"invalid streamingsector.json found at: {filepath} this plugin requires jsons generated with the latest version of Wolvenkit")
show_message(f"invalid streamingsector.json: {base_name} this plugin requires jsons generated with the latest version of Wolvenkit")
# Do something for .streamingsector.json

case _ if base_name.endswith('.streamingblock.json'):
if not cp77_addon_prefs.non_verbose:
print(f"Processing: {base_name}")
data=load_json(filepath)
# Do something for .streamingblock.json

case _ if base_name.endswith('.rig.json'):
if not cp77_addon_prefs.non_verbose:
print(f"Processing: {base_name}")
data=load_json(filepath)
if json_ver_validate(data) == False:
if not cp77_addon_prefs.non_verbose:
print(f"invalid rig.json found at: {filepath} this plugin requires jsons generated with the current version of Wolvenkit")
show_message(f"invalid rig.json found at: {filepath} this plugin requires jsons generated with the current version of Wolvenkit")
print(f"invalid rig.json found at: {filepath} this plugin requires jsons generated with the latest version of Wolvenkit")
show_message(f"invalid rig.json: {base_name} this plugin requires jsons generated with the latest version of Wolvenkit")
# Do something for .rig.json

case _ if base_name.endswith('.hp.json'):
if not cp77_addon_prefs.non_verbose:
print(f"Processing: {base_name}")
data=load_json(filepath)
if json_ver_validate(data) == False:
if not cp77_addon_prefs.non_verbose:
print(f"invalid hp.json found at: {filepath} this plugin requires jsons generated with the latest version of Wolvenkit")
show_message(f"invalid Hair Profile: {base_name} this plugin requires jsons generated with the latest version of Wolvenkit")
case _:
if not cp77_addon_prefs.non_verbose:
print(f"Incompatible Json: {base_name}")
print("json files must be generated with a current version of Wolvenkit")
show_message(f"Incompatible Json: {base_name} json files must be generated with a current version of Wolvenkit")
print("json files must be generated with a latest version of Wolvenkit")
show_message(f"Incompatible Json: {base_name} json files must be generated with a latest version of Wolvenkit")
# Do something for other json files

# Return or process the data as needed
return data
return data

def jsonloads(jsonstrings):

data=json.loads(jsonstrings)
normalize_paths(data)
return data

def openJSON(path, mode='r', ProjPath='', DepotPath=''):
path = path.replace('\\', os.sep)
ProjPath = ProjPath.replace('\\', os.sep)
DepotPath = DepotPath.replace('\\', os.sep)

inproj=os.path.join(ProjPath,path)
if os.path.exists(inproj):
file = open(inproj,mode)
else:
file = open(os.path.join(DepotPath,path),mode)
return file
55 changes: 36 additions & 19 deletions i_scene_cp77_gltf/main/bartmoss_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
def is_mesh(o: bpy.types.Object) -> bool:
return isinstance(o.data, bpy.types.Mesh)


def is_armature(o: bpy.types.Object) -> bool: # I just found out I could leave annotations like that -> future presto will appreciate knowing wtf I though I was going to return
return isinstance(o.data, bpy.types.Armature)


def has_anims(o: bpy.types.Object) -> bool:
return isinstance(o.data, bpy.types.Armature) and o.animation_data is not None


def rotate_quat_180(self,context):
if context.active_object and context.active_object.rotation_quaternion:
active_obj = context.active_object
Expand All @@ -33,16 +30,14 @@ def rotate_quat_180(self,context):

else:
return{'FINISHED'}



# deselects other objects and fully selects an object in both the viewport and the outliner
def select_object(obj):
for o in bpy.context.selected_objects:
o.select_set(False)
obj.select_set(True)
bpy.context.view_layer.objects.active = obj



## returns the volume of a given mesh by applying a rigid body with a material density of 1 and then returning the calculated mass
def calculate_mesh_volume(obj):
select_object(obj)
Expand All @@ -52,23 +47,19 @@ def calculate_mesh_volume(obj):
bpy.ops.rigidbody.objects_remove()
return volume


## Returns True if the given object has shape keys, works for meshes and curves
def hasShapeKeys(obj):
if obj.id_data.type in ['MESH', 'CURVE']:
return True if obj.data.shape_keys else False
else:
return False



# Return the name of the shape key data block if the object has shape keys.
def getShapeKeyName(obj):
if hasShapeKeys(obj):
return obj.data.shape_keys.name
return ""


# returns a dictionary with all the property names for the objects shape keys.
def getShapeKeyProps(obj):

Expand All @@ -80,7 +71,6 @@ def getShapeKeyProps(obj):

return props


# returns a list of the given objects custom properties.
def getCustomProps(obj):

Expand All @@ -91,16 +81,14 @@ def getCustomProps(obj):
props.append(prop)

return props



# returns a list of modifiers for the given object
def getMods(obj):
mods = []
for mod in obj.modifiers:
mods.append(mod.name)
return mods



# returns a list with the modifier properties of the given modifier.
def getModProps(modifier):
props = []
Expand All @@ -109,7 +97,6 @@ def getModProps(modifier):
props.append(prop)
return props


# checks the active object for a material by name and returns the material if found
def getMaterial(name):
obj = bpy.context.active_object
Expand All @@ -120,6 +107,36 @@ def getMaterial(name):
mat = obj.material_slots[index].material
if mat and mat.node_tree and mat.node_tree.name == name:
return mat



def UV_by_bounds(selected_objects):
current_mode = bpy.context.object.mode
min_vertex = Vector((float('inf'), float('inf'), float('inf')))
max_vertex = Vector((float('-inf'), float('-inf'), float('-inf')))
for obj in selected_objects:
if obj.type == 'MESH':
matrix = obj.matrix_world
mesh = obj.data
for vertex in mesh.vertices:
vertex_world = matrix @ vertex.co
min_vertex = Vector(min(min_vertex[i], vertex_world[i]) for i in range(3))
max_vertex = Vector(max(max_vertex[i], vertex_world[i]) for i in range(3))

for obj in selected_objects:
if len(obj.data.uv_layers)<1:
me = obj.data
bpy.ops.object.mode_set(mode='EDIT', toggle=False)
bm = bmesh.from_edit_mesh(me)

uv_layer = bm.loops.layers.uv.verify()

# adjust uv coordinates
for face in bm.faces:
for loop in face.loops:
loop_uv = loop[uv_layer]
# use xy position of the vertex as a uv coordinate
loop_uv.uv[0]=(loop.vert.co.x-min_vertex[0])/(max_vertex[0]-min_vertex[0])
loop_uv.uv[1]=(loop.vert.co.y-min_vertex[1])/(max_vertex[1]-min_vertex[1])

bmesh.update_edit_mesh(me)
bpy.ops.object.mode_set(mode=current_mode)

Loading
Loading