-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_cgp.py
150 lines (116 loc) · 4.45 KB
/
import_cgp.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Copyright (C) 2023 jackk25
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import bpy
import os
import bmesh
import json
from mathutils import Vector
if "bpy" in locals():
import importlib
if "utils" in locals():
importlib.reload(utils)
from . import utils
def make_base_pillar(context, BASE_PILLAR_SIZE, PILLAR_VERTICAL_SCALE):
bpy.ops.mesh.primitive_cube_add(
size=BASE_PILLAR_SIZE,
location=Vector((0,0, -PILLAR_VERTICAL_SCALE)),
scale=Vector((1, 1, PILLAR_VERTICAL_SCALE))
)
# Changing the pillar origin
old_cursor_position = context.scene.cursor.location
context.scene.cursor.location = Vector((0, 0, 0))
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
context.scene.cursor.location = old_cursor_position
original_pillar = context.object
#Locking the object position
original_pillar.lock_location[0] = True
original_pillar.lock_location[1] = True
fix_uvs(original_pillar.data)
return original_pillar
def build_grid(context, height_map, prefab_map, name):
PILLAR_VERTICAL_SCALE = 10
BASE_PILLAR_SIZE = 2
#Create the collection for our pillars, and register it as a pattern
collection = bpy.data.collections.new(name)
collection.is_pattern = True
context.collection.children.link(collection)
original_pillar = make_base_pillar(context, BASE_PILLAR_SIZE, PILLAR_VERTICAL_SCALE)
for x, row in enumerate(height_map):
position_offset = Vector((x * BASE_PILLAR_SIZE, 0, 0))
for y, height in enumerate(row):
position_offset.y = y * BASE_PILLAR_SIZE
position_offset.z = height
prefab = prefab_map[x][y]
pillar_copy = original_pillar.copy()
pillar_copy.location += position_offset
collection.objects.link(pillar_copy)
pillar_copy.is_pillar = True
pillar_copy.prefab_type = str(prefab)
bpy.data.objects.remove(original_pillar)
return {'FINISHED'}
def parse_height_map(height_map):
parsed_map = []
for row in height_map:
row_storage = []
block_storage = ""
in_block = False
for char in row:
if char == '(':
in_block = True
continue
if char == ')':
in_block = False
row_storage.append(int(block_storage))
block_storage = ""
continue
if in_block == False:
row_storage.append(int(char))
if in_block:
block_storage += char
parsed_map.append(row_storage)
return parsed_map
def parse_object_map(object_map):
parsed_map = []
for row in object_map:
row_storage = []
for col in row:
row_storage.append(col)
parsed_map.append(row_storage)
return parsed_map
def fix_uvs(mesh_data):
bm = bmesh.new()
bm.from_mesh(mesh_data)
uv_layer = bm.loops.layers.uv.active
loaded_uvs = []
current_file_dir = os.path.dirname(__file__)
uv_filepath = os.path.join(current_file_dir, "resources", "cube.json")
with open(uv_filepath,'r') as f:
loaded_uvs = json.load(f)
for index, face in enumerate(bm.faces):
face_uvs = loaded_uvs[index]
for loop_index, loop in enumerate(face.loops):
uv = loop[uv_layer].uv
loop_uv = face_uvs[loop_index]
uv.x = loop_uv[0]
uv.y = loop_uv[1]
bm.to_mesh(mesh_data)
mesh_data.update()
bm.free()
def load(context, filepath=""):
with open(filepath, 'r', encoding='utf-8') as f:
data = f.read().splitlines()
height_map = data[:16]
object_map = data[17:]
height_map = parse_height_map(height_map)
object_map = parse_object_map(object_map)
name = os.path.basename(filepath)
return build_grid(context, height_map, object_map, name)