Skip to content

Commit

Permalink
Merge pull request #180 from Simarilius-uk/OS_support
Browse files Browse the repository at this point in the history
4.3 support, and weight transfer fix.
  • Loading branch information
Simarilius-uk authored Nov 11, 2024
2 parents 5a0190f + 668b000 commit 3f19105
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 95 deletions.
16 changes: 13 additions & 3 deletions i_scene_cp77_gltf/importers/attribute_import.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import bpy

from io_scene_gltf2.io.imp.gltf2_io_binary import BinaryData
from io_scene_gltf2.blender.imp.gltf2_blender_mesh import points_edges_tris
from io_scene_gltf2.blender.imp.gltf2_blender_mesh import squish
vers = bpy.app.version
if vers[0] == 4 and vers[1] < 3:
from io_scene_gltf2.blender.imp.gltf2_blender_mesh import points_edges_tris
from io_scene_gltf2.blender.imp.gltf2_blender_mesh import squish
else:
from io_scene_gltf2.blender.imp.mesh import points_edges_tris
from io_scene_gltf2.blender.imp.mesh import squish

def rename_color_attribute(mesh, name_before, name_after):
if mesh.color_attributes is None:
Expand Down Expand Up @@ -39,6 +44,7 @@ def manage_garment_support(existingMeshes, gltf_importer_data):
curMeshCount = curMeshCount + 1



