alcea

FBX (accurig) to vrm

Nov 16th, 2024 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. Exporter Py:
  2. _________________
  3.  
  4. # Check it works at Blender 3.4.1
  5. # You can change the path and name
  6. filepathjson = "C:\\portableapps\\blender36\\scripts\\humanbody2.json"
  7. filepathVRM = "C:\\portableapps\\blender36\\scripts\\VRM"
  8. filenameVRM = "VRM"
  9.  
  10. # Main code is below the line.
  11. #----------------------------------------------------------------------
  12.  
  13. import os
  14. import bpy
  15.  
  16. # Ensure the VRM directory exists
  17. try:
  18. os.makedirs(filepathVRM)
  19. except FileExistsError:
  20. pass
  21.  
  22. # Deselect all objects in the scene
  23. bpy.ops.object.select_all(action='DESELECT')
  24.  
  25. # Check if "Armature" exists before selecting it
  26. if "Armature" in bpy.data.objects:
  27. object = bpy.data.objects["Armature"]
  28. object.select_set(True)
  29. else:
  30. print("Object 'Armature' not found in the scene.")
  31.  
  32. # Rename bones for the armature
  33. for obj in bpy.data.objects:
  34. if obj.type == 'ARMATURE':
  35. for bone in obj.pose.bones:
  36. bone.name = bone.name.replace("CC_Base_", "HumanBodyBones.")
  37.  
  38. for bone in obj.pose.bones:
  39. if obj.name != "RL_BoneRoot":
  40. bone.name = bone.name.replace("L_", "Left")
  41.  
  42. for bone in obj.pose.bones:
  43. bone.name = bone.name.replace("R_", "Right")
  44.  
  45. for bone in obj.pose.bones:
  46. bone.name = bone.name.replace("Clavicle", "Shoulder")
  47.  
  48. for bone in obj.pose.bones:
  49. bone.name = bone.name.replace("Fore", "Lower")
  50.  
  51. for bone in obj.pose.bones:
  52. bone.name = bone.name.replace("ToeBase", "Toes")
  53.  
  54. for bone in obj.pose.bones:
  55. bone.name = bone.name.replace("arm", "Arm")
  56.  
  57. for bone in obj.pose.bones:
  58. bone.name = bone.name.replace("Thigh", "UpperLeg")
  59.  
  60. for bone in obj.pose.bones:
  61. bone.name = bone.name.replace("Calf", "LowerLeg")
  62.  
  63. # Load human bone mappings from JSON
  64. bpy.ops.vrm.load_human_bone_mappings(filepath=filepathjson)
  65.  
  66. # Deselect all objects again
  67. bpy.ops.object.select_all(action='DESELECT')
  68.  
  69. # Check if "default" object exists before trying to select it
  70. if "default" in bpy.data.objects:
  71. object = bpy.data.objects["default"]
  72. object.select_set(True)
  73. else:
  74. print("Object 'default' not found in the scene.")
  75.  
  76. # Select all mesh objects (assuming you want to select all of them, not just 'default')
  77. selectob = bpy.data.objects
  78. for ob in selectob:
  79. if ob.type == 'MESH':
  80. ob.select_set(True)
  81. bpy.context.view_layer.objects.active = ob
  82.  
  83. # Switch to Shader Node Tree for material editing
  84. bpy.context.area.ui_type = 'ShaderNodeTree'
  85. bpy.context.object.active_material_index = 0
  86. mat = bpy.context.active_object.material_slots[0].material
  87.  
  88. # Node tree for material setup
  89. node_tree = bpy.context.active_object.material_slots[0].material.node_tree
  90. nodes = node_tree.nodes
  91. links = node_tree.links
  92.  
  93. # Create MToon Shader Node Group
  94. mtoon = nodes.new("ShaderNodeGroup")
  95. mtoon.node_tree = bpy.data.node_groups['MToon_unversioned']
  96.  
  97. # Get the Image Texture node
  98. dif = node_tree.nodes["Image Texture"]
  99.  
  100. # Print and check nodes for debugging
  101. print(dif)
  102. for n in nodes:
  103. print(n)
  104.  
  105. # Get the Material Output node and link everything up
  106. out1 = node_tree.nodes["Material Output"]
  107. for l in links:
  108. links.remove(l)
  109.  
  110. # Link nodes
  111. links.new(dif.outputs[0], mtoon.inputs[0])
  112. links.new(mtoon.outputs[0], out1.inputs[0])
  113.  
  114. # Enable backface culling and set blend method
  115. bpy.context.object.active_material.use_backface_culling = True
  116. bpy.context.object.active_material.blend_method = 'OPAQUE'
  117.  
  118. # Export the scene to a VRM file
  119. bpy.ops.export_scene.vrm(filepath=os.path.join(filepathVRM, filenameVRM + ".vrm"))
  120.  
  121. # Switch to Object mode
  122. bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
  123.  
  124.  
  125.  
  126. human2body.json
  127. ________________
  128.  
  129. {
  130. "chest": "HumanBodyBones.Spine02",
  131. "head": "HumanBodyBones.Head",
  132. "hips": "HumanBodyBones.Hip",
  133. "jaw": "HumanBodyBones.JawRoot",
  134. "leftEye": "HumanBodyBones.LeftEye",
  135. "leftFoot": "HumanBodyBones.LeftFoot",
  136. "leftHand": "HumanBodyBones.LeftHand",
  137. "leftIndexDistal": "HumanBodyBones.LeftIndex3",
  138. "leftIndexIntermediate": "HumanBodyBones.LeftIndex2",
  139. "leftIndexProximal": "HumanBodyBones.LeftIndex1",
  140. "leftLittleDistal": "HumanBodyBones.LeftPinky3",
  141. "leftLittleIntermediate": "HumanBodyBones.LeftPinky2",
  142. "leftLittleProximal": "HumanBodyBones.LeftPinky1",
  143. "leftLowerArm": "HumanBodyBones.LeftLowerArm",
  144. "leftLowerLeg": "HumanBodyBones.LeftLowerLeg",
  145. "leftMiddleDistal": "HumanBodyBones.LeftMid3",
  146. "leftMiddleIntermediate": "HumanBodyBones.LeftMid2",
  147. "leftMiddleProximal": "HumanBodyBones.LeftMid1",
  148. "leftRingDistal": "HumanBodyBones.LeftRing3",
  149. "leftRingIntermediate": "HumanBodyBones.LeftRing2",
  150. "leftRingProximal": "HumanBodyBones.LeftRing1",
  151. "leftShoulder": "HumanBodyBones.LeftShoulder",
  152. "leftThumbIntermediate": "HumanBodyBones.LeftThumb2",
  153. "leftThumbProximal": "HumanBodyBones.LeftThumb1",
  154. "leftToes": "HumanBodyBones.LeftToes",
  155. "leftUpperArm": "HumanBodyBones.LeftUpperArm",
  156. "leftUpperLeg": "HumanBodyBones.LeftUpperLeg",
  157. "neck": "HumanBodyBones.NeckTwist01",
  158. "rightEye": "HumanBodyBones.RightEye",
  159. "rightFoot": "HumanBodyBones.RightFoot",
  160. "rightHand": "HumanBodyBones.RightHand",
  161. "rightIndexDistal": "HumanBodyBones.RightIndex3",
  162. "rightIndexIntermediate": "HumanBodyBones.RightIndex2",
  163. "rightIndexProximal": "HumanBodyBones.RightIndex1",
  164. "rightLittleDistal": "HumanBodyBones.RightPinky3",
  165. "rightLittleIntermediate": "HumanBodyBones.RightPinky2",
  166. "rightLittleProximal": "HumanBodyBones.RightPinky1",
  167. "rightLowerArm": "HumanBodyBones.RightLowerArm",
  168. "rightLowerLeg": "HumanBodyBones.RightLowerLeg",
  169. "rightMiddleDistal": "HumanBodyBones.RightMid3",
  170. "rightMiddleIntermediate": "HumanBodyBones.RightMid2",
  171. "rightMiddleProximal": "HumanBodyBones.RightMid1",
  172. "rightRingDistal": "HumanBodyBones.RightRing3",
  173. "rightRingIntermediate": "HumanBodyBones.RightRing2",
  174. "rightRingProximal": "HumanBodyBones.RightRing1",
  175. "rightShoulder": "HumanBodyBones.RightShoulder",
  176. "rightThumbIntermediate": "HumanBodyBones.RightThumb2",
  177. "rightThumbProximal": "HumanBodyBones.RightThumb1",
  178. "rightToes": "HumanBodyBones.RightToes",
  179. "rightUpperArm": "HumanBodyBones.RightUpperArm",
  180. "rightUpperLeg": "HumanBodyBones.RightUpperLeg",
  181. "spine": "HumanBodyBones.Spine01"
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment