save plugins Fusion 360
This commit is contained in:
1
ControllByPs4/.env
Normal file
1
ControllByPs4/.env
Normal file
@@ -0,0 +1 @@
|
||||
PYTHONPATH=C:/Users/bartool/AppData/Local/Autodesk/webdeploy/production/9209df45963e1599ff476303834125d21fd43de4/Api/Python/packages
|
||||
20
ControllByPs4/.vscode/launch.json
vendored
Normal file
20
ControllByPs4/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [{
|
||||
"name": "Python: Attach",
|
||||
"type": "python",
|
||||
"request": "attach",
|
||||
"pathMappings": [{
|
||||
"localRoot": "${workspaceRoot}",
|
||||
"remoteRoot": "${workspaceRoot}"
|
||||
}],
|
||||
"osx": {
|
||||
"filePath": "${file}"
|
||||
},
|
||||
"windows": {
|
||||
"filePath": "${file}"
|
||||
},
|
||||
"port": 9000,
|
||||
"host": "localhost"
|
||||
}]
|
||||
}
|
||||
5
ControllByPs4/.vscode/settings.json
vendored
Normal file
5
ControllByPs4/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"python.autoComplete.extraPaths": ["C:/Users/bartool/AppData/Roaming/Autodesk/Autodesk Fusion 360/API/Python/defs"],
|
||||
"python.analysis.extraPaths": ["C:/Users/bartool/AppData/Roaming/Autodesk/Autodesk Fusion 360/API/Python/defs"],
|
||||
"python.pythonPath": "C:/Users/bartool/AppData/Local/Autodesk/webdeploy/production/9209df45963e1599ff476303834125d21fd43de4/Python/python.exe"
|
||||
}
|
||||
145
ControllByPs4/ControllByPs4 copy.py
Normal file
145
ControllByPs4/ControllByPs4 copy.py
Normal file
@@ -0,0 +1,145 @@
|
||||
# Assuming you have not changed the general structure of the template no modification is needed in this file.
|
||||
# from . import commands
|
||||
from tkinter import E
|
||||
from .lib import fusion360utils as futil
|
||||
import threading
|
||||
import adsk.core
|
||||
import adsk.fusion
|
||||
import adsk.cam
|
||||
import traceback
|
||||
import json
|
||||
|
||||
|
||||
app = None
|
||||
ui = adsk.core.UserInterface.cast(None)
|
||||
handlers = []
|
||||
stopFlag = None
|
||||
myCustomEvent = 'MyCustomEventId'
|
||||
customEvent = None
|
||||
|
||||
|
||||
# The event handler that responds to the custom event being fired.
|
||||
class ThreadEventHandler(adsk.core.CustomEventHandler):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.viewport = app.activeViewport
|
||||
|
||||
def notify(self, args: adsk.core.CustomEventArgs):
|
||||
global app
|
||||
try:
|
||||
# Make sure a command isn't running before changes are made.
|
||||
activeCmd = ui.activeCommand
|
||||
if activeCmd != 'SelectCommand':
|
||||
ui.commandDefinitions.itemById('SelectCommand').execute()
|
||||
|
||||
camera = self.viewport.camera
|
||||
eye = camera.eye
|
||||
target = camera.target
|
||||
up = camera.upVector
|
||||
zoom = camera.viewExtents
|
||||
|
||||
front = eye.vectorTo(target)
|
||||
right = up.crossProduct(front)
|
||||
rotate_matrix = adsk.core.Matrix3D.create()
|
||||
rotate_matrix.setWithCoordinateSystem(target, right, front, up)
|
||||
mat = rotate_matrix.asArray()
|
||||
|
||||
# Get the value from the JSON data passed through the event.
|
||||
eventArgs = json.loads(args.additionalInfo)
|
||||
|
||||
X_radians = float(eventArgs['X_rotation'])
|
||||
if X_radians:
|
||||
rotate_matrix.setToRotation(X_radians, front, target)
|
||||
eye.transformBy(rotate_matrix)
|
||||
up.transformBy(rotate_matrix)
|
||||
|
||||
Y_radians = float(eventArgs['Y_rotation'])
|
||||
if Y_radians:
|
||||
rotate_matrix.setToRotation(Y_radians, right, target)
|
||||
eye.transformBy(rotate_matrix)
|
||||
up.transformBy(rotate_matrix)
|
||||
|
||||
Z_radians = float(eventArgs['Z_rotation'])
|
||||
if Z_radians:
|
||||
rotate_matrix.setToRotation(Z_radians, up, target)
|
||||
eye.transformBy(rotate_matrix)
|
||||
up.transformBy(rotate_matrix)
|
||||
|
||||
X_pan = float(eventArgs['X_pan'])
|
||||
Y_pan = float(eventArgs['Y_pan'])
|
||||
Z_pan = float(eventArgs['Z_pan'])
|
||||
if any([X_pan, Z_pan]):
|
||||
move_vector = adsk.core.Vector3D.create(X_pan, 0, Z_pan)
|
||||
move_vector.transformBy(rotate_matrix)
|
||||
eye.translateBy(move_vector)
|
||||
target.translateBy(move_vector)
|
||||
|
||||
if Y_pan:
|
||||
zoom = zoom + Y_pan * 10
|
||||
|
||||
camera.isSmoothTransition = True
|
||||
camera.eye = eye
|
||||
camera.upVector = up
|
||||
camera.target = target
|
||||
camera.viewExtents = zoom
|
||||
app.activeViewport.camera = camera
|
||||
app.activeViewport.refresh()
|
||||
|
||||
except:
|
||||
if ui:
|
||||
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
|
||||
|
||||
def rotate(self, X_radians: float, Y_radians: float, Z_radians: float):
|
||||
pass
|
||||
|
||||
|
||||
# The class for the new thread.
|
||||
class MyThread(threading.Thread):
|
||||
def __init__(self, event):
|
||||
threading.Thread.__init__(self)
|
||||
self.stopped = event
|
||||
|
||||
def run(self):
|
||||
# Every five seconds fire a custom event, passing a random number.
|
||||
while not self.stopped.wait(2):
|
||||
args = {'X_rotation': 0.0, 'Y_rotation': 0.0, 'Z_rotation': 0.0,
|
||||
'X_pan': 0.0, 'Y_pan': 1.0, 'Z_pan': 0.0}
|
||||
app.fireCustomEvent(myCustomEvent, json.dumps(args))
|
||||
|
||||
|
||||
def run(context):
|
||||
global ui
|
||||
global app
|
||||
try:
|
||||
app = adsk.core.Application.get()
|
||||
ui = app.userInterface
|
||||
|
||||
# Register the custom event and connect the handler.
|
||||
global customEvent
|
||||
customEvent = app.registerCustomEvent(myCustomEvent)
|
||||
onThreadEvent = ThreadEventHandler()
|
||||
customEvent.add(onThreadEvent)
|
||||
handlers.append(onThreadEvent)
|
||||
|
||||
# Create a new thread for the other processing.
|
||||
global stopFlag
|
||||
stopFlag = threading.Event()
|
||||
myThread = MyThread(stopFlag)
|
||||
myThread.start()
|
||||
except:
|
||||
futil.handle_error('run')
|
||||
if ui:
|
||||
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
|
||||
|
||||
|
||||
def stop(context):
|
||||
try:
|
||||
if handlers.count:
|
||||
customEvent.remove(handlers[0])
|
||||
stopFlag.set()
|
||||
app.unregisterCustomEvent(myCustomEvent)
|
||||
ui.messageBox('Stop addin')
|
||||
except:
|
||||
futil.handle_error('stop')
|
||||
if ui:
|
||||
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
|
||||
13
ControllByPs4/ControllByPs4.manifest
Normal file
13
ControllByPs4/ControllByPs4.manifest
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"autodeskProduct": "Fusion360",
|
||||
"type": "addin",
|
||||
"id": "1a7fc1eb-71fd-42da-86f0-a7e640d647d0",
|
||||
"author": "",
|
||||
"description": {
|
||||
"": ""
|
||||
},
|
||||
"version": "",
|
||||
"runOnStartup": false,
|
||||
"supportedOS": "windows|mac",
|
||||
"editEnabled": true
|
||||
}
|
||||
24
ControllByPs4/ControllByPs4.py
Normal file
24
ControllByPs4/ControllByPs4.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# Assuming you have not changed the general structure of the template no modification is needed in this file.
|
||||
from . import commands
|
||||
from .lib import fusion360utils as futil
|
||||
|
||||
|
||||
def run(context):
|
||||
try:
|
||||
# This will run the start function in each of your commands as defined in commands/__init__.py
|
||||
commands.start()
|
||||
|
||||
except:
|
||||
futil.handle_error('run')
|
||||
|
||||
|
||||
def stop(context):
|
||||
try:
|
||||
# Remove all of the event handlers your app has created
|
||||
futil.clear_handlers()
|
||||
|
||||
# This will run the start function in each of your commands as defined in commands/__init__.py
|
||||
commands.stop()
|
||||
|
||||
except:
|
||||
futil.handle_error('stop')
|
||||
BIN
ControllByPs4/__pycache__/config.cpython-39.pyc
Normal file
BIN
ControllByPs4/__pycache__/config.cpython-39.pyc
Normal file
Binary file not shown.
29
ControllByPs4/commands/__init__.py
Normal file
29
ControllByPs4/commands/__init__.py
Normal file
@@ -0,0 +1,29 @@
|
||||
# Here you define the commands that will be added to your add-in.
|
||||
|
||||
# TODO Import the modules corresponding to the commands you created.
|
||||
# If you want to add an additional command, duplicate one of the existing directories and import it here.
|
||||
# You need to use aliases (import "entry" as "my_module") assuming you have the default module named "entry".
|
||||
import imp
|
||||
from .commandDialog import entry as commandDialog
|
||||
from .backend import backend
|
||||
|
||||
# TODO add your imported modules to this list.
|
||||
# Fusion will automatically call the start() and stop() functions.
|
||||
commands = [
|
||||
commandDialog,
|
||||
backend
|
||||
]
|
||||
|
||||
|
||||
# Assumes you defined a "start" function in each of your modules.
|
||||
# The start function will be run when the add-in is started.
|
||||
def start():
|
||||
for command in commands:
|
||||
command.start()
|
||||
|
||||
|
||||
# Assumes you defined a "stop" function in each of your modules.
|
||||
# The stop function will be run when the add-in is stopped.
|
||||
def stop():
|
||||
for command in commands:
|
||||
command.stop()
|
||||
BIN
ControllByPs4/commands/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
ControllByPs4/commands/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
0
ControllByPs4/commands/backend/__init__.py
Normal file
0
ControllByPs4/commands/backend/__init__.py
Normal file
Binary file not shown.
Binary file not shown.
178
ControllByPs4/commands/backend/backend.py
Normal file
178
ControllByPs4/commands/backend/backend.py
Normal file
@@ -0,0 +1,178 @@
|
||||
# Assuming you have not changed the general structure of the template no modification is needed in this file.
|
||||
# from . import commands
|
||||
from ast import arg
|
||||
from glob import glob
|
||||
from ...lib import fusion360utils as futil
|
||||
import threading
|
||||
import adsk.core
|
||||
import adsk.fusion
|
||||
import adsk.cam
|
||||
import traceback
|
||||
import json
|
||||
|
||||
CMD_NAME = 'View Controller backend'
|
||||
|
||||
app = adsk.core.Application.get()
|
||||
ui = app.userInterface
|
||||
stopFlag = None
|
||||
cameraViewEvent = 'CameraViewEventId'
|
||||
updateConstantsEvent = "UpdateConstantsEventId"
|
||||
|
||||
customEvents = []
|
||||
local_handlers = []
|
||||
|
||||
X_ROTATION = 0
|
||||
Y_ROTATION = 0
|
||||
Z_ROTATION = 0
|
||||
X_PAN = 0
|
||||
Y_PAN = 0
|
||||
Z_PAN = 0
|
||||
|
||||
|
||||
class UpdateConstantsEventHandler(adsk.core.CustomEventHandler):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def notify(self, args: adsk.core.CustomEventArgs) -> None:
|
||||
futil.log(f'{CMD_NAME} UpdateConstants notity...')
|
||||
eventArgs = json.loads(args.additionalInfo)
|
||||
global X_ROTATION, Y_ROTATION, Z_ROTATION
|
||||
global X_PAN, Y_PAN, Z_PAN
|
||||
X_ROTATION = float(eventArgs["X_rotation"])
|
||||
Y_ROTATION = float(eventArgs["Y_rotation"])
|
||||
Z_ROTATION = float(eventArgs["Z_rotation"])
|
||||
X_PAN = float(eventArgs["X_pan"])
|
||||
Y_PAN = float(eventArgs["Y_pan"])
|
||||
Z_PAN = float(eventArgs["Z_pan"])
|
||||
|
||||
|
||||
# The event handler that responds to the custom event being fired.
|
||||
class CameraViewEventHandler(adsk.core.CustomEventHandler):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.viewport = app.activeViewport
|
||||
|
||||
def notify(self, args: adsk.core.CustomEventArgs):
|
||||
global app
|
||||
try:
|
||||
# Make sure a command isn't running before changes are made.
|
||||
activeCmd = ui.activeCommand
|
||||
if activeCmd != 'SelectCommand':
|
||||
ui.commandDefinitions.itemById('SelectCommand').execute()
|
||||
|
||||
camera = self.viewport.camera
|
||||
eye = camera.eye
|
||||
target = camera.target
|
||||
up = camera.upVector
|
||||
zoom = camera.viewExtents
|
||||
|
||||
front = eye.vectorTo(target)
|
||||
right = up.crossProduct(front)
|
||||
rotate_matrix = adsk.core.Matrix3D.create()
|
||||
rotate_matrix.setWithCoordinateSystem(target, right, front, up)
|
||||
mat = rotate_matrix.asArray()
|
||||
|
||||
# Get the value from the JSON data passed through the event.
|
||||
eventArgs: dict = json.loads(args.additionalInfo)
|
||||
|
||||
X_radians = float(eventArgs.get('X_rotation', 0)) * X_ROTATION
|
||||
if X_radians:
|
||||
rotate_matrix.setToRotation(X_radians, front, target)
|
||||
eye.transformBy(rotate_matrix)
|
||||
up.transformBy(rotate_matrix)
|
||||
|
||||
Y_radians = float(eventArgs.get('Y_rotation', 0)) * Y_ROTATION
|
||||
if Y_radians:
|
||||
rotate_matrix.setToRotation(Y_radians, right, target)
|
||||
eye.transformBy(rotate_matrix)
|
||||
up.transformBy(rotate_matrix)
|
||||
|
||||
Z_radians = float(eventArgs.get('Z_rotation', 0)) * Z_ROTATION
|
||||
if Z_radians:
|
||||
rotate_matrix.setToRotation(Z_radians, up, target)
|
||||
eye.transformBy(rotate_matrix)
|
||||
up.transformBy(rotate_matrix)
|
||||
|
||||
X_pan = float(eventArgs.get('X_pan', 0)) * X_pan
|
||||
Y_pan = float(eventArgs.get('Y_pan', 0)) * Y_pan
|
||||
Z_pan = float(eventArgs.get('Z_pan', 0)) * Z_pan
|
||||
if any([X_pan, Z_pan]):
|
||||
move_vector = adsk.core.Vector3D.create(X_pan, 0, Z_pan)
|
||||
move_vector.transformBy(rotate_matrix)
|
||||
eye.translateBy(move_vector)
|
||||
target.translateBy(move_vector)
|
||||
|
||||
if Y_pan:
|
||||
zoom = zoom + Y_pan * 10
|
||||
|
||||
camera.isSmoothTransition = True
|
||||
camera.eye = eye
|
||||
camera.upVector = up
|
||||
camera.target = target
|
||||
camera.viewExtents = zoom
|
||||
app.activeViewport.camera = camera
|
||||
app.activeViewport.refresh()
|
||||
|
||||
except:
|
||||
if ui:
|
||||
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
|
||||
|
||||
def rotate(self, X_radians: float, Y_radians: float, Z_radians: float):
|
||||
pass
|
||||
|
||||
|
||||
# The class for the new thread.
|
||||
class ControllerThread(threading.Thread):
|
||||
def __init__(self, event):
|
||||
threading.Thread.__init__(self)
|
||||
self.stopped = event
|
||||
|
||||
def run(self):
|
||||
# Every five seconds fire a custom event, passing a random number.
|
||||
while not self.stopped.wait(2):
|
||||
futil.log(f'{CMD_NAME} Thread start sending data')
|
||||
args = {'X_rotation': 0.0, 'Y_rotation': 0.0, 'Z_rotation': 0.0,
|
||||
'X_pan': 0.0, 'Y_pan': 0.0, 'Z_pan': 0.0}
|
||||
app.fireCustomEvent(cameraViewEvent, json.dumps(args))
|
||||
|
||||
|
||||
def start():
|
||||
|
||||
futil.log(f'{CMD_NAME} Start...')
|
||||
# Register the custom event and connect the handler.
|
||||
global customEvents
|
||||
customEvents.append(app.registerCustomEvent(cameraViewEvent))
|
||||
onCameraViewEvent = CameraViewEventHandler()
|
||||
customEvents[0].add(onCameraViewEvent)
|
||||
local_handlers.append(onCameraViewEvent)
|
||||
futil.log(f'{CMD_NAME} CameraViewEvent register')
|
||||
|
||||
customEvents.append(app.registerCustomEvent(updateConstantsEvent))
|
||||
onUpadateConstantEvent = UpdateConstantsEventHandler()
|
||||
customEvents[1].add(onUpadateConstantEvent)
|
||||
local_handlers.append(onUpadateConstantEvent)
|
||||
futil.log(f'{CMD_NAME} UpdateConstantsEvent register')
|
||||
|
||||
# Create a new thread for the other processing.
|
||||
global stopFlag
|
||||
stopFlag = threading.Event()
|
||||
myThread = ControllerThread(stopFlag)
|
||||
# myThread.start()
|
||||
futil.log(f'{CMD_NAME} ControllerThread started')
|
||||
|
||||
|
||||
def stop():
|
||||
try:
|
||||
for event, handler in zip(customEvents, local_handlers):
|
||||
event.remove(handler)
|
||||
# if local_handlers.count:
|
||||
# customEvent.remove(local_handlers[0])
|
||||
|
||||
stopFlag.set()
|
||||
app.unregisterCustomEvent(cameraViewEvent)
|
||||
app.unregisterCustomEvent(updateConstantsEvent)
|
||||
ui.messageBox('Stop addin')
|
||||
except:
|
||||
futil.handle_error('stop')
|
||||
# if ui:
|
||||
# ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
|
||||
0
ControllByPs4/commands/commandDialog/__init__.py
Normal file
0
ControllByPs4/commands/commandDialog/__init__.py
Normal file
Binary file not shown.
Binary file not shown.
190
ControllByPs4/commands/commandDialog/entry.py
Normal file
190
ControllByPs4/commands/commandDialog/entry.py
Normal file
@@ -0,0 +1,190 @@
|
||||
import adsk.core
|
||||
import os
|
||||
from ...lib import fusion360utils as futil
|
||||
from ... import config
|
||||
import json
|
||||
app = adsk.core.Application.get()
|
||||
ui = app.userInterface
|
||||
|
||||
|
||||
# TODO *** Specify the command identity information. ***
|
||||
CMD_ID = f'{config.COMPANY_NAME}_{config.ADDIN_NAME}_cmdDialog'
|
||||
CMD_NAME = 'View Controller'
|
||||
CMD_Description = 'A Fusion 360 Add-in Command with a dialog'
|
||||
|
||||
# Specify that the command will be promoted to the panel.
|
||||
IS_PROMOTED = True
|
||||
|
||||
# TODO *** Define the location where the command button will be created. ***
|
||||
# This is done by specifying the workspace, the tab, and the panel, and the
|
||||
# command it will be inserted beside. Not providing the command to position it
|
||||
# will insert it at the end.
|
||||
WORKSPACE_ID = 'FusionSolidEnvironment'
|
||||
PANEL_ID = 'SolidScriptsAddinsPanel'
|
||||
COMMAND_BESIDE_ID = 'ScriptsManagerCommand'
|
||||
|
||||
# Resource location for command icons, here we assume a sub folder in this directory named "resources".
|
||||
ICON_FOLDER = os.path.join(os.path.dirname(
|
||||
os.path.abspath(__file__)), 'resources', '')
|
||||
|
||||
# Local list of event handlers used to maintain a reference so
|
||||
# they are not released and garbage collected.
|
||||
local_handlers = []
|
||||
|
||||
|
||||
# Executed when add-in is run.
|
||||
def start():
|
||||
# Create a command Definition.
|
||||
cmd_def = ui.commandDefinitions.addButtonDefinition(
|
||||
CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER)
|
||||
|
||||
# Define an event handler for the command created event. It will be called when the button is clicked.
|
||||
futil.add_handler(cmd_def.commandCreated, command_created)
|
||||
|
||||
# ******** Add a button into the UI so the user can run the command. ********
|
||||
# Get the target workspace the button will be created in.
|
||||
workspace = ui.workspaces.itemById(WORKSPACE_ID)
|
||||
|
||||
# Get the panel the button will be created in.
|
||||
panel = workspace.toolbarPanels.itemById(PANEL_ID)
|
||||
|
||||
# Create the button command control in the UI after the specified existing command.
|
||||
control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False)
|
||||
|
||||
# Specify if the command is promoted to the main toolbar.
|
||||
control.isPromoted = IS_PROMOTED
|
||||
|
||||
|
||||
# Executed when add-in is stopped.
|
||||
def stop():
|
||||
# Get the various UI elements for this command
|
||||
workspace = ui.workspaces.itemById(WORKSPACE_ID)
|
||||
panel = workspace.toolbarPanels.itemById(PANEL_ID)
|
||||
command_control = panel.controls.itemById(CMD_ID)
|
||||
command_definition = ui.commandDefinitions.itemById(CMD_ID)
|
||||
|
||||
# Delete the button command control
|
||||
if command_control:
|
||||
command_control.deleteMe()
|
||||
|
||||
# Delete the command definition
|
||||
if command_definition:
|
||||
command_definition.deleteMe()
|
||||
|
||||
|
||||
# Function that is called when a user clicks the corresponding button in the UI.
|
||||
# This defines the contents of the command dialog and connects to the command related events.
|
||||
def command_created(args: adsk.core.CommandCreatedEventArgs):
|
||||
# General logging for debug.
|
||||
futil.log(f'{CMD_NAME} Command Created Event')
|
||||
|
||||
# https://help.autodesk.com/view/fusion360/ENU/?contextId=CommandInputs
|
||||
inputs = args.command.commandInputs
|
||||
|
||||
# TODO Define the dialog for your command by adding different inputs to the command.
|
||||
|
||||
# # Create a simple text box input.
|
||||
# inputs.addTextBoxCommandInput(
|
||||
# 'text_box', 'Some Text', 'Enter some text.', 1, False)
|
||||
|
||||
default_angle = adsk.core.ValueInput.createByString('5')
|
||||
inputs.addAngleValueCommandInput("x_rotation", "X Rotation", default_angle)
|
||||
inputs.addAngleValueCommandInput("y_rotation", "Y Rotation", default_angle)
|
||||
inputs.addAngleValueCommandInput("z_rotation", "Z Rotation", default_angle)
|
||||
|
||||
# # Create a value input field and set the default using 1 unit of the default length unit.
|
||||
defaultLengthUnits = app.activeProduct.unitsManager.defaultLengthUnits
|
||||
default_value_x = adsk.core.ValueInput.createByString("1")
|
||||
default_value_y = adsk.core.ValueInput.createByString("2")
|
||||
default_value_z = adsk.core.ValueInput.createByString("3")
|
||||
inputs.addValueInput('x_pan', 'X Disytance',
|
||||
defaultLengthUnits, default_value_x)
|
||||
inputs.addValueInput('y_pan', 'Y Disytance',
|
||||
defaultLengthUnits, default_value_y)
|
||||
inputs.addValueInput('z_pan', 'Z Disytance',
|
||||
defaultLengthUnits, default_value_z)
|
||||
|
||||
# TODO Connect to the events that are needed by this command.
|
||||
futil.add_handler(args.command.execute, command_execute,
|
||||
local_handlers=local_handlers)
|
||||
# futil.add_handler(args.command.inputChanged, command_input_changed, local_handlers=local_handlers)
|
||||
# futil.add_handler(args.command.executePreview, command_preview, local_handlers=local_handlers)
|
||||
# futil.add_handler(args.command.validateInputs, command_validate_input, local_handlers=local_handlers)
|
||||
futil.add_handler(args.command.destroy, command_destroy,
|
||||
local_handlers=local_handlers)
|
||||
|
||||
|
||||
# This event handler is called when the user clicks the OK button in the command dialog or
|
||||
# is immediately called after the created event not command inputs were created for the dialog.
|
||||
def command_execute(args: adsk.core.CommandEventArgs):
|
||||
# General logging for debug.
|
||||
futil.log(f'{CMD_NAME} Command Execute Event')
|
||||
|
||||
# TODO ******************************** Your code here ********************************
|
||||
|
||||
# Get a reference to your command's inputs.
|
||||
inputs = args.command.commandInputs
|
||||
# text_box: adsk.core.TextBoxCommandInput = inputs.itemById('text_box')
|
||||
# value_input: adsk.core.ValueCommandInput = inputs.itemById('value_input')
|
||||
triad_input: adsk.core.TriadCommandInput = inputs.itemById('matrix_input')
|
||||
args = {}
|
||||
# value_input: adsk.core.ValueCommandInput = inputs.itemById["x_rotation"]
|
||||
args["X_rotation"] = inputs.itemById("x_rotation").value
|
||||
args["Y_rotation"] = inputs.itemById("y_rotation").value
|
||||
args["Z_rotation"] = inputs.itemById("z_rotation").value
|
||||
|
||||
# angle_input: adsk.core.AngleValueCommandInput = inputs.itemById["x_pan"]
|
||||
args["X_pan"] = inputs.itemById("x_pan").value
|
||||
args["Y_pan"] = inputs.itemById("y_pan").value
|
||||
args["Z_pan"] = inputs.itemById("z_pan").value
|
||||
# Do something interesting
|
||||
# text = text_box.text
|
||||
# expression = value_input.expression
|
||||
# msg = f'Your text: {text}<br>Your value: {expression}'
|
||||
# ui.messageBox(msg)
|
||||
|
||||
app.fireCustomEvent('UpdateConstantsEventId', json.dumps(args))
|
||||
|
||||
# This event handler is called when the command needs to compute a new preview in the graphics window.
|
||||
|
||||
|
||||
def command_preview(args: adsk.core.CommandEventArgs):
|
||||
# General logging for debug.
|
||||
futil.log(f'{CMD_NAME} Command Preview Event')
|
||||
inputs = args.command.commandInputs
|
||||
|
||||
|
||||
# This event handler is called when the user changes anything in the command dialog
|
||||
# allowing you to modify values of other inputs based on that change.
|
||||
def command_input_changed(args: adsk.core.InputChangedEventArgs):
|
||||
changed_input = args.input
|
||||
inputs = args.inputs
|
||||
|
||||
# General logging for debug.
|
||||
futil.log(
|
||||
f'{CMD_NAME} Input Changed Event fired from a change to {changed_input.id}')
|
||||
|
||||
|
||||
# This event handler is called when the user interacts with any of the inputs in the dialog
|
||||
# which allows you to verify that all of the inputs are valid and enables the OK button.
|
||||
def command_validate_input(args: adsk.core.ValidateInputsEventArgs):
|
||||
# General logging for debug.
|
||||
futil.log(f'{CMD_NAME} Validate Input Event')
|
||||
|
||||
inputs = args.inputs
|
||||
|
||||
# Verify the validity of the input values. This controls if the OK button is enabled or not.
|
||||
# valueInput = inputs.itemById('value_input')
|
||||
# if valueInput.value >= 0:
|
||||
# args.areInputsValid = True
|
||||
# else:
|
||||
# args.areInputsValid = False
|
||||
|
||||
|
||||
# This event handler is called when the command terminates.
|
||||
def command_destroy(args: adsk.core.CommandEventArgs):
|
||||
# General logging for debug.
|
||||
futil.log(f'{CMD_NAME} Command Destroy Event')
|
||||
|
||||
global local_handlers
|
||||
local_handlers = []
|
||||
BIN
ControllByPs4/commands/commandDialog/resources/16x16.png
Normal file
BIN
ControllByPs4/commands/commandDialog/resources/16x16.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 415 B |
BIN
ControllByPs4/commands/commandDialog/resources/32x32.png
Normal file
BIN
ControllByPs4/commands/commandDialog/resources/32x32.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 701 B |
BIN
ControllByPs4/commands/commandDialog/resources/64x64.png
Normal file
BIN
ControllByPs4/commands/commandDialog/resources/64x64.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
21
ControllByPs4/config.py
Normal file
21
ControllByPs4/config.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# Application Global Variables
|
||||
# This module serves as a way to share variables across different
|
||||
# modules (global variables).
|
||||
|
||||
import os
|
||||
|
||||
# Flag that indicates to run in Debug mode or not. When running in Debug mode
|
||||
# more information is written to the Text Command window. Generally, it's useful
|
||||
# to set this to True while developing an add-in and set it to False when you
|
||||
# are ready to distribute it.
|
||||
DEBUG = True
|
||||
|
||||
# Gets the name of the add-in from the name of the folder the py file is in.
|
||||
# This is used when defining unique internal names for various UI elements
|
||||
# that need a unique name. It's also recommended to use a company name as
|
||||
# part of the ID to better ensure the ID is unique.
|
||||
ADDIN_NAME = os.path.basename(os.path.dirname(__file__))
|
||||
COMPANY_NAME = 'BTL'
|
||||
|
||||
# Palettes
|
||||
sample_palette_id = f'{COMPANY_NAME}_{ADDIN_NAME}_palette_id'
|
||||
2
ControllByPs4/lib/fusion360utils/__init__.py
Normal file
2
ControllByPs4/lib/fusion360utils/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .general_utils import *
|
||||
from .event_utils import *
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
88
ControllByPs4/lib/fusion360utils/event_utils.py
Normal file
88
ControllByPs4/lib/fusion360utils/event_utils.py
Normal file
@@ -0,0 +1,88 @@
|
||||
# Copyright 2022 by Autodesk, Inc.
|
||||
# Permission to use, copy, modify, and distribute this software in object code form
|
||||
# for any purpose and without fee is hereby granted, provided that the above copyright
|
||||
# notice appears in all copies and that both that copyright notice and the limited
|
||||
# warranty and restricted rights notice below appear in all supporting documentation.
|
||||
#
|
||||
# AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. AUTODESK SPECIFICALLY
|
||||
# DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.
|
||||
# AUTODESK, INC. DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
|
||||
# UNINTERRUPTED OR ERROR FREE.
|
||||
|
||||
import sys
|
||||
from typing import Callable
|
||||
|
||||
import adsk.core
|
||||
from .general_utils import handle_error
|
||||
|
||||
|
||||
# Global Variable to hold Event Handlers
|
||||
_handlers = []
|
||||
|
||||
|
||||
def add_handler(
|
||||
event: adsk.core.Event,
|
||||
callback: Callable,
|
||||
*,
|
||||
name: str = None,
|
||||
local_handlers: list = None
|
||||
):
|
||||
"""Adds an event handler to the specified event.
|
||||
|
||||
Arguments:
|
||||
event -- The event object you want to connect a handler to.
|
||||
callback -- The function that will handle the event.
|
||||
name -- A name to use in logging errors associated with this event.
|
||||
Otherwise the name of the event object is used. This argument
|
||||
must be specified by its keyword.
|
||||
local_handlers -- A list of handlers you manage that is used to maintain
|
||||
a reference to the handlers so they aren't released.
|
||||
This argument must be specified by its keyword. If not
|
||||
specified the handler is added to a global list and can
|
||||
be cleared using the clear_handlers function. You may want
|
||||
to maintain your own handler list so it can be managed
|
||||
independently for each command.
|
||||
|
||||
:returns:
|
||||
The event handler that was created. You don't often need this reference, but it can be useful in some cases.
|
||||
"""
|
||||
module = sys.modules[event.__module__]
|
||||
handler_type = module.__dict__[event.add.__annotations__['handler']]
|
||||
handler = _create_handler(handler_type, callback, event, name, local_handlers)
|
||||
event.add(handler)
|
||||
return handler
|
||||
|
||||
|
||||
def clear_handlers():
|
||||
"""Clears the global list of handlers.
|
||||
"""
|
||||
global _handlers
|
||||
_handlers = []
|
||||
|
||||
|
||||
def _create_handler(
|
||||
handler_type,
|
||||
callback: Callable,
|
||||
event: adsk.core.Event,
|
||||
name: str = None,
|
||||
local_handlers: list = None
|
||||
):
|
||||
handler = _define_handler(handler_type, callback, name)()
|
||||
(local_handlers if local_handlers is not None else _handlers).append(handler)
|
||||
return handler
|
||||
|
||||
|
||||
def _define_handler(handler_type, callback, name: str = None):
|
||||
name = name or handler_type.__name__
|
||||
|
||||
class Handler(handler_type):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def notify(self, args):
|
||||
try:
|
||||
callback(args)
|
||||
except:
|
||||
handle_error(name)
|
||||
|
||||
return Handler
|
||||
64
ControllByPs4/lib/fusion360utils/general_utils.py
Normal file
64
ControllByPs4/lib/fusion360utils/general_utils.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# Copyright 2022 by Autodesk, Inc.
|
||||
# Permission to use, copy, modify, and distribute this software in object code form
|
||||
# for any purpose and without fee is hereby granted, provided that the above copyright
|
||||
# notice appears in all copies and that both that copyright notice and the limited
|
||||
# warranty and restricted rights notice below appear in all supporting documentation.
|
||||
#
|
||||
# AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. AUTODESK SPECIFICALLY
|
||||
# DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.
|
||||
# AUTODESK, INC. DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
|
||||
# UNINTERRUPTED OR ERROR FREE.
|
||||
|
||||
import os
|
||||
import traceback
|
||||
import adsk.core
|
||||
|
||||
app = adsk.core.Application.get()
|
||||
ui = app.userInterface
|
||||
|
||||
# Attempt to read DEBUG flag from parent config.
|
||||
try:
|
||||
from ... import config
|
||||
DEBUG = config.DEBUG
|
||||
except:
|
||||
DEBUG = False
|
||||
|
||||
|
||||
def log(message: str, level: adsk.core.LogLevels = adsk.core.LogLevels.InfoLogLevel, force_console: bool = False):
|
||||
"""Utility function to easily handle logging in your app.
|
||||
|
||||
Arguments:
|
||||
message -- The message to log.
|
||||
level -- The logging severity level.
|
||||
force_console -- Forces the message to be written to the Text Command window.
|
||||
"""
|
||||
# Always print to console, only seen through IDE.
|
||||
print(message)
|
||||
|
||||
# Log all errors to Fusion log file.
|
||||
if level == adsk.core.LogLevels.ErrorLogLevel:
|
||||
log_type = adsk.core.LogTypes.FileLogType
|
||||
app.log(message, level, log_type)
|
||||
|
||||
# If config.DEBUG is True write all log messages to the console.
|
||||
if DEBUG or force_console:
|
||||
log_type = adsk.core.LogTypes.ConsoleLogType
|
||||
app.log(message, level, log_type)
|
||||
|
||||
|
||||
def handle_error(name: str, show_message_box: bool = False):
|
||||
"""Utility function to simplify error handling.
|
||||
|
||||
Arguments:
|
||||
name -- A name used to label the error.
|
||||
show_message_box -- Indicates if the error should be shown in the message box.
|
||||
If False, it will only be shown in the Text Command window
|
||||
and logged to the log file.
|
||||
"""
|
||||
|
||||
log('===== Error =====', adsk.core.LogLevels.ErrorLogLevel)
|
||||
log(f'{name}\n{traceback.format_exc()}', adsk.core.LogLevels.ErrorLogLevel)
|
||||
|
||||
# If desired you could show an error as a message box.
|
||||
if show_message_box:
|
||||
ui.messageBox(f'{name}\n{traceback.format_exc()}')
|
||||
Reference in New Issue
Block a user