Skip to main content
Solved

Cannot Set Source Sampling in NSC


Matt H

I am unable to set the source sampling in NSC; here is my code:

 

led_source = TheNCE.GetObjectAt(1)
led_source_type = led_source.GetObjectTypeSettings(ZOSAPI.Editors.NCE.ObjectType.SourceRadial)
led_source.ChangeType(led_source_type)

 

led_source.SourcesData.PrePropagation = -1
print("Preprop: ", led_source.SourcesData.PrePropagation)
print("Sampling: ", led_source.SourcesData.SamplingMethod)
led_source.SourcesData.SamplingMethod = "Sobol"
print("Sampling: ", led_source.SourcesData.SamplingMethod)

 

Note Pre-propagation can be [set, get] through SourceData, whereas SamplingMethod can only be [get]. When I execute this code I get the following:

 

Preprop:  -1.0
Sampling:  Sobol
Traceback (most recent call last):
  File "C:\Users\matthew.haines\Documents\Zemax\ZOS-API Projects\PythonStandaloneApplication4\PythonStandaloneApplication4\PythonStandaloneApplication4.py", line 226, in <module>
    led_source.SourcesData.SamplingMethod = "Sobol"
TypeError: since Python.NET 3.0 int can not be converted to Enum implicitly. Use Enum(int_value)
Exception ignored in: <function PythonStandaloneApplication4.__del__ at 0x00000283CB6D1820>
Traceback (most recent call last):
  File "C:\Users\matthew.haines\Documents\Zemax\ZOS-API Projects\PythonStandaloneApplication4\PythonStandaloneApplication4\PythonStandaloneApplication4.py", line 75, in __del__
TypeError: 'MethodObject' object is not callable

Best answer by David.Nguyen

@Matt H 

 

TL;DR led_source.SourcesData.SamplingMethod = ZOSAPI.Editors.NCE.SourceSamplingMethod.Sobol

 

I haven’t tested, but this is a recurring issue we’ve seen in the Community.

In the Programming..ZOS-API Help..ZOS-API Syntax Help, you find the following description for SourceData

I imagine you have found it before. But I wanted to highlight something. If you look to the left of PrePropagation and SamplingMethod. There’s some text that I marked with a yellow marker. It reads double and SourceSamplingMethod, respectively. This tells you the type of variable that is expected from the corresponding property. Double is a standard floating-point numeric variable. Therefore, when you do:

led_source.SourcesData.PrePropagation = -1

Everything works. In effect, -1 is an integer (int), but it is automatically cast to a double (-1.0). No problem with that. But when you do:

led_source.SourcesData.SamplingMethod = "Sobol"

There’s a problem because “Sobol” is interpreted as a string because of the quotation marks. Therefore, SamplingMethod tries to cast the string to the required type SourceSamplingMethod, but it can’t.

So, how do you set the source sampling method then? You see an example a bit below in the same help page with ArrayType. I’m copying the Python implementation only since it seems to be what you are using.

Look at what is being set to ArrayType:ZOSAPI.Editors.NCE.ArrayMode.Rectangular. To do the same with the source sampling method, you can click on the SourceSamplingMethod that I highlighted in my first screenshot, and it’ll take you to this page.

Right under the section header, you see a sequence that looks like what’s been set for the ArrayType example. And this is exactly what you need to use:

led_source.SourcesData.SamplingMethod = ZOSAPI.Editors.NCE.SourceSamplingMethod.Sobol

I hope this helps and take care,

 

David

View original
Did this topic help you find an answer to your question?

2 replies

David.Nguyen
Luminary
Forum|alt.badge.img+2
  • Luminary
  • 1122 replies
  • Answer
  • May 23, 2025

@Matt H 

 

TL;DR led_source.SourcesData.SamplingMethod = ZOSAPI.Editors.NCE.SourceSamplingMethod.Sobol

 

I haven’t tested, but this is a recurring issue we’ve seen in the Community.

In the Programming..ZOS-API Help..ZOS-API Syntax Help, you find the following description for SourceData

I imagine you have found it before. But I wanted to highlight something. If you look to the left of PrePropagation and SamplingMethod. There’s some text that I marked with a yellow marker. It reads double and SourceSamplingMethod, respectively. This tells you the type of variable that is expected from the corresponding property. Double is a standard floating-point numeric variable. Therefore, when you do:

led_source.SourcesData.PrePropagation = -1

Everything works. In effect, -1 is an integer (int), but it is automatically cast to a double (-1.0). No problem with that. But when you do:

led_source.SourcesData.SamplingMethod = "Sobol"

There’s a problem because “Sobol” is interpreted as a string because of the quotation marks. Therefore, SamplingMethod tries to cast the string to the required type SourceSamplingMethod, but it can’t.

So, how do you set the source sampling method then? You see an example a bit below in the same help page with ArrayType. I’m copying the Python implementation only since it seems to be what you are using.

Look at what is being set to ArrayType:ZOSAPI.Editors.NCE.ArrayMode.Rectangular. To do the same with the source sampling method, you can click on the SourceSamplingMethod that I highlighted in my first screenshot, and it’ll take you to this page.

Right under the section header, you see a sequence that looks like what’s been set for the ArrayType example. And this is exactly what you need to use:

led_source.SourcesData.SamplingMethod = ZOSAPI.Editors.NCE.SourceSamplingMethod.Sobol

I hope this helps and take care,

 

David


Matt H
  • Author
  • Monochrome
  • 1 reply
  • May 25, 2025

Hi David,

 

Thank for the response, that clears the syntax use up here for me - the ZOSAPI help is very useful but can also be confusing.

I have tested your “led_source.SourcesData.SamplingMethod = ZOSAPI.Editors.NCE.SourceSamplingMethod.Sobol” implementation and it works without issue.

Thanks again for the help!

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings