-
Notifications
You must be signed in to change notification settings - Fork 11
/
__init__.py
257 lines (182 loc) · 5.75 KB
/
__init__.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from email.policy import default
import os
import sys
import numpy
import bpy
from bpy.types import Panel, Menu
from bpy.types import PropertyGroup
from bpy.props import (
BoolProperty,
IntProperty,
EnumProperty,
FloatVectorProperty,
StringProperty,
PointerProperty,
)
import importlib
from .hio import hio
importlib.reload(hio)
from . import exporter
importlib.reload(exporter)
from . import importer
importlib.reload(importer)
bl_info = {
"name": "Houdini Geo IO",
"author": "satoruhiga",
"version": (1, 0),
"blender": (4, 0, 0),
"location": "",
"description": "",
"warning": "",
"support": "COMMUNITY",
"wiki_url": "",
"tracker_url": "",
"category": "Mesh",
}
class SCENE_OT_LoadGeo(bpy.types.Operator):
bl_idname = "houdini_io.load_geo"
bl_label = "NOP"
bl_description = ""
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
ob = bpy.context.object
o = bpy.context.object.houdini_io
return update_geometry(o)
class SCENE_OT_SaveGeo(bpy.types.Operator):
bl_idname = "houdini_io.save_geo"
bl_label = "NOP"
bl_description = ""
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
ob = bpy.context.object
o = bpy.context.object.houdini_io
path = o.filepath.format(name=ob.name)
if not path:
return {"CANCELLED"}
bpy.ops.object.mode_set(mode="OBJECT")
path = bpy.path.abspath(path)
opts = {}
res = exporter.export(path, ob, opts)
if not res:
return {"CANCELLED"}
return {"FINISHED"}
###
def update_geometry(o):
if o.load_sequence:
try:
path = o.filepath_template.format(name=o.name, frame=o.frame)
except:
print("Invalid filepath format:", o)
return {"CANCELLED"}
else:
path = o.filepath
path = bpy.path.abspath(path)
bpy.ops.object.mode_set(mode="OBJECT")
if not os.path.exists(path):
print("File not found:", path)
return {"CANCELLED"}
ob = o.id_data
opts = {'skip_normals': o.skip_normals and o.load_sequence}
new_data = importer.import_(path, ob, opts)
if not new_data:
return {"CANCELLED"}
name = ob.data.name
old_data = ob.data
old_data.name = "_temp_" + name
new_data.name = name
# copy materials to new data
m = list(ob.data.materials)
for x in m:
new_data.materials.append(x)
ob.data = new_data
# remove old data
db = eval(repr(old_data).split("[")[0])
db.remove(old_data)
ob.update_from_editmode()
return {"FINISHED"}
def frame_number_changed_cb(self, context):
update_geometry(self)
###
class SCENE_PT_HoudiniIO(Panel):
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "data"
bl_label = "Houdini Geo IO"
@classmethod
def poll(cls, context):
ob = context.object
data = ob.data
if (
isinstance(data, bpy.types.Mesh)
or isinstance(data, bpy.types.Curve)
# or isinstance(data, bpy.types.GreasePencil)
):
return True
return False
def draw_header(self, context):
layout = self.layout
layout.label(text="", icon="MESH_MONKEY")
def draw(self, context):
layout = self.layout
layout.prop(bpy.context.object.houdini_io, "active", text="Active")
if not bpy.context.object.houdini_io.active:
return
layout.separator()
layout.prop(
bpy.context.object.houdini_io, "load_sequence", text="Load Sequence"
)
if bpy.context.object.houdini_io.load_sequence:
layout.prop(
bpy.context.object.houdini_io,
"filepath_template",
text="Path Template {frame}",
)
layout.prop(bpy.context.object.houdini_io, "frame", text="Frame")
layout.prop(bpy.context.object.houdini_io, "skip_normals", text="Skip Normals (Playback Faster)")
else:
layout.prop(bpy.context.object.houdini_io, "filepath", text="File")
layout.operator(SCENE_OT_LoadGeo.bl_idname, text="Load Geo")
layout.operator(SCENE_OT_SaveGeo.bl_idname, text="Save Geo")
###
class ObjectHoudiniIO(PropertyGroup):
active: BoolProperty(name="Active", default=False)
load_sequence: BoolProperty(name="Load Sequence")
filepath_template: StringProperty(
name="File Path Template",
subtype="FILE_PATH",
default="//geo/geo.{frame:04}.bgeo.sc",
)
frame: IntProperty(name="Frame", update=frame_number_changed_cb)
filepath: StringProperty(
name="File Path", subtype="FILE_PATH", default="//geo.bgeo.sc"
)
skip_normals: BoolProperty(name="Skip Normals", default=True)
classes = (
ObjectHoudiniIO,
SCENE_PT_HoudiniIO,
SCENE_OT_LoadGeo,
SCENE_OT_SaveGeo,
)
@bpy.app.handlers.persistent
def global_frame_change_cb(scene):
for x in bpy.data.objects:
hio = x.houdini_io
if not hio.active:
continue
if hio.load_sequence:
update_geometry(hio)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
bpy.types.Object.houdini_io = PointerProperty(type=ObjectHoudiniIO)
if not global_frame_change_cb in bpy.app.handlers.frame_change_post:
bpy.app.handlers.frame_change_post.append(global_frame_change_cb)
def unregister():
del bpy.types.Object.houdini_io
from bpy.utils import unregister_class
for cls in classes:
unregister_class(cls)
bpy.app.handlers.frame_change_post.remove(global_frame_change_cb)
if __name__ == "__main__":
register()