def function inside the main python script

def function inside the main python script

halitomi
Enthusiast Enthusiast
2,230 Views
16 Replies
Message 1 of 17

def function inside the main python script

halitomi
Enthusiast
Enthusiast

I would like to use an "inner" function in python multiple times to create a more advanced script where I do not need to use the same lines over and over again. But it seems anything created in the "inner" function does not exist in the main, outer script. Is there any solution for this? 

 

A very simple example here:

from varmain.primitiv import *
from varmain.custom import *
from math import *

@activate(Group="Support", TooltipShort="Class1", TooltipLong="Class1", LengthUnit="mm", Ports="1")
@group("MainDimensions")
@param(D=LENGTH, TooltipLong="length of box")

def Class1(s,D=50.0,**kw):

	def Inner():
		o1=BOX(s,L=D,H=D,W=D)
		return o1
		
	Inner()
	#up to this line the scripts runs smoothly, the box is generated by the "Inner" function with sides "D"
	
	o1.translate((0,10,0))
	#this translate generates and error because "o1" object seems to be not existing outside the inner function
	
	s.setPoint((0,0,0),(1,0,0))

 

0 Likes
2,231 Views
16 Replies
Replies (16)
Message 2 of 17

h_eger
Mentor
Mentor

Dear @halitomi ,

 

A question about your script structure where did you get this information?

-

If my reply was helpful, please give a "Kudo" or click the "Accept as Solution" button below (or both).

Hartmut Eger
Senior Engineer
Anlagenplanung + Elektotechnik
XING | LinkedIn

EESignature



0 Likes
Message 3 of 17

halitomi
Enthusiast
Enthusiast

I am not sure if this answers your question:

I have tested the script both with the translate function and without it. Without it, there is no error.

 

Also any other command which “needs” o1 generates an error if the command is outside the inner function.

0 Likes
Message 4 of 17

h_eger
Mentor
Mentor

Yes this will also happen in the future.
Attached is the tutorial for scripting in Plant 3D with Python.

 

-

If my reply was helpful, please give a "Kudo" or click the "Accept as Solution" button below (or both).

Hartmut Eger
Senior Engineer
Anlagenplanung + Elektotechnik
XING | LinkedIn

EESignature



Message 5 of 17

halitomi
Enthusiast
Enthusiast

I know this document. It is just strange that I can not use the “def” function here, as far as I understand it is basic python stuff.

0 Likes
Message 6 of 17

George.Endrulat
Advisor
Advisor

You can define multiple functions in a single file, they just need to be outside of each other (I haven't tested nested), I did that before (multiple separate functions) when I needed the user to be able to select different beam geometry in a support in the objects properties in plant, external function would do a very simple lookup and generate/return the created beam geometry that got stored in a main function variable.

George

If my post offers a solution, please click "Accept as Solution"
0 Likes
Message 7 of 17

halitomi
Enthusiast
Enthusiast

wow, thanks George! 

 

maybe I am asking too much here:

But would you share a sample script using multiple defs? (just a simple one where one is making a box while the other a cylinder or something like that, I do not need the actual big one)

0 Likes
Message 8 of 17

George.Endrulat
Advisor
Advisor

It's easier than that, I'm sure you've decompiled the .pyc files in the variants.zip file, after you've decompiled those have a look through them, there's plenty in there with examples of it and examples of how to do just about everything.

George

If my post offers a solution, please click "Accept as Solution"
Message 9 of 17

李榕华|Ronghua.LI
Advisor
Advisor

Change it to the following. It should be OK.

from varmain.primitiv import *
from varmain.custom import *
from math import *
def Inner():
	o1=BOX(s,L=D,H=D,W=D)
	return o1
@activate(Group="Support", TooltipShort="Class1", TooltipLong="Class1", LengthUnit="mm", Ports="1")
@group("MainDimensions")
@param(D=LENGTH, TooltipLong="length of box")

def Class1(s,D=50.0,**kw):
	o1=Inner().translate((0,10,0))
	s.setPoint((0,0,0),(1,0,0))

李榕华

13489140049@qq.com




Message 10 of 17

theshoaib.mughal
Advocate
Advocate

I am having an issue in calling other script into main script. I have two. First is the modelling script and second is the one that calls first for geometry creation. Everything works fine and geometry is createdbut I cannot rotate or translate or subtract/unite the geometry created this way.

 

First script (Modeller.py)

from varmain.primitiv import *

from varmain.custom import *

from math import *

 

def strBeam(s, L=25, W=25, H=1000):

->s1= BOX(s, L=L, W=W, H=H). translate ((H/2, 0, W/2)) # create and move box on positive XZ

 

Main Script (TestBeam.py)

from varmain.primitiv import *

from varmain.custom import *

from math import *

 

import os
import sys
scriptpath =os.path.normpath(sys.PnP3dNativeContentCustomScriptsPath)

if scriptpath not in sys.path:

->sys.path.append(scriptpath)

from Modeller import *

 

@Anonymous(Group="Sleeve", TooltipShort="Straight Beam", TooltipLong="Straight Beam", LengthUnit="mm", Ports="2")

@group("MainDimensions")

@Anonymous(LN=LENGTH, TooltipShort="length", TooltipLong="length")

@Anonymous(WI=LENGTH, TooltipShort="width", TooltipLong="width")

@Anonymous(HI=LENGTH, TooltipShort="height", TooltipLong="height")

 

@group(Name="meaningless enum")

@enum(1, "Align X")

@enum(2, "Align Y")

@enum(3, "Align Z")

 

def TestBeam(s, LN=2000.0, WI=50.0, HI=50.0, ID="TestBeam", **kw):

->beam1 = strBeam(s, s, L=WI, W=HI, H=LN)         #line1

->beam1.translate((50,50,50))                                 #line2 random move

->beam1.rotateZ(90.0)                                              #line3 random rotate

 

-> mark is to show proper indentation here

 

So when I compile and test run TestBeam script. It successfully calls the Modeller.py and uses strBeam to create a box (i.e. if the code termibate at line1). But if I try to move or rotate it (i.e. if the code also has either line2 or line3), geometry failed to create and I am thrown an error 'geometry cannot be created wuth given parameter.nil'

 

So any help guys??

0 Likes
Message 11 of 17

matt.worland
Advisor
Advisor

It looks like you have an extra parameter when you are creating beam1

 

2023-07-20_15-43-00.png

If my reply was helpful, please give a "Thumbs Up" or "Accept as Solution"
0 Likes
Message 12 of 17

theshoaib.mughal
Advocate
Advocate

Oh thats just typo error here. In the code its not there and like i said that geometry is created perfectly but I cannot perform any actions on it like translate, rotate, unite, subtract etc. That's what i need

0 Likes
Message 13 of 17

mikael.santospj
Enthusiast
Enthusiast

I am dfacing similar problem, any suggestions??

Thanks

0 Likes
Message 14 of 17

tri.ptPE4JB
Contributor
Contributor

I have same error with you. Successful call shade after that can't move or rotate.
Anyone have solution ?

0 Likes
Message 15 of 17

theshoaib.mughal
Advocate
Advocate

You simply need to add a ‘return’ value at the end of the function. Without that the function only creates geometry but main function cannot play with it. If you return it with a value then the holding variable in the main function can play around with it

0 Likes
Message 16 of 17

mikael.santospj
Enthusiast
Enthusiast

Thanks, it worked

mikaelsantospj_0-1702309139727.png

 

0 Likes
Message 17 of 17

theshoaib.mughal
Advocate
Advocate

My pleasure

0 Likes