Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix python >= 3.9,ModuleNotFoundError: No module named 'distutils' #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 88 additions & 35 deletions URDF_Exporter/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,54 @@

@author: syuntoku
"""

import sys
import adsk, adsk.core, adsk.fusion
import os.path, re
from xml.etree import ElementTree
from xml.dom import minidom
from distutils.dir_util import copy_tree
# from distutils.dir_util import copy_tree
# from shutil import copytree as copy_tree
import fileinput
import sys

def copy_occs(root):
"""
import shutil
import os


def copy_tree(src, dst):
# 如果目标目录不存在,创建目标目录
if not os.path.exists(dst):
os.makedirs(dst)

# 遍历源目录中的文件和文件夹
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
# 递归复制子目录
copy_tree(s, d)
else:
# 复制文件
shutil.copy2(s, d)


# 使用示例
# copy_tree('source_folder', 'destination_folder')


def copy_occs(root):
"""
duplicate all the components
"""
"""
def copy_body(allOccs, occs):
"""
"""
copy the old occs to new component
"""

bodies = occs.bRepBodies
transform = adsk.core.Matrix3D.create()

# Create new components from occs
# This support even when a component has some occses.
# This support even when a component has some occses.

new_occs = allOccs.addNewComponent(transform) # this create new occs
if occs.component.name == 'base_link':
Expand All @@ -38,7 +63,7 @@ def copy_body(allOccs, occs):
for i in range(bodies.count):
body = bodies.item(i)
body.copyToComponent(new_occs)

allOccs = root.occurrences
oldOccs = []
coppy_list = [occs for occs in allOccs]
Expand All @@ -51,33 +76,33 @@ def copy_body(allOccs, occs):
occs.component.name = 'old_component'


def export_stl(design, save_dir, components):
def export_stl(design, save_dir, components):
"""
export stl files into "sace_dir/"


Parameters
----------
design: adsk.fusion.Design.cast(product)
save_dir: str
directory path to save
components: design.allComponents
"""

# create a single exportManager instance
exportMgr = design.exportManager
# get the script location
try: os.mkdir(save_dir + '/meshes')
except: pass
scriptDir = save_dir + '/meshes'
scriptDir = save_dir + '/meshes'
# export the occurrence one by one in the component to a specified file
for component in components:
allOccus = component.allOccurrences
for occ in allOccus:
if 'old_component' not in occ.component.name:
try:
print(occ.component.name)
fileName = scriptDir + "/" + occ.component.name
fileName = scriptDir + "/" + occ.component.name
# create stl exportOptions
stlExportOptions = exportMgr.createSTLExportOptions(occ, fileName)
stlExportOptions.sendToPrintUtility = False
Expand All @@ -87,16 +112,16 @@ def export_stl(design, save_dir, components):
exportMgr.execute(stlExportOptions)
except:
print('Component ' + occ.component.name + 'has something wrong.')


def file_dialog(ui):

def file_dialog(ui):
"""
display the dialog to save the file
"""
# Set styles of folder dialog.
folderDlg = ui.createFolderDialog()
folderDlg.title = 'Fusion Folder Dialog'
folderDlg.title = 'Fusion Folder Dialog'

# Show folder dialog
dlgResult = folderDlg.showDialog()
if dlgResult == adsk.core.DialogResults.DialogOK:
Expand All @@ -106,16 +131,16 @@ def file_dialog(ui):

def origin2center_of_mass(inertia, center_of_mass, mass):
"""
convert the moment of the inertia about the world coordinate into
convert the moment of the inertia about the world coordinate into
that about center of mass coordinate


Parameters
----------
moment of inertia about the world coordinate: [xx, yy, zz, xy, yz, xz]
center_of_mass: [x, y, z]


Returns
----------
moment of inertia about center of mass : [xx, yy, zz, xy, yz, xz]
Expand All @@ -134,8 +159,8 @@ def prettify(elem):
Parameters
----------
elem : xml.etree.ElementTree.Element


Returns
----------
pretified xml : str
Expand All @@ -144,19 +169,48 @@ def prettify(elem):
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")

def copy_package(save_dir, package_dir):
def create_package(package_name, save_dir, package_dir):
try: os.mkdir(save_dir + '/launch')
except: pass
except: pass

try: os.mkdir(save_dir + '/urdf')
except: pass
except: pass

try: os.mkdir(save_dir + '/config')
except: pass

try: os.mkdir(save_dir + '/' +package_name)
except: pass
with open(os.path.join(save_dir, package_name, '__init__.py'), 'w'):
pass

try: os.mkdir(save_dir + '/resource')
except: pass
with open(os.path.join(save_dir, 'resource', package_name), 'w'):
pass

try: os.mkdir(save_dir + '/test')
except: pass

copy_tree(package_dir, save_dir)

def update_cmakelists(save_dir, package_name):
file_name = save_dir + '/CMakeLists.txt'
def update_setup_py(save_dir, package_name):
file_name = save_dir + '/setup.py'

for line in fileinput.input(file_name, inplace=True):
if "package_name = 'fusion2urdf_ros2'" in line:
sys.stdout.write("package_name = '" + package_name + "'\n")
else:
sys.stdout.write(line)

def update_setup_cfg(save_dir, package_name):
file_name = save_dir + '/setup.cfg'

for line in fileinput.input(file_name, inplace=True):
if 'project(fusion2urdf)' in line:
sys.stdout.write("project(" + package_name + ")\n")
if "script-dir" in line:
sys.stdout.write("script-dir=$base/lib/" + package_name + "\n")
elif "install-scripts" in line:
sys.stdout.write("install-scripts=$base/lib/" + package_name + "\n")
else:
sys.stdout.write(line)

Expand All @@ -165,9 +219,8 @@ def update_package_xml(save_dir, package_name):

for line in fileinput.input(file_name, inplace=True):
if '<name>' in line:
sys.stdout.write(" <name>" + package_name + "</name>\n")
sys.stdout.write("<name>" + package_name + "</name>\n")
elif '<description>' in line:
sys.stdout.write("<description>The " + package_name + " package</description>\n")
else:
sys.stdout.write(line)