save plugins Fusion 360
This commit is contained in:
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 |
Reference in New Issue
Block a user