def add_vertex_color_attribute(accessor_name, attribute_name, gltf_importer_data, mesh, prim, indices):
gltf_importer = gltf_importer_data
if not accessor_name in prim.attributes:
Expand All @@ -52,7 +58,11 @@ def add_vertex_color_attribute(accessor_name, attribute_name, gltf_importer_data
if layer is None:
print("WARNING: Vertex colors are ignored (maximum number of vertex color layers has been reached)")
else:
mesh.color_attributes[layer.name].data.foreach_set('color', squish(cols))
print(layer.name)
try:
mesh.color_attributes[layer.name].data.foreach_set('color', squish(cols))
except:
print('something blew up setting gs attributes')

def get_indices(gltf_importer_data, prim):
gltf_importer = gltf_importer_data
6 changes: 5 additions & 1 deletion i_scene_cp77_gltf/importers/import_with_materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import json
import time
from io_scene_gltf2.io.imp.gltf2_io_gltf import glTFImporter
from io_scene_gltf2.blender.imp.gltf2_blender_gltf import BlenderGlTF
vers = bpy.app.version
if vers[0] == 4 and vers[1] < 3:
from io_scene_gltf2.blender.imp.gltf2_blender_gltf import BlenderGlTF
else:
from io_scene_gltf2.blender.imp.blender_gltf import BlenderGlTF
from ..main.setup import MaterialBuilder
from ..main.bartmoss_functions import UV_by_bounds
from .import_from_external import *
Expand Down
5 changes: 3 additions & 2 deletions i_scene_cp77_gltf/importers/sector_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,8 +1021,9 @@ def importSectors( filepath, with_mats, remap_depot, want_collisions, am_modding
new['pivot']=inst['Pivot']
new['meshAppearance']=meshAppearance
new['appearanceName']=meshAppearance
if ntype=='worldClothMeshNode' and "windImpulseEnabled" in inst.keys():
new['windImpulseEnabled']= inst['windImpulseEnabled']
if ntype=='worldClothMeshNode':
if 'windImpulseEnabled' in inst.keys():
new['windImpulseEnabled']= inst['windImpulseEnabled']
if ntype=='worldRotatingMeshNode':
if 'rotationAxis' in data.keys():
new['rot_axis']=data['rotationAxis']
Expand Down
6 changes: 5 additions & 1 deletion i_scene_cp77_gltf/material_types/hair.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ def create(self,hair,Mat):
profile= profile["Data"]["RootChunk"]

Mat.blend_method = 'HASHED'
Mat.shadow_method = 'HASHED'
vers = bpy.app.version
if vers[0] == 4 and vers[1] <= 2:
Mat.shadow_method = 'HASHED'
else:
print('set shadows to hashed how in 4.3?')

CurMat = Mat.node_tree
Ns=CurMat.nodes
Expand Down
56 changes: 29 additions & 27 deletions i_scene_cp77_gltf/material_types/hologram.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ def __init__(self, BasePath,image_format, ProjPath):
def create(self,Data,Mat):
mat = Mat
mat.use_nodes = True
mat.shadow_method = 'HASHED'
vers = bpy.app.version
if vers[0] == 4 and vers[1] <= 2:
Mat.shadow_method = 'HASHED'
else:
print('set shadows to hashed how in 4.3?')
mat.blend_method = 'HASHED'
sockets=bsdf_socket_names()

Expand Down Expand Up @@ -161,55 +165,53 @@ def create(self,Data,Mat):
principled_bsdf_001.distribution = 'GGX'
principled_bsdf_001.subsurface_method = 'RANDOM_WALK_SKIN'
#Metallic
principled_bsdf_001.inputs[1].default_value = 0.0
principled_bsdf_001.inputs[loc('Metallic')].default_value = 0.0
#Roughness
principled_bsdf_001.inputs[2].default_value = 1.0
principled_bsdf_001.inputs[loc('Roughness')].default_value = 1.0
#IOR
principled_bsdf_001.inputs[3].default_value = 1.4500000476837158
principled_bsdf_001.inputs[loc('IOR')].default_value = 1.4500000476837158
#Normal
principled_bsdf_001.inputs[5].default_value = (0.0, 0.0, 0.0)
#Weight
principled_bsdf_001.inputs[6].default_value = 0.0
principled_bsdf_001.inputs[loc('Normal')].default_value = (0.0, 0.0, 0.0)
#Subsurface Weight
principled_bsdf_001.inputs[7].default_value = 0.0
principled_bsdf_001.inputs[loc('Subsurface Weight')].default_value = 0.0
#Subsurface Radius
principled_bsdf_001.inputs[8].default_value = (1.0, 0.20000000298023224, 0.10000000149011612)
principled_bsdf_001.inputs[loc('Subsurface Radius')].default_value = (1.0, 0.20000000298023224, 0.10000000149011612)
#Subsurface Scale
principled_bsdf_001.inputs[9].default_value = 0.05000000074505806
principled_bsdf_001.inputs[loc('Subsurface Scale')].default_value = 0.05000000074505806
#Subsurface IOR
principled_bsdf_001.inputs[10].default_value = 1.399999976158142
principled_bsdf_001.inputs[loc('Subsurface IOR')].default_value = 1.399999976158142
#Subsurface Anisotropy
principled_bsdf_001.inputs[11].default_value = 0.0
principled_bsdf_001.inputs[loc('Subsurface Anisotropy')].default_value = 0.0
#Specular IOR Level
principled_bsdf_001.inputs[12].default_value = 0.0
principled_bsdf_001.inputs[loc('Specular IOR Level')].default_value = 0.0
#Specular Tint
principled_bsdf_001.inputs[13].default_value = (1.0, 1.0, 1.0, 1.0)
principled_bsdf_001.inputs[loc('Specular Tint')].default_value = (1.0, 1.0, 1.0, 1.0)
#Anisotropic
principled_bsdf_001.inputs[14].default_value = 0.0
principled_bsdf_001.inputs[loc('Anisotropic')].default_value = 0.0
#Anisotropic Rotation
principled_bsdf_001.inputs[15].default_value = 0.0
principled_bsdf_001.inputs[loc('Anisotropic Rotation')].default_value = 0.0
#Tangent
principled_bsdf_001.inputs[16].default_value = (0.0, 0.0, 0.0)
principled_bsdf_001.inputs[loc('Tangent')].default_value = (0.0, 0.0, 0.0)
#Transmission Weight
principled_bsdf_001.inputs[17].default_value = 0.0
principled_bsdf_001.inputs[loc('Transmission Weight')].default_value = 0.0
#Coat Weight
principled_bsdf_001.inputs[18].default_value = 0.0
principled_bsdf_001.inputs[loc('Coat Weight')].default_value = 0.0
#Coat Roughness
principled_bsdf_001.inputs[19].default_value = 0.029999999329447746
principled_bsdf_001.inputs[loc('Coat Roughness')].default_value = 0.029999999329447746
#Coat IOR
principled_bsdf_001.inputs[20].default_value = 1.5
principled_bsdf_001.inputs[loc('Coat IOR')].default_value = 1.5
#Coat Tint
principled_bsdf_001.inputs[21].default_value = (1.0, 1.0, 1.0, 1.0)
principled_bsdf_001.inputs[loc('Coat Tint')].default_value = (1.0, 1.0, 1.0, 1.0)
#Coat Normal
principled_bsdf_001.inputs[22].default_value = (0.0, 0.0, 0.0)
principled_bsdf_001.inputs[loc('Coat Normal')].default_value = (0.0, 0.0, 0.0)
#Sheen Weight
principled_bsdf_001.inputs[23].default_value = 0.0
principled_bsdf_001.inputs[loc('Sheen Weight')].default_value = 0.0
#Sheen Roughness
principled_bsdf_001.inputs[24].default_value = 0.5
principled_bsdf_001.inputs[loc('Sheen Roughness')].default_value = 0.5
#Sheen Tint
principled_bsdf_001.inputs[25].default_value = (1.0, 1.0, 1.0, 1.0)
principled_bsdf_001.inputs[loc('Sheen Tint')].default_value = (1.0, 1.0, 1.0, 1.0)
#Emission Strength
principled_bsdf_001.inputs[27].default_value = 30.0
principled_bsdf_001.inputs[loc('Emission Strength')].default_value = 30.0

#node Mix Shader
mix_shader_003 = holo_blue_002.nodes.new("ShaderNodeMixShader")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ def __init__(self, BasePath,image_format, ProjPath):

def create(self,Data,Mat):
Mat.blend_method = 'HASHED'
Mat.shadow_method = 'HASHED'
vers = bpy.app.version
if vers[0] == 4 and vers[1] <= 2:
Mat.shadow_method = 'HASHED'
else:
print('set shadows to hashed how in 4.3?')
CurMat = Mat.node_tree
pBSDF = CurMat.nodes[loc('Principled BSDF')]
sockets=bsdf_socket_names()
Expand Down
6 changes: 2 additions & 4 deletions i_scene_cp77_gltf/meshtools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,23 @@ class CP77WeightTransfer(Operator):
bl_idname = 'cp77.trans_weights'
bl_label = "Cyberpunk 2077 Weight Transfer Tool"
bl_description = "Transfer weights from source mesh to target mesh"
bl_options = {'REGISTER', 'UNDO'}

vertInterop: BoolProperty(
name="Use Nearest Vert Interpolated",
description="Sometimes gives better results when the default mode fails",
default=False)

bySubmesh: BoolProperty(
name="Transfer by Submesh Order",
description="Because Mana Gets what Mana Wants :D",
default=False)
bl_options = {'REGISTER', 'UNDO'}


def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)

def execute(self, context):
# Call the trans_weights function with the provided arguments
result = trans_weights(self, context, vertInterop, bySubmesh)
result = trans_weights(self, context, self.properties.vertInterop ) #, self.properties.bySubmesh)
return {"FINISHED"}

def draw(self,context):
Expand Down
136 changes: 80 additions & 56 deletions i_scene_cp77_gltf/resources/scripts/Import_from_OS.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
#
#

project_path='C:\\CPMod\\notell'
GroupName='blender_group_1'

project_path=r'C:\CPMod\corpo_apt'
GroupName='corpo_wall_test'
with_mats=False

import bpy
Expand All @@ -24,6 +25,7 @@
D=bpy.data
C=bpy.context
coll_scene = C.scene.collection
shapes=["Box", "Capsule", "Sphere" ]

def get_position(obj):
pos = Vector((obj['spawnable']['position']['x'],obj['spawnable']['position']['y'],obj['spawnable']['position']['z']))
Expand All @@ -35,16 +37,14 @@ def get_position(obj):
else:
scale=(1,1,1)
return pos,rot,scale



def process_group(group,target_coll):
for child in group['childs']:
if child['type']=='group':
coll_target=bpy.data.collections.new(child['name'])
target_coll.children.link(coll_target)
process_group(child,coll_target)
elif child['type']=='object':
if 'type' in child.keys() and child['type']=='object':
process_object(child,target_coll)
elif 'type' in child.keys() and child['type']=='group':
process_group(child,target_coll)

def process_object(obj,parent_coll):
Masters=bpy.data.collections.get("MasterInstances")
Expand Down Expand Up @@ -86,61 +86,85 @@ def process_object(obj,parent_coll):
newobj.rotation_euler = rot
newobj.scale = scale
elif spawndata[-4:]=='.ent' :
app=obj['spawnable']['app']
entpath=os.path.join(project_path,'source','raw', spawndata).replace('\\', os.sep)+'.json'
ent_groupname=os.path.basename(entpath).split('.')[0]+'_'+app
while len(ent_groupname) > 63:
ent_groupname = ent_groupname[:-1]
imported=False
if ent_groupname in Masters.children.keys():
move_coll=Masters.children.get(ent_groupname)
imported=True
else:
try:
#print('Importing ',entpath, ' using app ',app)
incoll='MasterInstances'
bpy.ops.io_scene_gltf.cp77entity(with_mats, filepath=entpath, appearances=app, inColl=incoll)
if spawntype=="Entity Template":
app=obj['spawnable']['app']
entpath=os.path.join(project_path,'source','raw', spawndata).replace('\\', os.sep)+'.json'
ent_groupname=os.path.basename(entpath).split('.')[0]+'_'+app
while len(ent_groupname) > 63:
ent_groupname = ent_groupname[:-1]
imported=False
if ent_groupname in Masters.children.keys():
move_coll=Masters.children.get(ent_groupname)
imported=True
except:
print(traceback.print_exc())
print(f"Failed during Entity import on {entpath} from app {app}")
if imported:
group=move_coll
if (group):
groupname=move_coll.name
#print('Group found for ',groupname)
new=bpy.data.collections.new(groupname)
parent_coll.children.link(new)
new['nodeType']='worldEntityNode'
new['debugName']=obj['name']
new['entityTemplate']=spawndata
new['appearanceName']=obj['spawnable']['app']

pos,rot,scale=get_position(obj)
rot=rot.to_quaternion()
new['ent_rot']=rot
new['ent_pos']=pos
inst_trans_mat=Matrix.LocRotScale(pos,rot,scale)
for child in group.children:
newchild=bpy.data.collections.new(child.name)
new.children.link(newchild)
for old_obj in child.objects:
else:
try:
#print('Importing ',entpath, ' using app ',app)
incoll='MasterInstances'
bpy.ops.io_scene_gltf.cp77entity(with_mats, filepath=entpath, appearances=app, inColl=incoll)
move_coll=Masters.children.get(ent_groupname)
imported=True
except:
print(traceback.print_exc())
print(f"Failed during Entity import on {entpath} from app {app}")
if imported:
group=move_coll
if (group):
groupname=move_coll.name
#print('Group found for ',groupname)
new=bpy.data.collections.new(groupname)
parent_coll.children.link(new)
new['nodeType']='worldEntityNode'
new['debugName']=obj['name']
new['entityTemplate']=spawndata
new['appearanceName']=obj['spawnable']['app']

pos,rot,scale=get_position(obj)
rot=rot.to_quaternion()
new['ent_rot']=rot
new['ent_pos']=pos
inst_trans_mat=Matrix.LocRotScale(pos,rot,scale)
for child in group.children:
newchild=bpy.data.collections.new(child.name)
new.children.link(newchild)
for old_obj in child.objects:
obj=old_obj.copy()
obj.color = (0.567942, 0.0247339, 0.600028, 1)
newchild.objects.link(obj)
obj.matrix_local= inst_trans_mat @ obj.matrix_local
if 'Armature' in obj.name:
obj.hide_set(True)
for old_obj in group.objects:
obj=old_obj.copy()
obj.color = (0.567942, 0.0247339, 0.600028, 1)
newchild.objects.link(obj)
new.objects.link(obj)
obj.matrix_local= inst_trans_mat @ obj.matrix_local
if 'Armature' in obj.name:
obj.hide_set(True)
for old_obj in group.objects:
obj=old_obj.copy()
obj.color = (0.567942, 0.0247339, 0.600028, 1)
new.objects.link(obj)
obj.matrix_local= inst_trans_mat @ obj.matrix_local
if 'Armature' in obj.name:
obj.hide_set(True)
if len(group.all_objects)>0:
new['matrix']=group.all_objects[0].matrix_world
if len(group.all_objects)>0:
new['matrix']=group.all_objects[0].matrix_world
elif spawntype=="Collision Shape":
# from entspawner code: o.shapeTypes = { "Box", "Capsule", "Sphere" } so 0=box, 1=capsule, 2 = sphere
ShapeType=shapes[obj['spawnable']['shape']]
if ShapeType=='Box' :
#print('Box Collision Node')
#pprint(act['Shapes'])
extents=obj['spawnable']['extents']
position=obj['spawnable']['position']
if ShapeType=='Box':
bpy.ops.mesh.primitive_cube_add(size=1, scale=(float(extents['x'])*2,float(extents['y'])*2,float(extents['z'])*2),
location=(float(position['x']),float(position['y']),float(position['z'])))
#location=(float(position['x'])+float(extents['x'])*2,float(position['y'])+float(extents['y'])*2,float(position['z'])+float(extents['z'])*2))
crash=C.selected_objects[0]
crash.name=obj['name']
par_coll=crash.users_collection[0]
par_coll.objects.unlink(crash)
coll_scene.objects.link(crash)
crash['ShapeType']=obj['spawnable']['shape']
crash['matrix']=crash.matrix_world
rot=obj['spawnable']['rotation']
crash.rotation_mode='XYZ'
crash.rotation_euler=(radians(rot['pitch']),radians(rot['roll']),radians(rot['yaw']))

elif spawntype=='Decals':
vert = [(-0.5, -0.5, 0.0), (0.5, -0.5, 0.0), (-0.5, 0.5, 0.0), (0.5,0.5, 0.0)]
fac = [(0, 1, 3, 2)]
Expand Down

0 comments on commit 3f19105

Please sign in to comment.