Code with global variables

Code with global variables

Peter__B
Advocate Advocate
1,189 Views
3 Replies
Message 1 of 4

Code with global variables

Peter__B
Advocate
Advocate

Hi

The following code has been written just for my own education and understandig and does not generate anything of value.

However, does anybody know why the code does not print 'newstring' to the spyder console window ? Instead it prints 'string', what commands do I have to add to the code in order get the 'newstring' to the console window. I have tried with global variables without success.

 

Best Regards

Peter

0 Likes
Accepted solutions (1)
1,190 Views
3 Replies
Replies (3)
Message 2 of 4

Peter__B
Advocate
Advocate

Sorry I forgot the code:

 

import adsk.core, adsk.fusion, adsk.cam, traceback

 

text='string'

def run(context):

ui = None

 

try:

app = adsk.core.Application.get()

ui = app.userInterface

# ui.messageBox('Hello script')

product = app.activeProduct

design = adsk.fusion.Design.cast(product)

 

global trt

 

text='newstring'

rootComp = design.rootComponent

sketches = rootComp.sketches

 

xyPlane = rootComp.xYConstructionPlane

sketch = sketches.add(xyPlane)

sketch.name = 'FirstComponent'

 

xzPlane = rootComp.xZConstructionPlane

sketch = sketches.add(xzPlane)

sketch.name = 'SecondComponent'

 

exportMgr = design.exportManager

fusionExportOptions = exportMgr.\

createFusionArchiveExportOptions('D:/FusionData/CadTest')

exportMgr.execute(fusionExportOptions)

return text

 

except:

if ui:

ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

print(text)

 

/Peter

0 Likes
Message 3 of 4

Peter__B
Advocate
Advocate

The formatting disappeared, here is the code aqain;

 

#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback

text='string'     
def run(context):
    ui = None

    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
#        ui.messageBox('Hello script')
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)

        global trt

        text='newstring'        
        rootComp = design.rootComponent
        sketches = rootComp.sketches
        
        xyPlane = rootComp.xYConstructionPlane
        sketch = sketches.add(xyPlane)
        sketch.name = 'FirstComponent'
        
        xzPlane = rootComp.xZConstructionPlane
        sketch = sketches.add(xzPlane)
        sketch.name = 'SecondComponent'
                       
        exportMgr = design.exportManager
        fusionExportOptions = exportMgr.\
        createFusionArchiveExportOptions('D:/FusionData/CadTest')        
        exportMgr.execute(fusionExportOptions)
        return text
               
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
            
    
print(text)
0 Likes
Message 4 of 4

Anonymous
Not applicable
Accepted solution

The error is due to the nature of python. Your print statement is outside the body of the run method. When python loads a source file it executes it. When the file is executed the text='string' code is evaluated, the "def" statement is evaluated and the "run" method is created (in python methods ARE objects too!), then your print(text) statement is executed, and the execution of the file completes (without ever executing the contents of the run method. Then some time later the python layer inside Fusion looks for and finds a method with the name of "run" and executes it.

 

So the problem is the contents of your run method are NOT executed before your print statement. I hope this helps - if not the internet can do a better job of explaining things than I have I'm sure.