-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy-rtf-jsx.py
117 lines (98 loc) · 3.79 KB
/
copy-rtf-jsx.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
import bpy
from bpy.types import Panel, Operator
import unicodedata
import re
bl_info = {
"name": "React Three Fiber Copy",
"author": "@sinanisler",
"version": (0, 1),
"blender": (2, 93, 0),
"location": "RTF Tab > Copy JSX",
"description": "Copy scene objects in React Three Fiber JSX format",
"category": "Copy",
}
def remove_accents(input_str):
nfkd_form = unicodedata.normalize('NFKD', input_str)
only_ascii = nfkd_form.encode('ASCII', 'ignore')
return only_ascii.decode()
def format_name(name):
name = remove_accents(name)
name = ''.join([word.capitalize() for word in name.split()])
name = re.sub('[^A-Za-z0-9]+', '', name)
return name
def get_primitive_name(obj):
blender_to_r3f = {
"Cube": "boxGeometry",
"UV Sphere": "sphereGeometry",
"Icosphere": "icosahedronGeometry",
"Cylinder": "cylinderGeometry",
"Cone": "coneGeometry",
"Torus": "torusGeometry",
"Plane": "plane",
"Circle": "circleGeometry",
# ... (additional mappings if necessary)
}
object_type = obj.type
if object_type == "MESH" and obj.data.name in blender_to_r3f:
return blender_to_r3f[obj.data.name]
else:
return None # Return None if no matching primitive is found
class EXPORT_OT_jsx(Operator):
"""Copy to React Three Fiber JSX"""
bl_idname = "export.jsx_rtf"
bl_label = "Copy JSX for RTF"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.selected_objects is not None
def execute(self, context):
formatted_objects = []
for obj in bpy.context.scene.objects:
primitive_name = get_primitive_name(obj)
if primitive_name is not None: # Only add JSX for recognized primitives
formatted_objects.append(
f'''
<mesh position={{[{format(obj.location.x, ".4f")}, {format(obj.location.y, ".4f")}, {format(obj.location.z, ".4f")}]}}>
<{primitive_name} />
<meshStandardMaterial color="gray" />
</mesh>
'''
)
elif obj.type == 'LAMP':
light_type = obj.data.type
if light_type == 'POINT':
formatted_objects.append(
f'<pointLight position={{[{format(obj.location.x, ".4f")}, {format(obj.location.y, ".4f")}, {format(obj.location.z, ".4f")}]}} />\n'
)
elif light_type == 'SUN':
formatted_objects.append(
f'<ambientLight />\n'
)
# ... (additional conditions for other light types if necessary)
elif obj.type == 'CAMERA':
formatted_objects.append(
f'<Camera position={{[{format(obj.location.x, ".4f")}, {format(obj.location.y, ".4f")}, {format(obj.location.z, ".4f")}]}} />\n'
)
# ... (additional conditions for other object types if necessary)
jsx_content = "".join(formatted_objects)
bpy.context.window_manager.clipboard = jsx_content
self.report({'INFO'}, "Object names and positions copied to clipboard!")
return {'FINISHED'}
class RTF_PT_main_panel(Panel):
"""Main RTF Panel"""
bl_idname = "RTF_PT_main_panel"
bl_label = "RTF Exporter JSX Exporter"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "RTF"
def draw(self, context):
layout = self.layout
layout.operator("export.jsx_rtf", text="Copy JSX")
def register():
bpy.utils.register_class(EXPORT_OT_jsx)
bpy.utils.register_class(RTF_PT_main_panel)
def unregister():
bpy.utils.unregister_class(RTF_PT_main_panel)
bpy.utils.unregister_class(EXPORT_OT_jsx)
if __name__ == "__main__":
register()