This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblr_main.py
100 lines (83 loc) · 3.17 KB
/
blr_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import json
import bpy
import bmesh
#from mathutils import Vector
#import mathutils
from bpy import context, data, ops
import os
import logging
import sys
argv = sys.argv
argv = argv[argv.index("--") + 1:] # get all args after "--"
json_file = argv[0]
output_dir = argv[1]
basefile = os.path.basename(json_file) # huawei-tnf6dwss9.json
background_file = os.environ.get("TMP") + "/" + basefile.replace(".json",".out.png") # /tmp/huawei-tnf6dwss9.out.png
normalmap_file = os.environ.get("TMP") + "/" + basefile.replace(".json",".normal.png") # /tmp/huawei-tnf6dwss9.normal.png
fbx_file = argv[1] + "/" +basefile.replace(".json",".fbx")
def resize_model(obj):
json_f = open(json_file, "r")
loaded_json = json.load(json_f)
try:
total_size = str(loaded_json["sizeWDHmm"])
total_size = total_size.replace("[", "")
total_size = total_size.replace("]", "")
total_size = total_size.split(',')
x_size = total_size[0]
x_size.replace(",", "")
y_size = total_size[1]
y_size.replace(",", "")
z_size = total_size[2]
z_size.replace(",", "")
except:
print("Cannot find the sizeWDHmm. Wrong JSON.")
obj.name = os.path.basename(json_file)
print (obj.name + " == " + x_size + " " + y_size + " " + z_size)
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode = 'EDIT')
me = obj.data
#bm = bmesh.from_edit_mesh(me)
bpy.ops.transform.resize(value=(float(x_size)/100, float(y_size)/100, float(z_size)/100), orient_type='LOCAL')
obj = context.object
mb = obj.matrix_basis
if hasattr(ob.data, "transform"):
obj.data.transform(mb)
for c in ob.children:
c.matrix_local = mb @ c.matrix_local
obj.matrix_basis.identity()
bmesh.update_edit_mesh(me)
bpy.ops.object.mode_set(mode = 'OBJECT')
return obj
#
# Blender parameters
#
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects['Camera'].select_set(True) # Blender 2.8x
bpy.data.objects['Light'].select_set(True) # Blender 2.8x
bpy.data.objects['Cube'].select_set(True) # Blender 2.8x
bpy.ops.object.delete()
bpy.ops.import_scene.fbx( filepath = os.path.dirname(__file__) + "/models/model.fbx" )
#
# - - - TEXTURE PART - - -
#
mat = bpy.data.materials.new(name="Main")
mat.use_nodes = True
bsdf = mat.node_tree.nodes["Principled BSDF"]
texImage = mat.node_tree.nodes.new('ShaderNodeTexImage')
texImage.image = bpy.data.images.load(background_file)
mat.node_tree.links.new(bsdf.inputs['Base Color'], texImage.outputs['Color'])
# - - - NORMAL PART - - -
# Creating normal map
##norm = mat.node_tree.nodes.new["Normal Map"]
# Creating image for normal map
norm = mat.node_tree.nodes.new('ShaderNodeTexImage')
norm.image = bpy.data.images.load(normalmap_file)
mat.node_tree.links.new(bsdf.inputs['Normal'], norm.outputs['Color'])
ob = bpy.data.objects['model']
resize_model(ob)
# Assign it to object
if ob.data.materials:
ob.data.materials[0] = mat
else:
ob.data.materials.append(mat)
bpy.ops.export_scene.fbx(filepath = fbx_file, path_mode = "COPY",apply_scale_options='FBX_SCALE_UNITS', global_scale =0.1, embed_textures = True)