forked from johnnygizmo/asset_snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
180 lines (152 loc) · 5.58 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
import bpy
from bpy.types import (Panel,
# Menu,
Operator,
PropertyGroup,
)
from bpy.props import (
# FloatProperty,
IntProperty,
# BoolProperty,
# StringProperty,
# FloatVectorProperty,
PointerProperty,
)
from bpy import context
import os
import random
bl_info = {
"name": "Asset Snapshot",
"author": "Johnny Matthews (guitargeek), Draise",
"category": "View3d",
"location": "View3D > N Panel / Asset Browser Tab",
"version": (1, 0, 2),
"blender": (3, 0, 0),
"description": "Mark active object as asset and render the current view as the asset preview",
}
def snapshot(self,context,ob):
scene = context.scene
tool = scene.asset_snapshot
# Make sure we have a camera
if bpy.context.scene.camera == None:
bpy.ops.object.camera_add()
#Save some basic settings
camera = bpy.context.scene.camera
hold_camerapos = camera.location.copy()
hold_camerarot = camera.rotation_euler.copy()
hold_x = bpy.context.scene.render.resolution_x
hold_y = bpy.context.scene.render.resolution_y
hold_filepath = bpy.context.scene.render.filepath
# Find objects that are hidden in viewport and hide them in render
tempHidden = []
for o in bpy.data.objects:
if o.hide_get() == True:
o.hide_render = True
tempHidden.append(o)
# Change Settings
bpy.context.scene.render.resolution_y = tool.resolution
bpy.context.scene.render.resolution_x = tool.resolution
switchback = False
if bpy.ops.view3d.camera_to_view.poll():
bpy.ops.view3d.camera_to_view()
switchback = True
# Ensure outputfile is set to png (temporarily, at least)
previousFileFormat = scene.render.image_settings.file_format
if scene.render.image_settings.file_format != 'PNG':
scene.render.image_settings.file_format = 'PNG'
filename = str(random.randint(0,100000000000))+".png"
filepath = str(os.path.abspath(os.path.join(os.sep, 'tmp', filename)))
bpy.context.scene.render.filepath = filepath
#Render File, Mark Asset and Set Image
bpy.ops.render.render(write_still = True)
ob.asset_mark()
### Draise - Removed due to not working with 4.0.0
#context_override = bpy.context.copy()
#context_override['id'] = ob
#bpy.ops.ed.lib_id_load_custom_preview(context_override,filepath=filepath)
### Draise - Added to work with 4.0.0
with context.temp_override(id=ob):
bpy.ops.ed.lib_id_load_custom_preview(filepath=filepath) #
# Unhide the objects hidden for the render
for o in tempHidden:
o.hide_render = False
# Reset output file format
scene.render.image_settings.file_format = previousFileFormat
#Cleanup
os.unlink(filepath)
bpy.context.scene.render.resolution_y = hold_y
bpy.context.scene.render.resolution_x = hold_x
camera.location = hold_camerapos
camera.rotation_euler = hold_camerarot
bpy.context.scene.render.filepath = hold_filepath
if switchback:
bpy.ops.view3d.view_camera()
class properties(PropertyGroup):
resolution : IntProperty(
name="Preview Resolution",
description="Resolution to render the preview",
min=1,
soft_max=500,
default=256
)
class AssetSnapshotCollection(Operator):
"""Create a preview of a collection"""
bl_idname = "view3d.asset_snaphot_collection"
bl_label = "Asset Snapshot - Collection"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
#def poll(cls, context):
def poll(cls, context):
if context.area.type != 'VIEW_3D':
return False
if context.collection == None:
return False
return True
def execute(self, context):
snapshot(self, context,context.collection)
return {'FINISHED'}
class AssetSnapshotObject(Operator):
"""Create an asset preview of an object"""
bl_idname = "view3d.object_preview"
bl_label = "Asset Snapshot - Object"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
if context.area.type != 'VIEW_3D':
return False
if context.view_layer.objects.active == None:
return False
return True
def execute(self, context):
snapshot(self, context, bpy.context.view_layer.objects.active)
return {'FINISHED'}
class OBJECT_PT_panel(Panel):
bl_label = "Asset Snapshot"
bl_idname = "OBJECT_PT_asset_snapshot_panel"
bl_category = "Asset Snapshot"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
@classmethod
def poll(self,context):
return context.mode
def draw(self, context):
layout = self.layout
scene = context.scene
tool = scene.asset_snapshot
layout.prop(tool, "resolution")
layout.operator("view3d.object_preview")
layout.operator("view3d.asset_snaphot_collection")
def register():
bpy.utils.register_class(properties)
bpy.utils.register_class(AssetSnapshotCollection)
bpy.utils.register_class(AssetSnapshotObject)
bpy.utils.register_class(OBJECT_PT_panel)
bpy.types.Scene.asset_snapshot = PointerProperty(type=properties)
def unregister():
bpy.utils.unregister_class(properties)
bpy.utils.unregister_class(AssetSnapshotCollection)
bpy.utils.unregister_class(AssetSnapshotObject)
bpy.utils.unregister_class(OBJECT_PT_panel)
del bpy.types.Scene.asset_snapshot
if __name__ == "__main__":
register()